-
-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(language-core): extract SFC root tags to separate virtual code
- Loading branch information
1 parent
67be01c
commit b2965ff
Showing
9 changed files
with
113 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { CodeMapping } from '@volar/language-core'; | ||
import { replaceSourceRange } from 'muggle-string'; | ||
import type { VueCodeInformation, VueLanguagePlugin } from '../types'; | ||
import { allCodeFeatures } from './shared'; | ||
|
||
const plugin: VueLanguagePlugin = () => { | ||
|
||
return { | ||
|
||
version: 2.1, | ||
|
||
getEmbeddedCodes() { | ||
return [{ | ||
id: 'root_tags', | ||
lang: 'vue-root-tags', | ||
}]; | ||
}, | ||
|
||
resolveEmbeddedCode(_fileName, sfc, embeddedFile) { | ||
if (embeddedFile.id === 'root_tags') { | ||
embeddedFile.content.push([sfc.content, undefined, 0, allCodeFeatures]); | ||
for (const block of [ | ||
sfc.script, | ||
sfc.scriptSetup, | ||
sfc.template, | ||
...sfc.styles, | ||
...sfc.customBlocks, | ||
]) { | ||
if (block) { | ||
replaceSourceRange(embeddedFile.content, undefined, block.startTagEnd, block.endTagStart, '\n\n'); | ||
} | ||
} | ||
const mappings = embeddedFile.content | ||
.filter(s => typeof s !== 'string') | ||
.map<CodeMapping>(m => { | ||
const text = m[0]; | ||
const start = m[2] as number; | ||
return { | ||
sourceOffsets: [start], | ||
generatedOffsets: [start], | ||
lengths: [text.length], | ||
data: m[3] as VueCodeInformation, | ||
}; | ||
}); | ||
|
||
// fix folding range end position failed to mapping | ||
for (const block of [ | ||
sfc.script, | ||
sfc.scriptSetup, | ||
sfc.template, | ||
...sfc.styles, | ||
...sfc.customBlocks, | ||
]) { | ||
const offsets: number[] = []; | ||
if (block) { | ||
let content = block.content; | ||
if (content.endsWith('\r\n')) { | ||
content = content.slice(0, -2); | ||
} | ||
else if (content.endsWith('\n')) { | ||
content = content.slice(0, -1); | ||
} | ||
const offset = content.lastIndexOf('\n') + 1; | ||
offsets.push(block.startTagEnd + offset); | ||
} | ||
if (offsets.length) { | ||
mappings.push({ | ||
sourceOffsets: offsets, | ||
generatedOffsets: offsets, | ||
lengths: offsets.map(() => 0), | ||
data: { structure: true }, | ||
}); | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
}; | ||
|
||
export default plugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 0 additions & 70 deletions
70
packages/language-core/lib/virtualFile/computedMappings.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters