Skip to content

Commit

Permalink
Only update when indenttion changes.
Browse files Browse the repository at this point in the history
Closes #3
  • Loading branch information
JoeRobich committed Dec 21, 2015
1 parent bb79005 commit 03b1d6e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
34 changes: 16 additions & 18 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
Expand All @@ -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[] {
Expand All @@ -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();
Expand Down

0 comments on commit 03b1d6e

Please sign in to comment.