From ceb8b4337139373b50b0365f9e4a57ccfdb0a9b3 Mon Sep 17 00:00:00 2001 From: BoykoAlex Date: Wed, 16 May 2018 09:31:18 -0400 Subject: [PATCH] PT #157480703: switching boot hints on/off via preferences --- theia-extensions/theia-spring-boot/README.md | 4 +- .../spring-boot/package.json | 1 + .../src/browser/boot-preferences.ts | 54 +++++++++++++++++++ .../browser/language-client-contribution.ts | 31 ++++++++++- .../browser/spring-boot-frontend-module.ts | 2 + .../spring-boot/src/browser/utils.ts | 27 ++++++++++ .../src/node/spring-boot-ls-contribution.ts | 3 +- 7 files changed, 116 insertions(+), 6 deletions(-) create mode 100644 theia-extensions/theia-spring-boot/spring-boot/src/browser/boot-preferences.ts create mode 100644 theia-extensions/theia-spring-boot/spring-boot/src/browser/utils.ts diff --git a/theia-extensions/theia-spring-boot/README.md b/theia-extensions/theia-spring-boot/README.md index eb4299772..bc5caf34d 100644 --- a/theia-extensions/theia-spring-boot/README.md +++ b/theia-extensions/theia-spring-boot/README.md @@ -32,7 +32,7 @@ Open http://localhost:3000 in the browser. ## Developing with the browser example -Start watching of the hello world extension. +Start watching of the extension. cd spring-boot yarn watch @@ -49,7 +49,7 @@ Open http://localhost:3000 in the browser. ## Developing with the Electron example -Start watching of the hello world extension. +Start watching of extension. cd spring-boot yarn watch diff --git a/theia-extensions/theia-spring-boot/spring-boot/package.json b/theia-extensions/theia-spring-boot/spring-boot/package.json index 32258949d..ba4320b63 100644 --- a/theia-extensions/theia-spring-boot/spring-boot/package.json +++ b/theia-extensions/theia-spring-boot/spring-boot/package.json @@ -12,6 +12,7 @@ "dependencies": { "@theia/core": "latest", "@theia/languages": "latest", + "@theia/preferences": "latest", "@theia/editor": "latest", "@theia/monaco": "latest", "@theia/java": "latest", diff --git a/theia-extensions/theia-spring-boot/spring-boot/src/browser/boot-preferences.ts b/theia-extensions/theia-spring-boot/spring-boot/src/browser/boot-preferences.ts new file mode 100644 index 000000000..3318667d8 --- /dev/null +++ b/theia-extensions/theia-spring-boot/spring-boot/src/browser/boot-preferences.ts @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2018 TypeFox and others. + * + * Licensed 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 + */ + +import { interfaces } from 'inversify'; +import { createPreferenceProxy, PreferenceProxy, PreferenceService, PreferenceContribution, PreferenceSchema } from '@theia/core/lib/browser'; + +// tslint:disable:max-line-length + +export const BootConfigSchema: PreferenceSchema = { + 'type': 'object', + 'title': 'Spring Boot Java Configuration', + properties: { + 'boot-java.boot-hints.on': { + type: 'boolean', + description: 'Enable/Disable Spring running Boot application live hints decorators in Java source code.', + default: false + }, + 'spring-boot.ls.java.home': { + type: 'string', + description: `Override JAVA_HOME used for launching the spring-boot-language-server JVM process.`, + default: null + }, + 'spring-boot.ls.java.heap': { + type: 'string', + description: `Max JVM heap value, passed via -Xmx argument when launching spring-boot-language-server JVM process.`, + default: null + } + } +}; + +export interface BootConfiguration { + 'boot-java.boot-hints.on': boolean; + 'spring-boot.ls.java.home': string | null; + 'spring-boot.ls.java.heap': string | null; +} + +export const BootPreferences = Symbol('BootJavaPreferences'); +export type BootPreferences = PreferenceProxy; + +export function createBootPreferences(preferences: PreferenceService): BootPreferences { + return createPreferenceProxy(preferences, BootConfigSchema); +} + +export function bindBootPreferences(bind: interfaces.Bind): void { + bind(BootPreferences).toDynamicValue(ctx => { + const preferences = ctx.container.get(PreferenceService); + return createBootPreferences(preferences); + }); + bind(PreferenceContribution).toConstantValue({ schema: BootConfigSchema }); +} diff --git a/theia-extensions/theia-spring-boot/spring-boot/src/browser/language-client-contribution.ts b/theia-extensions/theia-spring-boot/spring-boot/src/browser/language-client-contribution.ts index 99d690890..d6d621802 100644 --- a/theia-extensions/theia-spring-boot/spring-boot/src/browser/language-client-contribution.ts +++ b/theia-extensions/theia-spring-boot/spring-boot/src/browser/language-client-contribution.ts @@ -1,4 +1,4 @@ -import { injectable, inject } from 'inversify'; +import { injectable, inject, postConstruct } from 'inversify'; import { BaseLanguageClientContribution, Workspace, Languages, LanguageClientFactory } from '@theia/languages/lib/browser'; import { SPRING_BOOT_SERVER_ID, @@ -10,6 +10,13 @@ import { DocumentSelector } from '@theia/languages/lib/common'; import { JAVA_LANGUAGE_ID } from '@theia/java/lib/common'; import { HighlightService} from './highlight-service'; import { ClasspathService } from './classpath-service'; +import { BootPreferences } from './boot-preferences'; +import { NotificationType } from 'vscode-jsonrpc'; +import { DidChangeConfigurationParams } from 'vscode-base-languageclient/lib/base'; +import { Utils } from './utils'; + +const CONFIG_CHANGED_NOTIFICATION_TYPE = new NotificationType('workspace/didChangeConfiguration'); + @injectable() export class SpringBootClientContribution extends BaseLanguageClientContribution { @@ -22,15 +29,35 @@ export class SpringBootClientContribution extends BaseLanguageClientContribution @inject(Languages) protected readonly languages: Languages, @inject(LanguageClientFactory) protected readonly languageClientFactory: LanguageClientFactory, @inject(HighlightService) protected readonly highlightService: HighlightService, - @inject(ClasspathService) protected readonly classpathService: ClasspathService + @inject(ClasspathService) protected readonly classpathService: ClasspathService, + @inject(BootPreferences) protected readonly preferences: BootPreferences ) { super(workspace, languages, languageClientFactory); + } + + @postConstruct() + protected async init() { + await this.preferences.ready; + + // Send settings to LS + this.sendConfig(); + this.preferences.onPreferenceChanged(() => this.sendConfig()); + this.languageClient.then(client => { this.highlightService.attach(client); // this.classpathService.attach(client); }); } + private sendConfig() { + return this.languageClient.then(client => { + const params = Utils.convertDotToNested(Object.assign({}, this.preferences)); + return client.sendNotification(CONFIG_CHANGED_NOTIFICATION_TYPE, { + settings: params + }); + }) + } + protected get documentSelector(): DocumentSelector | undefined { return [JAVA_LANGUAGE_ID, BOOT_PROPERTIES_YAML_LANGUAGE_ID, BOOT_PROPERTIES_LANGUAGE_ID]; } diff --git a/theia-extensions/theia-spring-boot/spring-boot/src/browser/spring-boot-frontend-module.ts b/theia-extensions/theia-spring-boot/spring-boot/src/browser/spring-boot-frontend-module.ts index c07f97fb2..82b85ea11 100644 --- a/theia-extensions/theia-spring-boot/spring-boot/src/browser/spring-boot-frontend-module.ts +++ b/theia-extensions/theia-spring-boot/spring-boot/src/browser/spring-boot-frontend-module.ts @@ -6,9 +6,11 @@ import './monaco-yaml-contribution'; import './monaco-properties-contribution'; import { HighlightService } from './highlight-service'; import { ClasspathService } from './classpath-service'; +import { bindBootPreferences } from './boot-preferences'; export default new ContainerModule(bind => { // add your contribution bindings here + bindBootPreferences(bind); bind(LanguageClientContribution).to(SpringBootClientContribution).inSingletonScope(); bind(HighlightService).toSelf().inSingletonScope(); bind(ClasspathService).toSelf().inSingletonScope(); diff --git a/theia-extensions/theia-spring-boot/spring-boot/src/browser/utils.ts b/theia-extensions/theia-spring-boot/spring-boot/src/browser/utils.ts new file mode 100644 index 000000000..5b5f929c2 --- /dev/null +++ b/theia-extensions/theia-spring-boot/spring-boot/src/browser/utils.ts @@ -0,0 +1,27 @@ +export class Utils { + + private static setNestedValue(properties: string[], value: any, obj: any){ + if (properties.length > 1) { + // The property doesn't exists OR is not an object (and so we overwritte it) so we create it + if (!obj.hasOwnProperty(properties[0]) || typeof obj[properties[0]] !== 'object') { + obj[properties[0]] = {}; + } + Utils.setNestedValue(properties.slice(1), value, obj[properties[0]]); + } else { + obj[properties[0]] = value; + } + } + + public static convertDotToNested(source: any): any { + const result = {}; + Object.keys(source).forEach(property => { + const properties = property.split('.'); + if (source[property] && typeof source[property] === 'object') { + Utils.setNestedValue(properties, Utils.convertDotToNested(source[property]), result); + } else { + Utils.setNestedValue(properties, source[property], result); + } + }); + return result; + } +} \ No newline at end of file diff --git a/theia-extensions/theia-spring-boot/spring-boot/src/node/spring-boot-ls-contribution.ts b/theia-extensions/theia-spring-boot/spring-boot/src/node/spring-boot-ls-contribution.ts index 058158653..2a95f3ee7 100644 --- a/theia-extensions/theia-spring-boot/spring-boot/src/node/spring-boot-ls-contribution.ts +++ b/theia-extensions/theia-spring-boot/spring-boot/src/node/spring-boot-ls-contribution.ts @@ -56,8 +56,7 @@ export class SpringBootLsContribution extends BaseLanguageServerContribution { '-Dsts.lsp.client=theia', '-Dlsp.completions.indentation.enable=true', '-Dlsp.yaml.completions.errors.disable=true', - '-Dorg.slf4j.simpleLogger.logFile=boot-java.log', - '-Dorg.slf4j.simpleLogger.defaultLogLevel=debug' + '-Dorg.slf4j.simpleLogger.logFile=boot-java.log' ]; args.push(`-Dserver.port=${env.CLIENT_PORT}`);