Skip to content

Commit 129a994

Browse files
committed
workspace folder events now work correctly
1 parent 0c64a31 commit 129a994

File tree

4 files changed

+100
-11
lines changed

4 files changed

+100
-11
lines changed

src/model.ts

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as fs from "fs";
99
import * as path from "path";
1010
import { Repository } from "./repository";
1111
import { Svn } from "./svn";
12-
import { dispose } from "./util";
12+
import { dispose, anyEvent, filterEvent } from "./util";
1313

1414
interface OpenRepository {
1515
repository: Repository;
@@ -19,6 +19,7 @@ export class Model {
1919
public openRepositories: OpenRepository[] = [];
2020
private disposables: Disposable[] = [];
2121
private enabled = false;
22+
private possibleSvnRepositoryPaths = new Set<string>();
2223

2324
get repositories(): Repository[] {
2425
return this.openRepositories.map(r => r.repository);
@@ -46,9 +47,41 @@ export class Model {
4647
removed: []
4748
});
4849

50+
const fsWatcher = workspace.createFileSystemWatcher("**");
51+
this.disposables.push(fsWatcher);
52+
53+
const onWorkspaceChange = anyEvent(
54+
fsWatcher.onDidChange,
55+
fsWatcher.onDidCreate,
56+
fsWatcher.onDidDelete
57+
);
58+
const onPossibleSvnRepositoryChange = filterEvent(
59+
onWorkspaceChange,
60+
uri => !this.getRepository(uri)
61+
);
62+
onPossibleSvnRepositoryChange(
63+
this.onPossibleSvnRepositoryChange,
64+
this,
65+
this.disposables
66+
);
67+
4968
this.scanWorkspaceFolders();
5069
}
5170

71+
private onPossibleSvnRepositoryChange(uri: Uri): void {
72+
const possibleSvnRepositoryPath = uri.fsPath.replace(/\.svn.*$/, "");
73+
this.possibleSvnRepositoryPaths.add(possibleSvnRepositoryPath);
74+
this.eventuallyScanPossibleSvnRepositories();
75+
}
76+
77+
private eventuallyScanPossibleSvnRepositories(): void {
78+
for (const path of this.possibleSvnRepositoryPaths) {
79+
this.tryOpenRepository(path);
80+
}
81+
82+
this.possibleSvnRepositoryPaths.clear();
83+
}
84+
5285
private disable(): void {
5386
this.repositories.forEach(repository => repository.dispose());
5487
this.openRepositories = [];
@@ -112,7 +145,7 @@ export class Model {
112145
return liveRepository && liveRepository.repository;
113146
}
114147

115-
getOpenRepository(hint: any) {
148+
getOpenRepository(hint: any): OpenRepository | undefined {
116149
if (!hint) {
117150
return undefined;
118151
}
@@ -121,20 +154,38 @@ export class Model {
121154
return this.openRepositories.filter(r => r.repository === hint)[0];
122155
}
123156

124-
hint = Uri.file(hint);
157+
if (typeof hint === "string") {
158+
hint = Uri.file(hint);
159+
}
160+
161+
if (hint instanceof Uri) {
162+
for (const liveRepository of this.openRepositories) {
163+
const relativePath = path.relative(
164+
liveRepository.repository.root,
165+
hint.fsPath
166+
);
167+
168+
if (!/^\.\./.test(relativePath)) {
169+
return liveRepository;
170+
}
171+
}
172+
173+
return undefined;
174+
}
125175

126176
for (const liveRepository of this.openRepositories) {
127-
const relativePath = path.relative(
128-
liveRepository.repository.root,
129-
hint.fsPath
130-
);
177+
const repository = liveRepository.repository;
131178

132-
if (!/^\.\./.test(relativePath)) {
179+
if (hint === repository.sourceControl) {
133180
return liveRepository;
134181
}
135182

136-
return undefined;
183+
if (hint === repository.changes || hint === repository.notTracked) {
184+
return liveRepository;
185+
}
137186
}
187+
188+
return undefined;
138189
}
139190

140191
private open(repository: Repository): void {

src/repository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { dispose } from "./util";
1818

1919
export class Repository {
2020
public watcher: FileSystemWatcher;
21-
private sourceControl: SourceControl;
21+
public sourceControl: SourceControl;
2222
public changes: SourceControlResourceGroup;
2323
public notTracked: SourceControlResourceGroup;
2424
private disposables: Disposable[] = [];

src/svn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export class Repository {
155155
.pop();
156156
return currentBranch;
157157
} catch (error) {
158-
return;
158+
return "";
159159
}
160160
}
161161
}

src/util.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,43 @@
1+
import { Event } from "vscode";
2+
3+
export interface BaseDisposable {
4+
dispose(): void;
5+
}
6+
7+
export function anyEvent<T>(...events: Event<T>[]): Event<T> {
8+
return (listener, thisArgs = null, disposables?) => {
9+
const result = combinedDisposable(
10+
events.map(event => event(i => listener.call(thisArgs, i)))
11+
);
12+
13+
if (disposables) {
14+
disposables.push(result);
15+
}
16+
17+
return result;
18+
};
19+
}
20+
21+
export function filterEvent<T>(
22+
event: Event<T>,
23+
filter: (e: T) => boolean
24+
): Event<T> {
25+
return (listener, thisArgs = null, disposables?) =>
26+
event(e => filter(e) && listener.call(thisArgs, e), null, disposables);
27+
}
28+
129
export function dispose(disposables: any[]): any[] {
230
disposables.forEach(disposable => disposable.dispose());
331

432
return [];
533
}
34+
35+
export function combinedDisposable(
36+
disposables: BaseDisposable[]
37+
): BaseDisposable {
38+
return toDisposable(() => dispose(disposables));
39+
}
40+
41+
export function toDisposable(dispose: () => void): BaseDisposable {
42+
return { dispose };
43+
}

0 commit comments

Comments
 (0)