@@ -5,7 +5,7 @@ import * as fs from '../utils/fs';
5
5
import * as shared from '@volar/shared' ;
6
6
import { userPick } from './splitEditors' ;
7
7
import { parse , SFCParseResult } from '@vue/compiler-sfc' ;
8
- import * as WebSocket from 'ws ' ;
8
+ import * as preview from '@volar/preview ' ;
9
9
10
10
interface PreviewState {
11
11
mode : 'vite' | 'nuxt' ,
@@ -28,40 +28,30 @@ export async function activate(context: vscode.ExtensionContext) {
28
28
statusBar . backgroundColor = new vscode . ThemeColor ( 'statusBarItem.warningBackground' ) ;
29
29
context . subscriptions . push ( statusBar ) ;
30
30
31
- const wsList : WebSocket . WebSocket [ ] = [ ] ;
32
- let wss : WebSocket . Server | undefined ;
31
+ let ws : ReturnType < typeof preview . createPreviewWebSocket > | undefined ;
33
32
34
- function startWsServer ( ) {
35
- wss = new WebSocket . Server ( {
36
- port : 56789
37
- } ) ;
38
-
39
- wss . on ( 'connection' , ws => {
40
- wsList . push ( ws ) ;
41
- ws . on ( 'message' , msg => {
42
- webviewEventHandler ( JSON . parse ( msg . toString ( ) ) ) ;
43
- } ) ;
44
- } ) ;
45
- }
46
33
if ( vscode . window . terminals . some ( terminal => terminal . name . startsWith ( 'volar-preview:' ) ) ) {
47
- startWsServer ( ) ;
34
+ ws = preview . createPreviewWebSocket ( {
35
+ goToCode : handleGoToCode ,
36
+ getOpenFileUrl : ( fileName , range ) => 'vscode://files:/' + fileName ,
37
+ } ) ;
48
38
}
49
39
vscode . window . onDidOpenTerminal ( e => {
50
40
if ( e . name . startsWith ( 'volar-preview:' ) ) {
51
- startWsServer ( ) ;
41
+ ws = preview . createPreviewWebSocket ( {
42
+ goToCode : handleGoToCode ,
43
+ getOpenFileUrl : ( fileName , range ) => 'vscode://files:/' + fileName ,
44
+ } ) ;
52
45
}
53
46
} ) ;
54
47
vscode . window . onDidCloseTerminal ( e => {
55
48
if ( e . name . startsWith ( 'volar-preview:' ) ) {
56
- wss ?. close ( ) ;
57
- wsList . length = 0 ;
49
+ ws ?. stop ( ) ;
58
50
}
59
51
} ) ;
60
52
61
53
const sfcs = new WeakMap < vscode . TextDocument , { version : number , sfc : SFCParseResult } > ( ) ;
62
54
63
- let goToTemplateReq = 0 ;
64
-
65
55
class FinderPanelSerializer implements vscode . WebviewPanelSerializer {
66
56
async deserializeWebviewPanel ( panel : vscode . WebviewPanel , state : PreviewState ) {
67
57
@@ -166,31 +156,16 @@ export async function activate(context: vscode.ExtensionContext) {
166
156
}
167
157
} ) ) ;
168
158
context . subscriptions . push ( vscode . window . onDidChangeTextEditorSelection ( e => {
169
- for ( const panel of panels ) {
170
- updateSelectionHighlights ( e . textEditor , panel , undefined ) ;
171
- }
172
- for ( const ws of wsList ) {
173
- updateSelectionHighlights ( e . textEditor , undefined , ws ) ;
174
- }
159
+ updateSelectionHighlights ( e . textEditor ) ;
175
160
} ) ) ;
176
161
context . subscriptions . push ( vscode . workspace . onDidChangeTextDocument ( e => {
177
162
if ( vscode . window . activeTextEditor ) {
178
- for ( const panel of panels ) {
179
- updateSelectionHighlights ( vscode . window . activeTextEditor , panel , undefined ) ;
180
- }
181
- for ( const ws of wsList ) {
182
- updateSelectionHighlights ( vscode . window . activeTextEditor , undefined , ws ) ;
183
- }
163
+ updateSelectionHighlights ( vscode . window . activeTextEditor ) ;
184
164
}
185
165
} ) ) ;
186
166
context . subscriptions . push ( vscode . workspace . onDidSaveTextDocument ( e => {
187
167
if ( vscode . window . activeTextEditor ) {
188
- for ( const panel of panels ) {
189
- updateSelectionHighlights ( vscode . window . activeTextEditor , panel , undefined ) ;
190
- }
191
- for ( const ws of wsList ) {
192
- updateSelectionHighlights ( vscode . window . activeTextEditor , undefined , ws ) ;
193
- }
168
+ updateSelectionHighlights ( vscode . window . activeTextEditor ) ;
194
169
}
195
170
} ) ) ;
196
171
@@ -223,33 +198,21 @@ export async function activate(context: vscode.ExtensionContext) {
223
198
}
224
199
}
225
200
226
- function updateSelectionHighlights ( textEditor : vscode . TextEditor , panel : vscode . WebviewPanel | undefined , ws : WebSocket . WebSocket | undefined ) {
201
+ function updateSelectionHighlights ( textEditor : vscode . TextEditor ) {
227
202
if ( textEditor . document . languageId === 'vue' ) {
228
203
const sfc = getSfc ( textEditor . document ) ;
229
204
const offset = sfc . descriptor . template ?. loc . start . offset ?? 0 ;
230
- const msg = {
231
- sender : 'volar' ,
232
- command : 'highlightSelections' ,
233
- data : {
234
- fileName : textEditor . document . fileName ,
235
- ranges : textEditor . selections . map ( selection => ( {
236
- start : textEditor . document . offsetAt ( selection . start ) - offset ,
237
- end : textEditor . document . offsetAt ( selection . end ) - offset ,
238
- } ) ) ,
239
- isDirty : textEditor . document . isDirty ,
240
- } ,
241
- } ;
242
- panel ?. webview . postMessage ( msg ) ;
243
- ws ?. send ( JSON . stringify ( msg ) ) ;
205
+ ws ?. highlight (
206
+ textEditor . document . fileName ,
207
+ textEditor . selections . map ( selection => ( {
208
+ start : textEditor . document . offsetAt ( selection . start ) - offset ,
209
+ end : textEditor . document . offsetAt ( selection . end ) - offset ,
210
+ } ) ) ,
211
+ textEditor . document . isDirty ,
212
+ ) ;
244
213
}
245
214
else {
246
- const msg = {
247
- sender : 'volar' ,
248
- command : 'highlightSelections' ,
249
- data : undefined ,
250
- } ;
251
- panel ?. webview . postMessage ( JSON . stringify ( msg ) ) ;
252
- ws ?. send ( JSON . stringify ( msg ) ) ;
215
+ ws ?. unhighlight ( ) ;
253
216
}
254
217
}
255
218
@@ -394,33 +357,29 @@ export async function activate(context: vscode.ExtensionContext) {
394
357
vscode . window . showErrorMessage ( text ) ;
395
358
break ;
396
359
}
397
- case 'goToTemplate' : {
398
- const req = ++ goToTemplateReq ;
399
- const data = message . data as {
400
- fileName : string ,
401
- range : [ number , number ] ,
402
- } ;
403
- const doc = await vscode . workspace . openTextDocument ( data . fileName ) ;
404
-
405
- if ( req !== goToTemplateReq )
406
- return ;
407
-
408
- const sfc = getSfc ( doc ) ;
409
- const offset = sfc . descriptor . template ?. loc . start . offset ?? 0 ;
410
- const start = doc . positionAt ( data . range [ 0 ] + offset ) ;
411
- const end = doc . positionAt ( data . range [ 1 ] + offset ) ;
412
- await vscode . window . showTextDocument ( doc , vscode . ViewColumn . One ) ;
413
-
414
- if ( req !== goToTemplateReq )
415
- return ;
416
-
417
- const editor = vscode . window . activeTextEditor ;
418
- if ( editor ) {
419
- editor . selection = new vscode . Selection ( start , end ) ;
420
- editor . revealRange ( new vscode . Range ( start , end ) ) ;
421
- }
422
- break ;
423
- }
360
+ }
361
+ }
362
+
363
+ async function handleGoToCode ( fileName : string , range : [ number , number ] , cancleToken : { readonly isCancelled : boolean } ) {
364
+
365
+ const doc = await vscode . workspace . openTextDocument ( fileName ) ;
366
+
367
+ if ( cancleToken . isCancelled )
368
+ return ;
369
+
370
+ const sfc = getSfc ( doc ) ;
371
+ const offset = sfc . descriptor . template ?. loc . start . offset ?? 0 ;
372
+ const start = doc . positionAt ( range [ 0 ] + offset ) ;
373
+ const end = doc . positionAt ( range [ 1 ] + offset ) ;
374
+ await vscode . window . showTextDocument ( doc , vscode . ViewColumn . One ) ;
375
+
376
+ if ( cancleToken . isCancelled )
377
+ return ;
378
+
379
+ const editor = vscode . window . activeTextEditor ;
380
+ if ( editor ) {
381
+ editor . selection = new vscode . Selection ( start , end ) ;
382
+ editor . revealRange ( new vscode . Range ( start , end ) ) ;
424
383
}
425
384
}
426
385
@@ -429,8 +388,8 @@ export async function activate(context: vscode.ExtensionContext) {
429
388
const port = await shared . getLocalHostAvaliablePort ( vscode . workspace . getConfiguration ( 'volar' ) . get ( 'preview.port' ) ?? 3334 ) ;
430
389
const terminal = vscode . window . createTerminal ( 'volar-preview:' + port ) ;
431
390
const viteProxyPath = type === 'vite'
432
- ? require . resolve ( './bin/vite' , { paths : [ context . extensionPath ] } )
433
- : require . resolve ( './bin/nuxi' , { paths : [ context . extensionPath ] } ) ;
391
+ ? require . resolve ( './dist/preview- bin/vite' , { paths : [ context . extensionPath ] } )
392
+ : require . resolve ( './dist/preview- bin/nuxi' , { paths : [ context . extensionPath ] } ) ;
434
393
435
394
terminal . sendText ( `cd ${ viteDir } ` ) ;
436
395
0 commit comments