Skip to content

Commit

Permalink
feat: LSP inlay hints for let and global (#5510)
Browse files Browse the repository at this point in the history
# Description

## Problem

Resolves #5513

## Summary

Shows type hints for `let` and `global`. Note that right now they will
always show for `let` and `global` that don't have an explicit type.
Eventually it would be nice to have a configuration to toggle them, but
the noise they introduce isn't that much so we could do that in a
separate PR (also paving the way for configuring other types of inlay
hints, like parameter names or names next to closing braces).


https://github.com/user-attachments/assets/4b743873-e1bf-430b-8632-4020ea53fbd8

## Additional Context

If you try it, hovering over types might not work well because of #5515,
so you could merge that branch into this one before testing things.

## Documentation

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.

---------

Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
  • Loading branch information
asterite and TomAFrench authored Jul 17, 2024
1 parent 45e82a6 commit 43f5b8d
Show file tree
Hide file tree
Showing 11 changed files with 696 additions and 9 deletions.
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.

4 changes: 4 additions & 0 deletions compiler/noirc_errors/src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ impl Span {
self.start() <= other.start() && self.end() >= other.end()
}

pub fn intersects(&self, other: &Span) -> bool {
self.end() > other.start() && self.start() < other.end()
}

pub fn is_smaller(&self, other: &Span) -> bool {
let self_distance = self.end() - self.start();
let other_distance = other.end() - other.start();
Expand Down
1 change: 1 addition & 0 deletions tooling/lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ license.workspace = true
acvm.workspace = true
codespan-lsp.workspace = true
lsp-types.workspace = true
lsp_types_0_88_0 = { package = "lsp-types", version = "0.88.0" }
nargo.workspace = true
nargo_fmt.workspace = true
nargo_toml.workspace = true
Expand Down
9 changes: 5 additions & 4 deletions tooling/lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use async_lsp::{
use fm::{codespan_files as files, FileManager};
use fxhash::FxHashSet;
use lsp_types::{
request::{HoverRequest, PrepareRenameRequest, References, Rename},
request::{HoverRequest, InlayHintRequest, PrepareRenameRequest, References, Rename},
CodeLens,
};
use nargo::{
Expand All @@ -46,9 +46,9 @@ use notifications::{
};
use requests::{
on_code_lens_request, on_formatting, on_goto_declaration_request, on_goto_definition_request,
on_goto_type_definition_request, on_hover_request, on_initialize, on_prepare_rename_request,
on_profile_run_request, on_references_request, on_rename_request, on_shutdown,
on_test_run_request, on_tests_request,
on_goto_type_definition_request, on_hover_request, on_initialize, on_inlay_hint_request,
on_prepare_rename_request, on_profile_run_request, on_references_request, on_rename_request,
on_shutdown, on_test_run_request, on_tests_request,
};
use serde_json::Value as JsonValue;
use thiserror::Error;
Expand Down Expand Up @@ -130,6 +130,7 @@ impl NargoLspService {
.request::<PrepareRenameRequest, _>(on_prepare_rename_request)
.request::<Rename, _>(on_rename_request)
.request::<HoverRequest, _>(on_hover_request)
.request::<InlayHintRequest, _>(on_inlay_hint_request)
.notification::<notification::Initialized>(on_initialized)
.notification::<notification::DidChangeConfiguration>(on_did_change_configuration)
.notification::<notification::DidOpenTextDocument>(on_did_open_text_document)
Expand Down
33 changes: 33 additions & 0 deletions tooling/lsp/src/requests/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ fn on_goto_definition_inner(
process_request(state, params.text_document_position_params, |args| {
args.interner
.get_definition_location_from(args.location, return_type_location_instead)
.or_else(|| {
args.interner
.reference_at_location(args.location)
.map(|reference| args.interner.reference_location(reference))
})
.and_then(|found_location| {
let file_id = found_location.file;
let definition_position =
Expand Down Expand Up @@ -202,4 +207,32 @@ mod goto_definition_tests {
async fn goto_for_local_variable() {
expect_goto_for_all_references("local_variable", "some_var", 0).await;
}

#[test]
async fn goto_at_struct_definition_finds_same_struct() {
expect_goto(
"go_to_definition",
Position { line: 21, character: 7 }, // "Foo" in "struct Foo"
"src/main.nr",
Range {
start: Position { line: 21, character: 7 },
end: Position { line: 21, character: 10 },
},
)
.await;
}

#[test]
async fn goto_at_trait_definition_finds_same_trait() {
expect_goto(
"go_to_definition",
Position { line: 25, character: 6 }, // "Trait" in "trait Trait"
"src/main.nr",
Range {
start: Position { line: 25, character: 6 },
end: Position { line: 25, character: 11 },
},
)
.await;
}
}
Loading

0 comments on commit 43f5b8d

Please sign in to comment.