Skip to content

Commit

Permalink
Some LSP improvements regarding hovering
Browse files Browse the repository at this point in the history
  - Resolve module select properly
  - Add some type annotations to _some patterns_ such as assignments
  • Loading branch information
KtorZ committed Oct 19, 2023
1 parent a08405c commit 41e26b2
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 26 deletions.
54 changes: 49 additions & 5 deletions crates/aiken-lang/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,8 @@ impl TypedDefinition {
})
| Definition::Test(Function { body, .. }) = self
{
if let Some(expression) = body.find_node(byte_index) {
return Some(Located::Expression(expression));
if let Some(located) = body.find_node(byte_index) {
return Some(located);
}
}

Expand All @@ -420,13 +420,18 @@ impl TypedDefinition {
#[derive(Debug, Clone, PartialEq)]
pub enum Located<'a> {
Expression(&'a TypedExpr),
Pattern(&'a TypedPattern, &'a TypedExpr),
Definition(&'a TypedDefinition),
}

impl<'a> Located<'a> {
pub fn definition_location(&self) -> Option<DefinitionLocation<'_>> {
match self {
Self::Expression(expression) => expression.definition_location(),
// TODO: Revise definition location semantic for 'Pattern'
// e.g. for constructors, we might want to show the type definition
// for that constructor.
Self::Pattern(_, _) => None,
Self::Definition(definition) => Some(DefinitionLocation {
module: None,
span: definition.location(),
Expand Down Expand Up @@ -499,7 +504,7 @@ impl CallArg<UntypedExpr> {
}

impl TypedCallArg {
pub fn find_node(&self, byte_index: usize) -> Option<&TypedExpr> {
pub fn find_node(&self, byte_index: usize) -> Option<Located<'_>> {
self.value.find_node(byte_index)
}
}
Expand Down Expand Up @@ -918,6 +923,45 @@ impl<A, B> Pattern<A, B> {
}
}

impl TypedPattern {
pub fn find_node<'a>(&'a self, byte_index: usize, value: &'a TypedExpr) -> Option<Located<'a>> {
if !self.location().contains(byte_index) {
return None;
}

match self {
Pattern::Int { .. }
| Pattern::Var { .. }
| Pattern::Assign { .. }
| Pattern::Discard { .. } => Some(Located::Pattern(self, value)),

Pattern::List { elements, .. }
| Pattern::Tuple {
elems: elements, ..
} => elements
.iter()
.find_map(|e| e.find_node(byte_index, value))
.or(Some(Located::Pattern(self, value))),

Pattern::Constructor { arguments, .. } => arguments
.iter()
.find_map(|e| e.value.find_node(byte_index, value))
.or(Some(Located::Pattern(self, value))),
}
}

pub fn tipo(&self, value: &TypedExpr) -> Option<Rc<Type>> {
match self {
Pattern::Int { .. } => Some(builtins::int()),
Pattern::Constructor { tipo, .. } => Some(tipo.clone()),
Pattern::Var { .. } | Pattern::Assign { .. } | Pattern::Discard { .. } => {
Some(value.tipo())
}
Pattern::List { .. } | Pattern::Tuple { .. } => None,
}
}
}

#[derive(Debug, Clone, PartialEq, Eq, Copy)]
pub enum ByteArrayFormatPreference {
HexadecimalString,
Expand Down Expand Up @@ -977,7 +1021,7 @@ impl TypedClause {
}
}

pub fn find_node(&self, byte_index: usize) -> Option<&TypedExpr> {
pub fn find_node(&self, byte_index: usize) -> Option<Located<'_>> {
self.then.find_node(byte_index)
}
}
Expand Down Expand Up @@ -1119,7 +1163,7 @@ pub struct TypedRecordUpdateArg {
}

impl TypedRecordUpdateArg {
pub fn find_node(&self, byte_index: usize) -> Option<&TypedExpr> {
pub fn find_node(&self, byte_index: usize) -> Option<Located<'_>> {
self.value.find_node(byte_index)
}
}
Expand Down
34 changes: 21 additions & 13 deletions crates/aiken-lang/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use vec1::Vec1;
use crate::{
ast::{
self, Annotation, Arg, AssignmentKind, BinOp, ByteArrayFormatPreference, CallArg,
DefinitionLocation, IfBranch, LogicalOpChainKind, ParsedCallArg, Pattern,
DefinitionLocation, IfBranch, Located, LogicalOpChainKind, ParsedCallArg, Pattern,
RecordUpdateSpread, Span, TraceKind, TypedClause, TypedRecordUpdateArg, UnOp,
UntypedClause, UntypedRecordUpdateArg,
},
Expand Down Expand Up @@ -312,7 +312,7 @@ impl TypedExpr {

// This could be optimised in places to exit early if the first of a series
// of expressions is after the byte index.
pub fn find_node(&self, byte_index: usize) -> Option<&Self> {
pub fn find_node(&self, byte_index: usize) -> Option<Located<'_>> {
if !self.location().contains(byte_index) {
return None;
}
Expand All @@ -323,38 +323,42 @@ impl TypedExpr {
| TypedExpr::UInt { .. }
| TypedExpr::String { .. }
| TypedExpr::ByteArray { .. }
| TypedExpr::ModuleSelect { .. } => Some(self),
| TypedExpr::ModuleSelect { .. } => Some(Located::Expression(self)),

TypedExpr::Trace { text, then, .. } => text
.find_node(byte_index)
.or_else(|| then.find_node(byte_index))
.or(Some(self)),
.or(Some(Located::Expression(self))),

TypedExpr::Pipeline { expressions, .. } | TypedExpr::Sequence { expressions, .. } => {
expressions.iter().find_map(|e| e.find_node(byte_index))
}

TypedExpr::Fn { body, .. } => body.find_node(byte_index).or(Some(self)),
TypedExpr::Fn { body, .. } => body
.find_node(byte_index)
.or(Some(Located::Expression(self))),

TypedExpr::Tuple {
elems: elements, ..
}
| TypedExpr::List { elements, .. } => elements
.iter()
.find_map(|e| e.find_node(byte_index))
.or(Some(self)),
.or(Some(Located::Expression(self))),

TypedExpr::Call { fun, args, .. } => args
.iter()
.find_map(|arg| arg.find_node(byte_index))
.or_else(|| fun.find_node(byte_index))
.or(Some(self)),
.or(Some(Located::Expression(self))),

TypedExpr::BinOp { left, right, .. } => left
.find_node(byte_index)
.or_else(|| right.find_node(byte_index)),

TypedExpr::Assignment { value, .. } => value.find_node(byte_index),
TypedExpr::Assignment { value, pattern, .. } => pattern
.find_node(byte_index, value)
.or_else(|| value.find_node(byte_index)),

TypedExpr::When {
subject, clauses, ..
Expand All @@ -365,20 +369,22 @@ impl TypedExpr {
.iter()
.find_map(|clause| clause.find_node(byte_index))
})
.or(Some(self)),
.or(Some(Located::Expression(self))),

TypedExpr::RecordAccess {
record: expression, ..
}
| TypedExpr::TupleIndex {
tuple: expression, ..
} => expression.find_node(byte_index).or(Some(self)),
} => expression
.find_node(byte_index)
.or(Some(Located::Expression(self))),

TypedExpr::RecordUpdate { spread, args, .. } => args
.iter()
.find_map(|arg| arg.find_node(byte_index))
.or_else(|| spread.find_node(byte_index))
.or(Some(self)),
.or(Some(Located::Expression(self))),

TypedExpr::If {
branches,
Expand All @@ -393,9 +399,11 @@ impl TypedExpr {
.or_else(|| branch.body.find_node(byte_index))
})
.or_else(|| final_else.find_node(byte_index))
.or(Some(self)),
.or(Some(Located::Expression(self))),

TypedExpr::UnOp { value, .. } => value.find_node(byte_index).or(Some(self)),
TypedExpr::UnOp { value, .. } => value
.find_node(byte_index)
.or(Some(Located::Expression(self))),
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions crates/aiken-lang/src/tipo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,15 +582,17 @@ impl ValueConstructor {
ValueConstructorVariant::Record {
module, location, ..
}
| ValueConstructorVariant::ModuleFn {
module, location, ..
}
| ValueConstructorVariant::ModuleConstant {
location, module, ..
} => DefinitionLocation {
module: Some(module.as_str()),
span: *location,
},

ValueConstructorVariant::ModuleFn { location, .. }
| ValueConstructorVariant::LocalVariable { location } => DefinitionLocation {
ValueConstructorVariant::LocalVariable { location } => DefinitionLocation {
module: None,
span: *location,
},
Expand Down
23 changes: 17 additions & 6 deletions crates/aiken-lsp/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ impl Server {
self.completion_for_import()
}

// TODO: autocompletion for patterns
Some(Located::Pattern(_pattern, _value)) => None,

// TODO: autocompletion for other definitions
Some(Located::Definition(_expression)) => None,

Expand Down Expand Up @@ -458,13 +461,17 @@ impl Server {
None => return Ok(None),
};

let expression = match found {
Located::Expression(expression) => expression,
let (location, definition_location, tipo) = match found {
Located::Expression(expression) => (
expression.location(),
expression.definition_location(),
Some(expression.tipo()),
),
Located::Pattern(pattern, value) => (pattern.location(), None, pattern.tipo(value)),
Located::Definition(_) => return Ok(None),
};

let doc = expression
.definition_location()
let doc = definition_location
.and_then(|loc| loc.module.map(|m| (m, loc.span)))
.and_then(|(m, span)| {
self.compiler
Expand All @@ -475,12 +482,16 @@ impl Server {
.and_then(|(checked_module, span)| checked_module.ast.find_node(span.start))
.and_then(|node| match node {
Located::Expression(_) => None,
Located::Pattern(_, _) => None,
Located::Definition(def) => def.doc(),
})
.unwrap_or_default();

// Show the type of the hovered node to the user
let type_ = Printer::new().pretty_print(expression.tipo().as_ref(), 0);
let type_ = match tipo {
Some(t) => Printer::new().pretty_print(t.as_ref(), 0),
None => "?".to_string(),
};

let contents = formatdoc! {r#"
```aiken
Expand All @@ -491,7 +502,7 @@ impl Server {

Ok(Some(lsp_types::Hover {
contents: lsp_types::HoverContents::Scalar(lsp_types::MarkedString::String(contents)),
range: Some(span_to_lsp_range(expression.location(), &line_numbers)),
range: Some(span_to_lsp_range(location, &line_numbers)),
}))
}

Expand Down

0 comments on commit 41e26b2

Please sign in to comment.