Skip to content

Commit 804129a

Browse files
committed
Add tests and update a bunch of imports
1 parent d2c7cff commit 804129a

File tree

7 files changed

+114
-40
lines changed

7 files changed

+114
-40
lines changed

src/extension.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import * as vscode from 'vscode';
2+
import { ExtensionContext } from 'vscode';
23
import { generateSnippet } from './lib/textHelpers';
34

4-
export function activate(context: vscode.ExtensionContext) {
5+
export function activate(context: ExtensionContext) {
56
let disposable = vscode.commands.registerTextEditorCommand('snippet-copy.copyWithoutLeadingIndentation', async (editor) => {
6-
const snippet = generateSnippet(editor);
7+
const snippet = generateSnippet(editor.document, editor.selections);
78

89
await vscode.env.clipboard.writeText(snippet);
910
});

src/lib/documentHelpers.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TextDocument, EndOfLine, TextLine, Range } from "vscode";
1+
import { EndOfLine, Range, TextDocument, TextLine } from "vscode";
22

33
export function linesForIndexes(document: TextDocument, lineIndexes: number[]): TextLine[] {
44
return lineIndexes.map((lineIndex) => {
@@ -26,7 +26,11 @@ export function contentOfLinesWithAdjustedIndentation(document: TextDocument, li
2626
return contentOfLinesWithAdjustedIndentation.join(eolCharacter);
2727
}
2828

29-
export function adjustedRangeWithMinimumIndentation(range: Range, minimumIndentation: number) {
29+
export function adjustedRangeWithMinimumIndentation(range: Range, minimumIndentation: number): Range {
30+
if (range.start.character !== 0) {
31+
console.warn('Adjusting range: Range does not start at character 0, this is not expected.');
32+
}
33+
3034
const adjustedRange = new Range(range.start.line, range.start.character + minimumIndentation, range.end.line, range.end.character);
3135
return adjustedRange;
3236
}

src/lib/textHelpers.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { TextDocument, Selection, TextEditor } from "vscode";
1+
import { Selection, TextDocument } from "vscode";
2+
import { contentOfLinesWithAdjustedIndentation, endOfLineCharacter, minimumIndentationForLineIndexes } from "./documentHelpers";
23
import { lineIndexesForSelection } from "./selectionHelpers";
3-
import { endOfLineCharacter, minimumIndentationForLineIndexes, contentOfLinesWithAdjustedIndentation } from "./documentHelpers";
4-
5-
export function generateSnippet(editor: TextEditor): string {
6-
const document = editor.document;
74

5+
export function generateSnippet(document: TextDocument, selections: Selection[]): string {
86
let texts: string[] = [];
9-
editor.selections.forEach(selection => {
7+
selections.forEach(selection => {
108
texts.push(generateCopyableText(document, selection));
119
});
1210

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
function doSomething(aValue) {
2-
if (aValue) {
3-
console.log(`Doing something with ${aValue}!`);
1+
class MyThing {
2+
doSomething(aValue) {
3+
if (aValue) {
4+
console.log(`Doing something with ${aValue}!`);
5+
}
6+
}
7+
8+
doSomethingElse() {
9+
throw new Error('Nope!');
410
}
511
}
Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,50 @@
11
import * as assert from 'assert';
22
import * as path from 'path';
33
import * as vscode from 'vscode';
4+
import { Range, TextDocument } from 'vscode';
5+
import { adjustedRangeWithMinimumIndentation, contentOfLinesWithAdjustedIndentation, endOfLineCharacter, linesForIndexes, minimumIndentationForLineIndexes } from '../../../lib/documentHelpers';
46

5-
import { minimumIndentationForLineIndexes, contentOfLinesWithAdjustedIndentation } from '../../../lib/documentHelpers';
67

78
const fixturesPath = '/../../../../src/test/fixtures/';
89
const uri = vscode.Uri.file(
910
path.join(__dirname + fixturesPath + 'javascript-example.js')
1011
);
1112

1213
describe('Document Helpers', function () {
13-
context('minimumIndentationLevelForLineIndexes', async () => {
14-
let document: vscode.TextDocument;
14+
let document: TextDocument;
15+
16+
before(async () => {
17+
document = await vscode.workspace.openTextDocument(uri);
18+
});
1519

16-
before(async () => {
17-
document = await vscode.workspace.openTextDocument(uri);
20+
context('linesForIndexes', () => {
21+
it('returns the correct lines', () => {
22+
const lines = linesForIndexes(document, [0, 1]);
23+
assert.equal(lines.length, 2);
24+
assert.equal(lines[0].lineNumber, 0);
25+
assert.equal(lines[0].text, 'class MyThing {');
26+
assert.equal(lines[1].lineNumber, 1);
27+
assert.equal(lines[1].text, ' doSomething(aValue) {');
1828
});
29+
});
1930

31+
context('minimumIndentationLevelForLineIndexes', async () => {
2032
it('calculates the correct minimum indentation level for a single line', () => {
21-
assert.equal(4, minimumIndentationForLineIndexes(document, [2]));
33+
assert.equal(minimumIndentationForLineIndexes(document, [3]), 6);
2234
});
2335

2436
it('calculates the correct minimum indentation level for multiple lines', () => {
25-
assert.equal(2, minimumIndentationForLineIndexes(document, [1, 2, 3]));
37+
assert.equal(minimumIndentationForLineIndexes(document, [1, 2, 3]), 2);
2638
});
2739
});
2840

2941
context('contentOfLinesWithAdjustedIndentation', async () => {
30-
let document: vscode.TextDocument;
31-
32-
before(async () => {
33-
document = await vscode.workspace.openTextDocument(uri);
34-
});
35-
3642
it('returns multiline text with the indentation adjusted correctly', () => {
37-
assert.equal('if (aValue) {\n console.log(`Doing something with ${aValue}!`);\n}', contentOfLinesWithAdjustedIndentation(document, [1, 2, 3], 2));
43+
assert.equal(contentOfLinesWithAdjustedIndentation(document, [2, 3, 4], 4), 'if (aValue) {\n console.log(`Doing something with ${aValue}!`);\n}');
3844
});
3945

4046
it('returns single line text with the indentation adjusted correctly', () => {
41-
assert.equal('console.log(`Doing something with ${aValue}!`);', contentOfLinesWithAdjustedIndentation(document, [2], 4));
47+
assert.equal(contentOfLinesWithAdjustedIndentation(document, [3], 6), 'console.log(`Doing something with ${aValue}!`);');
4248
});
4349

4450
it('returns text with CRLF characters if file is using them', async () => {
@@ -47,7 +53,30 @@ describe('Document Helpers', function () {
4753
);
4854
let crlfDocument = await vscode.workspace.openTextDocument(uri);
4955

50-
assert.equal('def polish\r\n puts "Polishing"\r\nend', contentOfLinesWithAdjustedIndentation(crlfDocument, [1, 2, 3], 2));
56+
assert.equal(contentOfLinesWithAdjustedIndentation(crlfDocument, [1, 2, 3], 2), 'def polish\r\n puts "Polishing"\r\nend');
57+
});
58+
});
59+
60+
context('adjustedRangeWithMinimumIndentation', () => {
61+
it('adjusts the range', () => {
62+
const adjustedRange = adjustedRangeWithMinimumIndentation(new Range(2, 0, 2, 17), 4);
63+
assert.equal(adjustedRange.start.line, 2);
64+
assert.equal(adjustedRange.start.character, 4);
65+
assert.equal(adjustedRange.end.line, 2);
66+
assert.equal(adjustedRange.end.character, 17);
67+
});
68+
});
69+
70+
context('endOfLineCharacter', () => {
71+
it('correctly returns LF', async () => {
72+
assert.equal(endOfLineCharacter(document), '\n');
73+
});
74+
75+
it('correctly returns CRLF', async () => {
76+
const uri = vscode.Uri.file(
77+
path.join(__dirname + fixturesPath + 'crlf-ruby-example.rb')
78+
);
79+
assert.equal(endOfLineCharacter(await vscode.workspace.openTextDocument(uri)), '\r\n');
5180
});
5281
});
5382
});
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import * as assert from 'assert';
2-
import * as vscode from 'vscode';
3-
2+
import { Selection } from 'vscode';
43
import { lineIndexesForSelection } from '../../../lib/selectionHelpers';
54

5+
66
describe('Selection Helpers', function () {
77
context('calculateSelectionLineIndexes', () => {
88
it('calculates the correct line indexes from an empty selection', () => {
9-
assert.deepEqual([0], lineIndexesForSelection(new vscode.Selection(0, 0, 0, 0)));
9+
assert.deepEqual([0], lineIndexesForSelection(new Selection(0, 0, 0, 0)));
1010
});
1111

1212
it('calculates the correct line indexes from a single line selection', () => {
13-
assert.deepEqual([1], lineIndexesForSelection(new vscode.Selection(1, 2, 1, 15)));
13+
assert.deepEqual([1], lineIndexesForSelection(new Selection(1, 2, 1, 15)));
1414
});
1515

1616
it('calculates the correct line indexes from a multiline selection', () => {
17-
assert.deepEqual([1, 2, 3], lineIndexesForSelection(new vscode.Selection(1, 2, 3, 3)));
17+
assert.deepEqual([1, 2, 3], lineIndexesForSelection(new Selection(1, 2, 3, 3)));
1818
});
1919

2020
it('calculates the correct line indexes from an reversed selection ', () => {
21-
assert.deepEqual([1, 2, 3], lineIndexesForSelection(new vscode.Selection(3, 0, 1, 0)));
21+
assert.deepEqual([1, 2, 3], lineIndexesForSelection(new Selection(3, 0, 1, 0)));
2222
});
2323
});
2424
});
Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,64 @@
11
import * as assert from 'assert';
22
import * as path from 'path';
33
import * as vscode from 'vscode';
4-
5-
import { generateCopyableText } from '../../../lib/textHelpers';
4+
import { Position, Selection, TextDocument } from 'vscode';
5+
import { generateCopyableText, generateSnippet } from '../../../lib/textHelpers';
66

77
const fixturesPath = '/../../../../src/test/fixtures/';
88
const uri = vscode.Uri.file(
99
path.join(__dirname + fixturesPath + 'javascript-example.js')
1010
);
1111

12+
interface TestSelection {
13+
selection: Selection;
14+
content: string;
15+
}
16+
1217
describe('Text Helpers', () => {
13-
let document: vscode.TextDocument;
18+
const testSelection1: TestSelection = {
19+
content: 'if (aValue) {\n console.log(`Doing something with ${aValue}!`);\n}',
20+
selection: new Selection(2, 4, 4, 5)
21+
};
22+
const testSelection2: TestSelection = {
23+
content: 'doSomethingElse() {\n throw new Error(\'Nope!\');\n}',
24+
selection: new Selection(7, 2, 9, 3)
25+
};
26+
let document: TextDocument;
1427

1528
before(async () => {
1629
document = await vscode.workspace.openTextDocument(uri);
1730
});
1831

32+
context('generateSnippet', () => {
33+
it('generates the correct snippet for a single selection', () => {
34+
assert.deepEqual(testSelection1.content, generateSnippet(document, [testSelection1.selection]));
35+
});
36+
37+
it('generates the correct snippet for multiple selections', () => {
38+
assert.deepEqual(testSelection1.content + '\n' + testSelection2.content,
39+
generateSnippet(document, [testSelection1.selection, testSelection2.selection])
40+
);
41+
});
42+
43+
it('generates the correct snippet for multiple selections where one ends on the beginning of a newline', () => {
44+
assert.deepEqual(testSelection1.content + '\n' + testSelection2.content,
45+
generateSnippet(document, [
46+
new Selection(testSelection1.selection.start, new Position(5, 0)),
47+
testSelection2.selection
48+
])
49+
);
50+
});
51+
});
52+
1953
context('generateCopyableText', () => {
2054
it('generates the correct text', () => {
21-
assert.deepEqual('if (aValue) {\n console.log(`Doing something with ${aValue}!`);\n}', generateCopyableText(document, new vscode.Selection(1, 2, 3, 3)));
55+
assert.deepEqual(testSelection1.content, generateCopyableText(document, testSelection1.selection));
2256
});
2357

2458
it('generates the correct text when the cursor is on a newline', () => {
25-
assert.deepEqual('if (aValue) {\n console.log(`Doing something with ${aValue}!`);\n}', generateCopyableText(document, new vscode.Selection(1, 2, 4, 0)));
59+
assert.deepEqual(testSelection1.content,
60+
generateCopyableText(document, new Selection(testSelection1.selection.start, new Position(5, 0)))
61+
);
2662
});
2763
});
2864
});

0 commit comments

Comments
 (0)