From 03b1d6eeaf44cfe56e5e0db2400bb3e98e21598d Mon Sep 17 00:00:00 2001 From: Joey Robichaud Date: Mon, 21 Dec 2015 11:19:52 -0500 Subject: [PATCH] Only update when indenttion changes. Closes #3 --- package.json | 2 +- src/extension.ts | 34 ++++++++++++++++------------------ 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 76f3f70..27aa922 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "indent-guides", "displayName": "Indent Guides", "description": "This extensions adds indent guides to the editor.", - "version": "0.0.3", + "version": "0.0.4", "publisher": "JoeyRobichaud", "icon": "/images/icon.png", "galleryBanner": { diff --git a/src/extension.ts b/src/extension.ts index 238657f..9cbc048 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,12 +1,12 @@ import {workspace, window, commands, Position, Range, TextLine, TextDocument, TextEditor, TextEditorDecorationType, ExtensionContext, Disposable} from 'vscode'; -import {range} from 'lodash'; +import {range, debounce} from 'lodash'; export function activate(context:ExtensionContext) { let guideDecorator = new GuideDecorator(); // Hook Events let subscriptions:Disposable[] = []; - window.onDidChangeTextEditorSelection(guideDecorator.updateActiveEditor, guideDecorator, subscriptions); + workspace.onDidChangeTextDocument(debounce(onEditorChange.bind(null, guideDecorator), 50), this, subscriptions); window.onDidChangeActiveTextEditor(guideDecorator.updateActiveEditor, guideDecorator, subscriptions); workspace.onDidChangeConfiguration(guideDecorator.reset, guideDecorator, subscriptions); let eventDisposable = Disposable.from(...subscriptions); @@ -18,9 +18,19 @@ export function activate(context:ExtensionContext) { guideDecorator.reset(); } +function onEditorChange(guideDecorator:GuideDecorator) { + let activeEditor = window.activeTextEditor; + let cursorPosition = activeEditor.selection.active; + let cursorLine = activeEditor.document.lineAt(cursorPosition.line); + let indentation = cursorLine.isEmptyOrWhitespace ? cursorLine.text.length : cursorLine.firstNonWhitespaceCharacterIndex; + + if (!activeEditor.selection.isEmpty || // Change to large area, possibly a snippet. + cursorPosition.character <= indentation) // Change within the indentation area. + guideDecorator.updateActiveEditor(); +} + class GuideDecorator { private _indentGuide:TextEditorDecorationType = null; - private _lastDocumentId:string = ""; public updateActiveEditor():void { this.updateIndentGuides(window.activeTextEditor); @@ -32,14 +42,14 @@ class GuideDecorator { } updateIndentGuides(editor:TextEditor):void { - if (this.doesEditorNeedUpdating(editor)) + if (editor === null) return; - let ranges:Range[] = this.getIndentedLines(editor.document) + let guideStops:Range[] = this.getIndentedLines(editor.document) .map(line => this.getGuideStops(line, editor.options.tabSize)) .reduce((all, ranges) => all.concat(ranges), []); - editor.setDecorations(this._indentGuide, ranges); + editor.setDecorations(this._indentGuide, guideStops); } getIndentedLines(document:TextDocument):TextLine[] { @@ -58,18 +68,6 @@ class GuideDecorator { .map(position => new Range(position, position)); } - doesEditorNeedUpdating(editor:TextEditor):boolean { - if (!editor) - return false; - - let documentId = `${editor.document.fileName}:${editor.document.version}`; - if (documentId === this._lastDocumentId) - return false; - - this._lastDocumentId = documentId; - return true; - } - public reset() { this.dispose(); this._indentGuide = this.createIndentGuideDecoration();