From cba965a562ee8b3c95e3317986fb1e5228234ccb Mon Sep 17 00:00:00 2001 From: zero-enthusiasm Date: Wed, 19 Nov 2025 10:58:58 +0000 Subject: [PATCH] Add a command to only copy selection New command codetohtml.runSelectionOnly added that only copies current selection instead of the whole file --- package-lock.json | 11 ++++++----- package.json | 4 ++++ src/extension.ts | 20 ++++++++++++++++---- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5ff59bf..72331f8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "codetohtml", - "version": "1.0.4", + "version": "1.0.8", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "codetohtml", - "version": "1.0.4", + "version": "1.0.8", "license": "MIT", "devDependencies": { "@types/mocha": "^10.0.6", @@ -276,10 +276,11 @@ "dev": true }, "node_modules/@types/node": { - "version": "18.19.17", - "resolved": "https://registry.npmjs.org/@types/node/-/node-18.19.17.tgz", - "integrity": "sha512-SzyGKgwPzuWp2SHhlpXKzCX0pIOfcI4V2eF37nNBJOhwlegQ83omtVQ1XxZpDE06V/d6AQvfQdPfnw0tRC//Ng==", + "version": "18.19.130", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.19.130.tgz", + "integrity": "sha512-GRaXQx6jGfL8sKfaIDD6OupbIHBr9jv7Jnaml9tB7l4v068PAOXqfcujMMo5PhbIs6ggR1XODELqahT2R8v0fg==", "dev": true, + "license": "MIT", "dependencies": { "undici-types": "~5.26.4" } diff --git a/package.json b/package.json index d7d0f90..5442219 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,10 @@ { "command": "codetohtml.run", "title": "[CodeToHTML] Code to HTML" + }, + { + "command": "codetohtml.runSelectionOnly", + "title": "[CodeToHTML] Code to HTML - Selection only" } ] }, diff --git a/src/extension.ts b/src/extension.ts index 461cdd0..5593bc8 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -9,7 +9,17 @@ export function activate(context: vscode.ExtensionContext) { if (!activeEditor) { return; } - const highlightHtml = await getHTML(context); + const highlightHtml = await getHTML(context, true); + await createWebview(context, highlightHtml); + })); + + // Version of the command that only copies the current selection + context.subscriptions.push(vscode.commands.registerCommand('codetohtml.runSelectionOnly', async () => { + const activeEditor = vscode.window.activeTextEditor; + if (!activeEditor) { + return; + } + const highlightHtml = await getHTML(context, false); await createWebview(context, highlightHtml); })); } @@ -40,14 +50,16 @@ function htmlPathToUri(context: vscode.ExtensionContext, webview: vscode.Webview // Webviewを使ってハイライトされたHTMLを取得 -async function getHTML(context: vscode.ExtensionContext) { +async function getHTML(context: vscode.ExtensionContext, wholeFile: boolean): Promise { // クリップボードのデータを変更するため、古いデータを一時的に保存 const clipboardText = await vscode.env.clipboard.readText(); - vscode.commands.executeCommand('editor.action.selectAll'); + if (wholeFile) { + vscode.commands.executeCommand('editor.action.selectAll'); + } vscode.commands.executeCommand('editor.action.clipboardCopyWithSyntaxHighlightingAction'); vscode.commands.executeCommand('cancelSelection'); -const webview = vscode.window.createWebviewPanel('codetohtml', 'Code to HTML', vscode.ViewColumn.One, { + const webview = vscode.window.createWebviewPanel('codetohtml', 'Code to HTML', vscode.ViewColumn.One, { enableScripts: true });