about summary refs log tree commit diff
path: root/src/tools/rust-analyzer
diff options
context:
space:
mode:
authorGiga Bowser <45986823+Giga-Bowser@users.noreply.github.com>2025-01-07 14:01:00 -0600
committerGiga Bowser <45986823+Giga-Bowser@users.noreply.github.com>2025-01-09 14:03:18 -0600
commit56d06fb40f1f7700ab38bf9208c01541dc560436 (patch)
treebe864cc4c486ee087d3b65d8d9100bdcbc150d6e /src/tools/rust-analyzer
parent628db5361238f3fd4614a042e273fd9820dbb523 (diff)
downloadrust-56d06fb40f1f7700ab38bf9208c01541dc560436.tar.gz
rust-56d06fb40f1f7700ab38bf9208c01541dc560436.zip
Add an action to copy an element from the syntax tree view
Diffstat (limited to 'src/tools/rust-analyzer')
-rw-r--r--src/tools/rust-analyzer/editors/code/package.json15
-rw-r--r--src/tools/rust-analyzer/editors/code/src/commands.ts39
-rw-r--r--src/tools/rust-analyzer/editors/code/src/main.ts1
3 files changed, 55 insertions, 0 deletions
diff --git a/src/tools/rust-analyzer/editors/code/package.json b/src/tools/rust-analyzer/editors/code/package.json
index 6deb5bfd775..69007194541 100644
--- a/src/tools/rust-analyzer/editors/code/package.json
+++ b/src/tools/rust-analyzer/editors/code/package.json
@@ -290,6 +290,12 @@
                 "category": "rust-analyzer (syntax tree)"
             },
             {
+                "command": "rust-analyzer.syntaxTreeCopy",
+                "title": "Copy",
+                "icon": "$(copy)",
+                "category": "rust-analyzer (syntax tree)"
+            },
+            {
                 "command": "rust-analyzer.syntaxTreeHideWhitespace",
                 "title": "Hide Whitespace",
                 "icon": "$(filter)",
@@ -3371,6 +3377,10 @@
                     "when": "false"
                 },
                 {
+                    "command": "rust-analyzer.syntaxTreeCopy",
+                    "when": "false"
+                },
+                {
                     "command": "rust-analyzer.syntaxTreeHideWhitespace",
                     "when": "false"
                 },
@@ -3405,6 +3415,11 @@
             ],
             "view/item/context": [
                 {
+                    "command": "rust-analyzer.syntaxTreeCopy",
+                    "group": "inline",
+                    "when": "view == rustSyntaxTree"
+                },
+                {
                     "command": "rust-analyzer.syntaxTreeReveal",
                     "group": "inline",
                     "when": "view == rustSyntaxTree"
diff --git a/src/tools/rust-analyzer/editors/code/src/commands.ts b/src/tools/rust-analyzer/editors/code/src/commands.ts
index f94c57eee88..b3aa04af7ed 100644
--- a/src/tools/rust-analyzer/editors/code/src/commands.ts
+++ b/src/tools/rust-analyzer/editors/code/src/commands.ts
@@ -372,6 +372,45 @@ export function syntaxTreeReveal(): Cmd {
     };
 }
 
+function elementToString(
+    activeDocument: vscode.TextDocument,
+    element: SyntaxElement,
+    depth: number = 0,
+): string {
+    let result = "  ".repeat(depth);
+    const start = element.istart ?? element.start;
+    const end = element.iend ?? element.end;
+
+    result += `${element.kind}@${start}..${end}`;
+
+    if (element.type === "Token") {
+        const startPosition = activeDocument.positionAt(element.start);
+        const endPosition = activeDocument.positionAt(element.end);
+        const text = activeDocument.getText(new vscode.Range(startPosition, endPosition));
+        // JSON.stringify quotes and escapes the string for us.
+        result += ` ${JSON.stringify(text)}\n`;
+    } else {
+        result += "\n";
+        for (const child of element.children) {
+            result += elementToString(activeDocument, child, depth + 1);
+        }
+    }
+
+    return result;
+}
+
+export function syntaxTreeCopy(): Cmd {
+    return async (element: SyntaxElement) => {
+        const activeDocument = vscode.window.activeTextEditor?.document;
+        if (!activeDocument) {
+            return;
+        }
+
+        const result = elementToString(activeDocument, element);
+        await vscode.env.clipboard.writeText(result);
+    };
+}
+
 export function syntaxTreeHideWhitespace(ctx: CtxInit): Cmd {
     return async () => {
         if (ctx.syntaxTreeProvider !== undefined) {
diff --git a/src/tools/rust-analyzer/editors/code/src/main.ts b/src/tools/rust-analyzer/editors/code/src/main.ts
index 59c131cb376..c84b69b66cd 100644
--- a/src/tools/rust-analyzer/editors/code/src/main.ts
+++ b/src/tools/rust-analyzer/editors/code/src/main.ts
@@ -199,6 +199,7 @@ function createCommands(): Record<string, CommandFactory> {
         openLogs: { enabled: commands.openLogs },
         revealDependency: { enabled: commands.revealDependency },
         syntaxTreeReveal: { enabled: commands.syntaxTreeReveal },
+        syntaxTreeCopy: { enabled: commands.syntaxTreeCopy },
         syntaxTreeHideWhitespace: { enabled: commands.syntaxTreeHideWhitespace },
         syntaxTreeShowWhitespace: { enabled: commands.syntaxTreeShowWhitespace },
     };