forked from lifeart/ember-language-server
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproject.ts
More file actions
209 lines (183 loc) · 6.16 KB
/
project.ts
File metadata and controls
209 lines (183 loc) · 6.16 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { PodMatcher, ClassicPathMatcher } from './utils/path-matcher';
import { addToRegistry, removeFromRegistry, normalizeMatchNaming, NormalizedRegistryItem } from './utils/registry-api';
import { ProjectProviders, collectProjectProviders, initBuiltinProviders, AddonMeta, DependencyMeta } from './utils/addon-api';
import {
getPodModulePrefix,
findTestsForProject,
findAddonItemsForProject,
findAppItemsForProject,
isRootStartingWithFilePath,
getDepIfExists,
getPackageJSON,
cached,
PackageInfo,
} from './utils/layout-helpers';
import Server from './server';
import { Diagnostic, FileChangeType } from 'vscode-languageserver/node';
import { TextDocument } from 'vscode-languageserver-textdocument';
import * as path from 'path';
import { logError, logInfo } from './utils/logger';
import { URI } from 'vscode-uri';
export type Executor = (server: Server, command: string, args: unknown[]) => Promise<unknown>;
export type Destructor = (project: Project) => void;
export type Linter = (document: TextDocument) => Promise<Diagnostic[] | null>;
export type Watcher = (uri: string, change: FileChangeType) => void;
export interface Executors {
[key: string]: Executor;
}
export class Project {
private classicMatcher!: ClassicPathMatcher;
private podMatcher!: PodMatcher;
providers!: ProjectProviders;
builtinProviders!: ProjectProviders;
addonsMeta: AddonMeta[] = [];
dependenciesMeta: DependencyMeta[] = [];
executors: Executors = {};
watchers: Watcher[] = [];
destructors: Destructor[] = [];
linters: Linter[] = [];
initIssues: Error[] = [];
disableInitialization = false;
flags: { enableEagerRegistryInitialization: boolean } = { enableEagerRegistryInitialization: true };
files: Map<string, { version: number }> = new Map();
podModulePrefix = '';
@cached
get roots() {
const mainRoot = this.root;
const otherRoots = this.addonsMeta.map((meta) => meta.root);
// because all registry searches based on "startsWith", we could omit roots in same namespace,
// like {root/a, root/b}, because we will get results of it from {root} itself
const differentRoots = otherRoots.filter((root) => !isRootStartingWithFilePath(root, mainRoot));
return [mainRoot, ...differentRoots];
}
matchPathToType(filePath: string) {
return this.classicMatcher.metaFromPath(filePath) || this.podMatcher.metaFromPath(filePath);
}
trackChange(uri: string, change: FileChangeType) {
// prevent leaks
if (this.files.size > 10000) {
logError('too many files for project ' + this.root);
this.files.clear();
}
const rawPath = URI.parse(uri).fsPath;
if (!rawPath) {
return;
}
const filePath = path.resolve(rawPath);
const item = this.matchPathToType(filePath);
let normalizedItem: undefined | NormalizedRegistryItem = undefined;
if (item) {
normalizedItem = normalizeMatchNaming(item) as NormalizedRegistryItem;
}
if (change === 3) {
this.files.delete(filePath);
if (normalizedItem) {
removeFromRegistry(normalizedItem.name, normalizedItem.type, [filePath]);
}
} else {
if (normalizedItem) {
addToRegistry(normalizedItem.name, normalizedItem.type, [filePath]);
}
if (!this.files.has(filePath)) {
this.files.set(filePath, { version: 0 });
}
const file = this.files.get(filePath);
if (file) {
file.version++;
}
}
this.watchers.forEach((cb) => cb(uri, change));
}
addCommandExecutor(key: string, cb: Executor) {
this.executors[key] = cb;
}
addLinter(cb: Linter) {
this.linters.push(cb);
}
addWatcher(cb: Watcher) {
this.watchers.push(cb);
}
@cached
get packageJSON(): PackageInfo {
return getPackageJSON(this.root);
}
get name() {
return this.packageJSON.name;
}
constructor(public readonly root: string, addons: string[]) {
this.providers = collectProjectProviders(root, addons);
this.addonsMeta = this.providers.addonsMeta.filter((el) => el.root !== this.root);
// for now, let's collect only interesting deps
const interestingDeps = ['ember-cli', 'ember-source', 'ember-template-lint', 'typescript', '@embroider/core'];
const pkg = this.packageJSON;
interestingDeps.forEach((dep) => {
const version = getDepIfExists(pkg, dep);
if (version !== null) {
this.dependenciesMeta.push({
name: dep,
version,
});
}
});
this.builtinProviders = initBuiltinProviders();
const maybePrefix = getPodModulePrefix(root);
if (maybePrefix) {
this.podModulePrefix = 'app/' + maybePrefix;
} else {
this.podModulePrefix = 'app';
}
this.classicMatcher = new ClassicPathMatcher(this.root);
this.podMatcher = new PodMatcher(this.root, this.podModulePrefix);
}
unload() {
this.initIssues = [];
this.destructors.forEach((fn) => {
try {
fn(this);
} catch (e) {
logError(e);
}
});
logInfo('--------------------');
logInfo(`Ember CLI project: ${this.root} unloaded`);
logInfo('--------------------');
}
init(server: Server) {
this.providers.initFunctions.forEach((initFn) => {
try {
const initResult = initFn(server, this);
if (typeof initResult === 'function') {
this.destructors.push(initResult);
}
} catch (e) {
logError(e);
this.initIssues.push(e);
}
});
this.builtinProviders.initFunctions.forEach((initFn) => {
try {
const initResult = initFn(server, this);
if (typeof initResult === 'function') {
this.destructors.push(initResult);
}
} catch (e) {
logError(e);
this.initIssues.push(e);
}
});
if (this.flags.enableEagerRegistryInitialization) {
// prefer explicit registry tree building
findTestsForProject(this);
findAppItemsForProject(this);
findAddonItemsForProject(this);
}
if (this.providers.info.length) {
logInfo('--------------------');
logInfo('loaded language server addons:');
this.providers.info.forEach((addonName) => {
logInfo(' ' + addonName);
});
logInfo('--------------------');
}
}
}