forked from phil294/coffeesense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanguageModes.ts
155 lines (132 loc) · 5 KB
/
languageModes.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
152
153
154
155
import {
CompletionItem,
Location,
SignatureHelp,
Definition,
Diagnostic,
Range,
Hover,
DocumentHighlight,
CompletionList,
Position,
SymbolInformation,
CodeActionContext,
CodeAction,
TextDocumentEdit
} from 'vscode-languageserver-types';
import type { TextDocument } from 'vscode-languageserver-textdocument';
import { getLanguageModelCache, LanguageModelCache } from './languageModelCache';
import {
getCoffeescriptDocumentRegions,
CoffeescriptDocumentRegions,
LanguageId,
LanguageRange
} from './embeddedSupport';
import { getJavascriptMode } from '../modes/script/javascript';
import { DependencyService } from '../services/dependencyService';
import { nullMode } from '../modes/nullMode';
import { getServiceHost, IServiceHost } from '../services/typescriptService/serviceHost';
import { VCancellationToken } from '../utils/cancellationToken';
import { EnvironmentService } from '../services/EnvironmentService';
import { LANGUAGE_ID } from '../language';
export interface LSPServices {
dependencyService: DependencyService;
}
export interface LanguageMode {
getId(): string;
updateFileInfo?(doc: TextDocument): void;
doValidation?(document: TextDocument, cancellationToken?: VCancellationToken): Promise<Diagnostic[]>;
getCodeActions?(document: TextDocument, range: Range, context: CodeActionContext): CodeAction[];
doCodeActionResolve?(document: TextDocument, action: CodeAction): CodeAction;
doComplete?(document: TextDocument, position: Position): CompletionList;
doResolve?(document: TextDocument, item: CompletionItem): CompletionItem;
doHover?(document: TextDocument, position: Position): Hover;
doSignatureHelp?(document: TextDocument, position: Position): SignatureHelp | null;
findDocumentHighlight?(document: TextDocument, position: Position): DocumentHighlight[];
findDocumentSymbols?(document: TextDocument): SymbolInformation[];
findDefinition?(document: TextDocument, position: Position): Definition;
findReferences?(document: TextDocument, position: Position): Location[];
onDocumentChanged?(filePath: string): void;
onDocumentRemoved(document: TextDocument): void;
dispose(): void;
}
export interface LanguageModeRange extends LanguageRange {
mode: LanguageMode;
}
export class LanguageModes {
private modes: { [k in LanguageId]: LanguageMode } = {
[LANGUAGE_ID]: nullMode,
javascript: nullMode,
typescript: nullMode,
unknown: nullMode
};
private documentRegions: LanguageModelCache<CoffeescriptDocumentRegions>;
private modelCaches: LanguageModelCache<any>[];
private serviceHost: IServiceHost;
constructor() {
this.documentRegions = getLanguageModelCache<CoffeescriptDocumentRegions>(10, 60, document =>
getCoffeescriptDocumentRegions(document)
);
this.modelCaches = [];
this.modelCaches.push(this.documentRegions);
}
async init(env: EnvironmentService, services: LSPServices) {
const tsModule = services.dependencyService.get('typescript', env.getPackagePath()).module;
/**
* Documents where everything outside `<script>` is replaced with whitespace
*/
const scriptRegionDocuments = getLanguageModelCache(10, 60, document => {
const coffeescriptDocument = this.documentRegions.refreshAndGet(document);
return coffeescriptDocument.getSingleTypeDocument('script');
});
this.serviceHost = getServiceHost(tsModule, env, scriptRegionDocuments);
const jsMode = await getJavascriptMode(tsModule, this.serviceHost, env, this.documentRegions, services.dependencyService);
this.modes['javascript'] = jsMode;
this.modes['typescript'] = jsMode;
}
getModeAtPosition(document: TextDocument, position: Position): LanguageMode | undefined {
const languageId = this.documentRegions.refreshAndGet(document).getLanguageAtPosition(position);
return this.modes?.[languageId];
}
getAllLanguageModeRangesInDocument(document: TextDocument): LanguageModeRange[] {
const result: LanguageModeRange[] = [];
const documentRegions = this.documentRegions.refreshAndGet(document);
documentRegions.getAllLanguageRanges().forEach(lr => {
const mode = this.modes[lr.languageId];
if (mode) {
result.push({
mode,
...lr
});
}
});
return result;
}
getAllModes(): LanguageMode[] {
const result = [];
for (const languageId in this.modes) {
const mode = this.modes[<LanguageId>languageId];
if (mode) {
result.push(mode);
}
}
return result;
}
getMode(languageId: LanguageId): LanguageMode | undefined {
return this.modes[languageId];
}
onDocumentRemoved(document: TextDocument) {
this.modelCaches.forEach(mc => mc.onDocumentRemoved(document));
for (const mode in this.modes) {
this.modes[<LanguageId>mode].onDocumentRemoved(document);
}
}
dispose(): void {
this.modelCaches.forEach(mc => mc.dispose());
this.modelCaches = [];
for (const mode in this.modes) {
this.modes[<LanguageId>mode].dispose();
}
this.serviceHost.dispose();
}
}