-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathextension.ts
More file actions
214 lines (198 loc) · 7.02 KB
/
extension.ts
File metadata and controls
214 lines (198 loc) · 7.02 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
210
211
212
213
/**
* 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 } from 'vscode-languageclient';
import * as requirements from './requirements';
import { languages, IndentAction, workspace, window, commands, ExtensionContext, TextDocument, Position, LanguageConfiguration } from "vscode";
import * as path from 'path';
import * as os from 'os';
import { activateTagClosing } from './tagClosing';
export interface ScopeInfo {
scope : "default" | "global" | "workspace" | "folder";
configurationTarget: boolean;
}
namespace TagCloseRequest {
export const type: RequestType<TextDocumentPositionParams, string, any, any> = new RequestType('xml/closeTag');
}
let ignoreAutoCloseTags = false;
let vmArgsCache;
let ignoreVMArgs = false;
export function activate(context: ExtensionContext) {
let storagePath = context.storagePath;
if (!storagePath) {
storagePath = os.homedir() + "/.lsp4xml";
}
let logfile = path.resolve(storagePath + '/lsp4xml.log');
return requirements.resolveRequirements().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 clientOptions: LanguageClientOptions = {
// Register the server for xml and xsl
documentSelector: ['xml', 'xsl'],
revealOutputChannelOn: RevealOutputChannelOn.Never,
initializationOptions: { settings: getSettings() },
synchronize: {
//preferences starting with these will trigger didChangeConfiguration
configurationSection: ['xml', '[xml]']
},
middleware: {
workspace: {
didChangeConfiguration: () => {
languageClient.sendNotification(DidChangeConfigurationNotification.type, { settings: getSettings() });
if(!ignoreAutoCloseTags) {
verifyAutoClosing();
}
!ignoreVMArgs ? verifyVMArgs(): undefined;
}
}
}
}
let serverOptions = prepareExecutable(requirements);
let languageClient = new LanguageClient('xml', 'XML Support', serverOptions, clientOptions);
let toDispose = context.subscriptions;
let disposable = languageClient.start();
toDispose.push(disposable);
languageClient.onReady().then(() => {
//init
let tagRequestor = (document: TextDocument, position: Position) => {
let param = languageClient.code2ProtocolConverter.asTextDocumentPositionParams(document, position);
return languageClient.sendRequest(TagCloseRequest.type, param);
};
disposable = activateTagClosing(tagRequestor, { xml: true, xsl: true }, 'xml.completion.autoCloseTags');
toDispose.push(disposable);
});
languages.setLanguageConfiguration('xml', getIndentationRules());
languages.setLanguageConfiguration('xsl', getIndentationRules());
});
function getSettings(): JSON {
let configXML = workspace.getConfiguration();
configXML = configXML.get('xml');
let settings: JSON;
if (!configXML) {
const defaultValue = {
trace: {
server: 'verbose'
},
logs: {
client: true
},
format: {
enabled: true,
splitAttributes: false
},
completion: {
autoCloseTags: false
}
}
const x = JSON.stringify(defaultValue);
settings = JSON.parse(x);
} else {
const x = JSON.stringify(configXML);
settings = JSON.parse(x);
}
settings['logs']['file'] = logfile;
settings['useCache'] = true;
return settings;
}
}
function verifyAutoClosing() {
let configXML = workspace.getConfiguration();
let closeTags = configXML.get("xml.completion.autoCloseTags");
let closeBrackets = configXML.get("[xml]")["editor.autoClosingBrackets"];
if (closeTags && closeBrackets != "never") {
window.showWarningMessage(
"The [xml].editor.autoClosingBrackets setting conflicts with xml.completion.autoCloseTags. It's recommended to disable it.",
"Disable",
"Ignore").then((selection) => {
if (selection == "Disable") {
let scopeInfo : ScopeInfo = getScopeLevel("", "[xml]");
workspace.getConfiguration().update("[xml]", { "editor.autoClosingBrackets": "never" }, scopeInfo.configurationTarget).then(
() => console.log('[xml].editor.autoClosingBrackets globally set to never'),
(error) => console.log(error)
);
}
else if(selection == "Ignore") {
ignoreAutoCloseTags = true;
}
});
}
}
function verifyVMArgs() {
let currentVMArgs = workspace.getConfiguration("xml.server").get("vmargs");
if(vmArgsCache != undefined) {
if(vmArgsCache != currentVMArgs) {
window.showWarningMessage(
"XML Language Server configuration changed, please restart VS Code.",
"Restart",
"Ignore").then((selection) => {
if (selection == "Restart") {
commands.executeCommand("workbench.action.reloadWindow");
}
else if(selection == "Ignore") {
ignoreVMArgs = true;
}
});
}
}
else {
vmArgsCache = currentVMArgs;
}
}
function getScopeLevel(configurationKey : string, key : string) : ScopeInfo{
let configXML = workspace.getConfiguration(configurationKey);
let result = configXML.inspect(key);
let scope, configurationTarget;
if(result.workspaceFolderValue == undefined) {
if(result.workspaceValue == undefined) {
if(result.globalValue == undefined) {
scope = "default"
configurationTarget = true;
}
else {
scope = "global";
configurationTarget = true;
}
}
else {
scope = "workspace";
configurationTarget = false;
}
}
else {
scope = "folder";
configurationTarget = undefined;
}
let scopeInfo : ScopeInfo = {"scope": scope, "configurationTarget": configurationTarget};
return scopeInfo;
}
function getIndentationRules(): LanguageConfiguration {
return {
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 }
}
],
};
}