[clang-tools-extra] r347509 - [clangd] Add 'Switch header/source' command in clangd-vscode

Marc-Andre Laperle via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 23 18:53:17 PST 2018


Author: malaperle
Date: Fri Nov 23 18:53:17 2018
New Revision: 347509

URL: http://llvm.org/viewvc/llvm-project?rev=347509&view=rev
Log:
[clangd] Add 'Switch header/source' command in clangd-vscode

Summary:
Alt+o is used on Windows/Linux and Option+Cmd+o on macOS.

Signed-off-by: Marc-Andre Laperle <malaperle at gmail.com>

Reviewers: hokein, ilya-biryukov, ioeric

Reviewed By: ioeric

Subscribers: sammccall, ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits

Differential Revision: https://reviews.llvm.org/D54781

Modified:
    clang-tools-extra/trunk/clangd/clients/clangd-vscode/package.json
    clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/extension.ts

Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/package.json
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/clients/clangd-vscode/package.json?rev=347509&r1=347508&r2=347509&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/clients/clangd-vscode/package.json (original)
+++ clang-tools-extra/trunk/clangd/clients/clangd-vscode/package.json Fri Nov 23 18:53:17 2018
@@ -74,6 +74,20 @@
                     "description": "Names a file that clangd should log a performance trace to, in chrome trace-viewer JSON format."
                 }
             }
-        }
+        },
+        "commands": [
+            {
+                "command": "clangd-vscode.switchheadersource",
+                "title": "Switch between Source/Header"
+            }
+        ],
+        "keybindings": [
+            {
+                "command": "clangd-vscode.switchheadersource",
+                "key": "Alt+o",
+                "mac": "Alt+cmd+o",
+                "when": "editorTextFocus"
+            }
+        ]
     }
 }

Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/extension.ts
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/extension.ts?rev=347509&r1=347508&r2=347509&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/extension.ts (original)
+++ clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/extension.ts Fri Nov 23 18:53:17 2018
@@ -12,6 +12,12 @@ function getConfig<T>(option: string, de
     return config.get<T>(option, defaultValue);
 }
 
+namespace SwitchSourceHeaderRequest {
+export const type =
+    new vscodelc.RequestType<vscodelc.TextDocumentIdentifier, string|undefined,
+                             void, void>('textDocument/switchSourceHeader');
+}
+
 /**
  *  this method is called when your extension is activate
  *  your extension is activated the very first time the command is executed
@@ -51,8 +57,25 @@ export function activate(context: vscode
         }
     };
 
-    const clangdClient = new vscodelc.LanguageClient('Clang Language Server', serverOptions, clientOptions);
-    console.log('Clang Language Server is now active!');
-
-    const disposable = clangdClient.start();
+  const clangdClient = new vscodelc.LanguageClient('Clang Language Server', serverOptions, clientOptions);
+  console.log('Clang Language Server is now active!');
+  context.subscriptions.push(clangdClient.start());
+  context.subscriptions.push(vscode.commands.registerCommand(
+      'clangd-vscode.switchheadersource', async () => {
+        const uri =
+            vscode.Uri.file(vscode.window.activeTextEditor.document.fileName);
+        if (!uri) {
+          return;
+        }
+        const docIdentifier =
+            vscodelc.TextDocumentIdentifier.create(uri.toString());
+        const sourceUri = await clangdClient.sendRequest(
+            SwitchSourceHeaderRequest.type, docIdentifier);
+        if (!sourceUri) {
+          return;
+        }
+        const doc = await vscode.workspace.openTextDocument(
+            vscode.Uri.parse(sourceUri));
+        vscode.window.showTextDocument(doc);
+      }));
 }




More information about the cfe-commits mailing list