From 7d48e04f316a384967d48a261f1e3b70b5f85a98 Mon Sep 17 00:00:00 2001 From: ivan770 Date: Sun, 14 Mar 2021 17:16:29 +0200 Subject: Introduce StructureNodeKind --- crates/ide/src/annotations.rs | 16 ++-- crates/ide/src/file_structure.rs | 126 +++++++++++++++++++---------- crates/ide/src/syntax_highlighting/tags.rs | 1 - crates/ide_completion/src/item.rs | 1 - crates/ide_db/src/lib.rs | 7 +- crates/rust-analyzer/src/handlers.rs | 4 +- crates/rust-analyzer/src/to_proto.rs | 12 ++- 7 files changed, 110 insertions(+), 57 deletions(-) diff --git a/crates/ide/src/annotations.rs b/crates/ide/src/annotations.rs index 2e8e82b70..fd317874e 100644 --- a/crates/ide/src/annotations.rs +++ b/crates/ide/src/annotations.rs @@ -1,7 +1,7 @@ use hir::Semantics; use ide_db::{ base_db::{FileId, FilePosition, FileRange, SourceDatabase}, - RootDatabase, SymbolKind, + RootDatabase, StructureNodeKind, SymbolKind, }; use syntax::TextRange; @@ -80,15 +80,17 @@ pub(crate) fn annotations( .filter(|node| { matches!( node.kind, - SymbolKind::Trait - | SymbolKind::Struct - | SymbolKind::Enum - | SymbolKind::Union - | SymbolKind::Const + StructureNodeKind::SymbolKind(SymbolKind::Trait) + | StructureNodeKind::SymbolKind(SymbolKind::Struct) + | StructureNodeKind::SymbolKind(SymbolKind::Enum) + | StructureNodeKind::SymbolKind(SymbolKind::Union) + | StructureNodeKind::SymbolKind(SymbolKind::Const) ) }) .for_each(|node| { - if config.annotate_impls && node.kind != SymbolKind::Const { + if config.annotate_impls + && node.kind != StructureNodeKind::SymbolKind(SymbolKind::Const) + { annotations.push(Annotation { range: node.node_range, kind: AnnotationKind::HasImpls { diff --git a/crates/ide/src/file_structure.rs b/crates/ide/src/file_structure.rs index c5ca6ff26..c21b3fa77 100644 --- a/crates/ide/src/file_structure.rs +++ b/crates/ide/src/file_structure.rs @@ -1,4 +1,4 @@ -use ide_db::SymbolKind; +use ide_db::{StructureNodeKind, SymbolKind}; use syntax::{ ast::{self, AttrsOwner, GenericParamsOwner, NameOwner}, match_ast, AstNode, AstToken, NodeOrToken, SourceFile, SyntaxNode, SyntaxToken, TextRange, @@ -11,7 +11,7 @@ pub struct StructureNode { pub label: String, pub navigation_range: TextRange, pub node_range: TextRange, - pub kind: SymbolKind, + pub kind: StructureNodeKind, pub detail: Option, pub deprecated: bool, } @@ -65,14 +65,14 @@ pub(crate) fn file_structure(file: &SourceFile) -> Vec { } fn structure_node(node: &SyntaxNode) -> Option { - fn decl(node: N, kind: SymbolKind) -> Option { + fn decl(node: N, kind: StructureNodeKind) -> Option { decl_with_detail(&node, None, kind) } fn decl_with_type_ref( node: &N, type_ref: Option, - kind: SymbolKind, + kind: StructureNodeKind, ) -> Option { let detail = type_ref.map(|type_ref| { let mut detail = String::new(); @@ -85,7 +85,7 @@ fn structure_node(node: &SyntaxNode) -> Option { fn decl_with_detail( node: &N, detail: Option, - kind: SymbolKind, + kind: StructureNodeKind, ) -> Option { let name = node.name()?; @@ -133,18 +133,18 @@ fn structure_node(node: &SyntaxNode) -> Option { collapse_ws(ret_type.syntax(), &mut detail); } - decl_with_detail(&it, Some(detail), SymbolKind::Function) + decl_with_detail(&it, Some(detail), StructureNodeKind::SymbolKind(SymbolKind::Function)) }, - ast::Struct(it) => decl(it, SymbolKind::Struct), - ast::Union(it) => decl(it, SymbolKind::Union), - ast::Enum(it) => decl(it, SymbolKind::Enum), - ast::Variant(it) => decl(it, SymbolKind::Variant), - ast::Trait(it) => decl(it, SymbolKind::Trait), - ast::Module(it) => decl(it, SymbolKind::Module), - ast::TypeAlias(it) => decl_with_type_ref(&it, it.ty(), SymbolKind::TypeAlias), - ast::RecordField(it) => decl_with_type_ref(&it, it.ty(), SymbolKind::Field), - ast::Const(it) => decl_with_type_ref(&it, it.ty(), SymbolKind::Const), - ast::Static(it) => decl_with_type_ref(&it, it.ty(), SymbolKind::Static), + ast::Struct(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Struct)), + ast::Union(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Union)), + ast::Enum(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Enum)), + ast::Variant(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Variant)), + ast::Trait(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Trait)), + ast::Module(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Module)), + ast::TypeAlias(it) => decl_with_type_ref(&it, it.ty(), StructureNodeKind::SymbolKind(SymbolKind::TypeAlias)), + ast::RecordField(it) => decl_with_type_ref(&it, it.ty(), StructureNodeKind::SymbolKind(SymbolKind::Field)), + ast::Const(it) => decl_with_type_ref(&it, it.ty(), StructureNodeKind::SymbolKind(SymbolKind::Const)), + ast::Static(it) => decl_with_type_ref(&it, it.ty(), StructureNodeKind::SymbolKind(SymbolKind::Static)), ast::Impl(it) => { let target_type = it.self_ty()?; let target_trait = it.trait_(); @@ -160,13 +160,13 @@ fn structure_node(node: &SyntaxNode) -> Option { label, navigation_range: target_type.syntax().text_range(), node_range: it.syntax().text_range(), - kind: SymbolKind::Impl, + kind: StructureNodeKind::SymbolKind(SymbolKind::Impl), detail: None, deprecated: false, }; Some(node) }, - ast::MacroRules(it) => decl(it, SymbolKind::Macro), + ast::MacroRules(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Macro)), _ => None, } } @@ -182,7 +182,7 @@ fn structure_token(token: SyntaxToken) -> Option { label: region_name.to_string(), navigation_range: comment.syntax().text_range(), node_range: comment.syntax().text_range(), - kind: SymbolKind::Region, + kind: StructureNodeKind::Region, detail: None, deprecated: false, }); @@ -268,7 +268,9 @@ fn g() {} label: "Foo", navigation_range: 8..11, node_range: 1..26, - kind: Struct, + kind: SymbolKind( + Struct, + ), detail: None, deprecated: false, }, @@ -279,7 +281,9 @@ fn g() {} label: "x", navigation_range: 18..19, node_range: 18..24, - kind: Field, + kind: SymbolKind( + Field, + ), detail: Some( "i32", ), @@ -290,7 +294,9 @@ fn g() {} label: "m", navigation_range: 32..33, node_range: 28..158, - kind: Module, + kind: SymbolKind( + Module, + ), detail: None, deprecated: false, }, @@ -301,7 +307,9 @@ fn g() {} label: "bar1", navigation_range: 43..47, node_range: 40..52, - kind: Function, + kind: SymbolKind( + Function, + ), detail: Some( "fn()", ), @@ -314,7 +322,9 @@ fn g() {} label: "bar2", navigation_range: 60..64, node_range: 57..81, - kind: Function, + kind: SymbolKind( + Function, + ), detail: Some( "fn(t: T) -> T", ), @@ -327,7 +337,9 @@ fn g() {} label: "bar3", navigation_range: 89..93, node_range: 86..156, - kind: Function, + kind: SymbolKind( + Function, + ), detail: Some( "fn(a: A, b: B) -> Vec< u32 >", ), @@ -338,7 +350,9 @@ fn g() {} label: "E", navigation_range: 165..166, node_range: 160..180, - kind: Enum, + kind: SymbolKind( + Enum, + ), detail: None, deprecated: false, }, @@ -349,7 +363,9 @@ fn g() {} label: "X", navigation_range: 169..170, node_range: 169..170, - kind: Variant, + kind: SymbolKind( + Variant, + ), detail: None, deprecated: false, }, @@ -360,7 +376,9 @@ fn g() {} label: "Y", navigation_range: 172..173, node_range: 172..178, - kind: Variant, + kind: SymbolKind( + Variant, + ), detail: None, deprecated: false, }, @@ -369,7 +387,9 @@ fn g() {} label: "T", navigation_range: 186..187, node_range: 181..193, - kind: TypeAlias, + kind: SymbolKind( + TypeAlias, + ), detail: Some( "()", ), @@ -380,7 +400,9 @@ fn g() {} label: "S", navigation_range: 201..202, node_range: 194..213, - kind: Static, + kind: SymbolKind( + Static, + ), detail: Some( "i32", ), @@ -391,7 +413,9 @@ fn g() {} label: "C", navigation_range: 220..221, node_range: 214..232, - kind: Const, + kind: SymbolKind( + Const, + ), detail: Some( "i32", ), @@ -402,7 +426,9 @@ fn g() {} label: "impl E", navigation_range: 239..240, node_range: 234..243, - kind: Impl, + kind: SymbolKind( + Impl, + ), detail: None, deprecated: false, }, @@ -411,7 +437,9 @@ fn g() {} label: "impl fmt::Debug for E", navigation_range: 265..266, node_range: 245..269, - kind: Impl, + kind: SymbolKind( + Impl, + ), detail: None, deprecated: false, }, @@ -420,7 +448,9 @@ fn g() {} label: "mc", navigation_range: 284..286, node_range: 271..303, - kind: Macro, + kind: SymbolKind( + Macro, + ), detail: None, deprecated: false, }, @@ -429,7 +459,9 @@ fn g() {} label: "mcexp", navigation_range: 334..339, node_range: 305..356, - kind: Macro, + kind: SymbolKind( + Macro, + ), detail: None, deprecated: false, }, @@ -438,7 +470,9 @@ fn g() {} label: "mcexp", navigation_range: 387..392, node_range: 358..409, - kind: Macro, + kind: SymbolKind( + Macro, + ), detail: None, deprecated: false, }, @@ -447,7 +481,9 @@ fn g() {} label: "obsolete", navigation_range: 428..436, node_range: 411..441, - kind: Function, + kind: SymbolKind( + Function, + ), detail: Some( "fn()", ), @@ -458,7 +494,9 @@ fn g() {} label: "very_obsolete", navigation_range: 481..494, node_range: 443..499, - kind: Function, + kind: SymbolKind( + Function, + ), detail: Some( "fn()", ), @@ -478,7 +516,9 @@ fn g() {} label: "m", navigation_range: 568..569, node_range: 543..606, - kind: Module, + kind: SymbolKind( + Module, + ), detail: None, deprecated: false, }, @@ -500,7 +540,9 @@ fn g() {} label: "f", navigation_range: 575..576, node_range: 572..581, - kind: Function, + kind: SymbolKind( + Function, + ), detail: Some( "fn()", ), @@ -513,7 +555,9 @@ fn g() {} label: "g", navigation_range: 598..599, node_range: 582..604, - kind: Function, + kind: SymbolKind( + Function, + ), detail: Some( "fn()", ), diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs index c7e74aed8..3c02fdb11 100644 --- a/crates/ide/src/syntax_highlighting/tags.rs +++ b/crates/ide/src/syntax_highlighting/tags.rs @@ -107,7 +107,6 @@ impl HlTag { SymbolKind::ValueParam => "value_param", SymbolKind::SelfParam => "self_keyword", SymbolKind::Impl => "self_type", - SymbolKind::Region => "region", }, HlTag::Attribute => "attribute", HlTag::BoolLiteral => "bool_literal", diff --git a/crates/ide_completion/src/item.rs b/crates/ide_completion/src/item.rs index 7b553ea7a..9a4b5217a 100644 --- a/crates/ide_completion/src/item.rs +++ b/crates/ide_completion/src/item.rs @@ -225,7 +225,6 @@ impl CompletionItemKind { SymbolKind::Local => "lc", SymbolKind::Macro => "ma", SymbolKind::Module => "md", - SymbolKind::Region => "rn", SymbolKind::SelfParam => "sp", SymbolKind::Static => "sc", SymbolKind::Struct => "st", diff --git a/crates/ide_db/src/lib.rs b/crates/ide_db/src/lib.rs index d478841e2..e8cafba43 100644 --- a/crates/ide_db/src/lib.rs +++ b/crates/ide_db/src/lib.rs @@ -135,6 +135,12 @@ fn line_index(db: &dyn LineIndexDatabase, file_id: FileId) -> Arc { Arc::new(LineIndex::new(&*text)) } +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub enum StructureNodeKind { + SymbolKind(SymbolKind), + Region, +} + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] pub enum SymbolKind { Const, @@ -148,7 +154,6 @@ pub enum SymbolKind { Local, Macro, Module, - Region, SelfParam, Static, Struct, diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 706a39dab..3ff8bd940 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -11,7 +11,7 @@ use ide::{ AnnotationConfig, FileId, FilePosition, FileRange, HoverAction, HoverGotoTypeData, Query, RangeInfo, Runnable, RunnableKind, SearchScope, SourceChange, TextEdit, }; -use ide_db::SymbolKind; +use ide_db::{StructureNodeKind, SymbolKind}; use itertools::Itertools; use lsp_server::ErrorCode; use lsp_types::{ @@ -289,7 +289,7 @@ pub(crate) fn handle_document_symbol( let doc_symbol = lsp_types::DocumentSymbol { name: symbol.label, detail: symbol.detail, - kind: to_proto::symbol_kind(symbol.kind), + kind: to_proto::structure_node_kind(symbol.kind), tags: Some(tags), deprecated: Some(symbol.deprecated), range: to_proto::range(&line_index, symbol.node_range), diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index e88818eb0..ab742a17c 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -11,7 +11,7 @@ use ide::{ Markup, NavigationTarget, ReferenceAccess, RenameError, Runnable, Severity, SourceChange, TextEdit, TextRange, TextSize, }; -use ide_db::SymbolKind; +use ide_db::{StructureNodeKind, SymbolKind}; use itertools::Itertools; use serde_json::to_value; @@ -60,7 +60,13 @@ pub(crate) fn symbol_kind(symbol_kind: SymbolKind) -> lsp_types::SymbolKind { | SymbolKind::ValueParam | SymbolKind::Label => lsp_types::SymbolKind::Variable, SymbolKind::Union => lsp_types::SymbolKind::Struct, - SymbolKind::Region => lsp_types::SymbolKind::Namespace, + } +} + +pub(crate) fn structure_node_kind(kind: StructureNodeKind) -> lsp_types::SymbolKind { + match kind { + StructureNodeKind::SymbolKind(symbol) => symbol_kind(symbol), + StructureNodeKind::Region => lsp_types::SymbolKind::Namespace, } } @@ -118,7 +124,6 @@ pub(crate) fn completion_item_kind( SymbolKind::Local => lsp_types::CompletionItemKind::Variable, SymbolKind::Macro => lsp_types::CompletionItemKind::Method, SymbolKind::Module => lsp_types::CompletionItemKind::Module, - SymbolKind::Region => lsp_types::CompletionItemKind::Keyword, SymbolKind::SelfParam => lsp_types::CompletionItemKind::Value, SymbolKind::Static => lsp_types::CompletionItemKind::Value, SymbolKind::Struct => lsp_types::CompletionItemKind::Struct, @@ -430,7 +435,6 @@ fn semantic_token_type_and_modifiers( SymbolKind::TypeAlias => semantic_tokens::TYPE_ALIAS, SymbolKind::Trait => lsp_types::SemanticTokenType::INTERFACE, SymbolKind::Macro => lsp_types::SemanticTokenType::MACRO, - SymbolKind::Region => lsp_types::SemanticTokenType::NAMESPACE, }, HlTag::BuiltinType => semantic_tokens::BUILTIN_TYPE, HlTag::None => semantic_tokens::GENERIC, -- cgit v1.2.3