Skip to content

Commit

Permalink
feat: suggest trait methods in LSP completion (#5735)
Browse files Browse the repository at this point in the history
# Description

## Problem

Part of #1577

## Summary


![lsp-complete-trait-methods](https://github.com/user-attachments/assets/2d608450-1d6d-475d-8433-9b9f6e60c357)

## Additional Context

Also includes a couple of refactors to further simplify things.

## Documentation

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
asterite authored Aug 15, 2024
1 parent e71c75a commit e2f7e95
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 140 deletions.
34 changes: 26 additions & 8 deletions tooling/lsp/src/requests/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use std::{

use async_lsp::ResponseError;
use completion_items::{
crate_completion_item, simple_completion_item, struct_field_completion_item,
crate_completion_item, field_completion_item, simple_completion_item,
struct_field_completion_item,
};
use fm::{FileId, PathString};
use kinds::{FunctionCompletionKind, FunctionKind, ModuleCompletionKind, RequestedItems};
Expand All @@ -24,6 +25,7 @@ use noirc_frontend::{
def_map::{CrateDefMap, LocalModuleId, ModuleId},
resolution::path_resolver::{PathResolver, StandardPathResolver},
},
hir_def::traits::Trait,
macros_api::{ModuleDefId, NodeInterner},
node_interner::ReferenceId,
parser::{Item, ItemKind},
Expand Down Expand Up @@ -674,8 +676,9 @@ impl<'a> NodeFinder<'a> {
self.complete_type_methods(&type_alias.typ, &prefix, FunctionKind::Any);
return;
}
ModuleDefId::TraitId(_) => {
// For now we don't suggest trait methods
ModuleDefId::TraitId(trait_id) => {
let trait_ = self.interner.get_trait(trait_id);
self.complete_trait_methods(trait_, &prefix, FunctionKind::Any);
return;
}
ModuleDefId::GlobalId(_) => return,
Expand Down Expand Up @@ -938,6 +941,25 @@ impl<'a> NodeFinder<'a> {
}
}

fn complete_trait_methods(
&mut self,
trait_: &Trait,
prefix: &str,
function_kind: FunctionKind,
) {
for (name, func_id) in &trait_.method_ids {
if name_matches(name, prefix) {
if let Some(completion_item) = self.function_completion_item(
*func_id,
FunctionCompletionKind::NameAndParameters,
function_kind,
) {
self.completion_items.push(completion_item);
}
}
}
}

fn complete_struct_fields(
&mut self,
struct_type: &StructType,
Expand All @@ -953,11 +975,7 @@ impl<'a> NodeFinder<'a> {

fn complete_tuple_fields(&mut self, types: &[Type]) {
for (index, typ) in types.iter().enumerate() {
self.completion_items.push(simple_completion_item(
index.to_string(),
CompletionItemKind::FIELD,
Some(typ.to_string()),
));
self.completion_items.push(field_completion_item(&index.to_string(), typ.to_string()));
}
}

Expand Down
6 changes: 5 additions & 1 deletion tooling/lsp/src/requests/completion/completion_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,11 @@ fn type_to_self_string(typ: &Type, string: &mut String) {
}

pub(super) fn struct_field_completion_item(field: &str, typ: &Type) -> CompletionItem {
simple_completion_item(field, CompletionItemKind::FIELD, Some(typ.to_string()))
field_completion_item(field, typ.to_string())
}

pub(super) fn field_completion_item(field: &str, typ: impl Into<String>) -> CompletionItem {
simple_completion_item(field, CompletionItemKind::FIELD, Some(typ.into()))
}

pub(super) fn simple_completion_item(
Expand Down
Loading

0 comments on commit e2f7e95

Please sign in to comment.