aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_db
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-12-17 11:29:05 +0000
committerAleksey Kladov <[email protected]>2020-12-18 16:28:48 +0000
commit55ba353b39db1e9d850f1df943ab6a16e7c15838 (patch)
treec29c3a31ea9e046d2767dfc189e0ab77b8ffdafe /crates/ide_db
parent2465fa02b7aa268d7d711b81417e7717427217c3 (diff)
Don't expose SyntaxKind from IDE API
SyntaxKind is somewhat of an internal type, but IDE is using it to basically specify an icon. Let's have a dedicated entity for this instead.
Diffstat (limited to 'crates/ide_db')
-rw-r--r--crates/ide_db/src/symbol_index.rs48
1 files changed, 40 insertions, 8 deletions
diff --git a/crates/ide_db/src/symbol_index.rs b/crates/ide_db/src/symbol_index.rs
index ca455fa03..0aa6a0765 100644
--- a/crates/ide_db/src/symbol_index.rs
+++ b/crates/ide_db/src/symbol_index.rs
@@ -39,7 +39,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
39use syntax::{ 39use syntax::{
40 ast::{self, NameOwner}, 40 ast::{self, NameOwner},
41 match_ast, AstNode, Parse, SmolStr, SourceFile, 41 match_ast, AstNode, Parse, SmolStr, SourceFile,
42 SyntaxKind::{self, *}, 42 SyntaxKind::*,
43 SyntaxNode, SyntaxNodePtr, TextRange, WalkEvent, 43 SyntaxNode, SyntaxNodePtr, TextRange, WalkEvent,
44}; 44};
45 45
@@ -323,7 +323,7 @@ impl Query {
323 let (start, end) = SymbolIndex::map_value_to_range(indexed_value.value); 323 let (start, end) = SymbolIndex::map_value_to_range(indexed_value.value);
324 324
325 for symbol in &symbol_index.symbols[start..end] { 325 for symbol in &symbol_index.symbols[start..end] {
326 if self.only_types && !is_type(symbol.kind) { 326 if self.only_types && !symbol.kind.is_type() {
327 continue; 327 continue;
328 } 328 }
329 if self.exact && symbol.name != self.query { 329 if self.exact && symbol.name != self.query {
@@ -341,23 +341,44 @@ impl Query {
341 } 341 }
342} 342}
343 343
344fn is_type(kind: SyntaxKind) -> bool {
345 matches!(kind, STRUCT | ENUM | TRAIT | TYPE_ALIAS)
346}
347
348/// The actual data that is stored in the index. It should be as compact as 344/// The actual data that is stored in the index. It should be as compact as
349/// possible. 345/// possible.
350#[derive(Debug, Clone, PartialEq, Eq, Hash)] 346#[derive(Debug, Clone, PartialEq, Eq, Hash)]
351pub struct FileSymbol { 347pub struct FileSymbol {
352 pub file_id: FileId, 348 pub file_id: FileId,
353 pub name: SmolStr, 349 pub name: SmolStr,
354 pub kind: SyntaxKind, 350 pub kind: FileSymbolKind,
355 pub range: TextRange, 351 pub range: TextRange,
356 pub ptr: SyntaxNodePtr, 352 pub ptr: SyntaxNodePtr,
357 pub name_range: Option<TextRange>, 353 pub name_range: Option<TextRange>,
358 pub container_name: Option<SmolStr>, 354 pub container_name: Option<SmolStr>,
359} 355}
360 356
357#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
358pub enum FileSymbolKind {
359 Function,
360 Struct,
361 Enum,
362 Trait,
363 Module,
364 TypeAlias,
365 Const,
366 Static,
367 Macro,
368}
369
370impl FileSymbolKind {
371 fn is_type(self: FileSymbolKind) -> bool {
372 matches!(
373 self,
374 FileSymbolKind::Struct
375 | FileSymbolKind::Enum
376 | FileSymbolKind::Trait
377 | FileSymbolKind::TypeAlias
378 )
379 }
380}
381
361fn source_file_to_file_symbols(source_file: &SourceFile, file_id: FileId) -> Vec<FileSymbol> { 382fn source_file_to_file_symbols(source_file: &SourceFile, file_id: FileId) -> Vec<FileSymbol> {
362 let mut symbols = Vec::new(); 383 let mut symbols = Vec::new();
363 let mut stack = Vec::new(); 384 let mut stack = Vec::new();
@@ -412,7 +433,18 @@ fn to_symbol(node: &SyntaxNode) -> Option<(SmolStr, SyntaxNodePtr, TextRange)> {
412fn to_file_symbol(node: &SyntaxNode, file_id: FileId) -> Option<FileSymbol> { 433fn to_file_symbol(node: &SyntaxNode, file_id: FileId) -> Option<FileSymbol> {
413 to_symbol(node).map(move |(name, ptr, name_range)| FileSymbol { 434 to_symbol(node).map(move |(name, ptr, name_range)| FileSymbol {
414 name, 435 name,
415 kind: node.kind(), 436 kind: match node.kind() {
437 FN => FileSymbolKind::Function,
438 STRUCT => FileSymbolKind::Struct,
439 ENUM => FileSymbolKind::Enum,
440 TRAIT => FileSymbolKind::Trait,
441 MODULE => FileSymbolKind::Module,
442 TYPE_ALIAS => FileSymbolKind::TypeAlias,
443 CONST => FileSymbolKind::Const,
444 STATIC => FileSymbolKind::Static,
445 MACRO_RULES => FileSymbolKind::Macro,
446 kind => unreachable!("{:?}", kind),
447 },
416 range: node.text_range(), 448 range: node.text_range(),
417 ptr, 449 ptr,
418 file_id, 450 file_id,