feat(lsp): hover and goto definition

This commit is contained in:
rvcas
2023-02-20 02:09:57 -05:00
committed by Lucas
parent 39ea803fe6
commit 815d7d80c6
11 changed files with 478 additions and 108 deletions

View File

@@ -230,7 +230,8 @@ mod test {
let (mut ast, extra) =
parser::module(source_code, kind).expect("Failed to parse module");
ast.name = name.clone();
let mut module = ParsedModule {
ParsedModule {
kind,
ast,
code: source_code.to_string(),
@@ -238,9 +239,7 @@ mod test {
path: PathBuf::new(),
extra,
package: self.package.to_string(),
};
module.attach_doc_and_module_comments();
module
}
}
fn check(&mut self, module: ParsedModule) -> CheckedModule {
@@ -261,7 +260,7 @@ mod test {
self.module_types
.insert(module.name.clone(), ast.type_info.clone());
CheckedModule {
let mut checked_module = CheckedModule {
kind: module.kind,
extra: module.extra,
name: module.name,
@@ -269,7 +268,11 @@ mod test {
package: module.package,
input_path: module.path,
ast,
}
};
checked_module.attach_doc_and_module_comments();
checked_module
}
}

View File

@@ -467,7 +467,7 @@ where
// Store the name
ast.name = name.clone();
let mut module = ParsedModule {
let module = ParsedModule {
kind,
ast,
code,
@@ -489,8 +489,6 @@ where
.into());
}
module.attach_doc_and_module_comments();
parsed_modules.insert(module.name.clone(), module);
}
Err(errs) => {
@@ -561,18 +559,19 @@ where
self.module_types
.insert(name.clone(), ast.type_info.clone());
self.checked_modules.insert(
name.clone(),
CheckedModule {
kind,
extra,
name,
code,
ast,
package,
input_path: path,
},
);
let mut checked_module = CheckedModule {
kind,
extra,
name: name.clone(),
code,
ast,
package,
input_path: path,
};
checked_module.attach_doc_and_module_comments();
self.checked_modules.insert(name, checked_module);
}
}

View File

@@ -1,7 +1,7 @@
use crate::error::Error;
use aiken_lang::{
ast::{
DataType, Definition, ModuleKind, TypedDataType, TypedFunction, TypedModule,
DataType, Definition, Located, ModuleKind, TypedDataType, TypedFunction, TypedModule,
TypedValidator, UntypedModule,
},
builder::{DataTypeKey, FunctionAccessKey},
@@ -41,55 +41,6 @@ impl ParsedModule {
(name, deps)
}
pub fn attach_doc_and_module_comments(&mut self) {
// Module Comments
self.ast.docs = self
.extra
.module_comments
.iter()
.map(|span| {
Comment::from((span, self.code.as_str()))
.content
.to_string()
})
.collect();
// Order definitions to avoid dissociating doc comments from them
let mut definitions: Vec<_> = self.ast.definitions.iter_mut().collect();
definitions.sort_by(|a, b| a.location().start.cmp(&b.location().start));
// Doc Comments
let mut doc_comments = self.extra.doc_comments.iter().peekable();
for def in &mut definitions {
let docs: Vec<&str> =
comments_before(&mut doc_comments, def.location().start, &self.code);
if !docs.is_empty() {
let doc = docs.join("\n");
def.put_doc(doc);
}
if let Definition::DataType(DataType { constructors, .. }) = def {
for constructor in constructors {
let docs: Vec<&str> =
comments_before(&mut doc_comments, constructor.location.start, &self.code);
if !docs.is_empty() {
let doc = docs.join("\n");
constructor.put_doc(doc);
}
for argument in constructor.arguments.iter_mut() {
let docs: Vec<&str> =
comments_before(&mut doc_comments, argument.location.start, &self.code);
if !docs.is_empty() {
let doc = docs.join("\n");
argument.put_doc(doc);
}
}
}
}
}
}
}
pub struct ParsedModules(HashMap<String, ParsedModule>);
@@ -223,6 +174,61 @@ pub struct CheckedModule {
pub extra: ModuleExtra,
}
impl CheckedModule {
pub fn find_node(&self, byte_index: usize) -> Option<Located<'_>> {
self.ast.find_node(byte_index)
}
pub fn attach_doc_and_module_comments(&mut self) {
// Module Comments
self.ast.docs = self
.extra
.module_comments
.iter()
.map(|span| {
Comment::from((span, self.code.as_str()))
.content
.to_string()
})
.collect();
// Order definitions to avoid dissociating doc comments from them
let mut definitions: Vec<_> = self.ast.definitions.iter_mut().collect();
definitions.sort_by(|a, b| a.location().start.cmp(&b.location().start));
// Doc Comments
let mut doc_comments = self.extra.doc_comments.iter().peekable();
for def in &mut definitions {
let docs: Vec<&str> =
comments_before(&mut doc_comments, def.location().start, &self.code);
if !docs.is_empty() {
let doc = docs.join("\n");
def.put_doc(doc);
}
if let Definition::DataType(DataType { constructors, .. }) = def {
for constructor in constructors {
let docs: Vec<&str> =
comments_before(&mut doc_comments, constructor.location.start, &self.code);
if !docs.is_empty() {
let doc = docs.join("\n");
constructor.put_doc(doc);
}
for argument in constructor.arguments.iter_mut() {
let docs: Vec<&str> =
comments_before(&mut doc_comments, argument.location.start, &self.code);
if !docs.is_empty() {
let doc = docs.join("\n");
argument.put_doc(doc);
}
}
}
}
}
}
}
#[derive(Default, Debug, Clone)]
pub struct CheckedModules(HashMap<String, CheckedModule>);