Skip to content

Commit

Permalink
Launch the nb-java-lsp-server application first and only then connect…
Browse files Browse the repository at this point in the history
… to it with an LSP client
  • Loading branch information
Jaroslav Tulach committed Aug 12, 2020
1 parent bbee0e9 commit fa22d9c
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 38 deletions.
2 changes: 1 addition & 1 deletion java/java.lsp.server/script/etc/nb-java-lsp-server.conf
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ default_cachedir="${DEFAULT_CACHEDIR_ROOT}/dev"

# options used by the launcher by default, can be overridden by explicit
# command line switches
default_options="--nogui --nosplash -J-Djava.awt.headless=true --start-java-language-server -J--add-opens=java.base/java.net=ALL-UNNAMED -J--add-opens=java.base/java.lang.ref=ALL-UNNAMED -J--add-opens=java.base/java.lang=ALL-UNNAMED -J--add-opens=java.base/java.security=ALL-UNNAMED -J--add-opens=java.base/java.util=ALL-UNNAMED -J--add-opens=java.desktop/javax.swing.plaf.basic=ALL-UNNAMED -J--add-opens=java.desktop/javax.swing.text=ALL-UNNAMED -J--add-opens=java.desktop/javax.swing=ALL-UNNAMED -J--add-opens=java.desktop/java.awt=ALL-UNNAMED -J--add-opens=java.desktop/java.awt.event=ALL-UNNAMED -J--add-opens=java.prefs/java.util.prefs=ALL-UNNAMED -J--add-opens=jdk.jshell/jdk.jshell=ALL-UNNAMED -J--add-modules=jdk.jshell -J--add-exports=java.desktop/sun.awt=ALL-UNNAMED -J--add-exports=java.desktop/java.awt.peer=ALL-UNNAMED -J--add-exports=java.desktop/com.sun.beans.editors=ALL-UNNAMED -J--add-exports=java.desktop/sun.swing=ALL-UNNAMED -J--add-exports=java.desktop/sun.awt.im=ALL-UNNAMED -J--add-exports=jdk.internal.jvmstat/sun.jvmstat.monitor=ALL-UNNAMED -J--add-exports=java.management/sun.management=ALL-UNNAMED -J--add-exports=java.base/sun.reflect.annotation=ALL-UNNAMED -J-XX:+IgnoreUnrecognizedVMOptions"
default_options="--nogui --nosplash -J-Dnetbeans.logger.console=true -J-Djava.awt.headless=true -J--add-opens=java.base/java.net=ALL-UNNAMED -J--add-opens=java.base/java.lang.ref=ALL-UNNAMED -J--add-opens=java.base/java.lang=ALL-UNNAMED -J--add-opens=java.base/java.security=ALL-UNNAMED -J--add-opens=java.base/java.util=ALL-UNNAMED -J--add-opens=java.desktop/javax.swing.plaf.basic=ALL-UNNAMED -J--add-opens=java.desktop/javax.swing.text=ALL-UNNAMED -J--add-opens=java.desktop/javax.swing=ALL-UNNAMED -J--add-opens=java.desktop/java.awt=ALL-UNNAMED -J--add-opens=java.desktop/java.awt.event=ALL-UNNAMED -J--add-opens=java.prefs/java.util.prefs=ALL-UNNAMED -J--add-opens=jdk.jshell/jdk.jshell=ALL-UNNAMED -J--add-modules=jdk.jshell -J--add-exports=java.desktop/sun.awt=ALL-UNNAMED -J--add-exports=java.desktop/java.awt.peer=ALL-UNNAMED -J--add-exports=java.desktop/com.sun.beans.editors=ALL-UNNAMED -J--add-exports=java.desktop/sun.swing=ALL-UNNAMED -J--add-exports=java.desktop/sun.awt.im=ALL-UNNAMED -J--add-exports=jdk.internal.jvmstat/sun.jvmstat.monitor=ALL-UNNAMED -J--add-exports=java.management/sun.management=ALL-UNNAMED -J--add-exports=java.base/sun.reflect.annotation=ALL-UNNAMED -J-XX:+IgnoreUnrecognizedVMOptions"
# for development purposes you may wish to append: -J-Dnetbeans.logger.console=true -J-ea

