Skip to content

Don't emit nested objects as document symbols #859

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

Open
wants to merge 4 commits into
base: feature/method-symbols
Choose a base branch
from
Open
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
36 changes: 33 additions & 3 deletions crates/ark/src/lsp/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ use crate::lsp;
use crate::lsp::diagnostics::DiagnosticsConfig;

/// Configuration of the LSP
#[derive(Clone, Debug)]
#[derive(Clone, Default, Debug)]
pub(crate) struct LspConfig {
pub(crate) diagnostics: DiagnosticsConfig,
pub(crate) symbols: SymbolsConfig,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct SymbolsConfig {
/// Whether to emit assignments in `{` bloks as document symbols.
pub include_assignments_in_blocks: bool,
}

/// Configuration of a document.
Expand Down Expand Up @@ -53,17 +60,23 @@ pub(crate) struct VscDiagnosticsConfig {
pub enable: bool,
}

#[derive(Serialize, Deserialize, FieldNamesAsArray, Clone, Debug)]
pub(crate) struct VscSymbolsConfig {
// DEV NOTE: Update `section_from_key()` method after adding a field
pub include_assignments_in_blocks: bool,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(untagged)]
pub(crate) enum VscIndentSize {
Alias(String),
Size(usize),
}

impl Default for LspConfig {
impl Default for SymbolsConfig {
fn default() -> Self {
Self {
diagnostics: Default::default(),
include_assignments_in_blocks: false,
}
}
}
Expand Down Expand Up @@ -134,6 +147,23 @@ impl From<VscDiagnosticsConfig> for DiagnosticsConfig {
}
}

impl VscSymbolsConfig {
pub(crate) fn section_from_key(key: &str) -> &str {
match key {
"include_assignments_in_blocks" => "positron.r.symbols.includeAssignmentsInBlocks",
_ => "unknown", // To be caught via downstream errors
}
}
}

impl From<VscSymbolsConfig> for SymbolsConfig {
fn from(value: VscSymbolsConfig) -> Self {
Self {
include_assignments_in_blocks: value.include_assignments_in_blocks,
}
}
}

pub(crate) fn indent_style_from_lsp(insert_spaces: bool) -> IndentStyle {
if insert_spaces {
IndentStyle::Space
Expand Down
6 changes: 6 additions & 0 deletions crates/ark/src/lsp/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use crate::lsp::completions::provide_completions;
use crate::lsp::completions::resolve_completion;
use crate::lsp::config::VscDiagnosticsConfig;
use crate::lsp::config::VscDocumentConfig;
use crate::lsp::config::VscSymbolsConfig;
use crate::lsp::definitions::goto_definition;
use crate::lsp::document_context::DocumentContext;
use crate::lsp::encoding::convert_lsp_range_to_tree_sitter_range;
Expand Down Expand Up @@ -109,12 +110,17 @@ pub(crate) async fn handle_initialized(
VscDocumentConfig::FIELD_NAMES_AS_ARRAY.to_vec(),
VscDocumentConfig::section_from_key,
);
let mut config_symbols_regs: Vec<Registration> = collect_regs(
VscSymbolsConfig::FIELD_NAMES_AS_ARRAY.to_vec(),
VscSymbolsConfig::section_from_key,
);
let mut config_diagnostics_regs: Vec<Registration> = collect_regs(
VscDiagnosticsConfig::FIELD_NAMES_AS_ARRAY.to_vec(),
VscDiagnosticsConfig::section_from_key,
);

regs.append(&mut config_document_regs);
regs.append(&mut config_symbols_regs);
regs.append(&mut config_diagnostics_regs);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
source: crates/ark/src/lsp/symbols.rs
expression: "test_symbol(\"foo <- function() { bar <- function() 1 }\")"
---
[
DocumentSymbol {
name: "foo",
detail: Some(
"function()",
),
kind: Function,
tags: None,
deprecated: None,
range: Range {
start: Position {
line: 0,
character: 0,
},
end: Position {
line: 0,
character: 41,
},
},
selection_range: Range {
start: Position {
line: 0,
character: 0,
},
end: Position {
line: 0,
character: 41,
},
},
children: Some(
[
DocumentSymbol {
name: "bar",
detail: Some(
"function()",
),
kind: Function,
tags: None,
deprecated: None,
range: Range {
start: Position {
line: 0,
character: 20,
},
end: Position {
line: 0,
character: 39,
},
},
selection_range: Range {
start: Position {
line: 0,
character: 20,
},
end: Position {
line: 0,
character: 39,
},
},
children: Some(
[],
),
},
],
),
},
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
source: crates/ark/src/lsp/symbols.rs
expression: "test_symbol(\"\nlocal({\n inner1 <- 1 # Not a symbol\n})\na <- function() {\n inner2 <- 2 # Not a symbol\n inner3 <- function() 3 # Symbol\n}\n\")"
---
[
DocumentSymbol {
name: "a",
detail: Some(
"function()",
),
kind: Function,
tags: None,
deprecated: None,
range: Range {
start: Position {
line: 4,
character: 0,
},
end: Position {
line: 7,
character: 1,
},
},
selection_range: Range {
start: Position {
line: 4,
character: 0,
},
end: Position {
line: 7,
character: 1,
},
},
children: Some(
[
DocumentSymbol {
name: "inner3",
detail: Some(
"function()",
),
kind: Function,
tags: None,
deprecated: None,
range: Range {
start: Position {
line: 6,
character: 2,
},
end: Position {
line: 6,
character: 24,
},
},
selection_range: Range {
start: Position {
line: 6,
character: 2,
},
end: Position {
line: 6,
character: 24,
},
},
children: Some(
[],
),
},
],
),
},
]
31 changes: 30 additions & 1 deletion crates/ark/src/lsp/state_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ use crate::lsp;
use crate::lsp::capabilities::Capabilities;
use crate::lsp::config::indent_style_from_lsp;
use crate::lsp::config::DocumentConfig;
use crate::lsp::config::SymbolsConfig;
use crate::lsp::config::VscDiagnosticsConfig;
use crate::lsp::config::VscDocumentConfig;
use crate::lsp::config::VscSymbolsConfig;
use crate::lsp::diagnostics::DiagnosticsConfig;
use crate::lsp::documents::Document;
use crate::lsp::encoding::get_position_encoding_kind;
Expand Down Expand Up @@ -244,6 +246,9 @@ pub(crate) async fn did_change_configuration(
// we should just ignore it. Instead we need to pull the settings again for
// all URI of interest.

// Note that the client sends notifications for settings for which we have
// declared interest in. This registration is done in `handle_initialized()`.

update_config(workspace_uris(state), client, state)
.instrument(tracing::info_span!("did_change_configuration"))
.await
Expand Down Expand Up @@ -296,6 +301,16 @@ async fn update_config(
.collect();
items.append(&mut diagnostics_items);

let symbols_keys = VscSymbolsConfig::FIELD_NAMES_AS_ARRAY;
let mut symbols_items: Vec<ConfigurationItem> = symbols_keys
.iter()
.map(|key| ConfigurationItem {
scope_uri: None,
section: Some(VscSymbolsConfig::section_from_key(key).into()),
})
.collect();
items.append(&mut symbols_items);

// For document configs we collect all pairs of URIs and config keys of
// interest in a flat vector
let document_keys = VscDocumentConfig::FIELD_NAMES_AS_ARRAY;
Expand All @@ -316,7 +331,8 @@ async fn update_config(
// by chunk
let n_document_items = document_keys.len();
let n_diagnostics_items = diagnostics_keys.len();
let n_items = n_diagnostics_items + (n_document_items * uris.len());
let n_symbols_items = symbols_keys.len();
let n_items = n_diagnostics_items + n_symbols_items + (n_document_items * uris.len());

if configs.len() != n_items {
return Err(anyhow!(
Expand Down Expand Up @@ -351,6 +367,19 @@ async fn update_config(
lsp::spawn_diagnostics_refresh_all(state.clone());
}

// --- Symbols
let keys = symbols_keys.into_iter();
let items: Vec<Value> = configs.by_ref().take(n_symbols_items).collect();

let mut map = serde_json::Map::new();
std::iter::zip(keys, items).for_each(|(key, item)| {
map.insert(key.into(), item);
});

let config: VscSymbolsConfig = serde_json::from_value(serde_json::Value::Object(map))?;
let config: SymbolsConfig = config.into();
state.config.symbols = config;

// --- Documents
// For each document, deserialise the vector of JSON values into a typed config
for uri in uris.into_iter() {
Expand Down
Loading
Loading