Skip to content
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tooling/lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ fxhash.workspace = true
iter-extended.workspace = true
convert_case = "0.6.0"
num-bigint.workspace = true
fuzzy-matcher = "0.3.7"

[target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dependencies]
wasm-bindgen.workspace = true
Expand Down
28 changes: 18 additions & 10 deletions tooling/lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use lsp_types::{
CodeLens,
request::{
CodeActionRequest, Completion, DocumentSymbolRequest, HoverRequest, InlayHintRequest,
PrepareRenameRequest, References, Rename, SignatureHelpRequest,
PrepareRenameRequest, References, Rename, SignatureHelpRequest, WorkspaceSymbolRequest,
},
};
use nargo::{
Expand Down Expand Up @@ -52,11 +52,12 @@ use notifications::{
on_did_open_text_document, on_did_save_text_document, on_exit, on_initialized,
};
use requests::{
LspInitializationOptions, on_code_action_request, on_code_lens_request, on_completion_request,
on_document_symbol_request, on_formatting, on_goto_declaration_request,
LspInitializationOptions, WorkspaceSymbolCache, on_code_action_request, on_code_lens_request,
on_completion_request, on_document_symbol_request, on_formatting, on_goto_declaration_request,
on_goto_definition_request, on_goto_type_definition_request, on_hover_request, on_initialize,
on_inlay_hint_request, on_prepare_rename_request, on_references_request, on_rename_request,
on_shutdown, on_signature_help_request, on_test_run_request, on_tests_request,
on_workspace_symbol_request,
};
use serde_json::Value as JsonValue;
use thiserror::Error;
Expand Down Expand Up @@ -100,6 +101,7 @@ pub struct LspState {
cached_parsed_files: HashMap<PathBuf, (usize, (ParsedModule, Vec<ParserError>))>,
workspace_cache: HashMap<PathBuf, WorkspaceCacheData>,
package_cache: HashMap<PathBuf, PackageCacheData>,
workspace_symbol_cache: WorkspaceSymbolCache,
options: LspInitializationOptions,

// Tracks files that currently have errors, by package root.
Expand Down Expand Up @@ -132,6 +134,7 @@ impl LspState {
cached_parsed_files: HashMap::new(),
workspace_cache: HashMap::new(),
package_cache: HashMap::new(),
workspace_symbol_cache: WorkspaceSymbolCache::default(),
open_documents_count: 0,
options: Default::default(),
files_with_errors: HashMap::new(),
Expand Down Expand Up @@ -169,6 +172,7 @@ impl NargoLspService {
.request::<Completion, _>(on_completion_request)
.request::<SignatureHelpRequest, _>(on_signature_help_request)
.request::<CodeActionRequest, _>(on_code_action_request)
.request::<WorkspaceSymbolRequest, _>(on_workspace_symbol_request)
.notification::<notification::Initialized>(on_initialized)
.notification::<notification::DidChangeConfiguration>(on_did_change_configuration)
.notification::<notification::DidOpenTextDocument>(on_did_open_text_document)
Expand Down Expand Up @@ -429,20 +433,24 @@ pub fn insert_all_files_for_workspace_into_file_manager(
workspace: &Workspace,
file_manager: &mut FileManager,
) {
// Source code for files we cached override those that are read from disk.
let mut overrides: HashMap<&Path, &str> = HashMap::new();
for (path, source) in &state.input_files {
let path = path.strip_prefix("file://").unwrap();
overrides.insert(Path::new(path), source);
}

let overrides = source_code_overrides(&state.input_files);
nargo::insert_all_files_for_workspace_into_file_manager_with_overrides(
workspace,
file_manager,
&overrides,
);
}

// Source code for files we cached override those that are read from disk.
pub fn source_code_overrides(input_files: &HashMap<String, String>) -> HashMap<PathBuf, &str> {
let mut overrides: HashMap<PathBuf, &str> = HashMap::new();
for (path, source) in input_files {
let path = path.strip_prefix("file://").unwrap();
overrides.insert(PathBuf::from_str(path).unwrap(), source);
}
overrides
}

#[test]
fn prepare_package_from_source_string() {
let source = r#"
Expand Down
2 changes: 2 additions & 0 deletions tooling/lsp/src/notifications/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub(super) fn on_did_change_text_document(
) -> ControlFlow<Result<(), async_lsp::Error>> {
let text = params.content_changes.into_iter().next().unwrap().text;
state.input_files.insert(params.text_document.uri.to_string(), text.clone());
state.workspace_symbol_cache.reprocess_uri(&params.text_document.uri);

let document_uri = params.text_document.uri;
let output_diagnostics = false;
Expand All @@ -78,6 +79,7 @@ pub(super) fn on_did_close_text_document(
) -> ControlFlow<Result<(), async_lsp::Error>> {
state.input_files.remove(&params.text_document.uri.to_string());
state.cached_lenses.remove(&params.text_document.uri.to_string());
state.workspace_symbol_cache.reprocess_uri(&params.text_document.uri);

state.open_documents_count -= 1;

Expand Down
13 changes: 12 additions & 1 deletion tooling/lsp/src/requests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
types::{InitializeResult, NargoCapability, NargoTestsOptions, ServerCapabilities},
};

pub(crate) use workspace_symbol::WorkspaceSymbolCache;

// Handlers
// The handlers for `request` are not `async` because it compiles down to lifetimes that can't be added to
// the router. To return a future that fits the trait, it is easiest wrap your implementations in an `async {}`
Expand All @@ -53,6 +55,7 @@
mod signature_help;
mod test_run;
mod tests;
mod workspace_symbol;

pub(crate) use {
code_action::on_code_action_request, code_lens_request::collect_lenses_for_package,
Expand All @@ -62,7 +65,7 @@
hover::on_hover_request, inlay_hint::on_inlay_hint_request, references::on_references_request,
rename::on_prepare_rename_request, rename::on_rename_request,
signature_help::on_signature_help_request, test_run::on_test_run_request,
tests::on_tests_request,
tests::on_tests_request, workspace_symbol::on_workspace_symbol_request,
};

/// LSP client will send initialization request after the server has started.
Expand Down Expand Up @@ -272,19 +275,27 @@
signature_help_provider: Some(lsp_types::OneOf::Right(
lsp_types::SignatureHelpOptions {
trigger_characters: Some(vec!["(".to_string(), ",".to_string()]),
retrigger_characters: None,

Check warning on line 278 in tooling/lsp/src/requests/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (retrigger)
work_done_progress_options: WorkDoneProgressOptions {
work_done_progress: None,
},
},
)),
code_action_provider: Some(lsp_types::OneOf::Right(lsp_types::CodeActionOptions {
code_action_kinds: Some(vec![CodeActionKind::QUICKFIX]),

Check warning on line 285 in tooling/lsp/src/requests/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (QUICKFIX)
work_done_progress_options: WorkDoneProgressOptions {
work_done_progress: None,
},
resolve_provider: None,
})),
workspace_symbol_provider: Some(lsp_types::OneOf::Right(
lsp_types::WorkspaceSymbolOptions {
work_done_progress_options: WorkDoneProgressOptions {
work_done_progress: None,
},
resolve_provider: None,
},
)),
},
server_info: None,
})
Expand Down
Loading
Loading