From 78c377f956ffb7c9d1cd5d1786f8873959681f50 Mon Sep 17 00:00:00 2001 From: Giga Bowser <45986823+Giga-Bowser@users.noreply.github.com> Date: Fri, 20 Dec 2024 19:12:04 -0500 Subject: Add a new and improved syntax tree viewer --- src/tools/rust-analyzer/editors/code/package.json | 70 +++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'src/tools/rust-analyzer/editors/code/package.json') diff --git a/src/tools/rust-analyzer/editors/code/package.json b/src/tools/rust-analyzer/editors/code/package.json index e1edab4ba24..bbfe9c2a5ce 100644 --- a/src/tools/rust-analyzer/editors/code/package.json +++ b/src/tools/rust-analyzer/editors/code/package.json @@ -288,6 +288,24 @@ "title": "Reveal File", "category": "rust-analyzer" }, + { + "command": "rust-analyzer.syntaxTreeReveal", + "title": "Reveal Syntax Element", + "icon": "$(search)", + "category": "rust-analyzer (syntax tree)" + }, + { + "command": "rust-analyzer.syntaxTreeHideWhitespace", + "title": "Hide Whitespace", + "icon": "$(filter)", + "category": "rust-analyzer (syntax tree)" + }, + { + "command": "rust-analyzer.syntaxTreeShowWhitespace", + "title": "Show Whitespace", + "icon": "$(filter-filled)", + "category": "rust-analyzer (syntax tree)" + }, { "command": "rust-analyzer.viewMemoryLayout", "title": "View Memory Layout", @@ -345,6 +363,11 @@ "default": true, "type": "boolean" }, + "rust-analyzer.showSyntaxTree": { + "markdownDescription": "Whether to show the syntax tree view.", + "default": true, + "type": "boolean" + }, "rust-analyzer.testExplorer": { "markdownDescription": "Whether to show the test explorer.", "default": false, @@ -3362,6 +3385,18 @@ }, { "command": "rust-analyzer.openWalkthrough" + }, + { + "command": "rust-analyzer.syntaxTreeReveal", + "when": "false" + }, + { + "command": "rust-analyzer.syntaxTreeHideWhitespace", + "when": "false" + }, + { + "command": "rust-analyzer.syntaxTreeShowWhitespace", + "when": "false" } ], "editor/context": [ @@ -3375,6 +3410,25 @@ "when": "inRustProject && editorTextFocus && editorLangId == rust", "group": "navigation@1001" } + ], + "view/title": [ + { + "command": "rust-analyzer.syntaxTreeHideWhitespace", + "group": "navigation", + "when": "view == rustSyntaxTree && !rustSyntaxTree.hideWhitespace" + }, + { + "command": "rust-analyzer.syntaxTreeShowWhitespace", + "group": "navigation", + "when": "view == rustSyntaxTree && rustSyntaxTree.hideWhitespace" + } + ], + "view/item/context": [ + { + "command": "rust-analyzer.syntaxTreeReveal", + "group": "inline", + "when": "view == rustSyntaxTree" + } ] }, "views": { @@ -3384,6 +3438,22 @@ "name": "Rust Dependencies", "when": "inRustProject && config.rust-analyzer.showDependenciesExplorer" } + ], + "rustSyntaxTreeContainer": [ + { + "id": "rustSyntaxTree", + "name": "Rust Syntax Tree", + "when": "inRustProject && config.rust-analyzer.showSyntaxTree" + } + ] + }, + "viewsContainers": { + "activitybar": [ + { + "id": "rustSyntaxTreeContainer", + "title": "Rust Syntax Tree", + "icon": "$(list-tree)" + } ] }, "jsonValidation": [ -- cgit 1.4.1-3-g733a5 From 628db5361238f3fd4614a042e273fd9820dbb523 Mon Sep 17 00:00:00 2001 From: Giga Bowser <45986823+Giga-Bowser@users.noreply.github.com> Date: Mon, 6 Jan 2025 14:55:44 -0600 Subject: Remove the old syntax tree viewer --- src/tools/rust-analyzer/crates/ide/src/lib.rs | 11 - .../rust-analyzer/crates/ide/src/syntax_tree.rs | 338 --------------------- .../crates/rust-analyzer/src/handlers/request.rs | 12 - .../crates/rust-analyzer/src/lsp/ext.rs | 15 - .../crates/rust-analyzer/src/main_loop.rs | 1 - src/tools/rust-analyzer/docs/dev/lsp-extensions.md | 2 +- src/tools/rust-analyzer/editors/code/package.json | 20 -- .../editors/code/src/ast_inspector.ts | 216 ------------- .../rust-analyzer/editors/code/src/commands.ts | 85 ------ src/tools/rust-analyzer/editors/code/src/main.ts | 1 - 10 files changed, 1 insertion(+), 700 deletions(-) delete mode 100644 src/tools/rust-analyzer/crates/ide/src/syntax_tree.rs delete mode 100644 src/tools/rust-analyzer/editors/code/src/ast_inspector.ts (limited to 'src/tools/rust-analyzer/editors/code/package.json') diff --git a/src/tools/rust-analyzer/crates/ide/src/lib.rs b/src/tools/rust-analyzer/crates/ide/src/lib.rs index 5cb77e62016..af9a7ce7881 100644 --- a/src/tools/rust-analyzer/crates/ide/src/lib.rs +++ b/src/tools/rust-analyzer/crates/ide/src/lib.rs @@ -48,7 +48,6 @@ mod ssr; mod static_index; mod status; mod syntax_highlighting; -mod syntax_tree; mod test_explorer; mod typing; mod view_crate_graph; @@ -330,16 +329,6 @@ impl Analysis { }) } - /// Returns a syntax tree represented as `String`, for debug purposes. - // FIXME: use a better name here. - pub fn syntax_tree( - &self, - file_id: FileId, - text_range: Option, - ) -> Cancellable { - self.with_db(|db| syntax_tree::syntax_tree(db, file_id, text_range)) - } - pub fn view_syntax_tree(&self, file_id: FileId) -> Cancellable { self.with_db(|db| view_syntax_tree::view_syntax_tree(db, file_id)) } diff --git a/src/tools/rust-analyzer/crates/ide/src/syntax_tree.rs b/src/tools/rust-analyzer/crates/ide/src/syntax_tree.rs deleted file mode 100644 index 86c6676f928..00000000000 --- a/src/tools/rust-analyzer/crates/ide/src/syntax_tree.rs +++ /dev/null @@ -1,338 +0,0 @@ -use hir::Semantics; -use ide_db::{FileId, RootDatabase}; -use syntax::{ - AstNode, NodeOrToken, SourceFile, SyntaxKind::STRING, SyntaxToken, TextRange, TextSize, -}; - -// Feature: Show Debug Syntax Tree -// -// Shows the textual parse tree of the current file. It exists mostly for debugging -// rust-analyzer itself. -// -// |=== -// | Editor | Action Name -// -// | VS Code | **rust-analyzer: Show Syntax Tree** -// |=== -// image::https://user-images.githubusercontent.com/48062697/113065586-068bdb80-91b1-11eb-9507-fee67f9f45a0.gif[] -pub(crate) fn syntax_tree( - db: &RootDatabase, - file_id: FileId, - text_range: Option, -) -> String { - let sema = Semantics::new(db); - let parse = sema.parse_guess_edition(file_id); - if let Some(text_range) = text_range { - let node = match parse.syntax().covering_element(text_range) { - NodeOrToken::Node(node) => node, - NodeOrToken::Token(token) => { - if let Some(tree) = syntax_tree_for_string(&token, text_range) { - return tree; - } - token.parent().unwrap() - } - }; - - format!("{node:#?}") - } else { - format!("{:#?}", parse.syntax()) - } -} - -/// Attempts parsing the selected contents of a string literal -/// as rust syntax and returns its syntax tree -fn syntax_tree_for_string(token: &SyntaxToken, text_range: TextRange) -> Option { - // When the range is inside a string - // we'll attempt parsing it as rust syntax - // to provide the syntax tree of the contents of the string - match token.kind() { - STRING => syntax_tree_for_token(token, text_range), - _ => None, - } -} - -fn syntax_tree_for_token(node: &SyntaxToken, text_range: TextRange) -> Option { - // Range of the full node - let node_range = node.text_range(); - let text = node.text().to_owned(); - - // We start at some point inside the node - // Either we have selected the whole string - // or our selection is inside it - let start = text_range.start() - node_range.start(); - - // how many characters we have selected - let len = text_range.len(); - - let node_len = node_range.len(); - - // We want to cap our length - let len = len.min(node_len); - - // Ensure our slice is inside the actual string - let end = - if start + len < TextSize::of(&text) { start + len } else { TextSize::of(&text) - start }; - - let text = &text[TextRange::new(start, end)]; - - // Remove possible extra string quotes from the start - // and the end of the string - let text = text - .trim_start_matches('r') - .trim_start_matches('#') - .trim_start_matches('"') - .trim_end_matches('#') - .trim_end_matches('"') - .trim() - // Remove custom markers - .replace("$0", ""); - - let parsed = SourceFile::parse(&text, span::Edition::CURRENT_FIXME); - - // If the "file" parsed without errors, - // return its syntax - if parsed.errors().is_empty() { - return Some(format!("{:#?}", parsed.tree().syntax())); - } - - None -} - -#[cfg(test)] -mod tests { - use expect_test::expect; - - use crate::fixture; - - fn check(ra_fixture: &str, expect: expect_test::Expect) { - let (analysis, file_id) = fixture::file(ra_fixture); - let syn = analysis.syntax_tree(file_id, None).unwrap(); - expect.assert_eq(&syn) - } - fn check_range(ra_fixture: &str, expect: expect_test::Expect) { - let (analysis, frange) = fixture::range(ra_fixture); - let syn = analysis.syntax_tree(frange.file_id, Some(frange.range)).unwrap(); - expect.assert_eq(&syn) - } - - #[test] - fn test_syntax_tree_without_range() { - // Basic syntax - check( - r#"fn foo() {}"#, - expect![[r#" - SOURCE_FILE@0..11 - FN@0..11 - FN_KW@0..2 "fn" - WHITESPACE@2..3 " " - NAME@3..6 - IDENT@3..6 "foo" - PARAM_LIST@6..8 - L_PAREN@6..7 "(" - R_PAREN@7..8 ")" - WHITESPACE@8..9 " " - BLOCK_EXPR@9..11 - STMT_LIST@9..11 - L_CURLY@9..10 "{" - R_CURLY@10..11 "}" - "#]], - ); - - check( - r#" -fn test() { - assert!(" - fn foo() { - } - ", ""); -}"#, - expect![[r#" - SOURCE_FILE@0..60 - FN@0..60 - FN_KW@0..2 "fn" - WHITESPACE@2..3 " " - NAME@3..7 - IDENT@3..7 "test" - PARAM_LIST@7..9 - L_PAREN@7..8 "(" - R_PAREN@8..9 ")" - WHITESPACE@9..10 " " - BLOCK_EXPR@10..60 - STMT_LIST@10..60 - L_CURLY@10..11 "{" - WHITESPACE@11..16 "\n " - EXPR_STMT@16..58 - MACRO_EXPR@16..57 - MACRO_CALL@16..57 - PATH@16..22 - PATH_SEGMENT@16..22 - NAME_REF@16..22 - IDENT@16..22 "assert" - BANG@22..23 "!" - TOKEN_TREE@23..57 - L_PAREN@23..24 "(" - STRING@24..52 "\"\n fn foo() {\n ..." - COMMA@52..53 "," - WHITESPACE@53..54 " " - STRING@54..56 "\"\"" - R_PAREN@56..57 ")" - SEMICOLON@57..58 ";" - WHITESPACE@58..59 "\n" - R_CURLY@59..60 "}" - "#]], - ) - } - - #[test] - fn test_syntax_tree_with_range() { - check_range( - r#"$0fn foo() {}$0"#, - expect![[r#" - FN@0..11 - FN_KW@0..2 "fn" - WHITESPACE@2..3 " " - NAME@3..6 - IDENT@3..6 "foo" - PARAM_LIST@6..8 - L_PAREN@6..7 "(" - R_PAREN@7..8 ")" - WHITESPACE@8..9 " " - BLOCK_EXPR@9..11 - STMT_LIST@9..11 - L_CURLY@9..10 "{" - R_CURLY@10..11 "}" - "#]], - ); - - check_range( - r#" -fn test() { - $0assert!(" - fn foo() { - } - ", "");$0 -}"#, - expect![[r#" - EXPR_STMT@16..58 - MACRO_EXPR@16..57 - MACRO_CALL@16..57 - PATH@16..22 - PATH_SEGMENT@16..22 - NAME_REF@16..22 - IDENT@16..22 "assert" - BANG@22..23 "!" - TOKEN_TREE@23..57 - L_PAREN@23..24 "(" - STRING@24..52 "\"\n fn foo() {\n ..." - COMMA@52..53 "," - WHITESPACE@53..54 " " - STRING@54..56 "\"\"" - R_PAREN@56..57 ")" - SEMICOLON@57..58 ";" - "#]], - ); - } - - #[test] - fn test_syntax_tree_inside_string() { - check_range( - r#"fn test() { - assert!(" -$0fn foo() { -}$0 -fn bar() { -} - ", ""); -}"#, - expect![[r#" - SOURCE_FILE@0..12 - FN@0..12 - FN_KW@0..2 "fn" - WHITESPACE@2..3 " " - NAME@3..6 - IDENT@3..6 "foo" - PARAM_LIST@6..8 - L_PAREN@6..7 "(" - R_PAREN@7..8 ")" - WHITESPACE@8..9 " " - BLOCK_EXPR@9..12 - STMT_LIST@9..12 - L_CURLY@9..10 "{" - WHITESPACE@10..11 "\n" - R_CURLY@11..12 "}" - "#]], - ); - - // With a raw string - check_range( - r###"fn test() { - assert!(r#" -$0fn foo() { -}$0 -fn bar() { -} - "#, ""); -}"###, - expect![[r#" - SOURCE_FILE@0..12 - FN@0..12 - FN_KW@0..2 "fn" - WHITESPACE@2..3 " " - NAME@3..6 - IDENT@3..6 "foo" - PARAM_LIST@6..8 - L_PAREN@6..7 "(" - R_PAREN@7..8 ")" - WHITESPACE@8..9 " " - BLOCK_EXPR@9..12 - STMT_LIST@9..12 - L_CURLY@9..10 "{" - WHITESPACE@10..11 "\n" - R_CURLY@11..12 "}" - "#]], - ); - - // With a raw string - check_range( - r###"fn test() { - assert!(r$0#" -fn foo() { -} -fn bar() { -}"$0#, ""); -}"###, - expect![[r#" - SOURCE_FILE@0..25 - FN@0..12 - FN_KW@0..2 "fn" - WHITESPACE@2..3 " " - NAME@3..6 - IDENT@3..6 "foo" - PARAM_LIST@6..8 - L_PAREN@6..7 "(" - R_PAREN@7..8 ")" - WHITESPACE@8..9 " " - BLOCK_EXPR@9..12 - STMT_LIST@9..12 - L_CURLY@9..10 "{" - WHITESPACE@10..11 "\n" - R_CURLY@11..12 "}" - WHITESPACE@12..13 "\n" - FN@13..25 - FN_KW@13..15 "fn" - WHITESPACE@15..16 " " - NAME@16..19 - IDENT@16..19 "bar" - PARAM_LIST@19..21 - L_PAREN@19..20 "(" - R_PAREN@20..21 ")" - WHITESPACE@21..22 " " - BLOCK_EXPR@22..25 - STMT_LIST@22..25 - L_CURLY@22..23 "{" - WHITESPACE@23..24 "\n" - R_CURLY@24..25 "}" - "#]], - ); - } -} diff --git a/src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/request.rs b/src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/request.rs index 4644ebd44d7..d01dc5fba1f 100644 --- a/src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/request.rs +++ b/src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/request.rs @@ -136,18 +136,6 @@ pub(crate) fn handle_memory_usage(state: &mut GlobalState, _: ()) -> anyhow::Res Ok(out) } -pub(crate) fn handle_syntax_tree( - snap: GlobalStateSnapshot, - params: lsp_ext::SyntaxTreeParams, -) -> anyhow::Result { - let _p = tracing::info_span!("handle_syntax_tree").entered(); - let id = from_proto::file_id(&snap, ¶ms.text_document.uri)?; - let line_index = snap.file_line_index(id)?; - let text_range = params.range.and_then(|r| from_proto::text_range(&line_index, r).ok()); - let res = snap.analysis.syntax_tree(id, text_range)?; - Ok(res) -} - pub(crate) fn handle_view_syntax_tree( snap: GlobalStateSnapshot, params: lsp_ext::ViewSyntaxTreeParams, diff --git a/src/tools/rust-analyzer/crates/rust-analyzer/src/lsp/ext.rs b/src/tools/rust-analyzer/crates/rust-analyzer/src/lsp/ext.rs index 48c2ff0a153..134de92feab 100644 --- a/src/tools/rust-analyzer/crates/rust-analyzer/src/lsp/ext.rs +++ b/src/tools/rust-analyzer/crates/rust-analyzer/src/lsp/ext.rs @@ -108,21 +108,6 @@ impl Request for RebuildProcMacros { const METHOD: &'static str = "rust-analyzer/rebuildProcMacros"; } -pub enum SyntaxTree {} - -impl Request for SyntaxTree { - type Params = SyntaxTreeParams; - type Result = String; - const METHOD: &'static str = "rust-analyzer/syntaxTree"; -} - -#[derive(Deserialize, Serialize, Debug)] -#[serde(rename_all = "camelCase")] -pub struct SyntaxTreeParams { - pub text_document: TextDocumentIdentifier, - pub range: Option, -} - pub enum ViewSyntaxTree {} impl Request for ViewSyntaxTree { diff --git a/src/tools/rust-analyzer/crates/rust-analyzer/src/main_loop.rs b/src/tools/rust-analyzer/crates/rust-analyzer/src/main_loop.rs index 325b0afc714..d6dc8b521fd 100644 --- a/src/tools/rust-analyzer/crates/rust-analyzer/src/main_loop.rs +++ b/src/tools/rust-analyzer/crates/rust-analyzer/src/main_loop.rs @@ -1145,7 +1145,6 @@ impl GlobalState { .on::(handlers::handle_workspace_symbol) .on::(handlers::handle_ssr) .on::(handlers::handle_view_recursive_memory_layout) - .on::(handlers::handle_syntax_tree) .on::(handlers::handle_view_syntax_tree) .on::(handlers::handle_view_hir) .on::(handlers::handle_view_mir) diff --git a/src/tools/rust-analyzer/docs/dev/lsp-extensions.md b/src/tools/rust-analyzer/docs/dev/lsp-extensions.md index 8cd04227738..a632fc6f5fb 100644 --- a/src/tools/rust-analyzer/docs/dev/lsp-extensions.md +++ b/src/tools/rust-analyzer/docs/dev/lsp-extensions.md @@ -1,5 +1,5 @@