Skip to content

Commit 1adb9f3

Browse files
authored
Use typescript parser (#294)
* use typescript parser and generate code without rewriting script code * resolve variable definition
1 parent f6af2b3 commit 1adb9f3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+3175
-2600
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"onwarn",
1010
"textdocument",
1111
"treeshake",
12+
"tsserverlibrary",
1213
"typecheck",
1314
"ucfirst",
1415
"Uncapitalize",

extensions/vscode-vue-language-features/src/scheme/vue.ts

Lines changed: 96 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { isVueFile, parseFileName } from '@vuedx/shared'
1+
import { first, isNotNull, parseFileName } from '@vuedx/shared'
2+
import { TextSpan } from '@vuedx/vue-virtual-textdocument'
23
import { inject, injectable } from 'inversify'
3-
import vscode from 'vscode'
4+
import vscode, { TextEditor } from 'vscode'
45
import { PluginCommunicationService } from '../services/PluginCommunicationService'
56
import { Installable } from '../utils/installable'
67
import { getVirtualFileNameFromUri } from '../utils/uri'
@@ -18,17 +19,20 @@ export class VueVirtualDocumentProvider
1819
}
1920

2021
private readonly openVueFiles = new Map<string, Set<string>>()
22+
private readonly decoration = vscode.window.createTextEditorDecorationType({
23+
outline: 'solid red 1px',
24+
})
25+
26+
private editors: readonly TextEditor[] = []
2127

