-
Notifications
You must be signed in to change notification settings - Fork 82
/
extension.ts
276 lines (253 loc) · 10.4 KB
/
extension.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
* Copyright (c) 2018 Red Hat, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat Inc. - initial API and implementation
* Microsoft Corporation - Auto Closing Tags
*/
import { prepareExecutable } from './javaServerStarter';
import { LanguageClientOptions, RevealOutputChannelOn, LanguageClient, DidChangeConfigurationNotification, RequestType, TextDocumentPositionParams, ReferencesRequest, NotificationType, MessageType } from 'vscode-languageclient';
import * as requirements from './requirements';
import { languages, IndentAction, workspace, window, commands, ExtensionContext, TextDocument, Position, LanguageConfiguration, Uri, extensions, Command } from "vscode";
import * as path from 'path';
import * as os from 'os';
import { activateTagClosing, AutoCloseResult } from './tagClosing';
import { Commands } from './commands';
import { onConfigurationChange, subscribeJDKChangeConfiguration } from './settings';
import { collectXmlJavaExtensions, onExtensionChange } from './plugin';
export interface ScopeInfo {
scope: "default" | "global" | "workspace" | "folder";
configurationTarget: boolean;
}
namespace TagCloseRequest {
export const type: RequestType<TextDocumentPositionParams, AutoCloseResult, any, any> = new RequestType('xml/closeTag');
}
namespace SymbolsLimitExceededNotification {
export const type: NotificationType<{commandId: string, message: string}, any> = new NotificationType('xml/symbolsLimitExceeded');
}
interface ActionableMessage {
severity: MessageType;
message: string;
data?: any;
commands?: Command[];
}
namespace ActionableNotification {
export const type = new NotificationType<ActionableMessage, void>('xml/actionableNotification');
}
export function activate(context: ExtensionContext) {
let storagePath = context.storagePath;
if (!storagePath) {
storagePath = os.homedir() + "/.lemminx";
}
let logfile = path.resolve(storagePath + '/lemminx.log');
return requirements.resolveRequirements(context).catch(error => {
//show error
window.showErrorMessage(error.message, error.label).then((selection) => {
if (error.label && error.label === selection && error.openUrl) {
commands.executeCommand('vscode.open', error.openUrl);
}
});
// rethrow to disrupt the chain.
throw error;
}).then(requirements => {
let XMLsettings = getXMLSettings(requirements.java_home);
let clientOptions: LanguageClientOptions = {
// Register the server for xml and xsl
documentSelector: [
{ scheme: 'file', language: 'xml' },
{ scheme: 'file', language: 'xsl' },
{ scheme: 'untitled', language: 'xml' },
{ scheme: 'untitled', language: 'xsl' }
],
revealOutputChannelOn: RevealOutputChannelOn.Never,
//wrap with key 'settings' so it can be handled same a DidChangeConfiguration
initializationOptions: {
settings: XMLsettings,
extendedClientCapabilities: {
codeLens: {
codeLensKind: {
valueSet: [
'references'
]
}
},
actionableNotificationSupport: true,
openSettingsCommandSupport: true
}
},
synchronize: {
//preferences starting with these will trigger didChangeConfiguration
configurationSection: ['xml', '[xml]', 'files.trimFinalNewlines', 'files.trimTrailingWhitespace', 'files.insertFinalNewline']
},
middleware: {
workspace: {
didChangeConfiguration: () => {
languageClient.sendNotification(DidChangeConfigurationNotification.type, { settings: getXMLSettings(requirements.java_home) });
onConfigurationChange();
}
}
}
}
let serverOptions = prepareExecutable(requirements, collectXmlJavaExtensions(extensions.all), context);
let languageClient = new LanguageClient('xml', 'XML Support', serverOptions, clientOptions);
let toDispose = context.subscriptions;
let disposable = languageClient.start();
toDispose.push(disposable);
languageClient.onReady().then(() => {
//Detect JDK configuration changes
disposable = subscribeJDKChangeConfiguration();
toDispose.push(disposable);
// Code Lens actions
context.subscriptions.push(commands.registerCommand(Commands.SHOW_REFERENCES, (uriString: string, position: Position) => {
const uri = Uri.parse(uriString);
workspace.openTextDocument(uri).then(document => {
// Consume references service from the XML Language Server
let param = languageClient.code2ProtocolConverter.asTextDocumentPositionParams(document, position);
languageClient.sendRequest(ReferencesRequest.type, param).then(locations => {
commands.executeCommand(Commands.EDITOR_SHOW_REFERENCES, uri, languageClient.protocol2CodeConverter.asPosition(position), locations.map(languageClient.protocol2CodeConverter.asLocation));
})
})
}));
setupActionableNotificationListener(languageClient);
context.subscriptions.push(commands.registerCommand(Commands.OPEN_SETTINGS, async (settingId?: string) => {
commands.executeCommand('workbench.action.openSettings', settingId);
}));
// Setup autoCloseTags
const tagProvider = (document: TextDocument, position: Position) => {
let param = languageClient.code2ProtocolConverter.asTextDocumentPositionParams(document, position);
let text = languageClient.sendRequest(TagCloseRequest.type, param);
return text;
};
context.subscriptions.push(activateTagClosing(tagProvider, { xml: true, xsl: true }, Commands.AUTO_CLOSE_TAGS));
if (extensions.onDidChange) {// Theia doesn't support this API yet
extensions.onDidChange(() => {
onExtensionChange(extensions.all);
});
}
});
languages.setLanguageConfiguration('xml', getIndentationRules());
languages.setLanguageConfiguration('xsl', getIndentationRules());
const api = {
//setXMLCatalog
setXMLCatalog: function (externalSettings) {
externalSettings['xml']['catalogs'].forEach(element => {
if (!XMLsettings['xml']['catalogs'].includes(externalSettings['xml']['catalogs'])) {
XMLsettings['xml']['catalogs'].push(element);
}
});
languageClient.sendNotification(DidChangeConfigurationNotification.type, { settings: XMLsettings });
onConfigurationChange();
},
//set XMLFileAssoication
setXMLFileAssociation: function (externalSettings) {
externalSettings['xml']['fileAssociations'].forEach(element => {
if (!XMLsettings['xml']['fileAssociations'].some((element: { systemId: string; }) => {
return element.systemId === externalSettings['xml']['fileAssociations']['systemId'];
})) {
XMLsettings['xml']['fileAssociations'].push(element);
}
});
languageClient.sendNotification(DidChangeConfigurationNotification.type, { settings: XMLsettings });
onConfigurationChange();
}
}
return api
});
/**
* Returns a json object with key 'xml' and a json object value that
* holds all xml. settings.
*
* Returns: {
* 'xml': {...}
* }
*/
function getXMLSettings(javaHome: string): JSON {
let configXML = workspace.getConfiguration().get('xml');
let xml;
if (!configXML) { //Set default preferences if not provided
const defaultValue =
{
xml: {
trace: {
server: 'verbose'
},
logs: {
client: true
},
format: {
enabled: true,
splitAttributes: false
},
completion: {
autoCloseTags: false
}
}
}
xml = defaultValue;
} else {
let x = JSON.stringify(configXML); //configXML is not a JSON type
xml = { "xml": JSON.parse(x) };
}
xml['xml']['logs']['file'] = logfile;
xml['xml']['useCache'] = true;
xml['xml']['java']['home'] = javaHome;
xml['xml']['format']['trimFinalNewlines'] = workspace.getConfiguration('files').get('trimFinalNewlines', true);
xml['xml']['format']['trimTrailingWhitespace'] = workspace.getConfiguration('files').get('trimTrailingWhitespace', false);
xml['xml']['format']['insertFinalNewline'] = workspace.getConfiguration('files').get('insertFinalNewline', false);
return xml;
}
}
function getIndentationRules(): LanguageConfiguration {
return {
// indentationRules referenced from:
// https://github.com/microsoft/vscode/blob/d00558037359acceea329e718036c19625f91a1a/extensions/html-language-features/client/src/htmlMain.ts#L114-L115
indentationRules: {
increaseIndentPattern: /<(?!\?|[^>]*\/>)([-_\.A-Za-z0-9]+)(?=\s|>)\b[^>]*>(?!.*<\/\1>)|<!--(?!.*-->)|\{[^}"']*$/,
decreaseIndentPattern: /^\s*(<\/[-_\.A-Za-z0-9]+\b[^>]*>|-->|\})/
},
onEnterRules: [
{
beforeText: new RegExp(`<([_:\\w][_:\\w-.\\d]*)([^/>]*(?!/)>)[^<]*$`, 'i'),
afterText: /^<\/([_:\w][_:\w-.\d]*)\s*>/i,
action: { indentAction: IndentAction.IndentOutdent }
},
{
beforeText: new RegExp(`<(\\w[\\w\\d]*)([^/>]*(?!/)>)[^<]*$`, 'i'),
action: { indentAction: IndentAction.Indent }
}
]
};
}
function setupActionableNotificationListener(languageClient: LanguageClient): void {
languageClient.onNotification(ActionableNotification.type, (notification: ActionableMessage) => {
let show = null;
switch (notification.severity) {
case MessageType.Info:
show = window.showInformationMessage;
break;
case MessageType.Warning:
show = window.showWarningMessage;
break;
case MessageType.Error:
show = window.showErrorMessage;
break;
}
if (!show) {
return;
}
const titles: string[] = notification.commands.map(a => a.title);
show(notification.message, ...titles).then((selection) => {
for (const action of notification.commands) {
if (action.title === selection) {
const args: any[] = (action.arguments) ? action.arguments : [];
commands.executeCommand(action.command, ...args);
break;
}
}
});
});
}