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

Extend the symbol range to include the rhs #1887

Merged
merged 2 commits into from
Apr 11, 2024
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
37 changes: 32 additions & 5 deletions lsp/nls/src/requests/symbols.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use lsp_server::{RequestId, Response, ResponseError};
use lsp_types::{DocumentSymbol, DocumentSymbolParams, SymbolKind};
use nickel_lang_core::position::RawSpan;
use nickel_lang_core::term::RichTerm;
use nickel_lang_core::typ::Type;

Expand Down Expand Up @@ -36,10 +37,36 @@ fn symbols(
.flat_map(|rt| {
rt.fields.into_iter().filter_map(|(id, field)| {
let ty = type_lookups.idents.get(&id.into());
let (file_id, span) = id.pos.into_opt()?.to_range();
let range =
crate::codespan_lsp::byte_span_to_range(world.cache.files(), file_id, span)
.ok()?;
let id_pos = id.pos.into_opt()?;
let (file_id, id_span) = id_pos.to_range();

// We need to find the span of the name (that's id_span above), but also the
// span of the "whole value," whatever that means. In vscode, there's a little
// outline bar at the top that shows you which symbol you're currently in, and it
// works by checking whether the cursor is inside the "whole value" range.
// We take this range large enough to contain the field value
// (if there is one) and any other annotations that we can work
// out the positions of.
let val_span = field
.metadata
.annotation
.iter()
.filter_map(|ty| ty.typ.pos.into_opt())
.chain(field.value.as_ref().and_then(|val| val.pos.into_opt()))
.fold(id_pos, |a, b| RawSpan::fuse(a, b).unwrap_or(a));

let selection_range = crate::codespan_lsp::byte_span_to_range(
world.cache.files(),
file_id,
id_span.clone(),
)
.ok()?;
let range = crate::codespan_lsp::byte_span_to_range(
world.cache.files(),
file_id,
val_span.to_range().1,
)
.ok()?;

let children = max_depth
.checked_sub(1)
Expand All @@ -53,7 +80,7 @@ fn symbols(
kind: SymbolKind::VARIABLE,
tags: None,
range,
selection_range: range,
selection_range,
children,
deprecated: None,
})
Expand Down
2 changes: 2 additions & 0 deletions lsp/nls/tests/inputs/symbols-basic.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ let func = fun x => 1 in
type_checked_block = {
inner_name = { name = "value" },
} : _,
annotated | String,
docced | doc "The symbol range doesn't currently include docs",
}
### [[request]]
### type = "Symbols"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: lsp/nls/tests/main.rs
expression: output
---
[name (Variable / "String")@3:2-3:6 in 3:2-3:6: [], other_name (Variable / "Dyn")@4:2-4:12 in 4:2-4:12: [inner_name (Variable / "Dyn")@5:4-5:14 in 5:4-5:14: []], type_checked_block (Variable / "Dyn")@7:2-7:20 in 7:2-7:20: [inner_name (Variable / "{ name : String }")@8:4-8:14 in 8:4-8:14: [name (Variable / "String")@8:19-8:23 in 8:19-8:23: []]]]
[name (Variable / "String")@3:2-3:6 in 3:2-3:16: [], other_name (Variable / "Dyn")@4:2-4:12 in 4:2-6:3: [inner_name (Variable / "Dyn")@5:4-5:14 in 5:4-5:49: []], type_checked_block (Variable / "Dyn")@7:2-7:20 in 7:2-9:7: [inner_name (Variable / "{ name : String }")@8:4-8:14 in 8:4-8:35: [name (Variable / "String")@8:19-8:23 in 8:19-8:33: []]], annotated (Variable / "String")@10:2-10:11 in 10:2-10:20: [], docced (Variable / "Dyn")@11:2-11:8 in 11:2-11:8: []]
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: lsp/nls/tests/main.rs
expression: output
---
[bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:19: []]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
[bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: [bar (Variable / "Dyn")@0:16-0:19 in 0:16-0:25: []]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
Loading