Skip to content
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
4 changes: 4 additions & 0 deletions src/config/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ impl WorkspaceProtoConfigs {
Some(ipath)
}

pub fn get_workspaces(&self) -> Vec<&Url> {
self.workspaces.iter().collect()
}

pub fn no_workspace_mode(&mut self) {
let wr = ProtolsConfig::default();
let rp = if cfg!(target_os = "windows") {
Expand Down
32 changes: 31 additions & 1 deletion src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use async_lsp::lsp_types::{
RenameParams, ServerCapabilities, ServerInfo, TextDocumentPositionParams,
TextDocumentSyncCapability, TextDocumentSyncKind, TextEdit, Url, WorkspaceEdit,
WorkspaceFileOperationsServerCapabilities, WorkspaceFoldersServerCapabilities,
WorkspaceServerCapabilities,
WorkspaceServerCapabilities, WorkspaceSymbolParams, WorkspaceSymbolResponse,
};
use async_lsp::{LanguageClient, ResponseError};
use futures::future::BoxFuture;
Expand Down Expand Up @@ -113,6 +113,7 @@ impl ProtoLanguageServer {
definition_provider: Some(OneOf::Left(true)),
hover_provider: Some(HoverProviderCapability::Simple(true)),
document_symbol_provider: Some(OneOf::Left(true)),
workspace_symbol_provider: Some(OneOf::Left(true)),
completion_provider: Some(CompletionOptions::default()),
rename_provider: Some(rename_provider),
document_formatting_provider: Some(OneOf::Left(true)),
Expand Down Expand Up @@ -379,6 +380,35 @@ impl ProtoLanguageServer {
Box::pin(async move { Ok(Some(response)) })
}

pub(super) fn workspace_symbol(
&mut self,
params: WorkspaceSymbolParams,
) -> BoxFuture<'static, Result<Option<WorkspaceSymbolResponse>, ResponseError>> {
let query = params.query.to_lowercase();
let work_done_token = params.work_done_progress_params.work_done_token;

// Parse all files from all workspaces
let workspaces = self.configs.get_workspaces();
let progress_sender = work_done_token.map(|token| self.with_report_progress(token));

for workspace in workspaces {
if let Ok(workspace_path) = workspace.to_file_path() {
self.state
.parse_all_from_workspace(workspace_path, progress_sender.clone());
}
}

let symbols = self.state.find_workspace_symbols(&query);

Box::pin(async move {
if symbols.is_empty() {
Ok(None)
} else {
Ok(Some(WorkspaceSymbolResponse::Nested(symbols)))
}
})
}

pub(super) fn formatting(
&mut self,
params: DocumentFormattingParams,
Expand Down
2 changes: 2 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use async_lsp::{
request::{
Completion, DocumentSymbolRequest, Formatting, GotoDefinition, HoverRequest,
Initialize, PrepareRenameRequest, RangeFormatting, References, Rename,
WorkspaceSymbolRequest,
},
},
router::Router,
Expand Down Expand Up @@ -59,6 +60,7 @@ impl ProtoLanguageServer {
router.request::<References, _>(|st, params| st.references(params));
router.request::<GotoDefinition, _>(|st, params| st.definition(params));
router.request::<DocumentSymbolRequest, _>(|st, params| st.document_symbol(params));
router.request::<WorkspaceSymbolRequest, _>(|st, params| st.workspace_symbol(params));
router.request::<Formatting, _>(|st, params| st.formatting(params));
router.request::<RangeFormatting, _>(|st, params| st.range_formatting(params));

Expand Down
72 changes: 71 additions & 1 deletion src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use std::{
use tracing::info;

use async_lsp::lsp_types::ProgressParamsValue;
use async_lsp::lsp_types::{CompletionItem, CompletionItemKind, PublishDiagnosticsParams, Url};
use async_lsp::lsp_types::{
CompletionItem, CompletionItemKind, Location, OneOf, PublishDiagnosticsParams, Url,
WorkspaceSymbol,
};
use std::sync::mpsc::Sender;
use tree_sitter::Node;
use walkdir::WalkDir;
Expand Down Expand Up @@ -73,6 +76,73 @@ impl ProtoLanguageState {
.collect()
}

pub fn find_workspace_symbols(&self, query: &str) -> Vec<WorkspaceSymbol> {
let mut symbols = Vec::new();

for tree in self.get_trees() {
let content = self.get_content(&tree.uri);
let doc_symbols = tree.find_document_locations(content.as_bytes());

for doc_symbol in doc_symbols {
self.collect_workspace_symbols(&doc_symbol, &tree.uri, query, None, &mut symbols);
}
}

// Sort symbols by name and then by URI for consistent ordering
symbols.sort_by(|a, b| {
let name_cmp = a.name.cmp(&b.name);
if name_cmp != std::cmp::Ordering::Equal {
return name_cmp;
}
// Extract URI from location
match (&a.location, &b.location) {
(OneOf::Left(loc_a), OneOf::Left(loc_b)) => {
loc_a.uri.as_str().cmp(loc_b.uri.as_str())
}
_ => std::cmp::Ordering::Equal,
}
});

symbols
}

fn collect_workspace_symbols(
&self,
doc_symbol: &async_lsp::lsp_types::DocumentSymbol,
uri: &Url,
query: &str,
container_name: Option<String>,
symbols: &mut Vec<WorkspaceSymbol>,
) {
let symbol_name_lower = doc_symbol.name.to_lowercase();

if query.is_empty() || symbol_name_lower.contains(query) {
symbols.push(WorkspaceSymbol {
name: doc_symbol.name.clone(),
kind: doc_symbol.kind,
tags: doc_symbol.tags.clone(),
container_name: container_name.clone(),
location: OneOf::Left(Location {
uri: uri.clone(),
range: doc_symbol.range,
}),
data: None,
});
}

if let Some(children) = &doc_symbol.children {
for child in children {
self.collect_workspace_symbols(
child,
uri,
query,
Some(doc_symbol.name.clone()),
symbols,
);
}
}
}

fn upsert_content_impl(
&mut self,
uri: &Url,
Expand Down
1 change: 1 addition & 0 deletions src/workspace/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod definition;
mod hover;
mod rename;
mod workspace_symbol;
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: src/workspace/workspace_symbol.rs
expression: address_symbols
---
- name: Address
kind: 23
containerName: Author
location:
uri: "file:///home/runner/work/protols/protols/src/workspace/input/b.proto"
range:
start:
line: 9
character: 3
end:
line: 11
character: 4
- name: Address
kind: 23
containerName: Author
location:
uri: "file://input/b.proto"
range:
start:
line: 9
character: 3
end:
line: 11
character: 4
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
---
source: src/workspace/workspace_symbol.rs
expression: all_symbols
---
- name: Address
kind: 23
containerName: Author
location:
uri: "file:///home/runner/work/protols/protols/src/workspace/input/b.proto"
range:
start:
line: 9
character: 3
end:
line: 11
character: 4
- name: Address
kind: 23
containerName: Author
location:
uri: "file://input/b.proto"
range:
start:
line: 9
character: 3
end:
line: 11
character: 4
- name: Author
kind: 23
location:
uri: "file:///home/runner/work/protols/protols/src/workspace/input/b.proto"
range:
start:
line: 5
character: 0
end:
line: 14
character: 1
- name: Author
kind: 23
location:
uri: "file://input/b.proto"
range:
start:
line: 5
character: 0
end:
line: 14
character: 1
- name: Baz
kind: 23
containerName: Foobar
location:
uri: "file:///home/runner/work/protols/protols/src/workspace/input/c.proto"
range:
start:
line: 8
character: 3
end:
line: 10
character: 4
- name: Baz
kind: 23
containerName: Foobar
location:
uri: "file://input/c.proto"
range:
start:
line: 8
character: 3
end:
line: 10
character: 4
- name: Book
kind: 23
location:
uri: "file://input/a.proto"
range:
start:
line: 9
character: 0
end:
line: 14
character: 1
- name: Foobar
kind: 23
location:
uri: "file:///home/runner/work/protols/protols/src/workspace/input/c.proto"
range:
start:
line: 5
character: 0
end:
line: 13
character: 1
- name: Foobar
kind: 23
location:
uri: "file://input/c.proto"
range:
start:
line: 5
character: 0
end:
line: 13
character: 1
- name: SomeSecret
kind: 23
location:
uri: "file:///home/runner/work/protols/protols/src/workspace/input/inner/secret/y.proto"
range:
start:
line: 5
character: 0
end:
line: 7
character: 1
- name: Why
kind: 23
location:
uri: "file:///home/runner/work/protols/protols/src/workspace/input/inner/x.proto"
range:
start:
line: 7
character: 0
end:
line: 11
character: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
source: src/workspace/workspace_symbol.rs
expression: author_symbols
---
- name: Author
kind: 23
location:
uri: "file:///home/runner/work/protols/protols/src/workspace/input/b.proto"
range:
start:
line: 5
character: 0
end:
line: 14
character: 1
- name: Author
kind: 23
location:
uri: "file://input/b.proto"
range:
start:
line: 5
character: 0
end:
line: 14
character: 1
40 changes: 40 additions & 0 deletions src/workspace/workspace_symbol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#[cfg(test)]
mod test {
use insta::assert_yaml_snapshot;

use crate::config::Config;
use crate::state::ProtoLanguageState;

#[test]
fn test_workspace_symbols() {
let ipath = vec![std::env::current_dir().unwrap().join("src/workspace/input")];
let a_uri = "file://input/a.proto".parse().unwrap();
let b_uri = "file://input/b.proto".parse().unwrap();
let c_uri = "file://input/c.proto".parse().unwrap();

let a = include_str!("input/a.proto");
let b = include_str!("input/b.proto");
let c = include_str!("input/c.proto");

let mut state: ProtoLanguageState = ProtoLanguageState::new();
state.upsert_file(&a_uri, a.to_owned(), &ipath, 3, &Config::default(), false);
state.upsert_file(&b_uri, b.to_owned(), &ipath, 2, &Config::default(), false);
state.upsert_file(&c_uri, c.to_owned(), &ipath, 2, &Config::default(), false);

// Test empty query - should return all symbols
let all_symbols = state.find_workspace_symbols("");
assert_yaml_snapshot!("all_symbols", all_symbols);

// Test query for "author" - should match Author and Address
let author_symbols = state.find_workspace_symbols("author");
assert_yaml_snapshot!("author_symbols", author_symbols);

// Test query for "address" - should match Address
let address_symbols = state.find_workspace_symbols("address");
assert_yaml_snapshot!("address_symbols", address_symbols);

// Test query that should not match anything
let no_match = state.find_workspace_symbols("nonexistent");
assert!(no_match.is_empty());
}
}