Skip to content

Commit 876bf6b

Browse files
authored
feat: Add confirmation dialog for taking files (#214)
1 parent f2f72c1 commit 876bf6b

File tree

7 files changed

+48
-26
lines changed

7 files changed

+48
-26
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ There are several ways to choose folders to compare:
8585
- `ignoreAllWhiteSpaces` - boolean - ignore all white space differences (similar to `diff -w`)
8686
- `ignoreEmptyLines` - boolean - ignore differences caused by empty lines (similar to `diff -B`)
8787
- `respectGitIgnore` - boolean - include / exclude files based on .gitignore - this option works together with `includeFilter` and `excludeFilter` options. ⚠️ The extension supports the main basic gitignore rules. For instance, it supports negation (`!`), but it doesn't support .gitignore files in subfolders. If there is an important use case that is not supported, please open an issue.
88+
- `warnBeforeDelete` - boolean - Show a warning message before deleting files
89+
- `warnBeforeTake` - boolean - Show a warning message before taking/replacing files
8890
- `defaultDiffViewMode` - One of the options: "tree" or "list". The default view mode when vscode is opened. Later it can be toggled by the "View as list / View as tree" button
8991

9092
## CLI

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "compare-folders",
33
"displayName": "Compare Folders",
44
"description": "Compare folders by contents, present the files that have differences and display the diffs side by side",
5-
"version": "0.25.2",
5+
"version": "0.25.3",
66
"repository": {
77
"type": "git",
88
"url": "https://github.com/moshfeu/vscode-compare-folders"
@@ -178,6 +178,12 @@
178178
"default": "true",
179179
"description": "Show a warning message before deleting files",
180180
"scope": "resource"
181+
},
182+
"compareFolders.warnBeforeTake": {
183+
"type": "boolean",
184+
"default": true,
185+
"description": "Show a warning message before taking/replacing files",
186+
"scope": "resource"
181187
}
182188
}
183189
},

src/providers/foldersCompareProvider.ts

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { getConfiguration } from '../services/configuration';
3030
import { setContext } from '../context/global';
3131
import { HAS_FOLDERS } from '../constants/contextKeys';
3232
import * as logger from '../services/logger';
33-
import { showErrorMessage, showErrorMessageWithMoreInfo, showInfoMessageWithTimeout } from '../utils/ui';
33+
import { showErrorMessage, showErrorMessageWithMoreInfo, showInfoMessageWithTimeout, warnBefore } from '../utils/ui';
3434
import { showUnaccessibleWarning } from '../services/validators';
3535
import { uiContext, type DiffViewMode } from '../context/ui';
3636

@@ -230,35 +230,30 @@ export class CompareFoldersProvider implements TreeDataProvider<File> {
230230
};
231231

232232
deleteFile = async (e: TreeItem) => {
233-
const yesMessage = `Yes. I know what I'm doing`;
234-
let shouldDelete = true;
235-
236-
if (getConfiguration('warnBeforeDelete')) {
237-
shouldDelete =
238-
yesMessage ===
239-
(await window.showInformationMessage(
240-
'Are you sure you want to delete this file?',
241-
{
242-
modal: true,
243-
},
244-
yesMessage
245-
));
246-
}
233+
const shouldDelete = await warnBefore('Are you sure you want to delete this file?');
247234

248235
if (shouldDelete) {
249236
removeSync(e.resourceUri!.fsPath);
250237
this.refresh(false);
251238
}
252239
};
253240

254-
takeMyFile = (e: TreeItem) => {
255-
const [[filePath]] = e.command!.arguments!;
256-
this.copyToFolder(Uri.file(filePath), 'to-compared');
241+
takeMyFile = async (e: TreeItem) => {
242+
const shouldTake = await warnBefore('Are you sure you want to take my file?');
243+
244+
if (shouldTake) {
245+
const [[filePath]] = e.command!.arguments!;
246+
this.copyToFolder(Uri.file(filePath), 'to-compared');
247+
}
257248
};
258249

259-
takeComparedFile = (e: TreeItem) => {
260-
const [[, filePath]] = e.command!.arguments!;
261-
this.copyToFolder(Uri.file(filePath), 'to-me');
250+
takeComparedFile = async (e: TreeItem) => {
251+
const shouldTake = await warnBefore('Are you sure you want to take the compared file?');
252+
253+
if (shouldTake) {
254+
const [[, filePath]] = e.command!.arguments!;
255+
this.copyToFolder(Uri.file(filePath), 'to-me');
256+
}
262257
};
263258

264259
copyToFolder(uri: Uri, direction: 'to-compared' | 'to-me') {

src/services/configuration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface IConfigurations {
1414
showIdentical: boolean;
1515
useDiffMerge: boolean;
1616
warnBeforeDelete: boolean;
17+
warnBeforeTake: boolean;
1718
folderLeft: string;
1819
folderRight: string;
1920
ignoreWhiteSpaces: boolean;

src/test/suite/includeExcludeFilesGetter/includeExcludeFilesGetter.testkit.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const defaultSettings: Partial<IConfigurations> = {
1616
showIdentical: false,
1717
useDiffMerge: false,
1818
warnBeforeDelete: false,
19+
warnBeforeTake: false,
1920
folderLeft: undefined,
2021
folderRight: undefined,
2122
ignoreWhiteSpaces: false,

src/utils/consts.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export const GLOB_ROOT = '*';
2-
export const GIT_FOLDER = '.git';
2+
export const GIT_FOLDER = '.git';
3+
export const YES_MESSAGE = 'Yes, go for it';

src/utils/ui.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { window, ProgressLocation, env, Uri, version } from 'vscode';
22
import os from 'os';
33
import * as logger from '../services/logger';
44
import { globalState } from '../services/globalState';
5+
import { getConfiguration } from '../services/configuration';
6+
import { YES_MESSAGE } from '../utils/consts';
57

68
export function showInfoMessageWithTimeout(message: string, timeout: number = 3000) {
79
const upTo = timeout / 10;
@@ -49,9 +51,8 @@ OS: ${os.platform()} ${os.release()}
4951
**Stack**
5052
${error.stack || error.message || error}
5153
`;
52-
const url = `https://github.com/moshfeu/vscode-compare-folders/issues/new?title=[error] ${
53-
error.message || error
54-
}&body=${body}`;
54+
const url = `https://github.com/moshfeu/vscode-compare-folders/issues/new?title=[error] ${error.message || error
55+
}&body=${body}`;
5556

5657
const uri = Uri.parse(url);
5758
env.openExternal(uri);
@@ -68,3 +69,18 @@ export async function showErrorMessageWithMoreInfo(message: string, link: string
6869
env.openExternal(Uri.parse(link));
6970
}
7071
}
72+
73+
export const warnBefore = async (message: string) => {
74+
if (getConfiguration('warnBeforeTake')) {
75+
return YES_MESSAGE ===
76+
(await window.showInformationMessage(
77+
message,
78+
{
79+
modal: true,
80+
},
81+
YES_MESSAGE
82+
));
83+
}
84+
85+
return true;
86+
}

0 commit comments

Comments
 (0)