Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(source-map): precision mapping #95

Merged
merged 2 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 103 additions & 57 deletions extensions/labs/src/common/showVirtualFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ export async function activate(info: ExportsInfoForLabs) {
if (!stacks) return;

const offset = document.offsetAt(position);
const stack = stacks.find(stack => stack[1][0] <= offset && offset <= stack[1][1]);
const stack = stacks.find(stack => stack.range[0] <= offset && offset <= stack.range[1]);
if (!stack) return;

const line = Number(stack[0].split(':').at(-2));
const character = Number(stack[0].split(':').at(-1));
const fileName = stack[0].split(':').slice(0, -2).join(':');
const line = Number(stack.source.split(':').at(-2));
const character = Number(stack.source.split(':').at(-1));
const fileName = stack.source.split(':').slice(0, -2).join(':');
const link: vscode.DefinitionLink = {
originSelectionRange: new vscode.Range(document.positionAt(stack[1][0]), document.positionAt(stack[1][1])),
originSelectionRange: new vscode.Range(document.positionAt(stack.range[0]), document.positionAt(stack.range[1])),
targetUri: vscode.Uri.file(fileName),
targetRange: new vscode.Range(line - 1, character - 1, line - 1, character - 1),
};
Expand Down Expand Up @@ -193,15 +193,21 @@ export async function activate(info: ExportsInfoForLabs) {
for (const [sourceUri, sourceVersion, map] of sources) {
const sourceEditor = vscode.window.visibleTextEditors.find(editor => editor.document.uri.toString() === sourceUri);
if (sourceEditor && sourceEditor.document.version === sourceVersion) {
const mappingDecorationRanges = map.codeMappings.map(mapping => new vscode.Range(
sourceEditor.document.positionAt(mapping[1][0]),
sourceEditor.document.positionAt(mapping[1][1]),
));
const mappingDecorationRanges = map.codeMappings
.map(mapping => mapping.sourceOffsets.map((offset, i) => new vscode.Range(
sourceEditor.document.positionAt(offset),
sourceEditor.document.positionAt(offset + mapping.lengths[i]),
)))
.flat();
sourceEditor.setDecorations(mappingDecorationType, mappingDecorationRanges);
virtualRanges1 = virtualRanges1.concat(map.codeMappings.map(mapping => new vscode.Range(
getGeneratedPosition(virtualUri, mapping[2][0]),
getGeneratedPosition(virtualUri, mapping[2][1]),
)));
virtualRanges1 = virtualRanges1.concat(
map.codeMappings
.map(mapping => mapping.generatedOffsets.map((offset, i) => new vscode.Range(
getGeneratedPosition(virtualUri, offset),
getGeneratedPosition(virtualUri, offset + mapping.lengths[i]),
)))
.flat()
);

/**
* selection
Expand All @@ -214,57 +220,97 @@ export async function activate(info: ExportsInfoForLabs) {

if (vscode.window.activeTextEditor === sourceEditor) {

const mappedStart = [...map.getGeneratedOffsets(startOffset)];
const mappedEnd = [...map.getGeneratedOffsets(endOffset, true)];

sourceEditor.setDecorations(mappingSelectionDecorationType, mappedStart.map(mapped => new vscode.Range(
sourceEditor.document.positionAt(mapped[1][1][0]),
sourceEditor.document.positionAt(mapped[1][1][1]),
)));
sourceEditor.setDecorations(mappingCursorDecorationType, [selection]);

virtualRanges2 = virtualRanges2.concat(mappedStart.map(mapped => new vscode.Range(
getGeneratedPosition(virtualUri, mapped[1][2][0]),
getGeneratedPosition(virtualUri, mapped[1][2][1]),
)));
virtualRanges3 = virtualRanges3.concat(mappedStart.map(mapped => new vscode.Range(
getGeneratedPosition(virtualUri, mapped[0]),
getGeneratedPosition(virtualUri, mappedEnd.find(mapped2 => mapped[1] === mapped2[1])?.[0] ?? mapped[0]),
)));

const mapped = mappedStart.sort((a, b) => a[1][2][0] - b[1][2][0])[0];
if (mapped) {
const mappedStarts = [...map.getGeneratedOffsets(startOffset)];
const mappedEnds = [...map.getGeneratedOffsets(endOffset)];

if (mappedStarts.length || mappedEnds.length) {
sourceEditor.setDecorations(
mappingCursorDecorationType,
[new vscode.Range(
mappedStarts.length ? selection.start : selection.end,
mappedEnds.length ? selection.end : selection.start
)]
);
}

sourceEditor.setDecorations(
mappingSelectionDecorationType,
mappedStarts
.map(([_, mapping]) => mapping.sourceOffsets.map((offset, i) => new vscode.Range(
sourceEditor.document.positionAt(offset),
sourceEditor.document.positionAt(offset + mapping.lengths[i]),
)))
.flat()
);

virtualRanges2 = virtualRanges2.concat(
mappedStarts
.map(([_, mapping]) => mapping.generatedOffsets.map((offset, i) => new vscode.Range(
getGeneratedPosition(virtualUri, offset),
getGeneratedPosition(virtualUri, offset + mapping.lengths[i]),
)))
.flat()
);
virtualRanges3 = virtualRanges3.concat(
mappedStarts.map(mapped => new vscode.Range(
getGeneratedPosition(virtualUri, mapped[0]),
getGeneratedPosition(virtualUri, mappedEnds.find(mapped2 => mapped[1] === mapped2[1])?.[0] ?? mapped[0]),
))
);

const mapping = mappedStarts.sort((a, b) => a[1].generatedOffsets[0] - b[1].generatedOffsets[0])[0]?.[1];
if (mapping) {
virtualEditor.revealRange(new vscode.Range(
getGeneratedPosition(virtualUri, mapped[1][2][0]),
getGeneratedPosition(virtualUri, mapped[1][2][1]),
getGeneratedPosition(virtualUri, mapping.generatedOffsets[0]),
getGeneratedPosition(virtualUri, mapping.generatedOffsets[0] + mapping.lengths[0]),
));
}
}
else if (vscode.window.activeTextEditor === virtualEditor) {

const mappedStart = [...map.getSourceOffsets(startOffset)];
const mappedEnd = [...map.getSourceOffsets(endOffset, true)];

sourceEditor.setDecorations(mappingSelectionDecorationType, mappedStart.map(mapped => new vscode.Range(
sourceEditor.document.positionAt(mapped[1][1][0]),
sourceEditor.document.positionAt(mapped[1][1][1]),
)));
sourceEditor.setDecorations(mappingCursorDecorationType, mappedStart.map(mapped => new vscode.Range(
sourceEditor.document.positionAt(mapped[0]),
sourceEditor.document.positionAt(mappedEnd.find(mapped2 => mapped[1] === mapped2[1])?.[0] ?? mapped[0]),
)));

virtualRanges2 = virtualRanges2.concat(mappedStart.map(mapped => new vscode.Range(
getGeneratedPosition(virtualUri, mapped[1][2][0]),
getGeneratedPosition(virtualUri, mapped[1][2][1]),
)));
virtualRanges3 = virtualRanges3.concat(selection);

const mapped = mappedStart.sort((a, b) => a[1][1][0] - b[1][1][0])[0];
if (mapped) {
const mappedStarts = [...map.getSourceOffsets(startOffset)];
const mappedEnds = [...map.getSourceOffsets(endOffset)];

if (mappedStarts.length || mappedEnds.length) {
virtualRanges3.push(
new vscode.Range(
mappedStarts.length ? selection.start : selection.end,
mappedEnds.length ? selection.end : selection.start
)
);
}

sourceEditor.setDecorations(
mappingSelectionDecorationType,
mappedStarts
.map(([_, mapping]) => mapping.sourceOffsets.map((offset, i) => new vscode.Range(
sourceEditor.document.positionAt(offset),
sourceEditor.document.positionAt(offset + mapping.lengths[i]),
)))
.flat()
);
sourceEditor.setDecorations(
mappingCursorDecorationType,
mappedStarts.map(mapped => new vscode.Range(
sourceEditor.document.positionAt(mapped[0]),
sourceEditor.document.positionAt(mappedEnds.find(mapped2 => mapped[1] === mapped2[1])?.[0] ?? mapped[0]),
))
);

virtualRanges2 = virtualRanges2.concat(
mappedStarts
.map(([_, mapping]) => mapping.generatedOffsets.map((offset, i) => new vscode.Range(
getGeneratedPosition(virtualUri, offset),
getGeneratedPosition(virtualUri, offset + mapping.lengths[i]),
)))
.flat()
);

const mapping = mappedStarts.sort((a, b) => a[1].sourceOffsets[0] - b[1].sourceOffsets[0])[0]?.[1];
if (mapping) {
sourceEditor.revealRange(new vscode.Range(
sourceEditor.document.positionAt(mapped[1][1][0]),
sourceEditor.document.positionAt(mapped[1][1][1]),
getGeneratedPosition(virtualUri, mapping.sourceOffsets[0]),
getGeneratedPosition(virtualUri, mapping.sourceOffsets[0] + mapping.lengths[0]),
));
}
}
Expand Down
10 changes: 5 additions & 5 deletions packages/language-core/lib/fileProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MappingKey, SourceMap } from '@volar/source-map';
import { SourceMap } from '@volar/source-map';
import type * as ts from 'typescript/lib/tsserverlibrary';
import { LinkedCodeMap } from './linkedCodeMap';
import type { CodeInformation, Language, SourceFile, VirtualFile } from './types';
Expand Down Expand Up @@ -138,17 +138,17 @@ export function updateVirtualFileMaps(

for (const mapping of virtualFile.mappings) {

if (sources.has(mapping[MappingKey.SOURCE_FILE]))
if (sources.has(mapping.source))
continue;

sources.add(mapping[MappingKey.SOURCE_FILE]);
sources.add(mapping.source);

const source = getSourceSnapshot(mapping[MappingKey.SOURCE_FILE]);
const source = getSourceSnapshot(mapping.source);
if (!source)
continue;

if (!map.has(source[0]) || map.get(source[0])![0] !== source[1]) {
map.set(source[0], [source[1], new SourceMap(virtualFile.mappings.filter(mapping2 => mapping2[MappingKey.SOURCE_FILE] === mapping[MappingKey.SOURCE_FILE]))]);
map.set(source[0], [source[1], new SourceMap(virtualFile.mappings.filter(mapping2 => mapping2.source === mapping.source))]);
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/language-core/lib/linkedCodeMap.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { MappingKey, SourceMap } from '@volar/source-map';
import { SourceMap } from '@volar/source-map';
import type { LinkedCodeTrigger } from './types';

export class LinkedCodeMap extends SourceMap<[LinkedCodeTrigger, LinkedCodeTrigger]> {
*toLinkedOffsets(start: number) {
for (const mapped of this.getGeneratedOffsets(start)) {
yield [mapped[0], mapped[1][MappingKey.DATA][1]] as const;
yield [mapped[0], mapped[1].data[1]] as const;
}
for (const mapped of this.getSourceOffsets(start)) {
yield [mapped[0], mapped[1][MappingKey.DATA][0]] as const;
yield [mapped[0], mapped[1].data[0]] as const;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { ServiceContext } from '../types';
import { NoneCancellationToken } from '../utils/cancellation';
import { notEmpty } from '../utils/common';
import { documentFeatureWorker } from '../utils/featureWorkers';
import { MappingKey } from '@volar/language-core';

export function register(context: ServiceContext) {

Expand All @@ -12,7 +11,7 @@ export function register(context: ServiceContext) {
return documentFeatureWorker(
context,
uri,
map => map.map.codeMappings.some(mapping => mapping[MappingKey.DATA].colors ?? true),
map => map.map.codeMappings.some(mapping => mapping.data.colors ?? true),
(service, document) => {
if (token.isCancellationRequested) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type * as vscode from 'vscode-languageserver-protocol';
import { isInsideRange, notEmpty } from '../utils/common';
import { NoneCancellationToken } from '../utils/cancellation';
import { transformDocumentSymbol } from '../utils/transform';
import { MappingKey } from '@volar/language-core';

export function register(context: ServiceContext) {

Expand All @@ -13,7 +12,7 @@ export function register(context: ServiceContext) {
return documentFeatureWorker(
context,
uri,
map => map.map.codeMappings.some(mapping => mapping[MappingKey.DATA].symbols ?? true),
map => map.map.codeMappings.some(mapping => mapping.data.symbols ?? true),
async (service, document) => {
if (token.isCancellationRequested) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { NoneCancellationToken } from '../utils/cancellation';

import type * as _ from 'vscode-languageserver-protocol';
import { transformFoldingRanges } from '../utils/transform';
import { MappingKey } from '@volar/language-core';

export function register(context: ServiceContext) {

Expand All @@ -13,7 +12,7 @@ export function register(context: ServiceContext) {
return documentFeatureWorker(
context,
uri,
map => map.map.codeMappings.some(mapping => mapping[MappingKey.DATA].foldingRanges ?? true),
map => map.map.codeMappings.some(mapping => mapping.data.foldingRanges ?? true),
(service, document) => {
if (token.isCancellationRequested) {
return;
Expand Down
Loading
Loading