-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.ts
191 lines (164 loc) · 6.8 KB
/
main.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
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
interface IViewCompletionParam {
document: vscode.TextDocument
position: vscode.Position
token: vscode.CancellationToken,
context: vscode.CompletionContext,
text: string,
offset: number,
triggerCharacter: string,
}
import 'reflect-metadata';
import {
createConnection,
IConnection,
TextDocuments,
InitializeParams,
InitializeResult,
Hover,
CompletionList,
InitializedParams,
TextDocumentPositionParams,
Definition,
DocumentLinkParams,
DocumentLink,
CodeLensParams,
CodeLens,
ReferenceParams,
Location,
DefinitionLink,
LocationLink,
Range,
} from 'vscode-languageserver';
import { MarkedString, Position, TextDocument } from 'vscode-languageserver';
import { camelCase } from 'aurelia-binding';
import { Container } from 'aurelia-dependency-injection';
import CompletionItemFactory from './CompletionItemFactory';
import ElementLibrary from './Completions/Library/_elementLibrary';
import AureliaSettings from './AureliaSettings';
import ProcessFiles from './FileParser/ProcessFiles';
import { HtmlValidator } from './Validations/HtmlValidator';
import { HtmlInvalidCaseCodeAction } from './CodeActions/HtmlInvalidCaseCodeAction';
import { OneWayBindingDeprecatedCodeAction } from './CodeActions/OneWayBindingDeprecatedCodeAction';
import * as ts from 'typescript';
import * as vscode from 'vscode';
import { AureliaApplication } from './FileParser/Model/AureliaApplication';
import { normalizePath } from './Util/NormalizePath';
import { connect } from 'net';
import { AureliaConfigProperties } from '../client/Model/AureliaConfigProperties';
import { exposeAureliaDefinitions } from './ExposeAureliaDefinitions';
import { sys } from 'typescript';
// Bind console.log & error to the Aurelia output
const connection: IConnection = createConnection();
console.log = connection.console.log.bind(connection.console);
console.error = connection.console.error.bind(connection.console);
const documents: TextDocuments = new TextDocuments();
documents.listen(connection);
// Setup Aurelia dependency injection
const globalContainer = new Container();
const completionItemFactory = <CompletionItemFactory> globalContainer.get(CompletionItemFactory);
const aureliaApplication = <AureliaApplication> globalContainer.get(AureliaApplication);
const settings = <AureliaSettings> globalContainer.get(AureliaSettings);
// Register characters to lisen for
connection.onInitialize(async (params: InitializeParams): Promise<InitializeResult> => {
// TODO: find better way/place to init this
const dummy = globalContainer.get(ElementLibrary);
return {
capabilities: {
completionProvider: { resolveProvider: false, triggerCharacters: ['<', ' ', '.', '[', '"', '\'', '{'] },
codeActionProvider: true,
definitionProvider: true,
textDocumentSync: documents.syncKind,
},
};
});
const codeActions = [
new HtmlInvalidCaseCodeAction(),
new OneWayBindingDeprecatedCodeAction()
];
connection.onCodeAction(async codeActionParams => {
const diagnostics = codeActionParams.context.diagnostics;
const document = documents.get(codeActionParams.textDocument.uri);
const commands = [];
for (const diagnostic of diagnostics) {
const action = codeActions.find(i => i.name == diagnostic.code);
if (action) {
commands.push(await action.commands(diagnostic, document));
}
}
return commands;
});
// Register and get changes to Aurelia settings
connection.onDidChangeConfiguration(async (change) => {
settings.quote = change.settings.aurelia.autocomplete.quotes === 'single' ? '\'' : '"';
settings.validation = change.settings.aurelia.validation;
settings.bindings.data = change.settings.aurelia.autocomplete.bindings.data;
settings.featureToggles = {
...settings.featureToggles,
...change.settings.aurelia.featureToggles,
};
await featureToggles(settings.featureToggles);
});
// Setup Validation
const validator = <HtmlValidator> globalContainer.get(HtmlValidator);
documents.onDidChangeContent(async change => {
const diagnostics = await validator.doValidation(change.document);
connection.sendDiagnostics({ uri: change.document.uri, diagnostics });
});
// Lisen for completion requests
connection.onRequest('aurelia-view-completion', async (params: IViewCompletionParam) => {
const {
document, position, token, context, text, offset, triggerCharacter,
} = params;
const completionResult = await completionItemFactory.create(triggerCharacter, position, text, offset, document.uri, aureliaApplication);
const result = CompletionList.create(completionResult, false);
return result;
});
connection.onRequest('aurelia-view-information', async (filePath: string) => {
let fileProcessor = new ProcessFiles();
await fileProcessor.processPath();
aureliaApplication.components = fileProcessor.components;
return aureliaApplication.components.find(doc => doc.paths.indexOf(normalizePath(filePath)) > -1);
});
connection.onRequest('aurelia-definition-provide', () => {
const { definitionsInfo, definitionsAttributesInfo } = exposeAureliaDefinitions(aureliaApplication);
definitionsInfo
definitionsAttributesInfo
return { definitionsInfo, definitionsAttributesInfo };
})
connection.onRequest('aurelia-smart-autocomplete-goto', () => {
return aureliaApplication.components;
})
connection.onDefinition((position: TextDocumentPositionParams): Definition => {
/**
* Need to have this onDefinition here, else we get following error in the console
* Request textDocument/definition failed.
* Message: Unhandled method textDocument/definition
* Code: -32601
*/
return null;
})
connection.listen();
async function featureToggles(featureToggles) {
console.log("TCL: featureToggles -> featureToggles function")
if (settings.featureToggles.smartAutocomplete) {
console.log('smart auto complete init');
// Aurelia project path to parse
console.log('>>> 1.1 This is the project\'s directory:');
const sourceDirectory = sys.getCurrentDirectory();
console.log(sourceDirectory);
console.log('>>> 1.2. The extension will try to parse Aurelia components inside:')
console.log(settings.featureToggles.pathToAureliaProject);
console.log('>>> 1.3. Eg.')
console.log(sourceDirectory + '/' + settings.featureToggles.pathToAureliaProject[0]);
try {
let fileProcessor = new ProcessFiles();
await fileProcessor.processPath();
aureliaApplication.components = fileProcessor.components;
console.log('>>> 2. The extension found this many components:');
console.log(aureliaApplication.components.length);
} catch (ex) {
console.log('------------- FILE PROCESSOR ERROR ---------------------');
console.log(JSON.stringify(ex));
}
}
}