|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import * as path from 'path' |
| 3 | +import { ShowDiffMessage } from '@/views/panel/types/messages' |
| 4 | +import { Logger } from '@shared/utils/logger' |
| 5 | +import { dictionary } from '@shared/constants/dictionary' |
| 6 | + |
| 7 | +export const handle_show_diff = async ( |
| 8 | + message: ShowDiffMessage |
| 9 | +): Promise<void> => { |
| 10 | + const { file_path } = message |
| 11 | + const workspace_folders = vscode.workspace.workspaceFolders |
| 12 | + if (!workspace_folders) { |
| 13 | + Logger.warn({ |
| 14 | + function_name: 'handle_show_diff', |
| 15 | + message: 'No workspace folders open' |
| 16 | + }) |
| 17 | + vscode.window.showWarningMessage( |
| 18 | + dictionary.error_message.NO_WORKSPACE_FOLDERS_FOUND |
| 19 | + ) |
| 20 | + return |
| 21 | + } |
| 22 | + |
| 23 | + let target_workspace: vscode.WorkspaceFolder | undefined |
| 24 | + |
| 25 | + if (workspace_folders.length == 1) { |
| 26 | + target_workspace = workspace_folders[0] |
| 27 | + } else { |
| 28 | + for (const folder of workspace_folders) { |
| 29 | + const potential_uri = vscode.Uri.joinPath(folder.uri, file_path) |
| 30 | + try { |
| 31 | + await vscode.workspace.fs.stat(potential_uri) |
| 32 | + target_workspace = folder |
| 33 | + break |
| 34 | + } catch {} |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + if (!target_workspace) { |
| 39 | + const error_message = |
| 40 | + dictionary.error_message.WORKSPACE_NOT_FOUND_FOR_FILE(file_path) |
| 41 | + Logger.error({ |
| 42 | + function_name: 'handle_show_diff', |
| 43 | + message: error_message, |
| 44 | + data: { file_path } |
| 45 | + }) |
| 46 | + vscode.window.showErrorMessage(error_message) |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + const rightUri = vscode.Uri.joinPath(target_workspace.uri, file_path) |
| 51 | + |
| 52 | + try { |
| 53 | + const leftUri = rightUri.with({ |
| 54 | + scheme: 'git', |
| 55 | + query: JSON.stringify({ path: rightUri.fsPath, ref: 'HEAD' }) |
| 56 | + }) |
| 57 | + const file_name = path.basename(file_path) |
| 58 | + const title = `${file_name} (HEAD ↔ Working Tree)` |
| 59 | + |
| 60 | + await vscode.commands.executeCommand( |
| 61 | + 'vscode.diff', |
| 62 | + leftUri, |
| 63 | + rightUri, |
| 64 | + title |
| 65 | + ) |
| 66 | + } catch (error) { |
| 67 | + const error_message = `Could not show diff for file: ${file_path}` |
| 68 | + Logger.error({ |
| 69 | + function_name: 'handle_show_diff', |
| 70 | + message: error_message, |
| 71 | + data: { error, file_path } |
| 72 | + }) |
| 73 | + vscode.window.showErrorMessage(`${error_message}. See logs for details.`) |
| 74 | + } |
| 75 | +} |
0 commit comments