Skip to content

Commit

Permalink
Make trait completion add a visible use path. (#6091)
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbens-starkware authored Jul 31, 2024
1 parent 3faddfb commit 8dce8e6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use cairo_lang_utils::{LookupIntern, Upcast};
use tower_lsp::lsp_types::{CompletionItem, CompletionItemKind, Position, Range, TextEdit};
use tracing::debug;

use crate::ide::utils::{find_methods_for_type, module_has_trait};
use crate::ide::utils::find_methods_for_type;
use crate::lang::db::{AnalysisDatabase, LsSemanticGroup};
use crate::lang::lsp::ToLsp;

Expand Down Expand Up @@ -268,14 +268,13 @@ pub fn completion_for_method(

// TODO(spapini): Add signature.
let detail = trait_id.full_path(db.upcast());
let trait_full_path = trait_id.full_path(db.upcast());
let mut additional_text_edits = vec![];

// If the trait is not in scope, add a use statement.
if !module_has_trait(db, module_id, trait_id)? {
if let Some(trait_path) = db.visible_traits_from_module(module_id).get(&trait_id) {
additional_text_edits.push(TextEdit {
range: Range::new(position, position),
new_text: format!("use {trait_full_path};\n"),
new_text: format!("use {};\n", trait_path),
});
}

Expand Down
25 changes: 2 additions & 23 deletions crates/cairo-lang-language-server/src/ide/utils.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::ids::{ModuleId, TraitFunctionId};
use cairo_lang_defs::ids::TraitFunctionId;
use cairo_lang_filesystem::db::FilesGroup;
use cairo_lang_semantic::db::SemanticGroup;
use cairo_lang_semantic::expr::inference::infers::InferenceEmbeddings;
use cairo_lang_semantic::expr::inference::solver::SolutionSet;
use cairo_lang_semantic::expr::inference::InferenceId;
use cairo_lang_semantic::items::us::SemanticUseEx;
use cairo_lang_semantic::lsp_helpers::TypeFilter;
use cairo_lang_semantic::resolve::{ResolvedGenericItem, Resolver};
use cairo_lang_semantic::resolve::Resolver;
use tracing::debug;

use crate::lang::db::AnalysisDatabase;
Expand Down Expand Up @@ -62,22 +60,3 @@ pub fn find_methods_for_type(
}
relevant_methods
}

/// Checks if a module has a trait in scope.
#[tracing::instrument(level = "trace", skip_all)]
pub fn module_has_trait(
db: &AnalysisDatabase,
module_id: ModuleId,
trait_id: cairo_lang_defs::ids::TraitId,
) -> Option<bool> {
if db.module_traits_ids(module_id).ok()?.contains(&trait_id) {
return Some(true);
}
// TODO(Gil): Check if the trait is visible, and return the path of the visible use item.
for use_id in db.module_uses_ids(module_id).ok()?.iter().copied() {
if db.use_resolved_item(use_id) == Ok(ResolvedGenericItem::Trait(trait_id)) {
return Some(true);
}
}
Some(false)
}

0 comments on commit 8dce8e6

Please sign in to comment.