-
Notifications
You must be signed in to change notification settings - Fork 443
/
Copy pathsettings.ts
247 lines (227 loc) · 9.28 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
'use strict';
import * as path from 'path';
import { window, Uri, workspace, WorkspaceConfiguration, commands, ConfigurationTarget, env, ExtensionContext, TextEditor, Range, Disposable } from 'vscode';
import { LanguageClient } from 'vscode-languageclient';
import { Commands } from './commands';
import { getJavaConfiguration } from './utils';
const DEFAULT_HIDDEN_FILES: string[] = ['**/.classpath', '**/.project', '**/.settings', '**/.factorypath'];
export const IS_WORKSPACE_JDK_ALLOWED = "java.ls.isJdkAllowed";
export const IS_WORKSPACE_VMARGS_ALLOWED = "java.ls.isVmargsAllowed";
const extensionName = 'Language Support for Java';
const changeItem = {
global: 'Exclude globally',
workspace: 'Exclude in workspace',
never: 'Never'
};
const EXCLUDE_FILE_CONFIG = 'configuration.checkProjectSettingsExclusions';
export const ORGANIZE_IMPORTS_ON_PASTE = 'actionsOnPaste.organizeImports'; // java.actionsOnPaste.organizeImports
let oldConfig: WorkspaceConfiguration = getJavaConfiguration();
const gradleWrapperPromptDialogs = [];
export function onConfigurationChange() {
return workspace.onDidChangeConfiguration(params => {
if (!params.affectsConfiguration('java')) {
return;
}
const newConfig = getJavaConfiguration();
if (newConfig.get(EXCLUDE_FILE_CONFIG)) {
excludeProjectSettingsFiles();
}
if (hasJavaConfigChanged(oldConfig, newConfig)) {
const msg = `Java Language Server configuration changed, please restart ${env.appName}.`;
const action = 'Restart Now';
const restartId = Commands.RELOAD_WINDOW;
window.showWarningMessage(msg, action).then((selection) => {
if (action === selection) {
commands.executeCommand(restartId);
}
});
}
// update old config
oldConfig = newConfig;
});
}
export function excludeProjectSettingsFiles() {
if (workspace.workspaceFolders && workspace.workspaceFolders.length) {
workspace.workspaceFolders.forEach((folder) => {
excludeProjectSettingsFilesForWorkspace(folder.uri);
});
}
}
function excludeProjectSettingsFilesForWorkspace(workspaceUri: Uri) {
const excudedConfig = getJavaConfiguration().get(EXCLUDE_FILE_CONFIG);
if (excudedConfig) {
const config = workspace.getConfiguration('files', workspaceUri);
const excludedValue: Object = config.get('exclude');
const needExcludeFiles: string[] = [];
let needUpdate = false;
for (const hiddenFile of DEFAULT_HIDDEN_FILES) {
if (!excludedValue.hasOwnProperty(hiddenFile)) {
needExcludeFiles.push(hiddenFile);
needUpdate = true;
}
}
if (needUpdate) {
const excludedInspectedValue = config.inspect('exclude');
const items = [changeItem.workspace, changeItem.never];
// Workspace file.exclude is undefined
if (!excludedInspectedValue.workspaceValue) {
items.unshift(changeItem.global);
}
window.showInformationMessage(`Do you want to exclude the ${env.appName} Java project settings files (.classpath, .project, .settings, .factorypath) from the file explorer?`, ...items).then((result) => {
if (result === changeItem.global) {
excludedInspectedValue.globalValue = excludedInspectedValue.globalValue || {};
for (const hiddenFile of needExcludeFiles) {
excludedInspectedValue.globalValue[hiddenFile] = true;
}
config.update('exclude', excludedInspectedValue.globalValue, ConfigurationTarget.Global);
} if (result === changeItem.workspace) {
excludedInspectedValue.workspaceValue = excludedInspectedValue.workspaceValue || {};
for (const hiddenFile of needExcludeFiles) {
excludedInspectedValue.workspaceValue[hiddenFile] = true;
}
config.update('exclude', excludedInspectedValue.workspaceValue, ConfigurationTarget.Workspace);
} else if (result === changeItem.never) {
const storeInWorkspace = getJavaConfiguration().inspect(EXCLUDE_FILE_CONFIG).workspaceValue;
getJavaConfiguration().update(EXCLUDE_FILE_CONFIG, false, storeInWorkspace ? ConfigurationTarget.Workspace : ConfigurationTarget.Global);
}
});
}
}
}
function hasJavaConfigChanged(oldConfig: WorkspaceConfiguration, newConfig: WorkspaceConfiguration) {
return hasConfigKeyChanged('home', oldConfig, newConfig)
|| hasConfigKeyChanged('jdt.ls.vmargs', oldConfig, newConfig)
|| hasConfigKeyChanged('progressReports.enabled', oldConfig, newConfig)
|| hasConfigKeyChanged('server.launchMode', oldConfig, newConfig);
}
function hasConfigKeyChanged(key, oldConfig, newConfig) {
return oldConfig.get(key) !== newConfig.get(key);
}
export function getJavaEncoding(): string {
const config = workspace.getConfiguration();
const languageConfig = config.get('[java]');
let javaEncoding = null;
if (languageConfig) {
javaEncoding = languageConfig['files.encoding'];
}
if (!javaEncoding) {
javaEncoding = config.get<string>('files.encoding', 'UTF-8');
}
return javaEncoding;
}
export async function checkJavaPreferences(context: ExtensionContext) {
const allow = 'Allow';
const disallow = 'Disallow';
let javaHome = workspace.getConfiguration().inspect<string>('java.home').workspaceValue;
let isVerified = javaHome === undefined || javaHome === null;
if (isVerified) {
javaHome = getJavaConfiguration().get('home');
}
const key = getKey(IS_WORKSPACE_JDK_ALLOWED, context.storagePath, javaHome);
const globalState = context.globalState;
if (!isVerified) {
isVerified = globalState.get(key);
if (isVerified === undefined) {
await window.showErrorMessage(`Security Warning! Do you allow this workspace to set the java.home variable? \n java.home: ${javaHome}`, disallow, allow).then(async selection => {
if (selection === allow) {
globalState.update(key, true);
} else if (selection === disallow) {
globalState.update(key, false);
await workspace.getConfiguration().update('java.home', undefined, ConfigurationTarget.Workspace);
}
});
isVerified = globalState.get(key);
}
}
const vmargs = workspace.getConfiguration().inspect('java.jdt.ls.vmargs').workspaceValue;
if (vmargs !== undefined) {
const agentFlag = getJavaagentFlag(vmargs);
if (agentFlag !== null) {
const keyVmargs = getKey(IS_WORKSPACE_VMARGS_ALLOWED, context.storagePath, vmargs);
const vmargsVerified = globalState.get(keyVmargs);
if (vmargsVerified === undefined || vmargsVerified === null) {
await window.showErrorMessage(`Security Warning! The java.jdt.ls.vmargs variable defined in ${env.appName} settings includes the (${agentFlag}) javagent preference. Do you allow it to be used?`, disallow, allow).then(async selection => {
if (selection === allow) {
globalState.update(keyVmargs, true);
} else if (selection === disallow) {
globalState.update(keyVmargs, false);
await workspace.getConfiguration().update('java.jdt.ls.vmargs', undefined, ConfigurationTarget.Workspace);
}
});
}
}
}
if (isVerified) {
return javaHome;
} else {
return workspace.getConfiguration().inspect<string>('java.home').globalValue;
}
}
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;
}
export enum ServerMode {
STANDARD = 'Standard',
LIGHTWEIGHT = 'LightWeight',
HYBRID = 'Hybrid'
}
export function getJavaServerMode(): ServerMode {
return workspace.getConfiguration().get('java.server.launchMode')
|| ServerMode.HYBRID;
}
export function setGradleWrapperChecksum(wrapper: string, sha256?: string) {
const opened = gradleWrapperPromptDialogs.filter(v => (v === sha256));
if (opened !== null && opened.length > 0) {
return;
}
gradleWrapperPromptDialogs.push(sha256);
const allow = 'Trust';
const disallow = 'Do not trust';
window.showErrorMessage(`"Security Warning! The gradle wrapper '${wrapper}'" [sha256 '${sha256}'] [could be malicious](https://github.com/redhat-developer/vscode-java/wiki/Gradle-Support#suspicious.wrapper). Should it be trusted?";`, disallow, allow)
.then(async selection => {
let allowed;
if (selection === allow) {
allowed = true;
} else if (selection === disallow) {
allowed = false;
} else {
unregisterGradleWrapperPromptDialog(sha256);
return false;
}
const key = "java.imports.gradle.wrapper.checksums";
let property: any = workspace.getConfiguration().inspect<string>(key).globalValue;
if (!Array.isArray(property)) {
property = [];
}
const entry = property.filter(p => (p.sha256 === sha256));
if (entry === null || entry.length === 0) {
property.push({ sha256: sha256, allowed: allowed });
workspace.getConfiguration().update(key, property, ConfigurationTarget.Global);
}
unregisterGradleWrapperPromptDialog(sha256);
});
}
function unregisterGradleWrapperPromptDialog(sha256: string) {
const index = gradleWrapperPromptDialogs.indexOf(sha256);
if (index > -1) {
gradleWrapperPromptDialogs.splice(index, 1);
}
}