@@ -9,6 +9,7 @@ import * as cp from "node:child_process";
9
9
import config , { send } from "./config" ;
10
10
import * as c from "./constants" ;
11
11
import * as chokidar from "chokidar" ;
12
+ import { fileCodeActions } from "./codeActions" ;
12
13
13
14
function debug ( ) {
14
15
return (
@@ -65,6 +66,8 @@ type IncrementallyCompiledFileInfo = {
65
66
/** The ReScript version. */
66
67
rescriptVersion : string ;
67
68
} ;
69
+ /** Any code actions for this incremental file. */
70
+ codeActions : Array < fileCodeActions > ;
68
71
} ;
69
72
70
73
const incrementallyCompiledFileInfo : Map <
@@ -368,6 +371,7 @@ function triggerIncrementalCompilationOfFile(
368
371
buildNinja : null ,
369
372
compilation : null ,
370
373
killCompilationListeners : [ ] ,
374
+ codeActions : [ ] ,
371
375
} ;
372
376
373
377
incrementalFileCacheEntry . project . callArgs = figureOutBscArgs (
@@ -526,7 +530,30 @@ async function compileContents(
526
530
}
527
531
// Reset compilation status as this compilation finished
528
532
entry . compilation = null ;
529
- const { result } = utils . parseCompilerLogOutput ( `${ stderr } \n#Done()` ) ;
533
+ const { result, codeActions } = utils . parseCompilerLogOutput (
534
+ `${ stderr } \n#Done()`
535
+ ) ;
536
+
537
+ const actions = Object . values ( codeActions ) [ 0 ] ?? [ ] ;
538
+
539
+ // Code actions will point to the locally saved incremental file, so we must remap
540
+ // them so the editor understand it's supposed to apply them to the unsaved doc,
541
+ // not the saved "dummy" incremental file.
542
+ actions . forEach ( ( ca ) => {
543
+ if (
544
+ ca . codeAction . edit != null &&
545
+ ca . codeAction . edit . changes != null
546
+ ) {
547
+ const change = Object . values ( ca . codeAction . edit . changes ) [ 0 ] ;
548
+
549
+ ca . codeAction . edit . changes = {
550
+ [ pathToFileURL ( entry . file . sourceFilePath ) . toString ( ) ] : change ,
551
+ } ;
552
+ }
553
+ } ) ;
554
+
555
+ entry . codeActions = actions ;
556
+
530
557
const res = ( Object . values ( result ) [ 0 ] ?? [ ] )
531
558
. map ( ( d ) => ( {
532
559
...d ,
@@ -628,3 +655,14 @@ export function handleClosedFile(filePath: string) {
628
655
originalTypeFileToFilePath . delete ( entry . file . originalTypeFileLocation ) ;
629
656
incrementalFilesWatcher . unwatch ( [ entry . file . originalTypeFileLocation ] ) ;
630
657
}
658
+
659
+ export function getCodeActionsFromIncrementalCompilation (
660
+ filePath : string
661
+ ) : Array < fileCodeActions > | null {
662
+ const entry = incrementallyCompiledFileInfo . get ( filePath ) ;
663
+ if ( entry != null ) {
664
+ return entry . codeActions ;
665
+ }
666
+
667
+ return null ;
668
+ }
0 commit comments