Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions x-pack/plugins/code/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ export const code = (kibana: any) =>
updateRepoFrequencyMs: Joi.number().default(moment.duration(1, 'hour').asMilliseconds()),
// The frequency which each repo tries to index. 1 day by default.
indexRepoFrequencyMs: Joi.number().default(moment.duration(1, 'day').asMilliseconds()),
// timeout a request over 30s.
lspRequestTimeoutMs: Joi.number().default(moment.duration(10, 'second').asMilliseconds()),
lsp: Joi.object({
// timeout of a request
requestTimeoutMs: Joi.number().default(moment.duration(10, 'second').asMilliseconds()),
// if we want the language server run in seperately
detach: Joi.boolean().default(false),
// whether we want to show more language server logs
verbose: Joi.boolean().default(false),
}),
repos: Joi.array().default([]),
maxWorkspace: Joi.number().default(5), // max workspace folder for each language server
disableScheduler: Joi.boolean().default(true), // Temp option to disable all schedulers.
Expand Down
6 changes: 5 additions & 1 deletion x-pack/plugins/code/server/__tests__/clone_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ const options = {
queueTimeout: 60 * 60 * 1000, // 1 hour by default
updateFreqencyMs: 5 * 60 * 1000, // 5 minutes by default
indexFrequencyMs: 24 * 60 * 60 * 1000, // 1 day by default
lspRequestTimeoutMs: 5 * 60, // timeout a request over 30s
lsp: {
requestTimeoutMs: 5 * 60, // timeout a request over 30s
detach: false,
verbose: false,
},
repos: [],
maxWorkspace: 5, // max workspace folder for each language server
disableScheduler: true, // Temp option to disable all schedulers.
Expand Down
6 changes: 5 additions & 1 deletion x-pack/plugins/code/server/__tests__/git_operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ describe('git_operations', () => {
queueTimeout: 60 * 60 * 1000, // 1 hour by default
updateFreqencyMs: 5 * 60 * 1000, // 5 minutes by default
indexFrequencyMs: 24 * 60 * 60 * 1000, // 1 day by default
lspRequestTimeoutMs: 5 * 60, // timeout a request over 30s
lsp: {
requestTimeoutMs: 5 * 60, // timeout a request over 30s
detach: false,
verbose: false,
},
repos: [],
maxWorkspace: 5, // max workspace folder for each language server
disableScheduler: true, // Temp option to disable all schedulers.
Expand Down
6 changes: 5 additions & 1 deletion x-pack/plugins/code/server/__tests__/lsp_indexer_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ const options = {
queueTimeout: 60 * 60 * 1000, // 1 hour by default
updateFreqencyMs: 5 * 60 * 1000, // 5 minutes by default
indexFrequencyMs: 24 * 60 * 60 * 1000, // 1 day by default
lspRequestTimeoutMs: 5 * 60, // timeout a request over 30s
lsp: {
requestTimeoutMs: 5 * 60, // timeout a request over 30s
detach: false,
verbose: false,
},
repos: [],
maxWorkspace: 5, // max workspace folder for each language server
disableScheduler: true, // Temp option to disable all schedulers.
Expand Down
6 changes: 5 additions & 1 deletion x-pack/plugins/code/server/__tests__/lsp_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ describe('lsp_service tests', () => {
queueTimeout: 60 * 60 * 1000, // 1 hour by default
updateFreqencyMs: 5 * 60 * 1000, // 5 minutes by default
indexFrequencyMs: 24 * 60 * 60 * 1000, // 1 day by default
lspRequestTimeoutMs: 5 * 60, // timeout a request over 30s
lsp: {
requestTimeoutMs: 5 * 60, // timeout a request over 30s
detach: false,
verbose: false,
},
repos: [],
maxWorkspace: 5, // max workspace folder for each language server
disableScheduler: true, // Temp option to disable all schedulers.
Expand Down
5 changes: 2 additions & 3 deletions x-pack/plugins/code/server/lsp/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export class LanguageServerController implements ILanguageServerHandler {
// a { lang -> server } map from above list
private readonly languageServerMap: { [lang: string]: LanguageServerData };
private log: Logger;
private readonly detach: boolean = process.env.LSP_DETACH === 'true';

constructor(
readonly options: ServerOptions,
Expand All @@ -62,7 +61,7 @@ export class LanguageServerController implements ILanguageServerHandler {
builtinWorkspaceFolders: def.builtinWorkspaceFolders,
languages: def.languages,
maxWorkspace: options.maxWorkspace,
launcher: new def.launcher(this.targetHost, this.detach, options, loggerFactory),
launcher: new def.launcher(this.targetHost, options, loggerFactory),
}));
this.languageServerMap = this.languageServers.reduce(
(map, ls) => {
Expand Down Expand Up @@ -241,7 +240,7 @@ export class LanguageServerController implements ILanguageServerHandler {
const ls = this.languageServerMap[lang];
if (ls) {
if (
!this.detach &&
!this.options.lsp.detach &&
this.installManager.status(ls.definition) !== LanguageServerStatus.READY
) {
throw new ResponseError(
Expand Down
3 changes: 1 addition & 2 deletions x-pack/plugins/code/server/lsp/go_launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export class GoLauncher implements ILanguageServerLauncher {
private isRunning: boolean = false;
constructor(
readonly targetHost: string,
readonly detach: boolean,
readonly options: ServerOptions,
readonly loggerFactory: LoggerFactory
) {}
Expand All @@ -26,7 +25,7 @@ export class GoLauncher implements ILanguageServerLauncher {
const port = 2091;

const log = this.loggerFactory.getLogger(['code', `go@${this.targetHost}:${port}`]);
const proxy = new LanguageServerProxy(port, this.targetHost, log);
const proxy = new LanguageServerProxy(port, this.targetHost, log, this.options.lsp);

log.info('Detach mode, expected langserver launch externally');
proxy.onConnected(() => {
Expand Down
7 changes: 3 additions & 4 deletions x-pack/plugins/code/server/lsp/java_launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export class JavaLauncher implements ILanguageServerLauncher {
private isRunning: boolean = false;
constructor(
readonly targetHost: string,
readonly detach: boolean,
readonly options: ServerOptions,
readonly loggerFactory: LoggerFactory
) {}
Expand All @@ -32,13 +31,13 @@ export class JavaLauncher implements ILanguageServerLauncher {
public async launch(builtinWorkspace: boolean, maxWorkspace: number, installationPath: string) {
let port = 2090;

if (!this.detach) {
if (!this.options) {
port = await getPort();
}
const log = this.loggerFactory.getLogger(['code', `java@${this.targetHost}:${port}`]);
const proxy = new LanguageServerProxy(port, this.targetHost, log);
const proxy = new LanguageServerProxy(port, this.targetHost, log, this.options.lsp);
proxy.awaitServerConnection();
if (this.detach) {
if (this.options.lsp.detach) {
// detach mode
proxy.onConnected(() => {
this.isRunning = true;
Expand Down
12 changes: 5 additions & 7 deletions x-pack/plugins/code/server/lsp/language_server_launcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ const tmpDataPath = fs.mkdtempSync(path.join(os.tmpdir(), 'code_test'));
const options: ServerOptions = {
workspacePath: `${tmpDataPath}/workspace`,
jdtWorkspacePath: `${tmpDataPath}/jdt`,
// @ts-ignore
lsp: {
detach: false,
},
};

beforeAll(async () => {
Expand All @@ -40,13 +44,7 @@ function delay(seconds: number) {
}

test('typescript language server could be shutdown', async () => {
// @ts-ignore
const tsLauncher = new TypescriptServerLauncher(
'localhost',
false,
options,
new ConsoleLoggerFactory()
);
const tsLauncher = new TypescriptServerLauncher('localhost', options, new ConsoleLoggerFactory());
const proxy = await tsLauncher.launch(true, 1, TYPESCRIPT.embedPath!);
await proxy.initialize(options.workspacePath);
await delay(2);
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/code/server/lsp/language_server_launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export interface ILanguageServerLauncher {
export interface LauncherConstructor {
new (
targetHost: string,
detach: boolean,
options: ServerOptions,
loggerFactory: LoggerFactory
): ILanguageServerLauncher;
Expand Down
10 changes: 7 additions & 3 deletions x-pack/plugins/code/server/lsp/lsp_test_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ const options = {
queueTimeout: 60 * 60 * 1000, // 1 hour by default
updateFreqencyMs: 5 * 60 * 1000, // 5 minutes by default
indexFrequencyMs: 24 * 60 * 60 * 1000, // 1 day by default
lspRequestTimeoutMs: 5 * 60, // timeout a request over 30s
lsp: {
requestTimeoutMs: 5 * 60, // timeout a request over 30s
detach: false,
verbose: false,
},
repos: [],
maxWorkspace: 5, // max workspace folder for each language server
disableScheduler: true, // Temp option to disable all schedulers.
Expand Down Expand Up @@ -249,12 +253,12 @@ export class LspTestRunner {
}

private async launchTypescriptLanguageServer() {
const launcher = new TypescriptServerLauncher('127.0.0.1', false, serverOptions, new ConsoleLoggerFactory());
const launcher = new TypescriptServerLauncher('127.0.0.1', serverOptions, new ConsoleLoggerFactory());
return await launcher.launch(false, 1, TYPESCRIPT.embedPath!);
}

private async launchJavaLanguageServer() {
const launcher = new JavaLauncher('127.0.0.1', false, serverOptions, new ConsoleLoggerFactory());
const launcher = new JavaLauncher('127.0.0.1', serverOptions, new ConsoleLoggerFactory());
// @ts-ignore
const installManager = new InstallManager(null, serverOptions);
return await launcher.launch(false, 1, installManager.installationPath(JAVA));
Expand Down
79 changes: 44 additions & 35 deletions x-pack/plugins/code/server/lsp/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { createConnection, IConnection } from 'vscode-languageserver/lib/main';

import { LspRequest } from '../../model';
import { Logger } from '../log';
import { LspOptions } from '../server_options';
import { HttpMessageReader } from './http_message_reader';
import { HttpMessageWriter } from './http_message_writer';
import { HttpRequestEmitter } from './http_request_emitter';
Expand Down Expand Up @@ -54,15 +55,17 @@ export class LanguageServerProxy implements ILanguageServerHandler {
private replies = createRepliesMap();
private readonly targetHost: string;
private readonly targetPort: number;
private readonly logger?: Logger;
private readonly logger: Logger;
private readonly lspOptions: LspOptions;
private eventEmitter = new EventEmitter();

private connectingPromise?: Promise<MessageConnection>;

constructor(targetPort: number, targetHost: string, logger?: Logger) {
constructor(targetPort: number, targetHost: string, logger: Logger, lspOptions: LspOptions) {
this.targetHost = targetHost;
this.targetPort = targetPort;
this.logger = logger;
this.lspOptions = lspOptions;
this.conn = createConnection(
new HttpMessageReader(this.httpEmitter),
new HttpMessageWriter(this.replies, logger)
Expand All @@ -80,7 +83,9 @@ export class LanguageServerProxy implements ILanguageServerHandler {
params,
};
return new Promise<ResponseMessage>((resolve, reject) => {
if (this.logger) {
if (this.lspOptions.verbose) {
this.logger.info(`emit message ${JSON.stringify(message)}`);
} else {
this.logger.debug(`emit message ${JSON.stringify(message)}`);
}
if (isNotification) {
Expand All @@ -106,9 +111,7 @@ export class LanguageServerProxy implements ILanguageServerHandler {
capabilities: clientCapabilities,
};
return await clientConn.sendRequest('initialize', params).then(r => {
if (this.logger) {
this.logger.info(`initialized at ${rootUri}`);
}
this.logger.info(`initialized at ${rootUri}`);

// @ts-ignore
// TODO fix this
Expand All @@ -120,11 +123,16 @@ export class LanguageServerProxy implements ILanguageServerHandler {

public listen() {
this.conn.onRequest((method: string, ...params) => {
if (this.logger) {
if (this.lspOptions.verbose) {
this.logger.info('received request method: ' + method);
} else {
this.logger.debug('received request method: ' + method);
}

return this.connect().then(clientConn => {
if (this.logger) {
if (this.lspOptions.verbose) {
this.logger.info(`proxy method:${method} to Language Server `);
} else {
this.logger.debug(`proxy method:${method} to Language Server `);
}

Expand All @@ -136,9 +144,7 @@ export class LanguageServerProxy implements ILanguageServerHandler {

public async shutdown() {
const clientConn = await this.connect();
if (this.logger) {
this.logger.info(`sending shutdown request`);
}
this.logger.info(`sending shutdown request`);
return await clientConn.sendRequest('shutdown');
}
/**
Expand All @@ -147,14 +153,11 @@ export class LanguageServerProxy implements ILanguageServerHandler {
*/
public async exit() {
if (this.clientConnection) {
if (this.logger) {
this.logger.info('sending `shutdown` request to language server.');
}
this.logger.info('sending `shutdown` request to language server.');
const clientConn = this.clientConnection;
await clientConn.sendRequest('shutdown').then(() => {
if (this.logger) {
this.logger.info('sending `exit` notification to language server.');
}
this.logger.info('sending `exit` notification to language server.');

// @ts-ignore
// TODO fix this
clientConn.sendNotification(ExitNotification.type);
Expand All @@ -172,9 +175,7 @@ export class LanguageServerProxy implements ILanguageServerHandler {
this.eventEmitter.emit('connect');
socket.on('close', () => this.onSocketClosed());

if (this.logger) {
this.logger.info('Java langserver connection established on port ' + this.targetPort);
}
this.logger.info('Java langserver connection established on port ' + this.targetPort);

const reader = new SocketMessageReader(socket);
const writer = new SocketMessageWriter(socket);
Expand All @@ -186,9 +187,7 @@ export class LanguageServerProxy implements ILanguageServerHandler {
server.on('error', rej);
server.listen(this.targetPort, () => {
server.removeListener('error', rej);
if (this.logger) {
this.logger.info('Wait Java langserver connection on port ' + this.targetPort);
}
this.logger.info('Wait Java langserver connection on port ' + this.targetPort);
});
});
}
Expand Down Expand Up @@ -271,21 +270,31 @@ export class LanguageServerProxy implements ILanguageServerHandler {
private registerOnNotificationHandler(clientConnection: MessageConnection) {
// @ts-ignore
clientConnection.onNotification(LogMessageNotification.type, notification => {
if (this.logger) {
switch (notification.type) {
case MessageType.Log:
switch (notification.type) {
case MessageType.Log:
this.logger.debug(notification.message);
break;
case MessageType.Info:
if (this.lspOptions.verbose) {
this.logger.info(notification.message);
} else {
this.logger.debug(notification.message);
break;
case MessageType.Info:
this.logger.debug(notification.message);
break;
case MessageType.Warning:
}
break;
case MessageType.Warning:
if (this.lspOptions.verbose) {
this.logger.warn(notification.message);
} else {
this.logger.log(notification.message);
break;
case MessageType.Error:
}
break;
case MessageType.Error:
if (this.lspOptions.verbose) {
this.logger.error(notification.message);
} else {
this.logger.warn(notification.message);
break;
}
}
break;
}
});
}
Expand Down
7 changes: 3 additions & 4 deletions x-pack/plugins/code/server/lsp/ts_launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export class TypescriptServerLauncher implements ILanguageServerLauncher {
private isRunning: boolean = false;
constructor(
readonly targetHost: string,
readonly detach: boolean,
readonly options: ServerOptions,
readonly loggerFactory: LoggerFactory
) {}
Expand All @@ -30,13 +29,13 @@ export class TypescriptServerLauncher implements ILanguageServerLauncher {
public async launch(builtinWorkspace: boolean, maxWorkspace: number, installationPath: string) {
let port = 2089;

if (!this.detach) {
if (!this.options.lsp.detach) {
port = await getPort();
}
const log: Logger = this.loggerFactory.getLogger(['code', `ts@${this.targetHost}:${port}`]);
const proxy = new LanguageServerProxy(port, this.targetHost, log);
const proxy = new LanguageServerProxy(port, this.targetHost, log, this.options.lsp);

if (this.detach) {
if (this.options.lsp.detach) {
log.info('Detach mode, expected langserver launch externally');
proxy.onConnected(() => {
this.isRunning = true;
Expand Down
Loading