Skip to content

Commit 96ef47b

Browse files
Expose a button to hide files marked on the checkbox (#141)
1 parent 2a9d406 commit 96ef47b

3 files changed

Lines changed: 67 additions & 4 deletions

File tree

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@
148148
"icon": "$(list-tree)",
149149
"category": "Git Tree Compare"
150150
},
151+
{
152+
"command": "gitTreeCompare.hideCheckedInTree",
153+
"title": "Hide checked files",
154+
"icon": "$(eye)",
155+
"category": "Git Tree Compare"
156+
},
157+
{
158+
"command": "gitTreeCompare.showCheckedInTree",
159+
"title": "Show checked files",
160+
"icon": "$(eye-closed)",
161+
"category": "Git Tree Compare"
162+
},
151163
{
152164
"command": "gitTreeCompare.searchChanges",
153165
"title": "Search Changes",
@@ -315,6 +327,16 @@
315327
"when": "view == gitTreeCompare && config.gitTreeCompare.showCheckboxes",
316328
"group": "3_options"
317329
},
330+
{
331+
"command": "gitTreeCompare.hideCheckedInTree",
332+
"when": "view == gitTreeCompare && config.gitTreeCompare.showCheckboxes && !gitTreeCompare.hideCheckedFiles",
333+
"group": "navigation@0"
334+
},
335+
{
336+
"command": "gitTreeCompare.showCheckedInTree",
337+
"when": "view == gitTreeCompare && config.gitTreeCompare.showCheckboxes && gitTreeCompare.hideCheckedFiles",
338+
"group": "navigation@0"
339+
},
318340
{
319341
"command": "gitTreeCompare.viewAsList",
320342
"when": "view == gitTreeCompare && !gitTreeCompare.viewAsList",

src/extension.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ export function activate(context: ExtensionContext) {
9494
commands.registerCommand(NAMESPACE + '.viewAsTree', () => {
9595
runAfterInit(() => provider!.viewAsTree(true));
9696
});
97+
commands.registerCommand(NAMESPACE + '.hideCheckedInTree', () => {
98+
runAfterInit(() => provider!.setHideCheckedFiles(true));
99+
});
100+
commands.registerCommand(NAMESPACE + '.showCheckedInTree', () => {
101+
runAfterInit(() => provider!.setHideCheckedFiles(false));
102+
});
97103
commands.registerCommand(NAMESPACE + '.searchChanges', () => {
98104
runAfterInit(() => provider!.searchChanges());
99105
});
@@ -133,6 +139,7 @@ export function activate(context: ExtensionContext) {
133139

134140
// Set initial context for menu enablement (starts in tree view mode)
135141
commands.executeCommand('setContext', NAMESPACE + '.viewAsList', false);
142+
commands.executeCommand('setContext', NAMESPACE + '.hideCheckedFiles', false);
136143
commands.executeCommand('setContext', NAMESPACE + '.isFiltered', false);
137144

138145
provider = new GitTreeCompareProvider(git, gitApi, outputChannel, context.globalState, context.asAbsolutePath);

src/treeProvider.ts

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ export class GitTreeCompareProvider implements TreeDataProvider<Element>, Dispos
146146
private repository: Repository | undefined;
147147
private baseRef: string;
148148
private viewAsList = false;
149+
private hideCheckedFiles = false;
149150
private searchFilter: string | undefined;
150151

151152
// Static state of repository
@@ -368,6 +369,9 @@ export class GitTreeCompareProvider implements TreeDataProvider<Element>, Dispos
368369
});
369370
}
370371
}
372+
if (this.hideCheckedFiles) {
373+
this._onDidChangeTreeData.fire();
374+
}
371375
}
372376

