Skip to content

Commit

Permalink
Merge pull request #6 from rcoopr/main
Browse files Browse the repository at this point in the history
fix: highlight entire line when used on empty selection
  • Loading branch information
mguellsegarra authored Apr 30, 2024
2 parents 3ee313c + 7c27b8d commit 3d6df36
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -44,3 +44,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;
}
25 changes: 25 additions & 0 deletions src/test/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 3d6df36

Please sign in to comment.