Skip to content

Commit

Permalink
Merge pull request #410 from Marwes/import_completions
Browse files Browse the repository at this point in the history
Provide better completion when writing imports
  • Loading branch information
Marwes authored Dec 7, 2017
2 parents 7978385 + d753672 commit 464176e
Show file tree
Hide file tree
Showing 26 changed files with 798 additions and 391 deletions.
16 changes: 16 additions & 0 deletions base/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,3 +845,19 @@ pub fn is_constructor(s: &str) -> bool {
.unwrap()
.starts_with(char::is_uppercase)
}

pub fn expr_to_path(expr: &SpannedExpr<Symbol>, path: &mut String) -> Result<(), &'static str> {
match expr.value {
Expr::Ident(ref id) => {
path.push_str(id.name.declared_name());
Ok(())
}
Expr::Projection(ref expr, ref id, _) => {
expr_to_path(expr, path)?;
path.push('.');
path.push_str(id.declared_name());
Ok(())
}
_ => return Err("Expected a string literal or path to import"),
}
}
39 changes: 25 additions & 14 deletions base/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,38 +180,39 @@ impl Hash for SymbolRef {
}
}

impl AsRef<str> for SymbolRef {
fn as_ref(&self) -> &str {
&self.0
}
}

impl Symbol {
pub fn strong_count(sym: &Symbol) -> usize {
Arc::strong_count(&sym.0)
}
}

impl SymbolRef {
#[inline]
pub fn new<N: ?Sized + AsRef<str>>(n: &N) -> &SymbolRef {
unsafe { ::std::mem::transmute::<&Name, &SymbolRef>(Name::new(n)) }
}

/// Checks whether the names of two symbols are equal
pub fn name_eq(&self, other: &SymbolRef) -> bool {
self.name() == other.name()
}

pub fn name(&self) -> &Name {
Name::new(self)
Name::new(&self.0)
}

/// Returns the name of this symbol as it was originally declared (strips location information
/// and module information)
pub fn declared_name(&self) -> &str {
let name = self.as_ref();
name.split(':')
.next()
.unwrap_or(name)
.rsplit('.')
.next()
.unwrap_or(name)
self.name().declared_name()
}

pub fn definition_name(&self) -> &str {
self.name().definition_name()
}

pub fn is_global(&self) -> bool {
self.0.chars().next() == Some('@')
}

fn ptr(&self) -> *const () {
Expand Down Expand Up @@ -268,6 +269,16 @@ impl Name {
.rfind('.')
.map_or(self, |i| Name::new(&self.0[i + 1..]))
}

pub fn declared_name(&self) -> &str {
let name = self.definition_name();
name.rsplit('.').next().unwrap_or(name)
}

pub fn definition_name(&self) -> &str {
let name = self.as_str().trim_left_matches('@');
name.split(':').next().unwrap_or(name)
}
}

impl NameBuf {
Expand Down
17 changes: 13 additions & 4 deletions base/src/types/pretty_print.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use std::borrow::Cow;

use pretty::{Arena, DocAllocator, DocBuilder};

use ast::{is_operator_char, Comment, CommentType};
use pos::{BytePos, Span};
use source::Source;

pub fn ident<'b>(arena: &'b Arena<'b>, name: &'b str) -> DocBuilder<'b, Arena<'b>> {
pub fn ident<'b, S>(arena: &'b Arena<'b>, name: S) -> DocBuilder<'b, Arena<'b>>
where
S: Into<Cow<'b, str>>,
{
let name = name.into();
if name.starts_with(is_operator_char) {
chain![arena; "(", name, ")"]
} else {
Expand All @@ -18,9 +24,12 @@ pub fn doc_comment<'a>(
) -> DocBuilder<'a, Arena<'a>> {
match text {
Some(comment) => match comment.typ {
CommentType::Line => arena.concat(comment.content.lines().map(|line| {
arena.text("/// ").append(line).append(arena.newline())
})),
CommentType::Line => arena.concat(
comment
.content
.lines()
.map(|line| arena.text("/// ").append(line).append(arena.newline())),
),
CommentType::Block => chain![arena;
"/**",
arena.newline(),
Expand Down
1 change: 1 addition & 0 deletions check/src/typecheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ impl<'a> Typecheck<'a> {
value: TypeError::from(value).into(),
});
}
println!("{}", self.errors);
Err(mem::replace(&mut self.errors, Errors::new()))
}
}
Expand Down
6 changes: 3 additions & 3 deletions check/tests/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl MockEnv {

impl KindEnv for MockEnv {
fn find_kind(&self, id: &SymbolRef) -> Option<ArcKind> {
match id.as_ref() {
match id.definition_name() {
"Bool" => Some(Kind::typ()),
_ => None,
}
Expand All @@ -87,14 +87,14 @@ impl KindEnv for MockEnv {

impl TypeEnv for MockEnv {
fn find_type(&self, id: &SymbolRef) -> Option<&ArcType> {
match id.as_ref() {
match id.definition_name() {
"False" | "True" => Some(&self.bool.as_type()),
_ => None,
}
}

fn find_type_info(&self, id: &SymbolRef) -> Option<&Alias<Symbol, ArcType>> {
match id.as_ref() {
match id.definition_name() {
"Bool" => Some(&self.bool),
_ => None,
}
Expand Down
1 change: 1 addition & 0 deletions completion/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ documentation = "https://docs.rs/gluon"
[dependencies]
either = "1.0.0"
itertools = "0.7.0"
walkdir = "2"

gluon_base = { path = "../base", version = "0.6.2" } # GLUON

Expand Down
Loading

0 comments on commit 464176e

Please sign in to comment.