373377
private handleActiveEditorChange(editor: TextEditor | undefined) {
@@ -889,6 +893,10 @@ export class GitTreeCompareProvider implements TreeDataProvider<Element>, Dispos
889893
const oldSortOrder = this.sortOrder;
890894
const oldShowDiffStats = this.showDiffStats;
891895
this.readConfig();
896+
if (oldshowCheckboxes && !this.showCheckboxes && this.hideCheckedFiles) {
897+
this.hideCheckedFiles = false;
898+
commands.executeCommand('setContext', NAMESPACE + '.hideCheckedFiles', false);
899+
}
892900
if (oldTreeRootIsRepo != this.treeRootIsRepo ||
893901
oldInclude != this.includeFilesOutsideWorkspaceFolderRoot ||
894902
oldOpenChangesOnSelect != this.openChangesOnSelect ||
@@ -950,8 +958,24 @@ export class GitTreeCompareProvider implements TreeDataProvider<Element>, Dispos
950958
relativePath.toLowerCase().includes(searchLower);
951959
}
952960

961+
private isFileCheckboxChecked(dstAbsPath: string): boolean {
962+
const stateInfo = this.checkboxStates.get(dstAbsPath);
963+
return stateInfo?.state === TreeItemCheckboxState.Checked;
964+
}
965+
966+
/** Whether this file row should appear in the tree (search + optional hide-checked). */
967+
private fileVisibleInTree(dstAbsPath: string, relPathBase: string): boolean {
968+
if (!this.matchesFilter(dstAbsPath, relPathBase)) {
969+
return false;
970+
}
971+
if (this.hideCheckedFiles && this.isFileCheckboxChecked(dstAbsPath)) {
972+
return false;
973+
}
974+
return true;
975+
}
976+
953977
private folderHasMatchingFiles(folder: string, useFilesOutsideTreeRoot: boolean): boolean {
954-
if (!this.searchFilter) {
978+
if (!this.searchFilter && !this.hideCheckedFiles) {
955979
return true;
956980
}
957981
const files = useFilesOutsideTreeRoot ? this.filesOutsideTreeRoot : this.filesInsideTreeRoot;
@@ -960,7 +984,7 @@ export class GitTreeCompareProvider implements TreeDataProvider<Element>, Dispos
960984
for (const [folderPath, fileEntries] of files.entries()) {
961985
if (folderPath === folder || folderPath.startsWith(folder + path.sep)) {
962986
for (const file of fileEntries) {
963-
if (this.matchesFilter(file.dstAbsPath, relPathBase)) {
987+
if (this.fileVisibleInTree(file.dstAbsPath, relPathBase)) {
964988
return true;
965989
}
966990
}
@@ -987,7 +1011,7 @@ export class GitTreeCompareProvider implements TreeDataProvider<Element>, Dispos
9871011
for (const folder2 of folders) {
9881012
const fileEntries = files.get(folder2)!;
9891013
for (const file of fileEntries) {
990-
if (this.matchesFilter(file.dstAbsPath, relPathBase)) {
1014+
if (this.fileVisibleInTree(file.dstAbsPath, relPathBase)) {
9911015
const dstRelPath = path.relative(relPathBase, file.dstAbsPath);
9921016
entries.push(new FileElement(file.srcAbsPath, file.dstAbsPath, dstRelPath, file.status, file.isSubmodule, file.stats));
9931017
}
@@ -1050,7 +1074,7 @@ export class GitTreeCompareProvider implements TreeDataProvider<Element>, Dispos
10501074
// there are no files within treeRoot, therefore, this is guarded
10511075
if (fileEntries) {
10521076
for (const file of fileEntries) {
1053-
if (this.matchesFilter(file.dstAbsPath, relPathBase)) {
1077+
if (this.fileVisibleInTree(file.dstAbsPath, relPathBase)) {
10541078
const dstRelPath = path.relative(relPathBase, file.dstAbsPath);
10551079
entries.push(new FileElement(file.srcAbsPath, file.dstAbsPath, dstRelPath, file.status, file.isSubmodule, file.stats));
10561080
}
@@ -1628,6 +1652,16 @@ export class GitTreeCompareProvider implements TreeDataProvider<Element>, Dispos
16281652
this.viewAsList = viewAsList;
16291653
commands.executeCommand('setContext', NAMESPACE + '.viewAsList', viewAsList);
16301654
this.log('Refreshing tree');
1655+
this._onDidChangeTreeData.fire();
1656+
}
1657+
1658+
setHideCheckedFiles(hide: boolean) {
1659+
if (hide === this.hideCheckedFiles) {
1660+
return;
1661+
}
1662+
this.hideCheckedFiles = hide;
1663+
commands.executeCommand('setContext', NAMESPACE + '.hideCheckedFiles', hide);
1664+
this.log('Refreshing tree');
16311665
this.fireTreeDataChange();
16321666
}
16331667

0 commit comments

Comments
 (0)