Skip to content

Commit

Permalink
updates for volarjs/volar.js#95
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Nov 26, 2023
1 parent e294393 commit 516eccd
Show file tree
Hide file tree
Showing 18 changed files with 568 additions and 534 deletions.
2 changes: 0 additions & 2 deletions packages/language-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
},
"dependencies": {
"@volar/language-core": "~1.11.0",
"@volar/source-map": "~1.11.0",
"@vue/compiler-dom": "^3.3.0",
"@vue/shared": "^3.3.0",
"computeds": "^0.0.1",
"minimatch": "^9.0.3",
"muggle-string": "^0.3.1",
"path-browserify": "^1.0.1",
"vue-template-compiler": "^2.7.14"
},
Expand Down
96 changes: 48 additions & 48 deletions packages/language-core/src/generators/script.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { LinkedCodeTrigger } from '@volar/language-core';
import * as SourceMaps from '@volar/source-map';
import { Segment, getLength } from '@volar/source-map';
import * as muggle from 'muggle-string';
import { LinkedCodeTrigger, Mapping, getLength, offsetStack, resetOffsetStack, setTracking, track } from '@volar/language-core';
import * as path from 'path-browserify';
import type * as ts from 'typescript/lib/tsserverlibrary';
import type * as templateGen from '../generators/template';
import type { ScriptRanges } from '../parsers/scriptRanges';
import type { ScriptSetupRanges } from '../parsers/scriptSetupRanges';
import type { TextRange, VueCodeInformation, VueCompilerOptions } from '../types';
import type { Code, VueCompilerOptions } from '../types';
import { Sfc } from '../types';
import { getSlotsPropertyName, hyphenateTag } from '../utils/shared';
import { walkInterpolationFragment } from '../utils/transform';
Expand All @@ -27,8 +24,8 @@ export function generate(
codegenStack: boolean,
) {

const [codes, codeStacks] = codegenStack ? muggle.track([] as Segment<VueCodeInformation>[]) : [[], []];
const mirrorBehaviorMappings: SourceMaps.Mapping<[LinkedCodeTrigger, LinkedCodeTrigger]>[] = [];
const [codes, codeStacks] = codegenStack ? track([] as Code[]) : [[], []];
const mirrorBehaviorMappings: Mapping<[LinkedCodeTrigger, LinkedCodeTrigger]>[] = [];

//#region monkey fix: https://github.com/vuejs/language-tools/pull/2113
if (!script && !scriptSetup) {
Expand Down Expand Up @@ -153,7 +150,7 @@ export function generate(
codes.push([
`'${src}'`,
'script',
[script.srcOffset - 1, script.srcOffset + script.src.length + 1],
script.srcOffset - 1,
{
renameEdits: src === script.src ? true : {
shouldRename: false,
Expand Down Expand Up @@ -267,7 +264,7 @@ export function generate(
return;
}

const definePropMirrors: Record<string, [number, number]> = {};
const definePropMirrors = new Map<string, number>();
let scriptSetupGeneratedOffset: number | undefined;

if (scriptSetup.generic) {
Expand Down Expand Up @@ -337,8 +334,7 @@ export function generate(
let propName = 'modelValue';
if (defineProp.name) {
propName = scriptSetup.content.substring(defineProp.name.start, defineProp.name.end);
const propMirrorStart = muggle.getLength(codes);
definePropMirrors[propName] = [propMirrorStart, propMirrorStart + propName.length];
definePropMirrors.set(propName, getLength(codes));
}
codes.push(`${propName}${defineProp.required ? '' : '?'}: `);
if (defineProp.type) {
Expand Down Expand Up @@ -408,19 +404,19 @@ export function generate(
continue;
}
const propName = scriptSetup.content.substring(defineProp.name.start, defineProp.name.end);
const propMirror = definePropMirrors[propName];
if (propMirror) {
mirrorBehaviorMappings.push([
undefined,
[defineProp.name.start + scriptSetupGeneratedOffset, defineProp.name.end + scriptSetupGeneratedOffset],
propMirror,
[{}, {}],
]);
const propMirror = definePropMirrors.get(propName);
if (propMirror !== undefined) {
mirrorBehaviorMappings.push({
sourceOffsets: [defineProp.name.start + scriptSetupGeneratedOffset],
generatedOffsets: [propMirror],
lengths: [defineProp.name.end - defineProp.name.start],
data: [{}, {}],
});
}
}
}
}
function generateSetupFunction(functional: boolean, mode: 'return' | 'export' | 'none', definePropMirrors: Record<string, [number, number]>) {
function generateSetupFunction(functional: boolean, mode: 'return' | 'export' | 'none', definePropMirrors: Map<string, number>) {

if (!scriptSetupRanges || !scriptSetup) {
return;
Expand Down Expand Up @@ -453,7 +449,7 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
`.trim() + '\n');
}

const scriptSetupGeneratedOffset = muggle.getLength(codes) - scriptSetupRanges.importSectionEndOffset;
const scriptSetupGeneratedOffset = getLength(codes) - scriptSetupRanges.importSectionEndOffset;

let setupCodeModifies: [() => void, number, number][] = [];
if (scriptSetupRanges.props.define && !scriptSetupRanges.props.name) {
Expand Down Expand Up @@ -534,8 +530,8 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
}
else if (defineProp.name) {
propName = scriptSetup.content.substring(defineProp.name.start, defineProp.name.end);
const start = muggle.getLength(codes);
definePropMirrors[propName] = [start, start + propName.length];
const start = getLength(codes);
definePropMirrors.set(propName, start);
codes.push(propName);
}
else {
Expand Down Expand Up @@ -749,22 +745,20 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
if (!templateUsageVars.has(varName) && !cssIds.has(varName)) {
continue;
}
const templateStart = getLength(codes);
const templateOffset = getLength(codes);
codes.push(varName);
const templateEnd = getLength(codes);
codes.push(`: ${varName} as typeof `);

const scriptStart = getLength(codes);
const scriptOffset = getLength(codes);
codes.push(varName);
const scriptEnd = getLength(codes);
codes.push(',\n');

mirrorBehaviorMappings.push([
undefined,
[scriptStart, scriptEnd],
[templateStart, templateEnd],
[{}, {}],
]);
mirrorBehaviorMappings.push({
sourceOffsets: [scriptOffset],
generatedOffsets: [templateOffset],
lengths: [varName.length],
data: [{}, {}],
});
}
}
codes.push(`};\n`); // return {
Expand Down Expand Up @@ -828,8 +822,8 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
for (const className of style.classNames) {
generateCssClassProperty(
i,
className.text.substring(1),
{ start: className.offset, end: className.offset + className.text.length },
className.text,
className.offset,
'string',
false,
true,
Expand Down Expand Up @@ -857,8 +851,8 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
for (const className of style.classNames) {
generateCssClassProperty(
i,
className.text.substring(1),
{ start: className.offset, end: className.offset + className.text.length },
className.text,
className.offset,
'boolean',
true,
!style.module,
Expand All @@ -874,11 +868,11 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
codes.push(`/* CSS variable injection end */\n`);

if (htmlGen) {
muggle.setTracking(false);
setTracking(false);
for (const s of htmlGen.codes) {
codes.push(s);
}
muggle.setTracking(true);
setTracking(true);
for (const s of htmlGen.codeStacks) {
codeStacks.push(s);
}
Expand All @@ -895,22 +889,22 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:

return { cssIds };

function generateCssClassProperty(styleIndex: number, className: string, classRange: TextRange, propertyType: string, optional: boolean, referencesCodeLens: boolean) {
function generateCssClassProperty(styleIndex: number, classNameWithDot: string, offset: number, propertyType: string, optional: boolean, referencesCodeLens: boolean) {
codes.push(`\n & { `);
codes.push([
'',
'style_' + styleIndex,
classRange.start,
offset,
{
references: true,
__referencesCodeLens: referencesCodeLens,
},
]);
codes.push(`'`);
codes.push([
className,
'',
'style_' + styleIndex,
[classRange.start, classRange.end],
offset,
{
references: true,
renameEdits: {
Expand All @@ -921,11 +915,17 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
},
},
]);
codes.push([
classNameWithDot.substring(1),
'style_' + styleIndex,
offset + 1,
{ __combineLastMappping: true },
]);
codes.push(`'`);
codes.push([
'',
'style_' + styleIndex,
classRange.end,
offset + classNameWithDot.length,
{},
]);
codes.push(`${optional ? '?' : ''}: ${propertyType}`);
Expand Down Expand Up @@ -992,17 +992,17 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
return usageVars;
}
function addVirtualCode(vueTag: 'script' | 'scriptSetup', start: number, end?: number) {
muggle.offsetStack();
offsetStack();
codes.push([
(vueTag === 'script' ? script : scriptSetup)!.content.substring(start, end),
vueTag,
start,
{}, // diagnostic also working for setup() returns unused in template checking
]);
muggle.resetOffsetStack();
resetOffsetStack();
}
function addExtraReferenceVirtualCode(vueTag: 'script' | 'scriptSetup', start: number, end: number) {
muggle.offsetStack();
offsetStack();
codes.push([
(vueTag === 'script' ? script : scriptSetup)!.content.substring(start, end),
vueTag,
Expand All @@ -1013,7 +1013,7 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
renameEdits: true,
},
]);
muggle.resetOffsetStack();
resetOffsetStack();
}
}

Expand Down
Loading

0 comments on commit 516eccd

Please sign in to comment.