Skip to content

Commit

Permalink
fix: correct the selection range of the last import that doesn't have…
Browse files Browse the repository at this point in the history
… a semicolon
  • Loading branch information
jasonlyu123 committed Jan 6, 2025
1 parent ac10174 commit 49605e3
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ts from 'typescript';
import { Position, Range, SelectionRange } from 'vscode-languageserver';
import { Document, mapSelectionRangeToParent } from '../../../lib/documents';
import { Document, mapRangeToOriginal } from '../../../lib/documents';
import { SelectionRangeProvider } from '../../interfaces';
import { SvelteDocumentSnapshot } from '../DocumentSnapshot';
import { LSAndTSDocResolver } from '../LSAndTSDocResolver';
Expand All @@ -20,7 +20,7 @@ export class SelectionRangeProviderImpl implements SelectionRangeProvider {
tsDoc.offsetAt(tsDoc.getGeneratedPosition(position))
);
const selectionRange = this.toSelectionRange(tsDoc, tsSelectionRange);
const mappedRange = mapSelectionRangeToParent(tsDoc, selectionRange);
const mappedRange = this.mapSelectionRangeToParent(tsDoc, document, selectionRange);

return this.filterOutUnmappedRange(mappedRange);
}
Expand All @@ -35,6 +35,35 @@ export class SelectionRangeProviderImpl implements SelectionRangeProvider {
};
}

private mapSelectionRangeToParent(
tsDoc: SvelteDocumentSnapshot,
document: Document,
selectionRange: SelectionRange
): SelectionRange {
const { range, parent } = selectionRange;
const originalRange = mapRangeToOriginal(tsDoc, range);

const originalLength = originalRange.end.character - originalRange.start.character;
const generatedLength = range.end.character - range.start.character;

// sourcemap off by one character issue + a generated semicolon
if (
originalLength === generatedLength - 2 &&
tsDoc.getFullText()[tsDoc.offsetAt(range.end) - 1] === ';'
) {
originalRange.end.character += 1;
}

if (!parent) {
return SelectionRange.create(originalRange);
}

return SelectionRange.create(
originalRange,
this.mapSelectionRangeToParent(tsDoc, document, parent)
);
}

private filterOutUnmappedRange(selectionRange: SelectionRange): SelectionRange | null {
const flattened = this.flattenAndReverseSelectionRange(selectionRange);
const filtered = flattened.filter((range) => range.start.line > 0 && range.end.line > 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@ const selectionRangeTestDir = path.join(testDir, 'testfiles', 'selection-range')
describe('SelectionRangeProvider', function () {
serviceWarmup(this, selectionRangeTestDir, pathToUrl(testDir));

function setup() {
function setup(fileName: string) {
const docManager = new DocumentManager(
(textDocument) => new Document(textDocument.uri, textDocument.text)
);
const filePath = path.join(
testDir,
'testfiles',
'selection-range',
'selection-range.svelte'
);
const filePath = path.join(testDir, 'testfiles', 'selection-range', fileName);
const lsAndTsDocResolver = new LSAndTSDocResolver(
docManager,
[pathToUrl(testDir)],
Expand All @@ -39,7 +34,7 @@ describe('SelectionRangeProvider', function () {
}

it('provides selection range', async () => {
const { provider, document } = setup();
const { provider, document } = setup('selection-range.svelte');

const selectionRange = await provider.getSelectionRange(document, Position.create(1, 9));

Expand Down Expand Up @@ -72,8 +67,67 @@ describe('SelectionRangeProvider', function () {
});
});

it('provides selection range for import without semicolon', async () => {
const { provider, document } = setup('selection-range-import.svelte');

const selectionRange = await provider.getSelectionRange(document, Position.create(2, 28));

assert.deepStrictEqual(selectionRange, <SelectionRange>{
parent: {
parent: {
parent: {
parent: undefined,
range: {
end: {
character: 34,
line: 2
},
start: {
character: 4,
line: 1
}
}
},
// import {onMount} from 'svelte';
range: {
end: {
character: 34,
line: 2
},
start: {
character: 4,
line: 2
}
}
},
// 'svelte';
range: {
end: {
character: 34,
line: 2
},
start: {
character: 26,
line: 2
}
}
},
// svelte
range: {
end: {
character: 33,
line: 2
},
start: {
character: 27,
line: 2
}
}
});
});

it('return null when in style', async () => {
const { provider, document } = setup();
const { provider, document } = setup('selection-range.svelte');

const selectionRange = await provider.getSelectionRange(document, Position.create(5, 0));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script>
import {} from 'svelte'
import {onMount} from 'svelte'
</script>

0 comments on commit 49605e3

Please sign in to comment.