Skip to content

Commit

Permalink
fix #2653: open log and restart ts server commands
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Kosyakov <[email protected]>
  • Loading branch information
akosyakov committed Sep 4, 2018
1 parent 00e8c09 commit 86ea7fa
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/json/src/browser/json-preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const jsonPreferenceSchema: PreferenceSchema = {
'verbose'
],
'default': 'off',
'description': 'Enable/disable default JSON formatter'
'description': 'Enable/disable tracing communications with the JSON language server'
}
}
};
Expand Down
27 changes: 23 additions & 4 deletions packages/languages/src/browser/language-client-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { FrontendApplication, WebSocketConnectionProvider, WebSocketOptions } fr
import {
LanguageContribution, ILanguageClient, LanguageClientOptions,
DocumentSelector, TextDocument, FileSystemWatcher,
Workspace, Languages
Workspace, Languages, State
} from './language-client-services';
import { MessageConnection } from 'vscode-jsonrpc';
import { LanguageClientFactory } from './language-client-factory';
Expand Down Expand Up @@ -111,7 +111,20 @@ export abstract class BaseLanguageClientContribution implements LanguageClientCo
return toDeactivate;
}

protected state: State | undefined;
get running(): boolean {
return this.state === State.Running;
}
restart(): void {
if (this._languageClient) {
this._languageClient.stop();
}
}

protected onWillStart(languageClient: ILanguageClient): void {
languageClient.onDidChangeState(({ newState }) => {
this.state = newState;
});
languageClient.onReady().then(() => this.onReady(languageClient));
}

Expand All @@ -133,7 +146,7 @@ export abstract class BaseLanguageClientContribution implements LanguageClientCo
}

protected createOptions(): LanguageClientOptions {
const { id, documentSelector, fileEvents, configurationSection } = this;
const { id, documentSelector, fileEvents, configurationSection, initializationOptions } = this;
return {
documentSelector,
synchronize: { fileEvents, configurationSection },
Expand All @@ -142,11 +155,17 @@ export abstract class BaseLanguageClientContribution implements LanguageClientCo
this.messageService.error(`Failed to start ${this.name} language server${detail}`);
return false;
},
diagnosticCollectionName: id
diagnosticCollectionName: id,
initializationOptions
};
}

protected get configurationSection(): string | string[] | undefined {
// tslint:disable-next-line:no-any
protected get initializationOptions(): any | (() => any) | undefined {
return undefined;
}

protected get configurationSection(): string | string[] | undefined {
return undefined;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"@theia/core": "^0.3.14",
"@theia/languages": "^0.3.14",
"@theia/monaco": "^0.3.14",
"typescript-language-server": "^0.3.0"
"typescript-language-server": "^0.3.1"
},
"publishConfig": {
"access": "public"
Expand Down
45 changes: 43 additions & 2 deletions packages/typescript/src/browser/typescript-client-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { injectable, inject } from 'inversify';
import { BaseLanguageClientContribution, Workspace, Languages, LanguageClientFactory } from '@theia/languages/lib/browser';
import { injectable, inject, postConstruct } from 'inversify';
import URI from '@theia/core/lib/common/uri';
import { BaseLanguageClientContribution, Workspace, Languages, LanguageClientFactory, ILanguageClient, State } from '@theia/languages/lib/browser';
import { TypeScriptInitializationOptions, TypeScriptInitializeResult } from 'typescript-language-server/lib/ts-protocol';
import { TYPESCRIPT_LANGUAGE_ID, TYPESCRIPT_LANGUAGE_NAME, TYPESCRIPT_REACT_LANGUAGE_ID, JAVASCRIPT_LANGUAGE_ID, JAVASCRIPT_REACT_LANGUAGE_ID } from '../common';
import { TypescriptPreferences } from './typescript-preferences';

@injectable()
export class TypeScriptClientContribution extends BaseLanguageClientContribution {

readonly id = TYPESCRIPT_LANGUAGE_ID;
readonly name = TYPESCRIPT_LANGUAGE_NAME;

@inject(TypescriptPreferences)
protected readonly preferences: TypescriptPreferences;

constructor(
@inject(Workspace) protected readonly workspace: Workspace,
@inject(Languages) protected readonly languages: Languages,
Expand All @@ -32,6 +38,15 @@ export class TypeScriptClientContribution extends BaseLanguageClientContribution
super(workspace, languages, languageClientFactory);
}

@postConstruct()
protected init(): void {
this.preferences.onPreferenceChanged(e => {
if (e.preferenceName === 'typescript.server.log') {
this.restart();
}
});
}

protected get documentSelector(): string[] {
return [
TYPESCRIPT_LANGUAGE_ID,
Expand All @@ -55,4 +70,30 @@ export class TypeScriptClientContribution extends BaseLanguageClientContribution
];
}

protected get initializationOptions(): TypeScriptInitializationOptions {
const options: TypeScriptInitializationOptions = {};
const logVerbosity = this.preferences['typescript.server.log'];
if (logVerbosity !== 'off') {
options.logVerbosity = logVerbosity;
}
return options;
}

protected _logFileUri: URI | undefined;
get logFileUri(): URI | undefined {
return this._logFileUri;
}
protected onReady(languageClient: ILanguageClient): void {
if (languageClient.initializeResult) {
const initializeResult = languageClient.initializeResult as TypeScriptInitializeResult;
this._logFileUri = initializeResult.logFileUri !== undefined ? new URI(initializeResult.logFileUri) : undefined;
}
languageClient.onDidChangeState(({ newState }) => {
if (newState === State.Stopped) {
this._logFileUri = undefined;
}
});
super.onReady(languageClient);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ export namespace TypeScriptCommands {
label: 'TypeScript: Organize Imports',
id: 'typescript.edit.organizeImports'
};
export const openServerLog: Command = {
label: 'TypeScript: Open Server Log',
id: 'typescript.server.openLog'
};
export const restartServer: Command = {
label: 'TypeScript: Restart Server',
id: 'typescript.server.restart'
};
}

@injectable()
Expand Down Expand Up @@ -70,6 +78,16 @@ export class TypeScriptFrontendContribution implements CommandContribution, Menu
isEnabled: () => !!this.currentEditor,
isVisible: () => !!this.currentEditor
});
commands.registerCommand(TypeScriptCommands.openServerLog, {
execute: () => this.openServerLog(),
isEnabled: () => !!this.clientContribution.logFileUri,
isVisible: () => !!this.clientContribution.logFileUri
});
commands.registerCommand(TypeScriptCommands.restartServer, {
execute: () => this.clientContribution.restart(),
isEnabled: () => this.clientContribution.running,
isVisible: () => this.clientContribution.running
});
}

