Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,36 @@
{
"command": "diffMerge.compareFileWithClipboard",
"group": "z@102"
},
{
"command": "diffMerge.compareSelectionWithClipboard",
"when": "editorHasSelection",
"group": "z@103"
},
{
"command": "diffMerge.compareClipboardWithPrevious",
"group": "z@104"
}
],
"editor/context": [
{
"command": "diffMerge.compareSelectionWithClipboard",
"when": "editorHasSelection",
"group": "3_compare@100"
}
],
"commandPalette": [
{
"command": "diffMerge.chooseFile",
"when": "editorIsOpen"
},
{
"command": "diffMerge.compareSelectionWithClipboard",
"when": "editorHasSelection"
},
{
"command": "diffMerge.compareClipboardWithPrevious"
},
{
"command": "diffMerge.scm.file",
"when": "false"
Expand Down Expand Up @@ -217,6 +240,14 @@
"title": "[Diff & Merge] Compare file with Clipboard",
"command": "diffMerge.compareFileWithClipboard"
},
{
"title": "[Diff & Merge] Compare selection with Clipboard",
"command": "diffMerge.compareSelectionWithClipboard"
},
{
"title": "[Diff & Merge] Compare clipboard with previous clipboard",
"command": "diffMerge.compareClipboardWithPrevious"
},
{
"title": "[Diff & Merge] Select file to compare",
"command": "diffMerge.selectToCompare"
Expand Down
73 changes: 73 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ export function init(context: ExtensionContext) {
'diffMerge.compareFileWithClipboard',
compareFileWithClipboard
),
commands.registerCommand(
'diffMerge.compareSelectionWithClipboard',
compareSelectionWithClipboard
),
commands.registerCommand(
'diffMerge.compareClipboardWithPrevious',
compareClipboardWithPrevious
),
commands.registerCommand('diffMerge.openWithDiffMerge', reopenCurrentWithDiffMerge),
commands.registerCommand('diffMerge.applyAllChanges', applyAllChanges)
);
Expand Down Expand Up @@ -73,6 +81,70 @@ export function init(context: ExtensionContext) {
});
}

async function compareSelectionWithClipboard() {
const editor = window.activeTextEditor;
if (!editor) {
window.showInformationMessage(
'This command has to be run only when a text based file is open'
);
log('This command has to be run only when a file is open');
return;
}

const selection = editor.selection;
if (selection.isEmpty) {
window.showInformationMessage(
'Please select some text to compare with clipboard'
);
log('No text selected for comparison');
return;
}

const selectedText = editor.document.getText(selection);
const clipboardText = await env.clipboard.readText();

showDiff({
context,
leftContent: clipboardText,
leftPath: 'Clipboard',
rightPath: `Selection from ${editor.document.uri.fsPath}`,
rightContent: selectedText,
});
}

async function compareClipboardWithPrevious() {
const currentClipboard = await env.clipboard.readText();

if (!previousClipboardContent) {
// Store current clipboard as previous for next time
previousClipboardContent = currentClipboard;
window.showInformationMessage(
'Previous clipboard content saved. Run this command again to compare with current clipboard.'
);
log('Previous clipboard content saved');
return;
}

if (currentClipboard === previousClipboardContent) {
window.showInformationMessage(
'Current clipboard content is the same as previous clipboard content.'
);
log('Clipboard content unchanged');
return;
}

showDiff({
context,
leftContent: previousClipboardContent,
leftPath: 'Previous Clipboard',
rightPath: 'Current Clipboard',
rightContent: currentClipboard,
});

// Update previous clipboard to current for next comparison
previousClipboardContent = currentClipboard;
}

function blank() {
showDiff({ leftContent: '', rightContent: '', rightPath: '', context });
}
Expand Down Expand Up @@ -130,6 +202,7 @@ export function init(context: ExtensionContext) {
}

let selectedFilePath: string;
let previousClipboardContent: string | undefined;
function selectToCompare(e: Uri) {
try {
selectedFilePath = tryToGetPath(e);
Expand Down
Loading