Skip to content

Commit 0372744

Browse files
edgardmessiasJohnstonCode
authored andcommitted
refactor: Removed check active editor from model (#443) (#517)
1 parent a6aeb92 commit 0372744

File tree

3 files changed

+88
-55
lines changed

3 files changed

+88
-55
lines changed

src/contexts/checkActiveEditor.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { commands, Disposable, window } from "vscode";
2+
import { Status } from "../common/types";
3+
import { debounce } from "../decorators";
4+
import { Model } from "../model";
5+
import { IDisposable } from "../util";
6+
7+
export class CheckActiveEditor implements IDisposable {
8+
private disposables: Disposable[] = [];
9+
10+
constructor(private model: Model) {
11+
// When repository update, like update
12+
model.onDidChangeStatusRepository(
13+
this.checkHasChangesOnActiveEditor,
14+
this,
15+
this.disposables
16+
);
17+
18+
window.onDidChangeActiveTextEditor(
19+
() => this.checkHasChangesOnActiveEditor(),
20+
this,
21+
this.disposables
22+
);
23+
}
24+
25+
@debounce(100)
26+
private checkHasChangesOnActiveEditor() {
27+
commands.executeCommand(
28+
"setContext",
29+
"svnActiveEditorHasChanges",
30+
this.hasChangesOnActiveEditor()
31+
);
32+
}
33+
34+
private hasChangesOnActiveEditor(): boolean {
35+
if (!window.activeTextEditor) {
36+
return false;
37+
}
38+
const uri = window.activeTextEditor.document.uri;
39+
40+
const repository = this.model.getRepository(uri);
41+
if (!repository) {
42+
return false;
43+
}
44+
45+
const resource = repository.getResourceFromFile(uri);
46+
if (!resource) {
47+
return false;
48+
}
49+
50+
switch (resource.type) {
51+
case Status.ADDED:
52+
case Status.DELETED:
53+
case Status.EXTERNAL:
54+
case Status.IGNORED:
55+
case Status.NONE:
56+
case Status.NORMAL:
57+
case Status.UNVERSIONED:
58+
return false;
59+
case Status.CONFLICTED:
60+
case Status.INCOMPLETE:
61+
case Status.MERGED:
62+
case Status.MISSING:
63+
case Status.MODIFIED:
64+
case Status.OBSTRUCTED:
65+
case Status.REPLACED:
66+
return true;
67+
}
68+
69+
// Show if not match
70+
return true;
71+
}
72+
73+
public dispose(): void {
74+
this.disposables.forEach(d => d.dispose());
75+
}
76+
}

src/extension.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from "vscode";
1010
import { registerCommands } from "./commands";
1111
import { ConstructorPolicy } from "./common/types";
12+
import { CheckActiveEditor } from "./contexts/checkActiveEditor";
1213
import SvnDecorations from "./decorations/svnDecorations";
1314
import { configuration } from "./helpers/configuration";
1415
import { ItemLogProvider } from "./historyView/itemLogProvider";
@@ -55,6 +56,8 @@ async function init(
5556
disposables.push(itemLogProvider);
5657
window.registerTreeDataProvider("itemlog", itemLogProvider);
5758

59+
disposables.push(new CheckActiveEditor(model));
60+
5861
// First, check the vscode has support to DecorationProvider
5962
if (hasSupportToDecorationProvider()) {
6063
const decoration = new SvnDecorations(model);

src/model.ts

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ export class Model implements IDisposable {
4949
public readonly onDidChangeRepository: Event<IModelChangeEvent> = this
5050
._onDidChangeRepository.event;
5151

52+
private _onDidChangeStatusRepository = new EventEmitter<Repository>();
53+
public readonly onDidChangeStatusRepository: Event<Repository> = this
54+
._onDidChangeStatusRepository.event;
55+
5256
public openRepositories: IOpenRepository[] = [];
5357
private disposables: Disposable[] = [];
5458
private enabled = false;
@@ -139,12 +143,6 @@ export class Model implements IDisposable {
139143
this.disposables
140144
);
141145

142-
window.onDidChangeActiveTextEditor(
143-
() => this.checkHasChangesOnActiveEditor(),
144-
this,
145-
this.disposables
146-
);
147-
148146
await this.scanWorkspaceFolders();
149147
}
150148

@@ -180,54 +178,6 @@ export class Model implements IDisposable {
180178
.forEach(p => this.eventuallyScanPossibleSvnRepository(p));
181179
}
182180

183-
private hasChangesOnActiveEditor(): boolean {
184-
if (!window.activeTextEditor) {
185-
return false;
186-
}
187-
const uri = window.activeTextEditor.document.uri;
188-
189-
const repository = this.getRepository(uri);
190-
if (!repository) {
191-
return false;
192-
}
193-
194-
const resource = repository.getResourceFromFile(uri);
195-
if (!resource) {
196-
return false;
197-
}
198-
199-
switch (resource.type) {
200-
case Status.ADDED:
201-
case Status.DELETED:
202-
case Status.EXTERNAL:
203-
case Status.IGNORED:
204-
case Status.NONE:
205-
case Status.NORMAL:
206-
case Status.UNVERSIONED:
207-
return false;
208-
case Status.CONFLICTED:
209-
case Status.INCOMPLETE:
210-
case Status.MERGED:
211-
case Status.MISSING:
212-
case Status.MODIFIED:
213-
case Status.OBSTRUCTED:
214-
case Status.REPLACED:
215-
return true;
216-
}
217-
218-
// Show if not match
219-
return true;
220-
}
221-
222-
@debounce(100)
223-
private checkHasChangesOnActiveEditor() {
224-
commands.executeCommand(
225-
"setContext",
226-
"svnActiveEditorHasChanges",
227-
this.hasChangesOnActiveEditor()
228-
);
229-
}
230-
231181
private disable(): void {
232182
this.repositories.forEach(repository => repository.dispose());
233183
this.openRepositories = [];
@@ -456,15 +406,19 @@ export class Model implements IDisposable {
456406
this._onDidChangeRepository.fire({ repository, uri })
457407
);
458408

409+
const changeStatus = repository.onDidChangeStatus(() => {
410+
this._onDidChangeStatusRepository.fire(repository);
411+
});
412+
459413
const statusListener = repository.onDidChangeStatus(() => {
460414
this.scanExternals(repository);
461-
this.checkHasChangesOnActiveEditor();
462415
});
463416
this.scanExternals(repository);
464417

465418
const dispose = () => {
466419
disappearListener.dispose();
467420
changeListener.dispose();
421+
changeStatus.dispose();
468422
statusListener.dispose();
469423
repository.dispose();
470424

0 commit comments

Comments
 (0)