Skip to content

Commit babcd71

Browse files
authored
Merge pull request #32 from lumaxis/feature/improve-empty-lines-handling
Improve handling of empty or whitespace-only lines
2 parents d95a00d + ec3e591 commit babcd71

7 files changed

+39
-3
lines changed

.eslintrc.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"semi": "off",
1616
"indent": ["error", "tab"]
1717
},
18+
"ignorePatterns": ["**/fixtures/**"],
1819
"overrides": [
1920
{
2021
"files": ["**/*.ts"],

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## [Unreleased]
4+
5+
### Fixes
6+
7+
- Ignore empty or whitespace-only lines when calculating minimum indentation of the snippet (#17)
8+
39
## [0.2.0]
410

511
### Features

src/lib/documentHelpers.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@ export function linesForIndexes(document: TextDocument, lineIndexes: number[]):
77
}
88

99
export function minimumIndentationForLineIndexes(document: TextDocument, lineIndexes: number[]): number {
10-
const indentationLevels = lineIndexes.map((lineIndex) => {
11-
return document.lineAt(lineIndex).firstNonWhitespaceCharacterIndex;
12-
});
10+
const indentationLevels = lineIndexes.reduce<number[]>((indentationLevels, lineIndex) => {
11+
const line = document.lineAt(lineIndex);
12+
13+
// Skip empty lines so they don't skew the calculation results
14+
if (line.isEmptyOrWhitespace) {
15+
return indentationLevels;
16+
}
17+
18+
indentationLevels.push(line.firstNonWhitespaceCharacterIndex);
19+
20+
return indentationLevels;
21+
}, []);
1322

1423
const minimumIndentationLevelInSelection = Math.min(...indentationLevels);
1524
return minimumIndentationLevelInSelection;

src/test/fixtures/.editorconfig

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
root = true

src/test/fixtures/javascript-example.js

+5
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ class MyThing {
88
doSomethingElse() {
99
throw new Error('Nope!');
1010
}
11+
12+
emptyFunction() {
13+
14+
15+
}
1116
}

src/test/suite/lib/documentHelpers.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ describe('Document Helpers', function () {
3636
it('calculates the correct minimum indentation level for multiple lines', () => {
3737
assert.equal(minimumIndentationForLineIndexes(document, [1, 2, 3]), 2);
3838
});
39+
40+
it('calculates the correct minimum indentation level when lines contain an empty line', () => {
41+
assert.equal(minimumIndentationForLineIndexes(document, [11, 12, 13, 14]), 4);
42+
});
3943
});
4044

4145
context('contentOfLinesWithAdjustedIndentation', async () => {

src/test/suite/lib/textHelpers.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ describe('Text Helpers', () => {
2323
content: 'doSomethingElse() {\n throw new Error(\'Nope!\');\n}',
2424
selection: new Selection(7, 2, 9, 3)
2525
};
26+
const testSelection3: TestSelection = {
27+
content: '}\n\ndoSomethingElse() {',
28+
selection: new Selection(5, 0, 7, 21)
29+
};
2630
let document: TextDocument;
2731

2832
before(async () => {
@@ -61,6 +65,12 @@ describe('Text Helpers', () => {
6165
);
6266
});
6367

68+
it('generates the correct text when selection contains empty line', () => {
69+
assert.deepEqual(testSelection3.content,
70+
generateCopyableText(document, testSelection3.selection)
71+
);
72+
});
73+
6474
context('wrapTextInMarkdownCodeBlock', () => {
6575
it('returns the text wrapped in a Markdown code block', () => {
6676
const codeSnippet = 'console.log("Yo");';

0 commit comments

Comments
 (0)