forked from gorkem/vscode-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.ts
More file actions
214 lines (189 loc) · 7.16 KB
/
extension.ts
File metadata and controls
214 lines (189 loc) · 7.16 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
214
'use strict';
import * as path from 'path';
import { workspace, ExtensionContext, window, StatusBarAlignment, commands, ViewColumn, Uri, CancellationToken, TextDocumentContentProvider } from 'vscode';
import { LanguageClient, LanguageClientOptions, Position as LSPosition, Location as LSLocation, Protocol2Code} from 'vscode-languageclient';
var electron = require('./electron_j');
var os = require('os');
var glob = require('glob');
import * as requirements from './requirements';
import { StatusNotification,ClassFileContentsRequest } from './protocol';
declare var v8debug;
const DEBUG = ( typeof v8debug === 'object') || startedInDebugMode();
var storagePath;
var oldConfig;
function runJavaServer(){
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 => new Promise(function(resolve, reject){
let child = path.resolve (requirements.java_home + '/bin/java');
let params = [];
let workspacePath = path.resolve( storagePath+'/jdt_ws');
if (DEBUG) {
params.push('-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1044');
// suspend=y is the default. Use this form if you need to debug the server startup code:
// params.push('-agentlib:jdwp=transport=dt_socket,server=y,address=1044');
}
params.push('-Declipse.application=org.jboss.tools.vscode.java.id1');
params.push('-Dosgi.bundles.defaultStartLevel=4');
params.push('-Declipse.product=org.jboss.tools.vscode.java.product');
if (DEBUG) {
params.push('-Dlog.protocol=true');
params.push('-Dlog.level=ALL');
}
let vmargs = workspace.getConfiguration('java').get('jdt.ls.vmargs','');
parseVMargs(params, vmargs);
let server_home :string = path.resolve( __dirname,'../../server');
let launchersFound:Array<string> = glob.sync('**/plugins/org.eclipse.equinox.launcher_*.jar', {cwd: server_home });
if( launchersFound.length ){
params.push('-jar'); params.push(path.resolve(server_home,launchersFound[0]));
}else{
reject('failed to find launcher');
}
//select configuration directory according to OS
let configDir = 'config_win';
if (process.platform === 'darwin') {
configDir = 'config_mac';
} else if (process.platform === 'linux') {
configDir = 'config_linux';
}
params.push('-configuration'); params.push(path.resolve( __dirname ,'../../server',configDir));
params.push('-data'); params.push(workspacePath);
console.log('Executing '+ child + ' '+ params.join(' '));
electron.fork(child, params, {}, function(err, result) {
if(err) { reject(err); }
if(result){ resolve(result); }
});
}));
}
export function activate(context: ExtensionContext) {
storagePath = context.storagePath;
if (!storagePath) {
storagePath = getTempWorkspace();
}
let serverOptions= runJavaServer;
// Options to control the language client
let clientOptions: LanguageClientOptions = {
// Register the server for java
documentSelector: ['java'],
synchronize: {
// Notify the server about file changes to .java files contain in the workspace
fileEvents: [
workspace.createFileSystemWatcher('**/*.java'),
workspace.createFileSystemWatcher('**/pom.xml')
],
}
};
let item = window.createStatusBarItem(StatusBarAlignment.Right, Number.MIN_VALUE);
oldConfig = workspace.getConfiguration('java');
// Create the language client and start the client.
let languageClient = new LanguageClient('java','Language Support for Java', serverOptions, clientOptions);
languageClient.onNotification(StatusNotification.type, (report) => {
console.log(report.message);
if(report.type === 'Started'){
item.text = '$(thumbsup)';
} else if(report.type === 'Error'){
item.text = '$(thumbsdown)';
} else {
item.text = report.message;
}
item.command = 'java.open.output';
item.tooltip = report.message;
toggleItem(window.activeTextEditor, item);
});
commands.registerCommand('java.open.output', ()=>{
languageClient.outputChannel.show(ViewColumn.Three);
});
commands.registerCommand('java.show.references', (uri:string, position: LSPosition, locations:LSLocation[])=>{
commands.executeCommand('editor.action.showReferences', Uri.parse(uri), Protocol2Code.asPosition(position), locations.map(Protocol2Code.asLocation));
});
window.onDidChangeActiveTextEditor((editor) =>{
toggleItem(editor, item);
});
let provider: TextDocumentContentProvider= <TextDocumentContentProvider> {
onDidChange: null,
provideTextDocumentContent: (uri: Uri, token: CancellationToken): Thenable<string> => {
return languageClient.sendRequest(ClassFileContentsRequest.type, { uri: uri.toString() }, token).then((v: string):string => {
return v || '';
});
}
};
workspace.registerTextDocumentContentProvider('jdt', provider);
item.text = 'Starting Java Language Server...';
toggleItem(window.activeTextEditor, item);
let disposable = languageClient.start();
// Push the disposable to the context's subscriptions so that the
// client can be deactivated on extension deactivation
context.subscriptions.push(disposable);
context.subscriptions.push(onConfigurationChange());
}
function toggleItem(editor, item) {
if(editor && editor.document && editor.document.languageId === 'java'){
item.show();
} else{
item.hide();
}
}
function onConfigurationChange() {
return workspace.onDidChangeConfiguration(params => {
let newConfig = workspace.getConfiguration('java');
if (hasJavaConfigChanged(oldConfig, newConfig)) {
let msg = 'Java Language Server configuration changed, please restart VS Code.';
let action = 'Restart Now';
let restartId = 'workbench.action.reloadWindow';
oldConfig = newConfig;
window.showWarningMessage(msg,action).then((selection )=>{
if(action === selection) {
commands.executeCommand(restartId);
}
});
}
});
}
function hasJavaConfigChanged(oldConfig, newConfig) {
return hasConfigKeyChanged('home', oldConfig, newConfig)
|| hasConfigKeyChanged('jdt.ls.vmargs', oldConfig, newConfig);
}
function hasConfigKeyChanged(key, oldConfig, newConfig) {
return oldConfig.get(key) !== newConfig.get(key);
}
export function parseVMargs(params:any[], vmargsLine:string) {
if (!vmargsLine) {
return;
}
let vmargs = vmargsLine.match(/(?:[^\s"]+|"[^"]*")+/g);
if (vmargs === null) {
return;
}
vmargs.forEach (function(arg) {
if (params.indexOf(arg) < 0) {
params.push(arg);
}
});
}
function getTempWorkspace() {
return path.resolve(os.tmpdir(),'vscodesws_'+makeRandomHexString(5));
}
function makeRandomHexString(length) {
var chars = ['0', '1', '2', '3', '4', '5', '6', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
var result = '';
for (var i = 0; i < length; i++) {
var idx = Math.floor(chars.length * Math.random());
result += chars[idx];
}
return result;
}
function startedInDebugMode(): boolean {
let args = (process as any).execArgv;
if (args) {
return args.some((arg) => /^--debug=?/.test(arg) || /^--debug-brk=?/.test(arg));
};
return false;
}