theia-spring-boot: avoid using EditorDecorationService

This commit is contained in:
BoykoAlex
2018-05-09 22:45:17 -04:00
parent 1ce02ff18b
commit f56c0fe59b
3 changed files with 54 additions and 30 deletions

View File

@@ -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<HighlightParams,void>("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<string, string[]>();
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(<DeltaDecorationParams & SetDecorationParams>{oldDecorations, ...decorationParams});
this.appliedDecorations.set(key, appliedDecorations);
}
}
private async findEditorByUri(uri: string): Promise<TextEditor | undefined> {
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 {

View File

@@ -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 {

View File

@@ -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