diff --git a/package.json b/package.json index 4cabb50..c9a2a9a 100644 --- a/package.json +++ b/package.json @@ -135,6 +135,22 @@ { "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": [ @@ -142,6 +158,13 @@ "command": "diffMerge.chooseFile", "when": "editorIsOpen" }, + { + "command": "diffMerge.compareSelectionWithClipboard", + "when": "editorHasSelection" + }, + { + "command": "diffMerge.compareClipboardWithPrevious" + }, { "command": "diffMerge.scm.file", "when": "false" @@ -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" diff --git a/src/commands.ts b/src/commands.ts index f4dc395..dd6ed8b 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -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) ); @@ -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 }); } @@ -130,6 +202,7 @@ export function init(context: ExtensionContext) { } let selectedFilePath: string; + let previousClipboardContent: string | undefined; function selectToCompare(e: Uri) { try { selectedFilePath = tryToGetPath(e);