Skip to content

Commit 6dce17e

Browse files
committed
refactor: Move symbol features into separate module
1 parent bb0915c commit 6dce17e

File tree

4 files changed

+103
-122
lines changed

4 files changed

+103
-122
lines changed

crates/roughly/src/index.rs

Lines changed: 1 addition & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use {
2-
crate::{
3-
lsp_types::{Url as Uri, *},
4-
tree, utils,
5-
},
2+
crate::{lsp_types::Range, tree, utils},
63
ropey::Rope,
74
std::{
85
collections::HashMap,
@@ -42,23 +39,6 @@ pub enum ItemInfo {
4239
R6Field,
4340
}
4441

45-
fn to_symbol_kind(info: &ItemInfo) -> SymbolKind {
46-
match info {
47-
ItemInfo::Unknown => SymbolKind::VARIABLE,
48-
ItemInfo::Integer | ItemInfo::Float | ItemInfo::Complex => SymbolKind::NUMBER,
49-
ItemInfo::Bool => SymbolKind::BOOLEAN,
50-
ItemInfo::String => SymbolKind::STRING,
51-
ItemInfo::Null => SymbolKind::NULL,
52-
ItemInfo::Function => SymbolKind::FUNCTION,
53-
ItemInfo::S4Class => SymbolKind::CLASS,
54-
ItemInfo::S4Generic => SymbolKind::INTERFACE,
55-
ItemInfo::S4Method { .. } => SymbolKind::METHOD,
56-
ItemInfo::R6Class => SymbolKind::CLASS,
57-
ItemInfo::R6Method => SymbolKind::METHOD,
58-
ItemInfo::R6Field => SymbolKind::FIELD,
59-
}
60-
}
61-
6242
impl Item {
6343
pub fn new(
6444
name: String,
@@ -84,25 +64,6 @@ impl Item {
8464
_ => self.name.clone(),
8565
}
8666
}
87-
88-
pub fn to_document_symbol(&self) -> DocumentSymbol {
89-
#[allow(deprecated)]
90-
DocumentSymbol {
91-
name: self.display_name(),
92-
kind: to_symbol_kind(&self.info),
93-
detail: self.detail.clone(),
94-
tags: None,
95-
range: self.range,
96-
selection_range: self.selection_range,
97-
children: self.children.as_ref().map(|children| {
98-
children
99-
.iter()
100-
.map(|child| child.to_document_symbol())
101-
.collect()
102-
}),
103-
deprecated: None,
104-
}
105-
}
10667
}
10768

10869
pub trait SymbolsMap {
@@ -131,42 +92,6 @@ impl SymbolsMap for HashMap<PathBuf, Vec<Item>> {
13192
}
13293
}
13394

134-
pub fn get_workspace_symbols(
135-
query: &str,
136-
workspace_symbols: &impl SymbolsMap,
137-
) -> Vec<WorkspaceSymbol> {
138-
workspace_symbols.filter_map(
139-
|path, symbols| {
140-
let uri = Uri::from_file_path(path).unwrap();
141-
symbols
142-
.iter()
143-
.flat_map(|symbol| {
144-
std::iter::once((symbol, None)).chain(
145-
symbol
146-
.children
147-
.as_ref()
148-
.into_iter()
149-
.flatten()
150-
.map(|child| (child, Some(symbol.name.as_ref()))),
151-
)
152-
})
153-
.filter(|(symbol, _)| utils::starts_with_lowercase(&symbol.display_name(), query))
154-
.map(move |(symbol, container_name)| WorkspaceSymbol {
155-
name: symbol.display_name(),
156-
kind: to_symbol_kind(&symbol.info),
157-
tags: None,
158-
container_name: container_name.map(str::to_string),
159-
location: OneOf::Left(Location {
160-
uri: uri.clone(),
161-
range: symbol.range,
162-
}),
163-
data: None,
164-
})
165-
},
166-
128,
167-
)
168-
}
169-
17095
#[derive(Debug)]
17196
pub struct IndexError;
17297

@@ -554,27 +479,6 @@ fn index_call(call: Node, rope: &Rope, nested: bool) -> Option<Item> {
554479
}
555480
}
556481

557-
pub fn document_symbol(
558-
name: String,
559-
kind: SymbolKind,
560-
detail: Option<String>,
561-
range: Range,
562-
selection_range: Range,
563-
children: Option<Vec<DocumentSymbol>>,
564-
) -> DocumentSymbol {
565-
#[allow(deprecated)]
566-
DocumentSymbol {
567-
name,
568-
kind,
569-
detail,
570-
tags: None,
571-
range,
572-
selection_range,
573-
children,
574-
deprecated: None, // required for struct, but deprecated; allow deprecated field
575-
}
576-
}
577-
578482
// note: this function shouldn't be used for keyword-only arguments (arguments after ...)
579483
pub fn get_argument<'a>(
580484
arguments: Node<'a>,

crates/roughly/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub mod index;
88
pub mod references;
99
pub mod rename;
1010
pub mod server;
11+
pub mod symbols;
1112
pub mod tree;
1213
pub mod utils;
1314

crates/roughly/src/server.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use {
1818
Url, WorkspaceEdit, WorkspaceSymbolParams, WorkspaceSymbolResponse,
1919
notification::{DidChangeWatchedFiles, Notification},
2020
},
21-
references, rename, tree, utils,
21+
references, rename, symbols, tree, utils,
2222
},
2323
async_lsp::{
2424
ClientSocket, ErrorCode, LanguageClient, LanguageServer, ResponseError,
@@ -91,9 +91,9 @@ struct ServerState {
9191
base_path: PathBuf,
9292
document_map: HashMap<PathBuf, Document>,
9393
/// stores symbolds for all other files
94-
document_symbols: HashMap<PathBuf, Vec<Item>>,
94+
document_items: HashMap<PathBuf, Vec<Item>>,
9595
/// stores index for all files in R/ folder
96-
workspace_symbols: HashMap<PathBuf, Vec<Item>>,
96+
workspace_items: HashMap<PathBuf, Vec<Item>>,
9797
parser: Parser,
9898
}
9999

@@ -114,8 +114,8 @@ impl ServerState {
114114
config,
115115
experimental_features,
116116
base_path: std::env::current_dir().unwrap().join("R"),
117-
workspace_symbols: HashMap::new(),
118-
document_symbols: HashMap::new(),
117+
workspace_items: HashMap::new(),
118+
document_items: HashMap::new(),
119119
document_map: HashMap::new(),
120120
parser: tree::new_parser(),
121121
})
@@ -133,7 +133,7 @@ impl LanguageServer for ServerState {
133133
tracing::info!(?self.experimental_features, "initialize");
134134

135135
match index::index_dir(&self.base_path, &mut self.parser) {
136-
Ok(symbols) => self.workspace_symbols.extend(symbols),
136+
Ok(items) => self.workspace_items.extend(items),
137137
Err(IndexError) => self
138138
.client
139139
.show_message(ShowMessageParams {
@@ -236,12 +236,12 @@ impl LanguageServer for ServerState {
236236

237237
let diagnostics = diagnostics::analyze_full(tree.root_node(), &rope, self.config.lint);
238238

239-
let symbols = index::index(tree.root_node(), &rope, false, false);
239+
let items = index::index(tree.root_node(), &rope, false, false);
240240
if path.starts_with(&self.base_path) {
241241
// note: we need to insert into workspace in case a new file is created
242-
self.workspace_symbols.insert(path.clone(), symbols);
242+
self.workspace_items.insert(path.clone(), items);
243243
} else {
244-
self.document_symbols.insert(path.clone(), symbols);
244+
self.document_items.insert(path.clone(), items);
245245
}
246246

247247
self.document_map.insert(path, Document { rope, tree });
@@ -330,14 +330,14 @@ impl LanguageServer for ServerState {
330330
// UPDATE DIAGNOSTICS
331331
let diagnostics = diagnostics::analyze_fast(tree.root_node(), rope, self.config.lint);
332332

333-
// UPDATE SYMBOLS
333+
// UPDATE ITEMS
334334
// note: We must re-index on every change (not just on save)
335335
// because textDocument/documentSymbol is triggered before textDocument/didSave.
336-
let symbols = index::index(tree.root_node(), rope, false, false);
336+
let items = index::index(tree.root_node(), rope, false, false);
337337
if path.starts_with(&self.base_path) {
338-
self.workspace_symbols.insert(path, symbols);
338+
self.workspace_items.insert(path, items);
339339
} else {
340-
self.document_symbols.insert(path, symbols);
340+
self.document_items.insert(path, items);
341341
}
342342

343343
if let Err(error) = self
@@ -403,11 +403,11 @@ impl LanguageServer for ServerState {
403403
match change.typ {
404404
FileChangeType::CREATED | FileChangeType::CHANGED => {
405405
// note: potential race condition if the user already has the file open and begins editing immediately.
406-
let symbols = index::index_file(&path, &mut self.parser);
407-
self.workspace_symbols.insert(path.clone(), symbols);
406+
let items = index::index_file(&path, &mut self.parser);
407+
self.workspace_items.insert(path.clone(), items);
408408
}
409409
FileChangeType::DELETED => {
410-
self.workspace_symbols.remove(&path);
410+
self.workspace_items.remove(&path);
411411
}
412412
_ => unreachable!(),
413413
}
@@ -440,7 +440,7 @@ impl LanguageServer for ServerState {
440440
position,
441441
&document.rope,
442442
&document.tree,
443-
&self.workspace_symbols,
443+
&self.workspace_items,
444444
);
445445

446446
box_future(Ok(completions))
@@ -471,7 +471,7 @@ impl LanguageServer for ServerState {
471471
position.character as usize,
472472
&document.rope,
473473
&document.tree,
474-
&self.workspace_symbols,
474+
&self.workspace_items,
475475
);
476476

477477
box_future(Ok(definitions))
@@ -585,7 +585,7 @@ impl LanguageServer for ServerState {
585585
include_declaration,
586586
&document.rope,
587587
&document.tree,
588-
&self.workspace_symbols,
588+
&self.workspace_items,
589589
);
590590

591591
box_future(Ok(references))
@@ -634,17 +634,17 @@ impl LanguageServer for ServerState {
634634
let uri = params.text_document.uri;
635635
let path = uri.to_file_path().unwrap();
636636

637-
let symbols_map = if path.starts_with(&self.base_path) {
638-
&self.workspace_symbols
637+
let items_map = if path.starts_with(&self.base_path) {
638+
&self.workspace_items
639639
} else {
640-
&self.document_symbols
640+
&self.document_items
641641
};
642642

643-
let Some(symbols) = symbols_map.get(&path) else {
643+
let Some(items) = items_map.get(&path) else {
644644
tracing::error!(?path, "symbols not found");
645645
return box_future(Err(path_not_found_error(&path)));
646646
};
647-
let symbols: Vec<DocumentSymbol> = symbols.iter().map(|item| item.to_document_symbol()).collect();
647+
let symbols: Vec<DocumentSymbol> = symbols::document(items);
648648

649649
box_future(Ok(Some(DocumentSymbolResponse::Nested(symbols))))
650650
}
@@ -657,7 +657,7 @@ impl LanguageServer for ServerState {
657657

658658
tracing::debug!(?query);
659659

660-
let symbols = index::get_workspace_symbols(&query, &self.workspace_symbols);
660+
let symbols = symbols::workspace(&query, &self.workspace_items);
661661

662662
box_future(Ok(Some(WorkspaceSymbolResponse::Nested(symbols))))
663663
}

crates/roughly/src/symbols.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use crate::{
2+
index::{Item, ItemInfo, SymbolsMap},
3+
lsp_types::{DocumentSymbol, Location, OneOf, SymbolKind, Url as Uri, WorkspaceSymbol},
4+
utils,
5+
};
6+
7+
pub fn document(items: &[Item]) -> Vec<DocumentSymbol> {
8+
items.iter().map(to_document_symbol).collect()
9+
}
10+
11+
pub fn workspace(query: &str, workspace_symbols: &impl SymbolsMap) -> Vec<WorkspaceSymbol> {
12+
workspace_symbols.filter_map(
13+
|path, symbols| {
14+
let uri = Uri::from_file_path(path).unwrap();
15+
symbols
16+
.iter()
17+
.flat_map(|symbol| {
18+
std::iter::once((symbol, None)).chain(
19+
symbol
20+
.children
21+
.as_ref()
22+
.into_iter()
23+
.flatten()
24+
.map(|child| (child, Some(symbol.name.as_ref()))),
25+
)
26+
})
27+
.filter(|(symbol, _)| utils::starts_with_lowercase(&symbol.display_name(), query))
28+
.map(move |(symbol, container_name)| WorkspaceSymbol {
29+
name: symbol.display_name(),
30+
kind: to_symbol_kind(&symbol.info),
31+
tags: None,
32+
container_name: container_name.map(str::to_string),
33+
location: OneOf::Left(Location {
34+
uri: uri.clone(),
35+
range: symbol.range,
36+
}),
37+
data: None,
38+
})
39+
},
40+
128,
41+
)
42+
}
43+
44+
pub fn to_document_symbol(item: &Item) -> DocumentSymbol {
45+
#[allow(deprecated)]
46+
DocumentSymbol {
47+
name: item.display_name(),
48+
kind: to_symbol_kind(&item.info),
49+
detail: item.detail.clone(),
50+
tags: None,
51+
range: item.range,
52+
selection_range: item.selection_range,
53+
children: item
54+
.children
55+
.as_ref()
56+
.map(|children| children.iter().map(to_document_symbol).collect()),
57+
deprecated: None,
58+
}
59+
}
60+
61+
fn to_symbol_kind(info: &ItemInfo) -> SymbolKind {
62+
match info {
63+
ItemInfo::Unknown => SymbolKind::VARIABLE,
64+
ItemInfo::Integer | ItemInfo::Float | ItemInfo::Complex => SymbolKind::NUMBER,
65+
ItemInfo::Bool => SymbolKind::BOOLEAN,
66+
ItemInfo::String => SymbolKind::STRING,
67+
ItemInfo::Null => SymbolKind::NULL,
68+
ItemInfo::Function => SymbolKind::FUNCTION,
69+
ItemInfo::S4Class => SymbolKind::CLASS,
70+
ItemInfo::S4Generic => SymbolKind::INTERFACE,
71+
ItemInfo::S4Method { .. } => SymbolKind::METHOD,
72+
ItemInfo::R6Class => SymbolKind::CLASS,
73+
ItemInfo::R6Method => SymbolKind::METHOD,
74+
ItemInfo::R6Field => SymbolKind::FIELD,
75+
}
76+
}

0 commit comments

Comments
 (0)