Skip to content

Commit

Permalink
Workaround native API's bug where insertSpace and tabSize may be
Browse files Browse the repository at this point in the history
undefined.
  • Loading branch information
aioutecism committed Dec 6, 2016
1 parent 077b74d commit 585c6fa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
10 changes: 4 additions & 6 deletions src/Actions/Indent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {window, Range} from 'vscode';
import {StaticReflect} from '../LanguageExtensions/StaticReflect';
import {SymbolMetadata} from '../Symbols/Metadata';
import {Configuration} from '../Configuration';
import {RangeOffset} from '../Types/RangeOffset';
import {ActionSelection} from './Selection';
import {ActionMoveCursor} from './MoveCursor';
Expand All @@ -11,10 +12,8 @@ import {UtilText} from '../Utils/Text';
export class ActionIndent {

private static getIndentUnit(): string {
const options = window.activeTextEditor.options;

if (options.insertSpaces) {
return ' '.repeat(options.tabSize as number);
if (Configuration.getInsertSpace()) {
return ' '.repeat(Configuration.getTabSize() as number);
}
else {
return '\t';
Expand All @@ -28,8 +27,7 @@ export class ActionIndent {
return 0;
}

const options = window.activeTextEditor.options;
const tabSize = options.tabSize as number;
const tabSize = Configuration.getTabSize();

const line = document.lineAt(lineNumber);

Expand Down
30 changes: 29 additions & 1 deletion src/Configuration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {commands, workspace, WorkspaceConfiguration, Disposable} from 'vscode';
import {commands, window, workspace, WorkspaceConfiguration, Disposable} from 'vscode';
import {UtilWord} from './Utils/Word';

export class Configuration {
Expand Down Expand Up @@ -46,6 +46,34 @@ export class Configuration {
return this.editorNamespace.get(section, defaultValue);
}

/**
* Remarks: Workaround undefined bug in native API.
*/
static getInsertSpace(): boolean {
if (window.activeTextEditor) {
const options = window.activeTextEditor.options;
if (options.insertSpaces !== undefined) {
return options.insertSpaces as boolean;
}
}

return this.getEditorSetting<boolean>('insertSpaces');
}

/**
* Remarks: Workaround undefined bug in native API.
*/
static getTabSize(): number {
if (window.activeTextEditor) {
const options = window.activeTextEditor.options;
if (options.tabSize !== undefined) {
return options.tabSize as number;
}
}

return this.getEditorSetting<number>('tabSize');
}

static dispose(): void {
Disposable.from(...this.disposables).dispose();
}
Expand Down

0 comments on commit 585c6fa

Please sign in to comment.