-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
closeUnchangedFiles.ts
151 lines (118 loc) · 3.89 KB
/
closeUnchangedFiles.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'use strict';
import { commands, TextEditor, Uri, window } from 'vscode';
import { TextEditorComparer, UriComparer } from '../comparers';
import { BuiltInCommands } from '../constants';
import type { Container } from '../container';
import { Logger } from '../logger';
import { Messages } from '../messages';
import { Functions } from '../system';
import { Command, command, Commands, getRepoPathOrPrompt } from './common';
export interface CloseUnchangedFilesCommandArgs {
uris?: Uri[];
}
@command()
export class CloseUnchangedFilesCommand extends Command {
private _onEditorChangedFn: ((editor: TextEditor | undefined) => void) | undefined;
constructor(private readonly container: Container) {
super(Commands.CloseUnchangedFiles);
}
async execute(args?: CloseUnchangedFilesCommandArgs) {
args = { ...args };
try {
if (args.uris == null) {
const repoPath = await getRepoPathOrPrompt('Close All Unchanged Files');
if (!repoPath) return;
const status = await this.container.git.getStatusForRepo(repoPath);
if (status == null) {
void window.showWarningMessage('Unable to close unchanged files');
return;
}
args.uris = status.files.map(f => f.uri);
}
if (args.uris.length === 0) {
void commands.executeCommand(BuiltInCommands.CloseAllEditors);
return;
}
const disposable = window.onDidChangeActiveTextEditor(
Functions.debounce((e: TextEditor | undefined) => this._onEditorChangedFn?.(e), 50),
);
let editor = window.activeTextEditor;
let count = 0;
let loopCount = 0;
const editors: TextEditor[] = [];
// Find out how many editors there are
while (true) {
if (editor != null) {
let found = false;
for (const e of editors) {
if (TextEditorComparer.equals(e, editor, { usePosition: true })) {
found = true;
break;
}
}
if (found) break;
// Start counting at the first real editor
count++;
editors.push(editor);
} else if (count !== 0) {
count++;
}
editor = await this.nextEditor();
loopCount++;
// Break out if we've looped 4 times and haven't found any editors
if (loopCount >= 4 && editors.length === 0) break;
}
if (editors.length) {
editor = window.activeTextEditor;
for (let i = 0; i <= count; i++) {
if (
editor == null ||
editor.document.isDirty ||
// eslint-disable-next-line no-loop-func
args.uris.some(uri => UriComparer.equals(uri, editor?.document.uri))
) {
editor = await this.nextEditor();
} else {
editor = await this.closeEditor();
}
}
}
disposable.dispose();
} catch (ex) {
Logger.error(ex, 'CloseUnchangedFilesCommand');
void Messages.showGenericErrorMessage('Unable to close all unchanged files');
}
}
private async closeEditor(timeout: number = 500): Promise<TextEditor | undefined> {
const editor = window.activeTextEditor;
void (await commands.executeCommand(BuiltInCommands.CloseActiveEditor));
if (editor !== window.activeTextEditor) {
return window.activeTextEditor;
}
return this.waitForEditorChange(timeout);
}
private async nextEditor(timeout: number = 500): Promise<TextEditor | undefined> {
const editor = window.activeTextEditor;
void (await commands.executeCommand(BuiltInCommands.NextEditor));
if (editor !== window.activeTextEditor) {
return window.activeTextEditor;
}
return this.waitForEditorChange(timeout);
}
private waitForEditorChange(timeout: number = 500): Promise<TextEditor | undefined> {
return new Promise<TextEditor | undefined>(resolve => {
let timer: ReturnType<typeof setTimeout> | undefined;
this._onEditorChangedFn = (editor: TextEditor | undefined) => {
if (timer != null) {
clearTimeout(timer);
timer = undefined;
resolve(editor);
}
};
timer = setTimeout(() => {
timer = undefined;
resolve(window.activeTextEditor);
}, timeout);
});
}
}