registerMenus(menus: MenuModelRegistry): void {
Expand All @@ -87,6 +105,13 @@ export class TypeScriptFrontendContribution implements CommandContribution, Menu
});
}

openServerLog(): void {
const logFileUri = this.clientContribution.logFileUri;
if (logFileUri) {
this.editorManager.open(logFileUri);
}
}

organizeImports(): void {
const editor = MonacoEditor.get(this.currentEditor);
if (editor) {
Expand Down
3 changes: 3 additions & 0 deletions packages/typescript/src/browser/typescript-frontend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ import { TypescriptGrammarContribution } from './typescript-language-config';
import { JavascriptGrammarContribution } from './javascript-language-config';
import { TypeScriptFrontendContribution } from './typescript-frontend-contribution';
import { TypeScriptEditorTextFocusContext } from './typescript-keybinding-contexts';
import { bindTypescriptPreferences } from './typescript-preferences';

export default new ContainerModule(bind => {
bindTypescriptPreferences(bind);

bind(TypeScriptClientContribution).toSelf().inSingletonScope();
bind(LanguageClientContribution).toService(TypeScriptClientContribution);

Expand Down
74 changes: 74 additions & 0 deletions packages/typescript/src/browser/typescript-preferences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/********************************************************************************
* Copyright (C) 2018 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { interfaces } from 'inversify';
import {
createPreferenceProxy,
PreferenceProxy,
PreferenceService,
PreferenceContribution,
PreferenceSchema,
PreferenceChangeEvent
} from '@theia/core/lib/browser/preferences';

export const typescriptPreferenceSchema: PreferenceSchema = {
'type': 'object',
'properties': {
'typescript.trace.server': {
'type': 'string',
'enum': [
'off',
'messages',
'verbose'
],
'default': 'off',
'description': 'Enable/disable tracing communications with the TS language server.'
},
'typescript.server.log': {
'type': 'string',
'enum': [
'off',
'terse',
'normal',
'verbose'
],
'default': 'off',
// tslint:disable:max-line-length
'description': 'Enables logging of the TS server to a file. This log can be used to diagnose TS Server issues. The log may contain file paths, source code, and other potentially sensitive information from your project.'
}
}
};

export interface TypescriptConfiguration {
'typescript.server.log': 'off' | 'terse' | 'normal' | 'verbose'
}
export type TypescriptPreferenceChange = PreferenceChangeEvent<TypescriptConfiguration>;

export const TypescriptPreferences = Symbol('TypescriptPreferences');
export type TypescriptPreferences = PreferenceProxy<TypescriptConfiguration>;

export function createTypescriptPreferences(preferences: PreferenceService): TypescriptPreferences {
return createPreferenceProxy(preferences, typescriptPreferenceSchema);
}

export function bindTypescriptPreferences(bind: interfaces.Bind): void {
bind(TypescriptPreferences).toDynamicValue(ctx => {
const preferences = ctx.container.get<PreferenceService>(PreferenceService);
return createTypescriptPreferences(preferences);
}).inSingletonScope();

bind(PreferenceContribution).toConstantValue({ schema: typescriptPreferenceSchema });
}
1 change: 0 additions & 1 deletion packages/typescript/src/node/typescript-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,4 @@ export class TypeScriptContribution extends BaseLanguageServerContribution {
const serverConnection = this.createProcessStreamConnection(command, args);
this.forward(clientConnection, serverConnection);
}

}
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9384,9 +9384,9 @@ typedoc@^0.8:
typedoc-default-themes "^0.5.0"
typescript "2.4.1"

typescript-language-server@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/typescript-language-server/-/typescript-language-server-0.3.0.tgz#f9594408ae7db2b21c1d2179d9470ed65c4b9906"
typescript-language-server@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/typescript-language-server/-/typescript-language-server-0.3.1.tgz#220b7396afe45b0f0a838996943a524c17906b71"
dependencies:
command-exists "1.2.6"
commander "^2.11.0"
Expand Down

0 comments on commit 86ea7fa

Please sign in to comment.