|
| 1 | +/* -------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) TypeFox. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + * ------------------------------------------------------------------------------------------ */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +import { Disposable, Uri, Range, window, DecorationRenderOptions, TextEditorDecorationType, workspace, TextEditor } from 'vscode'; |
| 8 | +import { |
| 9 | + TextDocumentRegistrationOptions, ClientCapabilities, ServerCapabilities, DocumentSelector, NotificationHandler, |
| 10 | + SemanticHighlightingNotification, SemanticHighlightingParams, SemanticHighlightingInformation |
| 11 | +} from 'vscode-languageserver-protocol'; |
| 12 | + |
| 13 | +import * as UUID from './utils/uuid'; |
| 14 | +import { TextDocumentFeature, BaseLanguageClient } from './client'; |
| 15 | + |
| 16 | +export class SemanticHighlightingFeature extends TextDocumentFeature<TextDocumentRegistrationOptions> { |
| 17 | + |
| 18 | + protected readonly toDispose: Disposable[]; |
| 19 | + protected readonly decorations: Map<string, any>; |
| 20 | + protected readonly handlers: NotificationHandler<SemanticHighlightingParams>[]; |
| 21 | + |
| 22 | + constructor(client: BaseLanguageClient) { |
| 23 | + super(client, SemanticHighlightingNotification.type); |
| 24 | + this.toDispose = []; |
| 25 | + this.decorations = new Map(); |
| 26 | + this.handlers = []; |
| 27 | + this.toDispose.push({ dispose: () => this.decorations.clear() }); |
| 28 | + this.toDispose.push(workspace.onDidCloseTextDocument(e => { |
| 29 | + const uri = e.uri.toString(); |
| 30 | + if (this.decorations.has(uri)) { |
| 31 | + // TODO: do the proper disposal of the decorations. |
| 32 | + this.decorations.delete(uri); |
| 33 | + } |
| 34 | + })); |
| 35 | + } |
| 36 | + |
| 37 | + dispose(): void { |
| 38 | + this.toDispose.forEach(disposable => disposable.dispose()); |
| 39 | + super.dispose(); |
| 40 | + } |
| 41 | + |
| 42 | + fillClientCapabilities(capabilities: ClientCapabilities): void { |
| 43 | + if (!!capabilities.textDocument) { |
| 44 | + capabilities.textDocument = {}; |
| 45 | + } |
| 46 | + capabilities.textDocument!.semanticHighlighting = true; |
| 47 | + } |
| 48 | + |
| 49 | + initialize(capabilities: ServerCapabilities, documentSelector: DocumentSelector): void { |
| 50 | + console.log('TODO: Currently, the server capabilities are ignored.', capabilities); |
| 51 | + if (documentSelector) { |
| 52 | + const id = UUID.generateUuid(); |
| 53 | + this.register(this.messages, { |
| 54 | + id, |
| 55 | + registerOptions: Object.assign({}, { documentSelector }) |
| 56 | + }); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + protected registerLanguageProvider(options: TextDocumentRegistrationOptions): Disposable { |
| 61 | + if (options.documentSelector === null) { |
| 62 | + return new Disposable(() => { }); |
| 63 | + } |
| 64 | + const handler = this.newNotificationHandler.bind(this)(); |
| 65 | + this._client.onNotification(SemanticHighlightingNotification.type, handler); |
| 66 | + return new Disposable(() => { |
| 67 | + const indexOf = this.handlers.indexOf(handler); |
| 68 | + if (indexOf !== -1) { |
| 69 | + this.handlers.splice(indexOf, 1); |
| 70 | + } |
| 71 | + }) |
| 72 | + } |
| 73 | + |
| 74 | + protected newNotificationHandler(): NotificationHandler<SemanticHighlightingParams> { |
| 75 | + return (params: SemanticHighlightingParams) => { |
| 76 | + const editorPredicate = this.editorPredicate(params.uri); |
| 77 | + window.visibleTextEditors.filter(editorPredicate).forEach(editor => this.applyDecorations(editor, params)); |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + protected editorPredicate(uri: string): (editor: TextEditor) => boolean { |
| 82 | + const predicateUri = Uri.parse(uri); |
| 83 | + return (editor: TextEditor) => editor.document.uri.toString() === predicateUri.toString(); |
| 84 | + } |
| 85 | + |
| 86 | + protected applyDecorations(editor: TextEditor, params: SemanticHighlightingParams): void { |
| 87 | + console.log('TODO: Apply the decorations on the editor.', editor, params); |
| 88 | + } |
| 89 | + |
| 90 | + protected decorationType(options: DecorationRenderOptions = {}) { |
| 91 | + return window.createTextEditorDecorationType(options); |
| 92 | + } |
| 93 | + |
| 94 | + protected map2Decoration(lines: SemanticHighlightingInformation[]): [TextEditorDecorationType, Range[]] { |
| 95 | + console.log('TODO: Map the lines (and the tokens) to the desired decoration type.', lines); |
| 96 | + return [this.decorationType(), []]; |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments