Skip to content

Exclude comment sections from workspace symbols by default #866

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 2 commits into
base: task/refactor-config
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
23 changes: 23 additions & 0 deletions crates/ark/src/lsp/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,22 @@ pub static SETTINGS: &[Setting] = &[
.unwrap_or_else(|| SymbolsConfig::default().include_assignments_in_blocks)
},
},
Setting {
key: "positron.r.workspaceSymbols.includeCommentSections",
set: |cfg, v| {
cfg.workspace_symbols.include_comment_sections = v
.as_bool()
.unwrap_or_else(|| WorkspaceSymbolsConfig::default().include_comment_sections)
},
},
];

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

Expand All @@ -78,6 +87,12 @@ pub struct SymbolsConfig {
pub include_assignments_in_blocks: bool,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct WorkspaceSymbolsConfig {
/// Whether to include sections like `# My section ---` in workspace symbols.
pub include_comment_sections: bool,
}

/// Configuration of a document.
///
/// The naming follows <https://editorconfig.org/> where possible.
Expand Down Expand Up @@ -141,6 +156,14 @@ impl Default for SymbolsConfig {
}
}

impl Default for WorkspaceSymbolsConfig {
fn default() -> Self {
Self {
include_comment_sections: false,
}
}
}

impl Default for IndentationConfig {
fn default() -> Self {
Self {
Expand Down
3 changes: 2 additions & 1 deletion crates/ark/src/lsp/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ pub(crate) async fn handle_initialized(
#[tracing::instrument(level = "info", skip_all)]
pub(crate) fn handle_symbol(
params: WorkspaceSymbolParams,
state: &WorldState,
) -> anyhow::Result<Option<Vec<SymbolInformation>>> {
symbols::symbols(&params)
symbols::symbols(&params, state)
.map(|res| Some(res))
.or_else(|err| {
// Missing doc: Why are we not propagating errors to the frontend?
Expand Down
13 changes: 10 additions & 3 deletions crates/ark/src/lsp/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,17 @@ fn insert(path: &Path, entry: IndexEntry) -> anyhow::Result<()> {

let index = index.entry(path.to_string()).or_default();

// Retain the first occurrence in the index. In the future we'll track every occurrences and
// their scopes but for now we only track the first definition of an object (in a way, its
// We generally retain only the first occurrence in the index. In the
// future we'll track every occurrences and their scopes but for now we
// only track the first definition of an object (in a way, its
// declaration).
if !index.contains_key(&entry.key) {
if let Some(existing_entry) = index.get(&entry.key) {
// Give priority to non-section entries.
if matches!(existing_entry.data, IndexEntryData::Section { .. }) {
index.insert(entry.key.clone(), entry);
}
// Else, ignore.
} else {
index.insert(entry.key.clone(), entry);
}

Expand Down
2 changes: 1 addition & 1 deletion crates/ark/src/lsp/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ impl GlobalState {
respond(tx, || state_handlers::initialize(params, &mut self.lsp_state, &mut self.world), LspResponse::Initialize)?;
},
LspRequest::WorkspaceSymbol(params) => {
respond(tx, || handlers::handle_symbol(params), LspResponse::WorkspaceSymbol)?;
respond(tx, || handlers::handle_symbol(params, &self.world), LspResponse::WorkspaceSymbol)?;
},
LspRequest::DocumentSymbol(params) => {
respond(tx, || handlers::handle_document_symbol(params, &self.world), LspResponse::DocumentSymbol)?;
Expand Down
30 changes: 18 additions & 12 deletions crates/ark/src/lsp/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ fn new_symbol_node(
symbol
}

pub fn symbols(params: &WorkspaceSymbolParams) -> anyhow::Result<Vec<SymbolInformation>> {
pub(crate) fn symbols(
params: &WorkspaceSymbolParams,
state: &WorldState,
) -> anyhow::Result<Vec<SymbolInformation>> {
let query = &params.query;
let mut info: Vec<SymbolInformation> = Vec::new();

Expand All @@ -81,18 +84,21 @@ pub fn symbols(params: &WorkspaceSymbolParams) -> anyhow::Result<Vec<SymbolInfor
},

IndexEntryData::Section { level: _, title } => {
info.push(SymbolInformation {
name: title.to_string(),
kind: SymbolKind::STRING,
location: Location {
uri: Url::from_file_path(path).unwrap(),
range: entry.range,
},
tags: None,
deprecated: None,
container_name: None,
});
if state.config.workspace_symbols.include_comment_sections {
info.push(SymbolInformation {
name: title.to_string(),
kind: SymbolKind::STRING,
location: Location {
uri: Url::from_file_path(path).unwrap(),
range: entry.range,
},
tags: None,
deprecated: None,
container_name: None,
});
}
},

IndexEntryData::Variable { name } => {
info.push(SymbolInformation {
name: name.clone(),
Expand Down
Loading