Skip to content

Commit

Permalink
Rework virtual files (#363)
Browse files Browse the repository at this point in the history
Before Volar, the ESM, JSX, and markdown parts of virtual files had to
map character by character, with parts shadowed by whitespace
characters. This was still how virtual files were built. However,
because of how Volar mappings work, this is unnecessary.

This change overhauls how virtual files are generated. This leads to
more flexibility of virtual files, meaning we can omit or inject
content.

This is already leveraged in several ways:

- Markdown nodes in MDX are represented by TypeScript fragments. This
  provides compatibility with JSX elements that require children on a
  type level.
- Markdown text nodes in MDX are represented by TypeScript JSX
  expressions containing an empty string.
- JavaScript chunks are represented in the virtual markdown files as
  HTML comments.
  • Loading branch information
remcohaszing authored Dec 11, 2023
1 parent a738090 commit 7c00fc3
Show file tree
Hide file tree
Showing 7 changed files with 454 additions and 330 deletions.
8 changes: 6 additions & 2 deletions packages/language-server/test/completion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ test('support completion in JSX', async () => {
fixturePath('node16/completion.mdx'),
'mdx'
)
await serverHandle.sendCompletionRequest(uri, {
line: 5,
character: 3
})
const result = await serverHandle.sendCompletionRequest(uri, {
line: 5,
character: 3
Expand All @@ -98,7 +102,7 @@ test('support completion in JSX', async () => {
original: {
data: {
fileName: fixturePath('node16/completion.mdx.jsx'),
offset: 430,
offset: 67,
originalItem: {name: 'Boolean'},
uri: fixtureUri('node16/completion.mdx.jsx')
}
Expand All @@ -118,7 +122,7 @@ test('support completion in JSX', async () => {
commitCharacters: ['.', ',', ';', '('],
data: {
fileName: fixturePath('node16/completion.mdx.jsx'),
offset: 430,
offset: 67,
originalItem: {name: 'Boolean'},
uri: fixtureUri('node16/completion.mdx.jsx')
},
Expand Down
110 changes: 102 additions & 8 deletions packages/language-server/test/document-symbols.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,111 @@ test('resolve document symbols', async () => {

assert.deepEqual(result, [
{
children: [],
kind: SymbolKind.Function,
name: 'exportedFunction',
name: '# Mixed content',
kind: SymbolKind.String,
range: {
end: {line: 15, character: 1},
start: {line: 10, character: 0}
start: {line: 6, character: 0},
end: {line: 46, character: 0}
},
selectionRange: {
end: {line: 10, character: 32},
start: {line: 10, character: 16}
}
start: {line: 6, character: 0},
end: {line: 46, character: 0}
},
children: [
{
name: '## Level 2 Header',
kind: SymbolKind.String,
range: {
start: {line: 17, character: 0},
end: {line: 32, character: 0}
},
selectionRange: {
start: {line: 17, character: 0},
end: {line: 32, character: 0}
},
children: [
{
name: '### Level 3 Header',
kind: SymbolKind.String,
range: {
start: {line: 21, character: 0},
end: {line: 24, character: 0}
},
selectionRange: {
start: {line: 21, character: 0},
end: {line: 24, character: 0}
},
children: []
},
{
name: '### Another Kevel 3 Header',
kind: SymbolKind.String,
range: {
start: {line: 25, character: 0},
end: {line: 32, character: 0}
},
selectionRange: {
start: {line: 25, character: 0},
end: {line: 32, character: 0}
},
children: [
{
name: '###### Level 6 Heading',
kind: SymbolKind.String,
range: {
start: {line: 29, character: 0},
end: {line: 32, character: 0}
},
selectionRange: {
start: {line: 29, character: 0},
end: {line: 32, character: 0}
},
children: []
}
]
}
]
},
{
name: '## Another Level 2 Header',
kind: SymbolKind.String,
range: {
start: {line: 33, character: 0},
end: {line: 42, character: 1}
},
selectionRange: {
start: {line: 33, character: 0},
end: {line: 42, character: 1}
},
children: []
},
{
name: '## > ## Heading inside a block quote',
kind: SymbolKind.String,
range: {
start: {line: 43, character: 0},
end: {line: 46, character: 0}
},
selectionRange: {
start: {line: 43, character: 0},
end: {line: 46, character: 0}
},
children: []
},
{
name: 'exportedFunction',
kind: SymbolKind.Function,
range: {
start: {line: 10, character: 0},
end: {line: 15, character: 1}
},
selectionRange: {
start: {line: 10, character: 16},
end: {line: 10, character: 32}
},
children: []
}
]
}
])
})
Expand Down
Loading

0 comments on commit 7c00fc3

Please sign in to comment.