1
- import { type CodeInformation , defaultMapperFactory , type Mapping } from '@volar/language-core' ;
1
+ import { SourceMap } from '@volar/language-core' ;
2
2
import type { SFCBlock } from '@vue/compiler-sfc' ;
3
3
import { type Segment , toString } from 'muggle-string' ;
4
4
import type { VueLanguagePlugin } from '../types' ;
@@ -10,9 +10,9 @@ const codeblockReg = /(`{3,})[\s\S]+?\1/g;
10
10
const inlineCodeblockReg = / ` [ ^ \n ` ] + ?` / g;
11
11
const latexBlockReg = / ( \$ { 2 , } ) [ \s \S ] + ?\1/ g;
12
12
const scriptSetupReg = / \\ < [ \s \S ] + ?> \n ? / g;
13
- const sfcBlockReg = / < ( s c r i p t | s t y l e ) \b [ \s \S ] * ?> ( [ \s \S ] * ?) < \/ \1> / g;
14
13
const angleBracketReg = / < \S * : \S * > / g;
15
14
const linkReg = / \[ [ \s \S ] * ?\] \( [ \s \S ] * ?\) / g;
15
+ const sfcBlockReg = / < ( s c r i p t | s t y l e ) \b [ \s \S ] * ?> ( [ \s \S ] * ?) < \/ \1> / g;
16
16
const codeSnippetImportReg = / ^ \s * < < < \s * .+ / gm;
17
17
18
18
const plugin : VueLanguagePlugin = ( { vueCompilerOptions } ) => {
@@ -46,48 +46,42 @@ const plugin: VueLanguagePlugin = ({ vueCompilerOptions }) => {
46
46
// # \<script setup>
47
47
. replace ( scriptSetupReg , match => ' ' . repeat ( match . length ) )
48
48
// <<< https://vitepress.dev/guide/markdown#import-code-snippets
49
- . replace ( codeSnippetImportReg , match => ' ' . repeat ( match . length ) ) ;
49
+ . replace ( codeSnippetImportReg , match => ' ' . repeat ( match . length ) )
50
+ // angle bracket: <http://foo.com>
51
+ . replace ( angleBracketReg , match => ' ' . repeat ( match . length ) )
52
+ // [foo](http://foo.com)
53
+ . replace ( linkReg , match => ' ' . repeat ( match . length ) ) ;
50
54
51
55
const codes : Segment [ ] = [ ] ;
52
56
53
57
for ( const match of content . matchAll ( sfcBlockReg ) ) {
54
- if ( match . index !== undefined ) {
55
- const matchText = match [ 0 ] ;
56
- codes . push ( [ matchText , undefined , match . index ] ) ;
57
- codes . push ( '\n\n' ) ;
58
- content = content . slice ( 0 , match . index ) + ' ' . repeat ( matchText . length )
59
- + content . slice ( match . index + matchText . length ) ;
60
- }
58
+ const matchText = match [ 0 ] ;
59
+ codes . push ( [ matchText , undefined , match . index ] ) ;
60
+ codes . push ( '\n\n' ) ;
61
+ content = content . slice ( 0 , match . index ) + ' ' . repeat ( matchText . length )
62
+ + content . slice ( match . index + matchText . length ) ;
61
63
}
62
64
63
- content = content
64
- // angle bracket: <http://foo.com>
65
- . replace ( angleBracketReg , match => ' ' . repeat ( match . length ) )
66
- // [foo](http://foo.com)
67
- . replace ( linkReg , match => ' ' . repeat ( match . length ) ) ;
68
-
69
65
codes . push ( '<template>\n' ) ;
70
66
codes . push ( [ content , undefined , 0 ] ) ;
71
67
codes . push ( '\n</template>' ) ;
72
68
73
- const file2VueSourceMap = defaultMapperFactory ( buildMappings ( codes ) as unknown as Mapping < CodeInformation > [ ] ) ;
69
+ const mappings = buildMappings ( codes ) ;
70
+ const mapper = new SourceMap ( mappings ) ;
74
71
const sfc = parse ( toString ( codes ) ) ;
75
72
76
- if ( sfc . descriptor . template ) {
77
- sfc . descriptor . template . lang = 'md' ;
78
- transformRange ( sfc . descriptor . template ) ;
79
- }
80
- if ( sfc . descriptor . script ) {
81
- transformRange ( sfc . descriptor . script ) ;
82
- }
83
- if ( sfc . descriptor . scriptSetup ) {
84
- transformRange ( sfc . descriptor . scriptSetup ) ;
85
- }
86
- for ( const style of sfc . descriptor . styles ) {
87
- transformRange ( style ) ;
88
- }
89
- for ( const customBlock of sfc . descriptor . customBlocks ) {
90
- transformRange ( customBlock ) ;
73
+ for (
74
+ const block of [
75
+ sfc . descriptor . template ,
76
+ sfc . descriptor . script ,
77
+ sfc . descriptor . scriptSetup ,
78
+ ...sfc . descriptor . styles ,
79
+ ...sfc . descriptor . customBlocks ,
80
+ ]
81
+ ) {
82
+ if ( block ) {
83
+ transformRange ( block ) ;
84
+ }
91
85
}
92
86
93
87
return sfc ;
@@ -98,11 +92,11 @@ const plugin: VueLanguagePlugin = ({ vueCompilerOptions }) => {
98
92
const endOffset = end . offset ;
99
93
start . offset = - 1 ;
100
94
end . offset = - 1 ;
101
- for ( const [ offset ] of file2VueSourceMap . toSourceLocation ( startOffset ) ) {
95
+ for ( const [ offset ] of mapper . toSourceLocation ( startOffset ) ) {
102
96
start . offset = offset ;
103
97
break ;
104
98
}
105
- for ( const [ offset ] of file2VueSourceMap . toSourceLocation ( endOffset ) ) {
99
+ for ( const [ offset ] of mapper . toSourceLocation ( endOffset ) ) {
106
100
end . offset = offset ;
107
101
break ;
108
102
}
0 commit comments