Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LS: Collect modules for files inside refresh_file_diagnostics #6589

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 19 additions & 38 deletions crates/cairo-lang-language-server/src/lang/diagnostics/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,12 @@ pub fn refresh_diagnostics(
open_files.iter().filter_map(|uri| db.file_for_url(uri)).collect::<HashSet<FileId>>()
});

let open_files_modules = get_files_modules(db, open_files_ids.iter().copied());

// Refresh open files modules first for better UX
info_span!("refresh_open_files_modules").in_scope(|| {
for (file, file_modules_ids) in open_files_modules {
for &file in &open_files_ids {
refresh_file_diagnostics(
db,
&file,
&file_modules_ids,
file,
trace_macro_diagnostics,
&mut processed_modules,
&mut files_with_set_diagnostics,
Expand All @@ -73,15 +70,12 @@ pub fn refresh_diagnostics(
rest_of_files
});

let rest_of_files_modules = get_files_modules(db, rest_of_files.iter().copied());

// Refresh the rest of files after, since they are not viewed currently
info_span!("refresh_other_files_modules").in_scope(|| {
for (file, file_modules_ids) in rest_of_files_modules {
for file in rest_of_files {
refresh_file_diagnostics(
db,
&file,
&file_modules_ids,
file,
trace_macro_diagnostics,
&mut processed_modules,
&mut files_with_set_diagnostics,
Expand Down Expand Up @@ -114,21 +108,23 @@ pub fn refresh_diagnostics(
}

/// Refresh diagnostics for a single file.
#[expect(clippy::too_many_arguments)]
fn refresh_file_diagnostics(
db: &AnalysisDatabase,
file: &FileId,
modules_ids: &Vec<ModuleId>,
file: FileId,
trace_macro_diagnostics: bool,
processed_modules: &mut HashSet<ModuleId>,
files_with_set_diagnostics: &mut HashSet<Url>,
file_diagnostics: &mut HashMap<Url, FileDiagnostics>,
notifier: &Notifier,
) {
let Some(file_uri) = db.url_for_file(*file) else {
let Some(file_uri) = db.url_for_file(file) else {
trace!("url for file not found: {:?}", file.lookup_intern(db));
return;
};
let Ok(module_ids) = db.file_modules(file) else {
trace!("modules for file not found: {:?}", file.lookup_intern(db));
return;
};

let mut semantic_file_diagnostics: Vec<SemanticDiagnostic> = vec![];
let mut lowering_file_diagnostics: Vec<LoweringDiagnostic> = vec![];
Expand All @@ -151,22 +147,22 @@ fn refresh_file_diagnostics(
};
}

for module_id in modules_ids {
if !processed_modules.contains(module_id) {
for &module_id in module_ids.iter() {
if !processed_modules.contains(&module_id) {
semantic_file_diagnostics.extend(
diags!(db.module_semantic_diagnostics(*module_id), Result::unwrap_or_default)
diags!(db.module_semantic_diagnostics(module_id), Result::unwrap_or_default)
.get_all(),
);
lowering_file_diagnostics.extend(
diags!(db.module_lowering_diagnostics(*module_id), Result::unwrap_or_default)
diags!(db.module_lowering_diagnostics(module_id), Result::unwrap_or_default)
.get_all(),
);

processed_modules.insert(*module_id);
processed_modules.insert(module_id);
}
}

let parser_file_diagnostics = diags!(db.file_syntax_diagnostics(*file), |r| r);
let parser_file_diagnostics = diags!(db.file_syntax_diagnostics(file), |r| r);

let new_file_diagnostics = FileDiagnostics {
parser: parser_file_diagnostics,
Expand All @@ -192,21 +188,21 @@ fn refresh_file_diagnostics(
(*db).upcast(),
&mut diags,
&new_file_diagnostics.parser,
file,
&file,
trace_macro_diagnostics,
);
map_cairo_diagnostics_to_lsp(
(*db).upcast(),
&mut diags,
&new_file_diagnostics.semantic,
file,
&file,
trace_macro_diagnostics,
);
map_cairo_diagnostics_to_lsp(
(*db).upcast(),
&mut diags,
&new_file_diagnostics.lowering,
file,
&file,
trace_macro_diagnostics,
);

Expand All @@ -216,18 +212,3 @@ fn refresh_file_diagnostics(
version: None,
});
}

/// Gets the mapping of files to their respective modules.
#[tracing::instrument(skip_all)]
fn get_files_modules(
db: &AnalysisDatabase,
files_ids: impl Iterator<Item = FileId>,
) -> HashMap<FileId, Vec<ModuleId>> {
let mut result = HashMap::default();
for file_id in files_ids {
if let Ok(file_modules) = db.file_modules(file_id) {
result.insert(file_id, file_modules.iter().cloned().collect::<Vec<_>>());
}
}
result
}