From f69911159755bde0e6cdff051dde965cc0b8a0ba Mon Sep 17 00:00:00 2001 From: Ross Cooper <37559715+Froskk@users.noreply.github.com> Date: Tue, 30 Apr 2024 13:52:32 +0100 Subject: [PATCH 1/3] fix: highlight entire line when used on empty selection --- src/extension.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index fefcff3..3ad9594 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -29,7 +29,7 @@ export function activate(context: vscode.ExtensionContext) { }); // Apply decoration - editor.setDecorations(decorationType, [...editor.selections]); + editor.setDecorations(decorationType, getSelections(editor)); // Remove decoration after specified timeout setTimeout(() => { @@ -42,5 +42,11 @@ export function activate(context: vscode.ExtensionContext) { } export function deactivate() { - vscode.commands.executeCommand("setContext", "highlightOnCopy.init", false); + +function getSelections(editor: vscode.TextEditor): readonly vscode.Selection[] | vscode.Range[] { + const selections = editor.selections; + if (selections.length === 1 && selections[0].isEmpty) { + return [editor.document.lineAt(selections[0].anchor).range]; + } + return selections; } From 4154d2caadc5ffe6ae6d702099df431cc0927841 Mon Sep 17 00:00:00 2001 From: Ross Cooper <37559715+Froskk@users.noreply.github.com> Date: Tue, 30 Apr 2024 13:58:21 +0100 Subject: [PATCH 2/3] fix: add missing test --- src/test/extension.test.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/test/extension.test.ts b/src/test/extension.test.ts index 8aa53e3..ae606af 100644 --- a/src/test/extension.test.ts +++ b/src/test/extension.test.ts @@ -35,6 +35,31 @@ suite("Extension Test Suite", () => { ); }); + test("Copy and highlight command effectively updates the clipboard for an empty selection", async () => { + const document = await vscode.workspace.openTextDocument({ + content: "Test text to be copied", + }); + + // Show the text document in the editor + const editor = await vscode.window.showTextDocument(document); + + // Select the full text in the editor + const firstLine = editor.document.lineAt(0); + editor.selection = new vscode.Selection(firstLine.range.start, firstLine.range.end); + + // Execute the command + await vscode.commands.executeCommand("highlightOnCopy.run"); + + // Get the paste content + const clipboardContent = await vscode.env.clipboard.readText(); + + assert.strictEqual( + clipboardContent, + document.getText(), + "The clipboard content should match the text in the editor." + ); + }); + test("Multi-cursor copy command effectively updates the clipboard", async () => { // Create a new text document with multiple lines const document = await vscode.workspace.openTextDocument({ From 7c27b8d635640336383cb8deeb688bdc44664ca3 Mon Sep 17 00:00:00 2001 From: Ross Cooper <37559715+Froskk@users.noreply.github.com> Date: Tue, 30 Apr 2024 14:16:38 +0100 Subject: [PATCH 3/3] fix: remove lost code --- src/extension.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/extension.ts b/src/extension.ts index 3ad9594..4eb0623 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -42,6 +42,8 @@ export function activate(context: vscode.ExtensionContext) { } export function deactivate() { + vscode.commands.executeCommand("setContext", "highlightOnCopy.init", false); +} function getSelections(editor: vscode.TextEditor): readonly vscode.Selection[] | vscode.Range[] { const selections = editor.selections;