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

fix(typescript): fix inlay hints mapping for large chunks of source code mapped verbatim to generated code. #236

Merged
merged 1 commit into from
Sep 4, 2024
Merged
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
27 changes: 21 additions & 6 deletions packages/typescript/lib/node/proxyLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -936,14 +936,29 @@ function provideInlayHints(language: Language<string>, provideInlayHints: ts.Lan
if (serviceScript) {
let start: number | undefined;
let end: number | undefined;
const map = language.maps.get(serviceScript.code, targetScript);
const map = language.maps.get(serviceScript.code, sourceScript);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all maps were taken for the wrong script. It should be a source script to have correct mappings from the template to generated code

for (const mapping of map.mappings) {
if (isInlayHintsEnabled(mapping.data) && mapping.sourceOffsets[0] >= span.start && mapping.sourceOffsets[0] <= span.start + span.length) {
start ??= mapping.generatedOffsets[0];
end ??= mapping.generatedOffsets[mapping.generatedOffsets.length - 1];
start = Math.min(start, mapping.generatedOffsets[0]);
end = Math.max(end, mapping.generatedOffsets[mapping.generatedOffsets.length - 1]);
if (!isInlayHintsEnabled(mapping.data)) {
continue;
}
let mappingStart = mapping.sourceOffsets[0];
let genStart: number | undefined;
let genEnd: number | undefined;
if (mappingStart >= span.start && mappingStart <= span.start + span.length) {
genStart = mapping.generatedOffsets[0];
genEnd = mapping.generatedOffsets[mapping.generatedOffsets.length - 1]
+ (mapping.generatedLengths ?? mapping.lengths)[mapping.generatedOffsets.length - 1];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The end offset should include the whole length of the generated code.

} else if (mappingStart < span.start && span.start < mappingStart + mapping.lengths[0]
&& mapping.sourceOffsets.length == 1
&& (!mapping.generatedLengths || mapping.generatedLengths[0] === mapping.lengths[0])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My Angular VirtualCode has large pieces of code copied verbatim from the original file. This condition ensures that it is correctly handled and not ignored.

) {
genStart = mapping.generatedOffsets[0] + span.start - mappingStart
genEnd = Math.min(genStart + span.length, mapping.generatedOffsets[0] + mapping.lengths[0])
} else {
continue;
}
start = Math.min(start ?? genStart, genStart);
end = Math.max(end ?? genEnd, genEnd);
}
if (start === undefined || end === undefined) {
start = 0;
Expand Down
Loading