Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 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 @@ -16,6 +16,7 @@ workspace = true
acvm.workspace = true
codespan-lsp.workspace = true
nargo = { workspace = true, features = ["rpc"] }
nargo_expand.workspace = true
nargo_fmt.workspace = true
nargo_toml.workspace = true
noirc_driver.workspace = true
Expand Down
3 changes: 3 additions & 0 deletions tooling/lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ use solver::WrapperSolver;
use types::{NargoTest, NargoTestId, Position, Range, Url, notification, request};
use with_file::parsed_module_with_file;

use crate::{requests::on_expand_request, types::request::NargoExpand};

#[derive(Debug, Error)]
pub enum LspError {
/// Error while Resolving Workspace.
Expand Down Expand Up @@ -170,6 +172,7 @@ impl NargoLspService {
.request::<SignatureHelpRequest, _>(on_signature_help_request)
.request::<CodeActionRequest, _>(on_code_action_request)
.request::<WorkspaceSymbolRequest, _>(on_workspace_symbol_request)
.request::<NargoExpand, _>(on_expand_request)
.notification::<notification::Initialized>(on_initialized)
.notification::<notification::DidChangeConfiguration>(on_did_change_configuration)
.notification::<notification::DidOpenTextDocument>(on_did_open_text_document)
Expand Down
2 changes: 1 addition & 1 deletion tooling/lsp/src/requests/code_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
byte_range,
args.crate_id,
args.def_maps,
args.dependencies,
args.dependencies(),
args.interner,
args.usage_tracker,
);
Expand Down Expand Up @@ -186,7 +186,7 @@

CodeAction {
title,
kind: Some(CodeActionKind::QUICKFIX),

Check warning on line 189 in tooling/lsp/src/requests/code_action.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (QUICKFIX)
diagnostics: None,
edit: Some(workspace_edit),
command: None,
Expand Down
2 changes: 1 addition & 1 deletion tooling/lsp/src/requests/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
use super::{TraitReexport, process_request};

mod auto_import;
mod builtins;

Check warning on line 55 in tooling/lsp/src/requests/completion.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (builtins)
mod completion_items;
mod kinds;
mod sort_text;
Expand Down Expand Up @@ -86,7 +86,7 @@
byte,
args.crate_id,
args.def_maps,
args.dependencies,
args.dependencies(),
args.interner,
);
finder.find(&parsed_module)
Expand Down Expand Up @@ -1993,8 +1993,8 @@
///
/// For example:
///
/// // "merk" and "ro" match "merkle" and "root" and are in order

Check warning on line 1996 in tooling/lsp/src/requests/completion.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (merk)
/// name_matches("compute_merkle_root", "merk_ro") == true

Check warning on line 1997 in tooling/lsp/src/requests/completion.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (merk)
///
/// // "ro" matches "root", but "merkle" comes before it, so no match
/// name_matches("compute_merkle_root", "ro_mer") == false
Expand Down
26 changes: 26 additions & 0 deletions tooling/lsp/src/requests/expand.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::future;

use async_lsp::{ResponseError, lsp_types::TextDocumentPositionParams};
use nargo_expand::get_expanded_crate;

use crate::{
LspState,
requests::process_request,
types::{NargoExpandParams, NargoExpandResult},
};

pub(crate) fn on_expand_request(
state: &mut LspState,
params: NargoExpandParams,
) -> impl Future<Output = Result<NargoExpandResult, ResponseError>> + use<> {
let text_document_position_params = TextDocumentPositionParams {
text_document: params.text_document,
position: params.position,
};

let result = process_request(state, text_document_position_params, |args| {
get_expanded_crate(args.crate_id, args.crate_graph, args.def_maps, args.interner)
});

future::ready(result)
}
11 changes: 8 additions & 3 deletions tooling/lsp/src/requests/hover/from_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn format_module(id: ModuleId, args: &ProcessRequestCallbackArgs) -> Option<Stri
let mut string = String::new();

if id.local_id == crate_root {
let dep = args.dependencies.iter().find(|dep| dep.crate_id == id.krate)?;
let dep = args.dependencies().iter().find(|dep| dep.crate_id == id.krate)?;
string.push_str(" crate ");
string.push_str(&dep.name.to_string());
} else {
Expand Down Expand Up @@ -682,8 +682,13 @@ fn format_parent_module_from_module_id(
args: &ProcessRequestCallbackArgs,
string: &mut String,
) -> bool {
let full_path =
module_full_path(module, args.interner, args.crate_id, &args.crate_name, args.dependencies);
let full_path = module_full_path(
module,
args.interner,
args.crate_id,
&args.crate_name,
args.dependencies(),
);
if full_path.is_empty() {
return false;
}
Expand Down
22 changes: 15 additions & 7 deletions tooling/lsp/src/requests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use nargo_fmt::Config;

use noirc_frontend::ast::Ident;
use noirc_frontend::graph::CrateId;
use noirc_frontend::graph::{CrateGraph, CrateId};
use noirc_frontend::hir::def_map::{CrateDefMap, ModuleId};
use noirc_frontend::node_interner::ReferenceId;
use noirc_frontend::parser::ParserError;
Expand Down Expand Up @@ -49,6 +49,7 @@
mod code_lens_request;
mod completion;
mod document_symbol;
mod expand;
mod goto_declaration;
mod goto_definition;
mod hover;
Expand All @@ -63,9 +64,10 @@
pub(crate) use {
code_action::on_code_action_request, code_lens_request::collect_lenses_for_package,
code_lens_request::on_code_lens_request, completion::on_completion_request,
document_symbol::on_document_symbol_request, goto_declaration::on_goto_declaration_request,
goto_definition::on_goto_definition_request, goto_definition::on_goto_type_definition_request,
hover::on_hover_request, inlay_hint::on_inlay_hint_request, references::on_references_request,
document_symbol::on_document_symbol_request, expand::on_expand_request,
goto_declaration::on_goto_declaration_request, goto_definition::on_goto_definition_request,
goto_definition::on_goto_type_definition_request, 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, workspace_symbol::on_workspace_symbol_request,
Expand Down Expand Up @@ -279,14 +281,14 @@
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 284 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 291 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,
},
Expand Down Expand Up @@ -458,11 +460,17 @@
package_cache: &'a HashMap<PathBuf, PackageCacheData>,
crate_id: CrateId,
crate_name: String,
dependencies: &'a Vec<Dependency>,
crate_graph: &'a CrateGraph,
def_maps: &'a BTreeMap<CrateId, CrateDefMap>,
usage_tracker: &'a UsageTracker,
}

impl<'a> ProcessRequestCallbackArgs<'a> {
pub(crate) fn dependencies(&self) -> &'a Vec<Dependency> {
&self.crate_graph[self.crate_id].dependencies
}
}

pub(crate) fn process_request<F, T>(
state: &mut LspState,
text_document_position_params: TextDocumentPositionParams,
Expand Down Expand Up @@ -516,7 +524,7 @@
package_cache: &state.package_cache,
crate_id,
crate_name: package.name.to_string(),
dependencies: &crate_graph[crate_id].dependencies,
crate_graph,
def_maps,
usage_tracker,
}))
Expand Down Expand Up @@ -581,7 +589,7 @@
package_cache: &state.package_cache,
crate_id,
crate_name: package.name.to_string(),
dependencies: &context.crate_graph[crate_id].dependencies,
crate_graph: &context.crate_graph,
def_maps,
usage_tracker,
}))
Expand Down
22 changes: 21 additions & 1 deletion tooling/lsp/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use async_lsp::lsp_types::{
CodeActionOptions, CompletionOptions, DeclarationCapability, DefinitionOptions,
DocumentSymbolOptions, HoverOptions, InlayHintOptions, OneOf, ReferencesOptions, RenameOptions,
SignatureHelpOptions, TypeDefinitionProviderCapability, WorkspaceSymbolOptions,
SignatureHelpOptions, TextDocumentIdentifier, TypeDefinitionProviderCapability,
WorkspaceSymbolOptions,
};
use noirc_frontend::graph::CrateName;
use serde::{Deserialize, Serialize};
Expand All @@ -17,6 +18,8 @@ pub(crate) use async_lsp::lsp_types::{
pub(crate) mod request {
use async_lsp::lsp_types::{InitializeParams, request::Request};

use crate::types::{NargoExpandParams, NargoExpandResult};

use super::{
InitializeResult, NargoTestRunParams, NargoTestRunResult, NargoTestsParams,
NargoTestsResult,
Expand Down Expand Up @@ -51,6 +54,14 @@ pub(crate) mod request {
type Result = NargoTestsResult;
const METHOD: &'static str = "nargo/tests";
}

#[derive(Debug)]
pub(crate) struct NargoExpand;
impl Request for NargoExpand {
type Params = NargoExpandParams;
type Result = NargoExpandResult;
const METHOD: &'static str = "nargo/expand";
}
}

pub(crate) mod notification {
Expand Down Expand Up @@ -245,6 +256,15 @@ pub(crate) struct NargoTestRunResult {
pub(crate) message: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct NargoExpandParams {
pub(crate) text_document: TextDocumentIdentifier,
pub(crate) position: Position,
}

pub(crate) type NargoExpandResult = String;

pub(crate) type CodeLensResult = Option<Vec<CodeLens>>;
pub(crate) type GotoDefinitionResult = Option<async_lsp::lsp_types::GotoDefinitionResponse>;
pub(crate) type GotoDeclarationResult =
Expand Down
2 changes: 1 addition & 1 deletion tooling/nargo_cli/src/cli/expand_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ fn get_expanded_package_or_error(

check_crate_and_report_errors(&mut context, crate_id, compile_options)?;

Ok(get_expanded_crate(&context, crate_id))
Ok(get_expanded_crate(crate_id, &context.crate_graph, &context.def_maps, &context.def_interner))
}
30 changes: 17 additions & 13 deletions tooling/nargo_expand/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use std::collections::BTreeMap;

use nargo_fmt::ImportsGranularity;
use noirc_driver::CrateId;
use noirc_frontend::{
hir::{Context, def_map::ModuleId},
graph::CrateGraph,
hir::def_map::{CrateDefMap, ModuleId},
node_interner::NodeInterner,
parse_program_with_dummy_file,
};

Expand All @@ -11,24 +15,24 @@ mod items;
mod printer;

/// Returns the expanded code for the given crate.
/// Note that `context` must have `activate_lsp_mode` called on it before invoking this function.
pub fn get_expanded_crate(context: &Context, crate_id: CrateId) -> String {
let root_module_id = context.def_maps[&crate_id].root();
/// Note that `context` that holds the crate graph, def maps and interner
/// must have `activate_lsp_mode` called on it before invoking this function.
pub fn get_expanded_crate(
crate_id: CrateId,
crate_graph: &CrateGraph,
def_maps: &BTreeMap<CrateId, CrateDefMap>,
interner: &NodeInterner,
) -> String {
let root_module_id = def_maps[&crate_id].root();
let module_id = ModuleId { krate: crate_id, local_id: root_module_id };

let mut builder = ItemBuilder::new(crate_id, &context.def_interner, &context.def_maps);
let mut builder = ItemBuilder::new(crate_id, interner, def_maps);
let item = builder.build_module(module_id);

let dependencies = &context.crate_graph[context.root_crate_id()].dependencies;
let dependencies = &crate_graph[crate_id].dependencies;

let mut string = String::new();
let mut printer = ItemPrinter::new(
crate_id,
&context.def_interner,
&context.def_maps,
dependencies,
&mut string,
);
let mut printer = ItemPrinter::new(crate_id, interner, def_maps, dependencies, &mut string);
printer.show_item(item);

let (parsed_module, errors) = parse_program_with_dummy_file(&string);
Expand Down
Loading