|  | 
|  | 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. | 
|  | 2 | +// Licensed under the MIT License. | 
|  | 3 | +'use strict'; | 
|  | 4 | +import { inject, injectable } from 'inversify'; | 
|  | 5 | +import * as vscode from 'vscode'; | 
|  | 6 | + | 
|  | 7 | +import { IExtensionActivationService } from '../../activation/types'; | 
|  | 8 | +import { IDocumentManager } from '../../common/application/types'; | 
|  | 9 | +import { PYTHON_LANGUAGE } from '../../common/constants'; | 
|  | 10 | +import { IConfigurationService, IDisposable, IDisposableRegistry, Resource } from '../../common/types'; | 
|  | 11 | +import { generateCellRanges } from '../cellFactory'; | 
|  | 12 | + | 
|  | 13 | +@injectable() | 
|  | 14 | +export class Decorator implements IExtensionActivationService, IDisposable { | 
|  | 15 | + | 
|  | 16 | + private activeCellType: vscode.TextEditorDecorationType; | 
|  | 17 | + private timer: NodeJS.Timer | undefined; | 
|  | 18 | + | 
|  | 19 | + constructor(@inject(IDocumentManager) private documentManager: IDocumentManager, | 
|  | 20 | + @inject(IDisposableRegistry) disposables: IDisposableRegistry, | 
|  | 21 | + @inject(IConfigurationService) private configuration: IConfigurationService) | 
|  | 22 | + { | 
|  | 23 | + this.activeCellType = this.documentManager.createTextEditorDecorationType({ | 
|  | 24 | + backgroundColor: new vscode.ThemeColor('sideBarSectionHeader.background'), | 
|  | 25 | + isWholeLine: true | 
|  | 26 | + }); | 
|  | 27 | + disposables.push(this); | 
|  | 28 | + disposables.push(this.configuration.getSettings().onDidChange(this.settingsChanged, this)); | 
|  | 29 | + disposables.push(this.documentManager.onDidChangeActiveTextEditor(this.changedEditor, this)); | 
|  | 30 | + disposables.push(this.documentManager.onDidChangeTextEditorSelection(this.changedSelection, this)); | 
|  | 31 | + disposables.push(this.documentManager.onDidChangeTextDocument(this.changedDocument, this)); | 
|  | 32 | + this.settingsChanged(); | 
|  | 33 | + } | 
|  | 34 | + | 
|  | 35 | + public activate(_resource: Resource) : Promise<void> { | 
|  | 36 | + // We don't need to do anything here as we already did all of our work in the | 
|  | 37 | + // constructor. | 
|  | 38 | + return Promise.resolve(); | 
|  | 39 | + } | 
|  | 40 | + | 
|  | 41 | + public dispose() { | 
|  | 42 | + if (this.timer) { | 
|  | 43 | + clearTimeout(this.timer); | 
|  | 44 | + } | 
|  | 45 | + } | 
|  | 46 | + | 
|  | 47 | + private settingsChanged() { | 
|  | 48 | + if (this.documentManager.activeTextEditor) { | 
|  | 49 | + this.triggerUpdate(this.documentManager.activeTextEditor); | 
|  | 50 | + } | 
|  | 51 | + } | 
|  | 52 | + | 
|  | 53 | + private changedEditor(editor: vscode.TextEditor | undefined) { | 
|  | 54 | + this.triggerUpdate(editor); | 
|  | 55 | + } | 
|  | 56 | + | 
|  | 57 | + private changedDocument(e: vscode.TextDocumentChangeEvent) { | 
|  | 58 | + if (this.documentManager.activeTextEditor && e.document === this.documentManager.activeTextEditor.document) { | 
|  | 59 | + this.triggerUpdate(this.documentManager.activeTextEditor); | 
|  | 60 | + } | 
|  | 61 | + } | 
|  | 62 | + | 
|  | 63 | + private changedSelection(e: vscode.TextEditorSelectionChangeEvent) { | 
|  | 64 | + if (e.textEditor && e.textEditor.selection.anchor) { | 
|  | 65 | + this.triggerUpdate(e.textEditor); | 
|  | 66 | + } | 
|  | 67 | + } | 
|  | 68 | + | 
|  | 69 | + private triggerUpdate(editor: vscode.TextEditor | undefined) { | 
|  | 70 | + if (this.timer) { | 
|  | 71 | + clearTimeout(this.timer); | 
|  | 72 | + } | 
|  | 73 | + this.timer = setTimeout(() => this.update(editor), 100); | 
|  | 74 | + } | 
|  | 75 | + | 
|  | 76 | + private update(editor: vscode.TextEditor | undefined) { | 
|  | 77 | + if (editor && editor.document && editor.document.languageId === PYTHON_LANGUAGE) { | 
|  | 78 | + const settings = this.configuration.getSettings().datascience; | 
|  | 79 | + if (settings.decorateCells && settings.enabled) { | 
|  | 80 | + // Find all of the cells | 
|  | 81 | + const cells = generateCellRanges(editor.document, this.configuration.getSettings().datascience); | 
|  | 82 | + const cellRanges = cells.map(c => c.range).filter(r => r.contains(editor.selection.anchor)); | 
|  | 83 | + editor.setDecorations(this.activeCellType, cellRanges); | 
|  | 84 | + } else { | 
|  | 85 | + editor.setDecorations(this.activeCellType, []); | 
|  | 86 | + } | 
|  | 87 | + } | 
|  | 88 | + } | 
|  | 89 | +} | 
0 commit comments