Skip to content

Commit

Permalink
[folding] fix indent computation with tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
aeschli committed Feb 25, 2016
1 parent e68a6bd commit 78b557a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/vs/editor/contrib/folding/common/indentFoldStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,15 @@ export function computeRanges(model: IModel, tabSize: number, minimumRangeSize:
}


function computeIndentLevel(line: string, tabSize: number): number {
export function computeIndentLevel(line: string, tabSize: number): number {
let i = 0;
let indent = 0;
while (i < line.length) {
let ch = line.charAt(i);
if (ch === ' ') {
indent++;
} else if (ch === '\t') {
indent++;
indent += (indent % tabSize);
indent = indent - indent % tabSize + tabSize;
} else {
break;
}
Expand Down
29 changes: 28 additions & 1 deletion src/vs/editor/contrib/folding/test/indentFold.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import * as assert from 'assert';
import {Model} from 'vs/editor/common/model/model';
import {IFoldingRange} from 'vs/editor/contrib/folding/common/foldingRange';
import {computeRanges, limitByIndent} from 'vs/editor/contrib/folding/common/indentFoldStrategy';
import {computeRanges, limitByIndent, computeIndentLevel} from 'vs/editor/contrib/folding/common/indentFoldStrategy';
import {DefaultEndOfLine} from 'vs/editor/common/editorCommon';

suite('Indentation Folding', () => {
Expand Down Expand Up @@ -102,6 +102,19 @@ suite('Indentation Folding', () => {
], 4, [r(1, 7, 0), r(3, 5, 2)] );
});

test('Fold Tabs', () => {
assertRanges([
/* 1*/ 'class A {',
/* 2*/ '\t\t',
/* 3*/ '\tvoid foo() {',
/* 4*/ '\t \t//hello',
/* 5*/ '\t return 0;',
/* 6*/ ' \t}',
/* 7*/ ' ',
/* 8*/ '}',
], 4, [r(1, 7, 0), r(3, 5, 4)] );
});

test('Limit By indent', () => {
let ranges = [r(1, 4, 0), r(3, 4, 2), r(5, 8, 0), r(6, 7, 1), r(9, 15, 0), r(10, 15, 10), r(11, 12, 2000), r(14, 15, 2000)];
assert.deepEqual(limitByIndent(ranges, 8), [r(1, 4, 0), r(3, 4, 2), r(5, 8, 0), r(6, 7, 1), r(9, 15, 0), r(10, 15, 10), r(11, 12, 2000), r(14, 15, 2000)]);
Expand All @@ -115,4 +128,18 @@ suite('Indentation Folding', () => {
assert.deepEqual(limitByIndent(ranges, 0), []);
});

test('Compute indent level', () => {
assert.equal(computeIndentLevel("Hello", 4), 0);
assert.equal(computeIndentLevel(" Hello", 4), 1);
assert.equal(computeIndentLevel(" Hello", 4), 3);
assert.equal(computeIndentLevel("\tHello", 4), 4);
assert.equal(computeIndentLevel(" \tHello", 4), 4);
assert.equal(computeIndentLevel(" \tHello", 4), 4);
assert.equal(computeIndentLevel(" \tHello", 4), 4);
assert.equal(computeIndentLevel(" \tHello", 4), 8);
assert.equal(computeIndentLevel(" \tHello", 4), 8);
assert.equal(computeIndentLevel("\t Hello", 4), 5);
assert.equal(computeIndentLevel("\t \tHello", 4), 8);
});

});

0 comments on commit 78b557a

Please sign in to comment.