From f56c0fe59bc50f111209707f309936b61e2e22e5 Mon Sep 17 00:00:00 2001 From: BoykoAlex Date: Wed, 9 May 2018 22:45:17 -0400 Subject: [PATCH] theia-spring-boot: avoid using EditorDecorationService --- .../src/browser/highlight-service.ts | 74 ++++++++++++------- .../browser/language-client-contribution.ts | 4 +- .../browser/spring-boot-frontend-module.ts | 6 +- 3 files changed, 54 insertions(+), 30 deletions(-) diff --git a/theia-extensions/theia-spring-boot/spring-boot/src/browser/highlight-service.ts b/theia-extensions/theia-spring-boot/spring-boot/src/browser/highlight-service.ts index a05dcc39e..0e64a5c9a 100644 --- a/theia-extensions/theia-spring-boot/spring-boot/src/browser/highlight-service.ts +++ b/theia-extensions/theia-spring-boot/spring-boot/src/browser/highlight-service.ts @@ -1,8 +1,10 @@ import { injectable, inject } from 'inversify'; import { NotificationType } from 'vscode-jsonrpc'; import { TextDocumentIdentifier, Range } from 'vscode-base-languageclient/lib/base'; -import { EditorDecorationsService, SetDecorationParams, EditorDecorationStyle } from '@theia/editor/lib/browser'; +import { SetDecorationParams, EditorDecorationStyle, TextEditor, DeltaDecorationParams, EditorManager } from '@theia/editor/lib/browser'; import { ILanguageClient } from '@theia/languages/lib/common'; +import { DiffUris } from '@theia/core/lib/browser/diff-uris'; +import URI from '@theia/core/lib/common/uri'; const HIGHLIGHTS_NOTIFICATION_TYPE = new NotificationType("sts/highlight"); @@ -14,44 +16,66 @@ const INLINE_BOOT_HINT_DECORATION_STYLE = new EditorDecorationStyle('inline-boot style.borderWidth = '1px'; }); -// const LINE_BOOT_HINT_DECORATION_STYLE = new EditorDecorationStyle('line-boot-hint-decoration', style => { -// style.content = '../../images/boot-icon.png'; -// style.display = 'block'; -// style.width = '10px'; -// style.height = '1em'; -// style.margin = '0 2px 0 0'; -// }); +const LINE_BOOT_HINT_DECORATION_STYLE = new EditorDecorationStyle('line-boot-hint-decoration', style => { + style.backgroundImage = 'url(../../images/boot-icon.png)'; + style.display = 'block'; + style.width = '10px'; + style.height = '1em'; + style.margin = '0 2px 0 0'; +}); @injectable() export class HighlightService { + protected readonly appliedDecorations = new Map(); + constructor( - @inject(EditorDecorationsService) protected readonly decorationService: EditorDecorationsService + @inject(EditorManager) protected readonly editorManager: EditorManager ) {} attach(client: ILanguageClient) { client.onNotification(HIGHLIGHTS_NOTIFICATION_TYPE, (params) => this.highlight(params)); } - highlight(params: HighlightParams) { - const decorationParams: SetDecorationParams = { - uri: params.doc.uri, - kind: BOOT_LIVE_HINTS, - newDecorations: params.ranges.map(r => { - return { - range: Range.create(r.start.line, r.start.character, r.end.line, r.end.character), - options: { - inlineClassName: INLINE_BOOT_HINT_DECORATION_STYLE.className, - // linesDecorationsClassName: LINE_BOOT_HINT_DECORATION_STYLE.className, - hoverMessage: 'Ho-ho, Boot Hint!', - isWholeLine: false + async highlight(params: HighlightParams) { + const editor = await this.findEditorByUri(params.doc.uri); + if (editor) { + const decorationParams: SetDecorationParams = { + uri: params.doc.uri, + kind: BOOT_LIVE_HINTS, + newDecorations: params.ranges.map(r => { + return { + range: Range.create(r.start.line, r.start.character, r.end.line, r.end.character), + options: { + inlineClassName: INLINE_BOOT_HINT_DECORATION_STYLE.className, + glyphMarginClassName: LINE_BOOT_HINT_DECORATION_STYLE.className, + hoverMessage: 'Ho-ho, Boot Hint!', + isWholeLine: false + } } - } - }) - }; - this.decorationService.setDecorations(decorationParams); + }) + }; + const key = `${params.doc.uri}`; + const oldDecorations = this.appliedDecorations.get(key) || []; + const appliedDecorations = editor.deltaDecorations({oldDecorations, ...decorationParams}); + this.appliedDecorations.set(key, appliedDecorations); + } } + private async findEditorByUri(uri: string): Promise { + const editorWidget = await this.editorManager.getByUri(new URI(uri)); + if (editorWidget) { + const editorUri = editorWidget.editor.uri; + const openedInEditor = editorUri.toString() === uri; + const openedInDiffEditor = DiffUris.isDiffUri(editorUri) && DiffUris.decode(editorUri).some(u => u.toString() === uri); + if (openedInEditor || openedInDiffEditor) { + return editorWidget.editor; + } + } + return undefined; + } + + } export interface HighlightParams { 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 79c6db294..99d690890 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 @@ -8,8 +8,8 @@ import { } from '../common'; 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 { HighlightService} from './highlight-service'; +import { ClasspathService } from './classpath-service'; @injectable() export class SpringBootClientContribution extends BaseLanguageClientContribution { 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 63696352d..c07f97fb2 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 @@ -1,11 +1,11 @@ +import '../../images/boot-icon.png'; import { SpringBootClientContribution} from './language-client-contribution'; import { LanguageClientContribution } from '@theia/languages/lib/browser'; import { ContainerModule } from 'inversify'; - import './monaco-yaml-contribution'; import './monaco-properties-contribution'; -import {HighlightService} from "./highlight-service"; -import {ClasspathService} from "./classpath-service"; +import { HighlightService } from './highlight-service'; +import { ClasspathService } from './classpath-service'; export default new ContainerModule(bind => { // add your contribution bindings here