-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implemented semantic code completion
- Loading branch information
1 parent
5457244
commit 5926700
Showing
6 changed files
with
96 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use either::Either; | ||
use tower_lsp::lsp_types::{ | ||
CompletionItem, CompletionItemKind, CompletionParams, CompletionResponse, | ||
}; | ||
use wit_compiler::{ | ||
ast::{self, AstNode}, | ||
queries::{SourceFile, Workspace}, | ||
}; | ||
|
||
pub fn complete( | ||
db: &dyn wit_compiler::Db, | ||
_ws: Workspace, | ||
file: SourceFile, | ||
params: CompletionParams, | ||
) -> Option<CompletionResponse> { | ||
let point = crate::utils::position_to_ts(params.text_document_position.position); | ||
|
||
let mut completions = Vec::new(); | ||
|
||
// First, add all known keywords | ||
let keywords = wit_compiler::ast::KEYWORDS.iter().map(|kw| CompletionItem { | ||
label: kw.to_string(), | ||
kind: Some(CompletionItemKind::KEYWORD), | ||
..Default::default() | ||
}); | ||
completions.extend(keywords); | ||
|
||
// Now, we do the hard job of figuring out which identifiers are in scope. | ||
// FIXME: This doesn't take imported items into account | ||
let items = wit_compiler::queries::file_items(db, file); | ||
if let Some(index) = items.enclosing_item(db, point) { | ||
let types = match index { | ||
Either::Left(index) => items.get_world(db, index).items(db), | ||
Either::Right(index) => items.get_interface(db, index).items(db), | ||
}; | ||
|
||
completions.extend(types.names().map(|label| CompletionItem { | ||
label: label.to_string(), | ||
..Default::default() | ||
})); | ||
} | ||
|
||
let ast = wit_compiler::queries::parse(db, file); | ||
if let Some(ident) = ast | ||
.tree(db) | ||
.ancestors(point) | ||
.find_map(ast::Identifier::cast) | ||
{ | ||
let src = file.contents(db); | ||
let ident = ident.value(&src); | ||
// The user has started writing an identifier, so limit the completions | ||
// to whatever might match what they've written. | ||
completions.retain(|c| c.label.starts_with(ident)); | ||
} | ||
|
||
Some(CompletionResponse::Array(completions)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod completion; | ||
mod diagnostics; | ||
mod fold; | ||
|
||
pub use self::{diagnostics::file_diagnostics, fold::folding_range}; | ||
pub use self::{completion::complete, diagnostics::file_diagnostics, fold::folding_range}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters