我正在構建我的第一個 VSCode 擴展并且有點掙扎。
是否可以收聽context menu事件,copy例如?
例如:當用戶在背景關系選單(背景關系選單的螢屏截圖)中單擊“復制”時, 我想獲取復制的文本。
有一個選項可以將命令添加到context menu. 但我不想那樣,我想聽現有的內置copy命令。
我知道,我可以收聽keybinding,但這不會觸發context menu事件。
uj5u.com熱心網友回復:
這是我以前的答案的更好版本 - 它只是以更簡單的方法獲取剪貼板文本:
let typeDisposable = vscode.commands.registerCommand('editor.action.clipboardCopyAction', async (arg) => myCopy(typeDisposable) );
async function myCopy(typeDisposable) {
typeDisposable.dispose(); // must dispose to avoid endless loops
// run the built-in copy command
await vscode.commands.executeCommand('editor.action.clipboardCopyAction');
// get the copied text
const clipboardText = await vscode.env.clipboard.readText();
// use your clipboard text here
console.log(clipboardText);
// re-register to continue intercepting copy commands
typeDisposable = vscode.commands.registerCommand('editor.action.clipboardCopyAction', async (arg) => myCopy(typeDisposable) );
context.subscriptions.push(typeDisposable);
}
context.subscriptions.push(typeDisposable);
- [上一版答案]
這似乎有效 - 但應該進行徹底測驗(此時它是一種思想實驗):
let typeDisposable = vscode.commands.registerCommand('editor.action.clipboardCopyAction', async (arg) => myCopy(typeDisposable) );
async function myCopy(typeDisposable) {
typeDisposable.dispose();
// get the selectedText from the editor here
const selectedRange = new vscode.Range(vscode.window.activeTextEditor.selection.start, vscode.window.activeTextEditor.selection.end);
const copiedText = vscode.window.activeTextEditor.document.getText(selectedRange);
// use your copiedText here
await vscode.commands.executeCommand('editor.action.clipboardCopyAction');
typeDisposable = vscode.commands.registerCommand('editor.action.clipboardCopyAction', async (arg) => myCopy(typeDisposable) );
context.subscriptions.push(typeDisposable);
}
context.subscriptions.push(typeDisposable);
您必須為多個選擇修改它,這應該很容易。
這將攔截對 的所有呼叫copy,包括Ctrl C。也許您可以將注冊命令限制在更有限的情況下?
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/382001.html
下一篇:在python源檔案中創建列注釋
