Skip to content

Commit

Permalink
Add tests for module name completions (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
Draggu authored Feb 11, 2025
1 parent 1d4fbe9 commit 464b651
Show file tree
Hide file tree
Showing 4 changed files with 334 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/ide/completion/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use attribute::{attribute_completions, derive_completions};
use cairo_lang_syntax::node::ast::{
self, Attribute, BinaryOperator, ExprBinary, ExprPath, ExprStructCtorCall, ItemModule,
TokenIdentifier, UsePathLeaf, UsePathSingle,
TerminalIdentifier, UsePathLeaf, UsePathSingle,
};
use cairo_lang_syntax::node::kind::SyntaxKind;
use cairo_lang_syntax::node::{Token, TypedSyntaxNode};
use cairo_lang_syntax::node::{Terminal, TypedSyntaxNode};
use colon_colon::{expr_path, use_statement};
use completions::{dot_completions, struct_constructor_completions};
use if_chain::if_chain;
Expand Down Expand Up @@ -134,7 +134,7 @@ pub fn complete(params: CompletionParams, db: &AnalysisDatabase) -> Option<Compl
);

if_chain!(
if let Some(ident) = TokenIdentifier::cast(db, node.clone());
if let Some(ident) = TerminalIdentifier::cast(db, node.clone());
if let Some(module_item) = node.parent_of_type::<ItemModule>(db);
// We are in nested mod, we should not show completions for file modules.
if module_item.as_syntax_node().ancestor_of_kind(db, SyntaxKind::ItemModule).is_none();
Expand All @@ -147,7 +147,7 @@ pub fn complete(params: CompletionParams, db: &AnalysisDatabase) -> Option<Compl

if_chain!(
// if there is no name `mod <cursor>` we will be on `mod`.
if node.kind(db) == SyntaxKind::TokenModule;
if node.kind(db) == SyntaxKind::TerminalModule;
if let Some(module_item) = node.parent_of_type::<ItemModule>(db);
// We are in nested mod, we should not show completions for file modules.
if module_item.as_syntax_node().ancestor_of_kind(db, SyntaxKind::ItemModule).is_none();
Expand Down
13 changes: 10 additions & 3 deletions src/ide/completion/mod_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ pub fn mod_completions(

let mut url = db.url_for_file(file)?;

let current_file = url.path().to_string();

let file_name = pop_path(&mut url)?;

let module_files = db.file_modules(file).ok()?;

let existing_modules_files = collect_existing_modules(db, &module_files)?;
let mut existing_modules_files = collect_existing_modules(db, &module_files)?;
existing_modules_files.insert(current_file);

let current_dir = url.to_file_path().ok()?;

Expand Down Expand Up @@ -91,10 +94,14 @@ fn collect_existing_modules(
submodule_ids.iter().copied().map(ModuleId::Submodule).collect::<Vec<_>>()
})
{
// This sometimes returns paths like `[ROOT_DIR]/src/.cairo`
// This is not an issue for us as we can add this invalid paths to set here.
// This sometimes returns paths like `[ROOT_DIR]/src/.cairo`.
// It means we are on `mod <caret>;`
let path = db.module_main_file(module).ok()?.full_path(db);

if path.ends_with("/.cairo") {
continue;
}

existing_modules_files.insert(path);
}

Expand Down
27 changes: 19 additions & 8 deletions tests/e2e/completions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use serde::Serialize;

use crate::support::cairo_project_toml::CAIRO_PROJECT_TOML_2024_07;
use crate::support::cursor::peek_caret;
use crate::support::{cursors, sandbox};
use crate::support::{MockClient, cursors, sandbox};

mod attribute;
mod methods_text_edits;
mod mod_file;
mod module_items;
mod structs;

Expand All @@ -20,14 +21,24 @@ mod structs;
/// The function then requests quick fixes at each caret position and compares the result with the
/// expected quick fixes from the snapshot file.
fn test_completions_text_edits(cairo_code: &str) -> Report {
test_completions_text_edits_inner(cairo_code, "src/lib.cairo", |cairo| {
sandbox! {
files {
"cairo_project.toml" => CAIRO_PROJECT_TOML_2024_07,
"src/lib.cairo" => cairo,
}
}
})
}

fn test_completions_text_edits_inner(
cairo_code: &str,
file: &str,
ls: impl FnOnce(&str) -> MockClient,
) -> Report {
let (cairo, cursors) = cursors(cairo_code);

let mut ls = sandbox! {
files {
"cairo_project.toml" => CAIRO_PROJECT_TOML_2024_07,
"src/lib.cairo" => cairo.clone(),
}
};
let mut ls = ls(&cairo);

ls.open_all_cairo_files_and_wait_for_project_update();

Expand All @@ -38,7 +49,7 @@ fn test_completions_text_edits(cairo_code: &str) -> Report {

let completion_params = CompletionParams {
text_document_position: TextDocumentPositionParams {
text_document: ls.doc_id("src/lib.cairo"),
text_document: ls.doc_id(file),
position,
},
work_done_progress_params: Default::default(),
Expand Down
Loading

0 comments on commit 464b651

Please sign in to comment.