2228
public install(): vscode.Disposable {
2329
super.install()
2430

25-
let selectionWatcher: vscode.Disposable | undefined
26-
let cancellationToken: vscode.CancellationTokenSource | undefined
27-
2831
return vscode.Disposable.from(
2932
vscode.workspace.registerTextDocumentContentProvider('vue', this),
30-
vscode.workspace.onDidChangeTextDocument(({ document }) => {
33+
vscode.workspace.onDidChangeTextDocument(async ({ document }) => {
3134
if (document.languageId === 'vue') {
35+
await delay(100)
3236
this.openVueFiles.get(document.fileName)?.forEach((uri) => {
3337
this.onDidChangeEmitter.fire(vscode.Uri.parse(uri))
3438
})
@@ -56,72 +60,103 @@ export class VueVirtualDocumentProvider
5660
if (openFiles.size === 0) this.openVueFiles.delete(parsed.fileName)
5761
}
5862
}),
59-
vscode.window.onDidChangeActiveTextEditor((editor) => {
60-
selectionWatcher?.dispose()
61-
if (editor == null || !isVueFile(editor.document.fileName)) return
62-
const fileName = editor.document.fileName
63-
64-
selectionWatcher = vscode.window.onDidChangeTextEditorSelection(
65-
async ({ textEditor, selections }) => {
66-
if (textEditor !== editor) return // ignore others
67-
if (selections.length !== 1) return
68-
if (
69-
!vscode.window.visibleTextEditors.some(
70-
(editor) =>
71-
parseFileName(editor.document.fileName).fileName === fileName,
72-
)
73-
) {
74-
return // No active virtual document
75-
}
76-
77-
cancellationToken?.cancel()
78-
const current = new vscode.CancellationTokenSource()
79-
cancellationToken = current
80-
81-
const start = textEditor.document.offsetAt(editor.selection.start)
82-
const end = textEditor.document.offsetAt(editor.selection.end)
83-
const result = await this.plugin.first(async (_conneciton) => {
84-
console.log(start, end)
85-
return null as null | {
86-
fileName: string
87-
start: number
88-
end: number
89-
} // TODO: fix this
90-
})
91-
92-
// not found or cancelled
93-
if (result == null || current.token.isCancellationRequested) return
94-
95-
const virtualEditor = vscode.window.visibleTextEditors.find(
96-
(editor) => editor.document.fileName === result.fileName,
63+
vscode.window.onDidChangeActiveTextEditor(() => {
64+
this.resetAllDecorations()
65+
}),
66+
vscode.window.onDidChangeVisibleTextEditors((editors) => {
67+
this.editors = editors
68+
}),
69+
vscode.window.onDidChangeTextEditorSelection(
70+
async ({ textEditor, selections }) => {
71+
if (textEditor !== vscode.window.activeTextEditor) return
72+
if (textEditor.document.languageId === 'vue') {
73+
const fileName = textEditor.document.fileName
74+
const editor = this.editors.find(
75+
(editor) =>
76+
editor.document.uri.scheme === 'vue' &&
77+
editor.document.fileName.startsWith(fileName),
9778
)
9879

99-
if (virtualEditor == null) return // not active
100-
101-
const range = new vscode.Range(
102-
virtualEditor.document.positionAt(result.start),
103-
virtualEditor.document.positionAt(result.end),
80+
if (editor == null) return
81+
const textSpans = await Promise.all(
82+
selections.map(
83+
async (selection) =>
84+
await this.plugin.first(async (connection) => {
85+
const start = textEditor.document.offsetAt(selection.start)
86+
const end = textEditor.document.offsetAt(selection.end)
87+
88+
return await connection.findGeneratedTextSpan(fileName, {
89+
start: Math.min(start, end),
90+
length: Math.abs(end - start),
91+
})
92+
}),
93+
),
10494
)
105-
106-
virtualEditor.options.cursorStyle =
107-
vscode.TextEditorCursorStyle.Underline
108-
virtualEditor.selection = new vscode.Selection(
109-
range.start,
110-
range.end,
95+
this.setDecorations(textSpans, editor)
96+
} else if (textEditor.document.uri.scheme === 'vue') {
97+
const fileName = textEditor.document.fileName.replace(
98+
/\.[tj]sx$/,
99+
'',
111100
)
112-
virtualEditor.revealRange(
113-
range,
114-
vscode.TextEditorRevealType.Default,
101+
const editor = this.editors.find((editor) =>
102+
editor.document.fileName.startsWith(fileName),
115103
)
116-
},
117-
)
118-
}),
104+
105+
if (editor == null) return
106+
const textSpans = await Promise.all(
107+
selections.map(
108+
async (selection) =>
109+
await this.plugin.first(async (connection) => {
110+
const start = textEditor.document.offsetAt(selection.start)
111+
const end = textEditor.document.offsetAt(selection.end)
112+
113+
return await connection.findOriginalTextSpan(fileName, {
114+
start: Math.min(start, end),
115+
length: Math.abs(end - start),
116+
})
117+
}),
118+
),
119+
)
120+
this.setDecorations(textSpans, editor)
121+
}
122+
},
123+
),
119124
)
120125
}
121126

122127
private readonly onDidChangeEmitter = new vscode.EventEmitter<vscode.Uri>()
123128
public onDidChange = this.onDidChangeEmitter.event
124129

130+
private resetAllDecorations(): void {
131+
this.editors.forEach((editor) => {
132+
editor.setDecorations(this.decoration, [])
133+
})
134+
}
135+
136+
private setDecorations(
137+
textSpans: Array<TextSpan | null | undefined>,
138+
editor: vscode.TextEditor,
139+
): void {
140+
const ranges = textSpans
141+
.filter(isNotNull)
142+
.map(
143+
(range) =>
144+
new vscode.Range(
145+
editor.document.positionAt(range.start),
146+
editor.document.positionAt(range.start + range.length),
147+
),
148+
)
149+
150+
editor.setDecorations(this.decoration, [])
151+
editor.setDecorations(this.decoration, ranges)
152+
if (ranges.length > 0) {
153+
editor.revealRange(
154+
first(ranges),
155+
vscode.TextEditorRevealType.InCenterIfOutsideViewport,
156+
)
157+
}
158+
}
159+
125160
async provideTextDocumentContent(
126161
request: vscode.Uri,
127162
): Promise<string | undefined> {

extensions/vscode-vue-language-features/src/services/VirtualFileSwitcher.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ import {
22
isProjectRuntimeFile,
33
isVueFile,
44
parseFileName,
5-
toFileName
5+
toFileName,
66
} from '@vuedx/shared'
7-
import { } from '@vuedx/vue-virtual-textdocument'
87
import { inject, injectable } from 'inversify'
98
import {
109
Disposable,
1110
StatusBarAlignment,
1211
StatusBarItem,
1312
TextEditor,
1413
Uri,
15-
window
14+
window,
1615
} from 'vscode'
1716
import { Installable } from '../utils/installable'
1817
import { getVirtualFileUri } from '../utils/uri'
@@ -41,8 +40,8 @@ export class VirtualFileSwitcher extends Installable {
4140
window.onDidChangeActiveTextEditor(async (editor) => {
4241
await this.showStatusBar(editor)
4342
}),
44-
this.plugin.onChange(() => {
45-
void this.showStatusBar(window.activeTextEditor)
43+
this.plugin.onChange(async () => {
44+
await this.showStatusBar(window.activeTextEditor)
4645
}),
4746
)
4847
}

packages/compiler-tsx/compiler-tsx.api.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ export interface CompileOutput extends TransformedCode {
5656
errors: Array<CompilerError | SyntaxError>;
5757
// (undocumented)
5858
template?: RootNode;
59-
// (undocumented)
60-
unusedIdentifiers: string[];
6159
}
6260

6361
// @public (undocumented)

0 commit comments

Comments
 (0)