Introduce StructureNodeKind

This commit is contained in:
ivan770 2021-03-14 17:16:29 +02:00
parent 8602f9573b
commit 7d48e04f31
No known key found for this signature in database
GPG key ID: D8C4BD5AE4D9CC4D
7 changed files with 110 additions and 57 deletions

View file

@ -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 {

View file

@ -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<String>,
pub deprecated: bool,
}
@ -65,14 +65,14 @@ pub(crate) fn file_structure(file: &SourceFile) -> Vec<StructureNode> {
}
fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
fn decl<N: NameOwner + AttrsOwner>(node: N, kind: SymbolKind) -> Option<StructureNode> {
fn decl<N: NameOwner + AttrsOwner>(node: N, kind: StructureNodeKind) -> Option<StructureNode> {
decl_with_detail(&node, None, kind)
}
fn decl_with_type_ref<N: NameOwner + AttrsOwner>(
node: &N,
type_ref: Option<ast::Type>,
kind: SymbolKind,
kind: StructureNodeKind,
) -> Option<StructureNode> {
let detail = type_ref.map(|type_ref| {
let mut detail = String::new();
@ -85,7 +85,7 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
fn decl_with_detail<N: NameOwner + AttrsOwner>(
node: &N,
detail: Option<String>,
kind: SymbolKind,
kind: StructureNodeKind,
) -> Option<StructureNode> {
let name = node.name()?;
@ -133,18 +133,18 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
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<StructureNode> {
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<StructureNode> {
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) -> 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, B>(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()",
),

View file

@ -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",

View file

@ -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",

View file

@ -135,6 +135,12 @@ fn line_index(db: &dyn LineIndexDatabase, file_id: FileId) -> Arc<LineIndex> {
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,

View file

@ -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),

View file

@ -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,