# default location of JDK/JRE, can be overridden by using --jdkhome <dir> switch
Expand Down
148 changes: 111 additions & 37 deletions java/java.lsp.server/vscode/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
/*Heavily influenced by the extension for Kotlin Language Server which is:
* Copyright (c) 2016 George Fraser
* Copyright (c) 2018 fwcd
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
'use strict';

Expand All @@ -17,13 +27,17 @@ import {
} from 'vscode-languageclient';

import * as path from 'path';
import { execSync } from 'child_process';
import { execSync, spawn, ChildProcess } from 'child_process';
import { resolve } from 'path';
import { rejects } from 'assert';
import * as vscode from 'vscode';

let client: LanguageClient;
let nbProcess : ChildProcess | null = null;

export function activate(context: ExtensionContext) {
//verify acceptable JDK is available/set:
let specifiedJDK: string = workspace.getConfiguration('netbeans').get('jdkhome');
let specifiedJDK = workspace.getConfiguration('netbeans').get('jdkhome');

try {
let targetJava = specifiedJDK != null ? specifiedJDK + '/bin/java' : 'java';
Expand All @@ -35,45 +49,105 @@ export function activate(context: ExtensionContext) {
let serverPath = path.resolve(context.extensionPath, "nb-java-lsp-server", "bin", "nb-java-lsp-server");

let serverOptions: ServerOptions;
let args: string[] = [];
if (specifiedJDK != null) {
args = ['--jdkhome', specifiedJDK];
let ideArgs: string[] = [];
if (specifiedJDK) {
ideArgs = ['--jdkhome', specifiedJDK as string];
}
let serverArgs: string[] = new Array<string>(...ideArgs);
serverArgs.push("--start-java-language-server");

serverOptions = {
command: serverPath,
args: args,
options: { cwd: workspace.rootPath }
}
args: serverArgs,
options: { cwd: workspace.rootPath },

// Options to control the language client
let clientOptions: LanguageClientOptions = {
// Register the server for java documents
documentSelector: ['java'],
synchronize: {
configurationSection: 'java',
fileEvents: [
workspace.createFileSystemWatcher('**/*.java')
]
},
outputChannelName: 'Java',
revealOutputChannelOn: 4 // never
}

// Create the language client and start the client.
client = new LanguageClient(
'java',
'NetBeans Java',
serverOptions,
clientOptions
);
// give the process some reasonable command
ideArgs.push("--modules");
ideArgs.push("--list");

let log = vscode.window.createOutputChannel("Java Language Server");
log.show(true);
log.appendLine("Launching Java Language Server");
vscode.window.showInformationMessage("Launching Java Language Server");

let ideRunning = new Promise((resolve, reject) => {
let collectedText : string | null = '';
function logAndWaitForEnabled(text: string) {
log.append(text);
if (collectedText == null) {
return;
}
collectedText += text;
if (collectedText.match(/org.netbeans.modules.java.lsp.server.*Enabled/)) {
resolve(text);
collectedText = null;
}
}

let p = spawn(serverPath, ideArgs, {
stdio : ["ignore", "pipe", "pipe"]
});
p.stdout.on('data', function(d: any) {
logAndWaitForEnabled(d.toString());
});
p.stderr.on('data', function(d: any) {
logAndWaitForEnabled(d.toString());
});
nbProcess = p;
nbProcess.on('close', function(code: number) {
if (code != 0) {
vscode.window.showWarningMessage("Java Language Server exited with " + code);
}
log.appendLine("");
if (collectedText != null) {
reject("Exit code " + code);
} else {
log.appendLine("Exit code " + code);
}
nbProcess = null;
});
});

ideRunning.then((value) => {
// Options to control the language client
let clientOptions: LanguageClientOptions = {
// Register the server for java documents
documentSelector: ['java'],
synchronize: {
configurationSection: 'java',
fileEvents: [
workspace.createFileSystemWatcher('**/*.java')
]
},
outputChannelName: 'Java',
revealOutputChannelOn: 4 // never
}

// Create the language client and start the client.
client = new LanguageClient(
'java',
'NetBeans Java',
serverOptions,
clientOptions
);

// Start the client. This will also launch the server
client.start();
}).catch((reason) => {
log.append(reason);
window.showErrorMessage('Error initializing ' + reason);
});

// Start the client. This will also launch the server
client.start();
}

export function deactivate(): Thenable<void> {
if (nbProcess != null) {
nbProcess.kill();
}
if (!client) {
return undefined;
return Promise.resolve();
}
return client.stop();
}

0 comments on commit fa22d9c

Please sign in to comment.