@@ -9,6 +9,7 @@ import * as cp from "node:child_process";
99import config , { send } from "./config" ;
1010import * as c from "./constants" ;
1111import * as chokidar from "chokidar" ;
12+ import { fileCodeActions } from "./codeActions" ;
1213
1314function debug ( ) {
1415 return (
@@ -65,6 +66,8 @@ type IncrementallyCompiledFileInfo = {
6566 /** The ReScript version. */
6667 rescriptVersion : string ;
6768 } ;
69+ /** Any code actions for this incremental file. */
70+ codeActions : Array < fileCodeActions > ;
6871} ;
6972
7073const incrementallyCompiledFileInfo : Map <
@@ -368,6 +371,7 @@ function triggerIncrementalCompilationOfFile(
368371 buildNinja : null ,
369372 compilation : null ,
370373 killCompilationListeners : [ ] ,
374+ codeActions : [ ] ,
371375 } ;
372376
373377 incrementalFileCacheEntry . project . callArgs = figureOutBscArgs (
@@ -526,7 +530,30 @@ async function compileContents(
526530 }
527531 // Reset compilation status as this compilation finished
528532 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+
530557 const res = ( Object . values ( result ) [ 0 ] ?? [ ] )
531558 . map ( ( d ) => ( {
532559 ...d ,
@@ -628,3 +655,14 @@ export function handleClosedFile(filePath: string) {
628655 originalTypeFileToFilePath . delete ( entry . file . originalTypeFileLocation ) ;
629656 incrementalFilesWatcher . unwatch ( [ entry . file . originalTypeFileLocation ] ) ;
630657}
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