-
Notifications
You must be signed in to change notification settings - Fork 82
/
settings.ts
255 lines (231 loc) · 8.77 KB
/
settings.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
import * as path from 'path';
import { commands, Extension, extensions, window, workspace, WorkspaceConfiguration } from "vscode";
import { getVariableSubstitutedAssociations } from "./variableSubstitution";
export interface ScopeInfo {
scope: "default" | "global" | "workspace" | "folder";
configurationTarget: boolean;
}
let vmArgsCache;
let ignoreAutoCloseTags = false;
let ignoreVMArgs = false;
let oldXMLConfig: WorkspaceConfiguration = getXMLConfiguration();
let oldJavaConfig: WorkspaceConfiguration = getJavaConfiguration();
const restartButton = 'Restart Now';
const ignoreButton = 'Ignore'
const restartId = "workbench.action.reloadWindow";
export const IS_WORKSPACE_JDK_ALLOWED = "java.ls.isJdkAllowed";
export const IS_WORKSPACE_JDK_XML_ALLOWED = "java.ls.isJdkXmlAllowed";
export const IS_WORKSPACE_VMARGS_XML_ALLOWED = "java.ls.isVmargsXmlAllowed";
export const xmlServerVmargs = 'xml.server.vmargs';
export function getXMLConfiguration(): WorkspaceConfiguration {
return getXConfiguration("xml")
}
export function getJavaConfiguration(): WorkspaceConfiguration {
return getXConfiguration("java");
}
export function getXConfiguration(configName: string) {
return workspace.getConfiguration(configName);
}
export function onConfigurationChange() {
if(!ignoreVMArgs) {
verifyVMArgs();
}
if(!ignoreAutoCloseTags) {
verifyAutoClosing();
}
}
export function subscribeJDKChangeConfiguration() {
return workspace.onDidChangeConfiguration(params => {
//handle "xml.java.home" change
if(params.affectsConfiguration("xml")) {
const newXMLConfig = getXMLConfiguration();
if(hasPreferenceChanged(oldXMLConfig, newXMLConfig, "java.home")) { // checks "xml.java.home", not "java.home"
createReloadWindowMessage("`xml.java.home` path has changed. Please restart VS Code.");
}
if (params.affectsConfiguration("xml.server.vmargs")) {
createReloadWindowMessage("Arguments to the JVM have changed. Please reload VS Code to apply this change.");
}
// update to newest version of config
oldXMLConfig = newXMLConfig;
return;
}
//handle "java.home" change
if(oldXMLConfig.get("java.home") == null) { // if "xml.java.home" exists, dont even look at "java.home"
if(params.affectsConfiguration("java")) {
const newJavaConfig = getJavaConfiguration();
//don't need to handle reload message if redhat.java extension exists (it will handle it)
const redhatJavaExtension: Extension<any> = extensions.getExtension("redhat.java");
const isJavaExtensionActive: boolean = redhatJavaExtension != null && redhatJavaExtension.isActive;
if(!isJavaExtensionActive && hasPreferenceChanged(oldJavaConfig, newJavaConfig, "home")) { // checks "java.home"
createReloadWindowMessage("`java.home` path has changed. Please restart VS Code.");
}
oldJavaConfig = newJavaConfig;
}
return;
}
});
}
function hasPreferenceChanged(oldConfig: WorkspaceConfiguration, newConfig: WorkspaceConfiguration, preference: string) {
return oldConfig.get(preference) != newConfig.get(preference);
}
function createReloadWindowMessage(message: string) : string{
window.showWarningMessage(message, restartButton, ignoreButton).then((selection) => {
if (restartButton === selection) {
commands.executeCommand(restartId);
}
});
return ignoreButton;
}
function verifyVMArgs() {
const currentVMArgs = workspace.getConfiguration("xml.server").get("vmargs");
if(vmArgsCache != undefined) {
if(vmArgsCache != currentVMArgs) {
const selection = createReloadWindowMessage("XML Language Server configuration changed, please restart VS Code.");
if(selection == ignoreButton) {
ignoreVMArgs = true;
}
}
}
else {
vmArgsCache = currentVMArgs;
}
}
function verifyAutoClosing() {
const configXML = workspace.getConfiguration();
const closeTags = configXML.get("xml.completion.autoCloseTags");
const 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",
ignoreButton).then((selection) => {
if (selection == "Disable") {
const 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 getScopeLevel(configurationKey : string, key : string) : ScopeInfo{
const configXML = workspace.getConfiguration(configurationKey);
const 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;
}
const scopeInfo : ScopeInfo = {"scope": scope, "configurationTarget": configurationTarget};
return scopeInfo;
}
export function getKey(prefix, storagePath, value) {
const workspacePath = path.resolve(storagePath + '/jdt_ws');
if (workspace.name !== undefined) {
return `${prefix}::${workspacePath}::${value}`;
}
else {
return `${prefix}::${value}`;
}
}
export function getJavaagentFlag(vmargs) {
const javaagent = '-javaagent:';
const args = vmargs.split(" ");
let agentFlag = null;
for (const arg of args) {
if (arg.startsWith(javaagent)) {
agentFlag = arg.substring(javaagent.length);
break;
}
}
return agentFlag;
}
/**
* Returns a json object with key 'xml' and a json object value that
* holds all xml. settings.
*
* Returns: {
* 'xml': {...}
* }
*/
export function getXMLSettings(javaHome: string | undefined, logfile: string, externalXmlSettings: any): JSON {
const 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 {
const 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);
xml['xml']['linkedEditingEnabled'] = workspace.getConfiguration('editor')['linkedEditing'] || false;
xml['xml']['telemetry'] = {
enabled: workspace.getConfiguration('redhat.telemetry').get('enabled', false)
};
// Check workspace trust
const isWorkspaceTrusted = (workspace as any).isTrusted;
if (isWorkspaceTrusted !== undefined && !isWorkspaceTrusted) {
xml['xml']['validation']['resolveExternalEntities'] = false;
xml['xml']['downloadExternalResources']['enabled'] = false;
} else {
xml['xml']['validation']['resolveExternalEntities'] = workspace.getConfiguration('xml').get('validation.resolveExternalEntities', false);
xml['xml']['downloadExternalResources']['enabled'] = workspace.getConfiguration('xml').get('downloadExternalResources.enabled', false);
}
//applying externalXmlSettings to the xmlSettings
externalXmlSettings.xmlCatalogs.forEach(catalog => {
if (!xml['xml']['catalogs'].includes(catalog)) {
xml['xml']['catalogs'].push(catalog);
}
})
externalXmlSettings.xmlFileAssociations.forEach(element => {
if (!xml['xml']['fileAssociations'].some(fileAssociation => fileAssociation.systemId === element.systemId)) {
xml['xml']['fileAssociations'].push(element);
}
});
// Apply variable substitutions for file associations
xml['xml']['fileAssociations'] = [...getVariableSubstitutedAssociations(xml['xml']['fileAssociations'])];
return xml;
}