Last active
July 3, 2022 23:06
-
-
Save exceedsystem/4dfa1e2e1dcdad8cc3e748cd660e5ac2 to your computer and use it in GitHub Desktop.
An example of 'Diff two files' macro for VSCodeMacros extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // [VSCode Macros] extension | |
| // https://marketplace.visualstudio.com/items?itemName=EXCEEDSYSTEM.vscode-macros | |
| // License:MIT | |
| const vscode = require('vscode'); | |
| const fs = require('fs'); | |
| module.exports.macroCommands = { | |
| DiffFiles: { | |
| no: 1, | |
| func: diffFiles, | |
| }, | |
| }; | |
| async function diffFiles() { | |
| // Create the last opened path if it does not exist in the global variable area | |
| if (!global.bahW6wau) { | |
| global.bahW6wau = { last_opened_uri: undefined }; | |
| } | |
| // A Local function which returns a selected file | |
| const checkFile = (uris) => { | |
| if (!uris) { | |
| vscode.window.showInformationMessage('Cancelled'); | |
| return; | |
| } | |
| return uris[0]; | |
| }; | |
| // Get last opened path from the global variable area | |
| let lastUri; | |
| if (global.bahW6wau.last_opened_uri && fs.existsSync(global.bahW6wau.last_opened_uri.fsPath)) { | |
| lastUri = global.bahW6wau.last_opened_uri; | |
| } | |
| // Select both left and right files with an open file dialog | |
| const leftFiles = await vscode.window.showOpenDialog({ canSelectFiles: true, canSelectFolders: false, canSelectMany: false, defaultUri: lastUri, title: 'Select a left file for compare' }); | |
| const leftFile = checkFile(leftFiles); | |
| if (!leftFile) return; | |
| lastUri = leftFile; | |
| // Save last opened path to the global variable area | |
| global.bahW6wau.last_opened_uri = lastUri; | |
| const rightFiles = await vscode.window.showOpenDialog({ canSelectFiles: true, canSelectFolders: false, canSelectMany: false, defaultUri: lastUri, title: 'Select a right file for compare' }); | |
| const rightFile = checkFile(rightFiles); | |
| if (!rightFile) return; | |
| // Show the file differences with the built-in command of the VSCode | |
| await vscode.commands.executeCommand('vscode.diff', leftFile, rightFile, `Compare(${leftFile.fsPath} ↔ ${rightFile.fsPath})`); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment