From 71a97a2d8c30fc4061a545f1e2db10e3559eae36 Mon Sep 17 00:00:00 2001 From: ivan770 Date: Thu, 11 Mar 2021 18:14:41 +0200 Subject: Provide regions in file structure --- crates/ide/src/file_structure.rs | 53 +++++++++++++++++++++++++++--- crates/ide/src/syntax_highlighting/tags.rs | 1 + 2 files changed, 50 insertions(+), 4 deletions(-) (limited to 'crates/ide') diff --git a/crates/ide/src/file_structure.rs b/crates/ide/src/file_structure.rs index 26793bdb4..713c76957 100644 --- a/crates/ide/src/file_structure.rs +++ b/crates/ide/src/file_structure.rs @@ -1,7 +1,8 @@ use ide_db::SymbolKind; use syntax::{ ast::{self, AttrsOwner, GenericParamsOwner, NameOwner}, - match_ast, AstNode, SourceFile, SyntaxNode, TextRange, WalkEvent, + match_ast, AstNode, AstToken, NodeOrToken, SourceFile, SyntaxNode, SyntaxToken, TextRange, + WalkEvent, }; #[derive(Debug, Clone)] @@ -32,20 +33,32 @@ pub(crate) fn file_structure(file: &SourceFile) -> Vec { let mut res = Vec::new(); let mut stack = Vec::new(); - for event in file.syntax().preorder() { + for event in file.syntax().preorder_with_tokens() { match event { - WalkEvent::Enter(node) => { + WalkEvent::Enter(NodeOrToken::Node(node)) => { if let Some(mut symbol) = structure_node(&node) { symbol.parent = stack.last().copied(); stack.push(res.len()); res.push(symbol); } } - WalkEvent::Leave(node) => { + WalkEvent::Leave(NodeOrToken::Node(node)) => { if structure_node(&node).is_some() { stack.pop().unwrap(); } } + WalkEvent::Enter(NodeOrToken::Token(token)) => { + if let Some(mut symbol) = structure_token(token) { + symbol.parent = stack.last().copied(); + stack.push(res.len()); + res.push(symbol); + } + } + WalkEvent::Leave(NodeOrToken::Token(token)) => { + if structure_token(token).is_some() { + stack.pop().unwrap(); + } + } } } res @@ -159,6 +172,26 @@ fn structure_node(node: &SyntaxNode) -> Option { } } +fn structure_token(token: SyntaxToken) -> Option { + if let Some(comment) = ast::Comment::cast(token) { + let text = comment.text().trim(); + + if let Some(region_name) = text.strip_prefix("// region:").map(|text| text.trim()) { + return Some(StructureNode { + parent: None, + label: region_name.to_string(), + navigation_range: comment.syntax().text_range(), + node_range: comment.syntax().text_range(), + kind: SymbolKind::Region, + detail: None, + deprecated: false, + }); + } + } + + None +} + #[cfg(test)] mod tests { use expect_test::{expect, Expect}; @@ -217,6 +250,9 @@ fn obsolete() {} #[deprecated(note = "for awhile")] fn very_obsolete() {} + +// region: Some region name +// endregion "#, expect![[r#" [ @@ -421,6 +457,15 @@ fn very_obsolete() {} ), deprecated: true, }, + StructureNode { + parent: None, + label: "Some region name", + navigation_range: 501..528, + node_range: 501..528, + kind: Region, + detail: None, + deprecated: false, + }, ] "#]], ); diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs index 3c02fdb11..c7e74aed8 100644 --- a/crates/ide/src/syntax_highlighting/tags.rs +++ b/crates/ide/src/syntax_highlighting/tags.rs @@ -107,6 +107,7 @@ impl HlTag { SymbolKind::ValueParam => "value_param", SymbolKind::SelfParam => "self_keyword", SymbolKind::Impl => "self_type", + SymbolKind::Region => "region", }, HlTag::Attribute => "attribute", HlTag::BoolLiteral => "bool_literal", -- cgit v1.2.3 From 56ca843695e0927dbe3dc28a15d108707ce9c3ba Mon Sep 17 00:00:00 2001 From: ivan770 Date: Fri, 12 Mar 2021 11:01:43 +0200 Subject: Shorten trim call --- crates/ide/src/file_structure.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ide') diff --git a/crates/ide/src/file_structure.rs b/crates/ide/src/file_structure.rs index 713c76957..33d3c5323 100644 --- a/crates/ide/src/file_structure.rs +++ b/crates/ide/src/file_structure.rs @@ -176,7 +176,7 @@ fn structure_token(token: SyntaxToken) -> Option { if let Some(comment) = ast::Comment::cast(token) { let text = comment.text().trim(); - if let Some(region_name) = text.strip_prefix("// region:").map(|text| text.trim()) { + if let Some(region_name) = text.strip_prefix("// region:").map(str::trim) { return Some(StructureNode { parent: None, label: region_name.to_string(), -- cgit v1.2.3 From 8602f9573b3a450b6a29c23bb4bfb7bd4108a89c Mon Sep 17 00:00:00 2001 From: ivan770 Date: Sun, 14 Mar 2021 12:52:04 +0200 Subject: Added region intersection test --- crates/ide/src/file_structure.rs | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'crates/ide') diff --git a/crates/ide/src/file_structure.rs b/crates/ide/src/file_structure.rs index 33d3c5323..c5ca6ff26 100644 --- a/crates/ide/src/file_structure.rs +++ b/crates/ide/src/file_structure.rs @@ -253,6 +253,13 @@ fn very_obsolete() {} // region: Some region name // endregion + +// region: dontpanic +mod m { +fn f() {} +// endregion +fn g() {} +} "#, expect![[r#" [ @@ -466,6 +473,52 @@ fn very_obsolete() {} detail: None, deprecated: false, }, + StructureNode { + parent: None, + label: "m", + navigation_range: 568..569, + node_range: 543..606, + kind: Module, + detail: None, + deprecated: false, + }, + StructureNode { + parent: Some( + 20, + ), + label: "dontpanic", + navigation_range: 543..563, + node_range: 543..563, + kind: Region, + detail: None, + deprecated: false, + }, + StructureNode { + parent: Some( + 20, + ), + label: "f", + navigation_range: 575..576, + node_range: 572..581, + kind: Function, + detail: Some( + "fn()", + ), + deprecated: false, + }, + StructureNode { + parent: Some( + 20, + ), + label: "g", + navigation_range: 598..599, + node_range: 582..604, + kind: Function, + detail: Some( + "fn()", + ), + deprecated: false, + }, ] "#]], ); -- cgit v1.2.3 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 - 3 files changed, 94 insertions(+), 49 deletions(-) (limited to 'crates/ide') 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", -- cgit v1.2.3