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: expose custom blocks attrs #3099

Merged
merged 7 commits into from May 1, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/vue-language-core/src/generators/script.ts
Expand Up @@ -51,6 +51,7 @@ export function generate(
endTagStart: 0,
generic: undefined,
genericOffset: 0,
attrs: {},
};
scriptSetupRanges = {
bindings: [],
Expand Down
21 changes: 18 additions & 3 deletions packages/vue-language-core/src/sourceFile.ts
Expand Up @@ -474,6 +474,7 @@ export class VueFile implements VirtualFile {
endTagStart: block.loc.end.offset,
content: block.content,
lang: block.lang ?? 'html',
attrs: block.attrs,
} : null;

if (this.sfc.template && newData) {
Expand All @@ -496,6 +497,7 @@ export class VueFile implements VirtualFile {
lang: block.lang ?? 'js',
src: block.src,
srcOffset: block.src ? this.snapshot.getText(0, block.loc.start.offset).lastIndexOf(block.src) - block.loc.start.offset : -1,
attrs: block.attrs,
} : null;

if (this.sfc.script && newData) {
Expand All @@ -518,6 +520,7 @@ export class VueFile implements VirtualFile {
lang: block.lang ?? 'js',
generic: typeof block.attrs.generic === 'string' ? block.attrs.generic : undefined,
genericOffset: typeof block.attrs.generic === 'string' ? this.snapshot.getText(0, block.loc.start.offset).lastIndexOf(block.attrs.generic) - block.loc.start.offset : -1,
attrs: block.attrs,
} : null;

if (this.sfc.scriptSetup && newData) {
Expand All @@ -542,6 +545,7 @@ export class VueFile implements VirtualFile {
lang: block.lang ?? 'css',
module: typeof block.module === 'string' ? block.module : block.module ? '$style' : undefined,
scoped: !!block.scoped,
attrs: block.attrs,
};

if (this.sfc.styles.length > i) {
Expand Down Expand Up @@ -569,6 +573,7 @@ export class VueFile implements VirtualFile {
content: block.content,
lang: block.lang ?? 'txt',
type: block.type,
attrs: block.attrs,
};

if (this.sfc.customBlocks.length > i) {
Expand All @@ -583,9 +588,19 @@ export class VueFile implements VirtualFile {
}
}

updateBlock<T>(oldBlock: T, newBlock: T) {
for (let key in newBlock) {
oldBlock[key] = newBlock[key];
updateBlock<T extends object>(oldBlock: T, newBlock: T) {
for (const key in newBlock) {
if (typeof oldBlock[key] === 'object' && typeof newBlock[key] === 'object') {
this.updateBlock(oldBlock[key] as object, newBlock[key] as object);
}
else {
oldBlock[key] = newBlock[key];
}
}
for (const key in oldBlock) {
if (!(key in newBlock)) {
delete oldBlock[key];
}
}
}
}
1 change: 1 addition & 0 deletions packages/vue-language-core/src/types.ts
Expand Up @@ -72,6 +72,7 @@ export interface SfcBlock {
endTagStart: number;
lang: string;
content: string;
attrs: Record<string, string | true>;
}

export interface Sfc {
Expand Down