This repository was archived by the owner on May 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprojectSources.ts
95 lines (87 loc) · 3.06 KB
/
projectSources.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
import {
IConnection,
TextDocument,
DidChangeWatchedFilesParams,
FileChangeType
} from 'vscode-languageserver';
import {
swiftSourcesIn,
stat,
convertFileToTextDocument
} from './sourceSwiftInProject';
const uriPrefix = 'file://';
/**
* Remove the prefix, if present, from a string and return the new string.
*
* @return The string argument without the prefix. If the string does not contain the prefix no
* modification was made.
*/
function removePrefixFrom(string: string, prefix: string): string {
if (string.startsWith(prefix)) {
return string.substring(prefix.length);
} else {
return string;
}
}
/**
* A simple manager for the Swift sources in the workspace.
*/
export class ProjectSources {
private _projectURI: string;
private _sources: Map<string, TextDocument>;
/**
* Create a new `ProjectSources` instance to manage the sources for.
* @param projectURI The URI to the project workspace.
*/
constructor(projectURI: string) {
this._projectURI = removePrefixFrom(projectURI, uriPrefix);
this._sources = new Map<string, TextDocument>();
swiftSourcesIn(this._projectURI).then((documents) => {
for (let document of documents) {
this._sources.set(document.uri, document);
}
});
}
/**
* Generate a set of build arguments that will be sent to SourceKit
* for the current set of project sources.
*
* @return the URI's of all Swift source documents.
*/
public getBuildArgumentsFor(uri: string): string[] {
let sources = Array.from(this._sources.keys());
let uriWithoutPrefix = removePrefixFrom(uri, uriPrefix);
let index = sources.indexOf(uriWithoutPrefix);
if (index >= 0) {
sources.splice(index, 1);
}
return sources;
}
/**
* Listens for low level notification on the given connection to
* update the text documents managed by this instance.
*
* @param connection The connection to listen on.
*/
public listen(connection: IConnection): void {
// This change watcher is looking for modifications to Swift source files
// in the project workspace. It attempts to keep the `workspaceDocuments`
// Map in sync with the actual files in the workspace.
connection.onDidChangeWatchedFiles((change) => {
for (let event of change.changes) {
let uriWithoutPrefix = removePrefixFrom(event.uri, uriPrefix);
switch (event.type) {
case FileChangeType.Created:
let applyToMap = this._sources.set.bind(this._sources, uriWithoutPrefix);
stat(uriWithoutPrefix)
.then(convertFileToTextDocument)
.then(applyToMap);
break;
case FileChangeType.Deleted:
this._sources.delete(uriWithoutPrefix)
break;
}
}
});
}
}