@@ -372,6 +372,45 @@ export function syntaxTreeReveal(): Cmd {
372
372
} ;
373
373
}
374
374
375
+ function elementToString (
376
+ activeDocument : vscode . TextDocument ,
377
+ element : SyntaxElement ,
378
+ depth : number = 0 ,
379
+ ) : string {
380
+ let result = " " . repeat ( depth ) ;
381
+ const start = element . istart ?? element . start ;
382
+ const end = element . iend ?? element . end ;
383
+
384
+ result += `${ element . kind } @${ start } ..${ end } ` ;
385
+
386
+ if ( element . type === "Token" ) {
387
+ const startPosition = activeDocument . positionAt ( element . start ) ;
388
+ const endPosition = activeDocument . positionAt ( element . end ) ;
389
+ const text = activeDocument . getText ( new vscode . Range ( startPosition , endPosition ) ) ;
390
+ // JSON.stringify quotes and escapes the string for us.
391
+ result += ` ${ JSON . stringify ( text ) } \n` ;
392
+ } else {
393
+ result += "\n" ;
394
+ for ( const child of element . children ) {
395
+ result += elementToString ( activeDocument , child , depth + 1 ) ;
396
+ }
397
+ }
398
+
399
+ return result ;
400
+ }
401
+
402
+ export function syntaxTreeCopy ( ) : Cmd {
403
+ return async ( element : SyntaxElement ) => {
404
+ const activeDocument = vscode . window . activeTextEditor ?. document ;
405
+ if ( ! activeDocument ) {
406
+ return ;
407
+ }
408
+
409
+ const result = elementToString ( activeDocument , element ) ;
410
+ await vscode . env . clipboard . writeText ( result ) ;
411
+ } ;
412
+ }
413
+
375
414
export function syntaxTreeHideWhitespace ( ctx : CtxInit ) : Cmd {
376
415
return async ( ) => {
377
416
if ( ctx . syntaxTreeProvider !== undefined ) {
0 commit comments