@@ -6,27 +6,32 @@ import * as vscode from "vscode";
66import { getFullTextRange } from "./utils" ;
77
88export function register ( ) {
9- const configuration = vscode . workspace . getConfiguration ( "ocaml- reason-format " ) ;
9+ const configuration = vscode . workspace . getConfiguration ( "reason" ) ;
1010 const rootPath = vscode . workspace . rootPath || "" ;
1111
12- vscode . languages . registerDocumentFormattingEditProvider ( "ocaml" , {
13- provideDocumentFormattingEdits ( _document : vscode . TextDocument ) : vscode . TextEdit [ ] {
14- const formatterPath = configuration . get < string | undefined > ( "ocamlformat" ) ;
15- const formatter = formatterPath ? path . resolve ( rootPath , formatterPath ) : "ocamlformat" ;
16- const textEditor = vscode . window . activeTextEditor ;
12+ vscode . languages . registerDocumentFormattingEditProvider (
13+ { scheme : "file" , language : "ocaml" } ,
14+ {
15+ provideDocumentFormattingEdits ( _document : vscode . TextDocument ) : vscode . TextEdit [ ] {
16+ const formatterPath = configuration . get < string | undefined > ( "path.ocamlformat" ) ;
17+ const formatter = formatterPath ? path . resolve ( rootPath , formatterPath ) : "ocamlformat" ;
18+ const textEditor = vscode . window . activeTextEditor ;
1719
18- if ( textEditor ) {
19- const tempFileName = `/tmp/vscode-reasonml-${ uuidv4 ( ) } ` ;
20- fs . writeFileSync ( tempFileName , textEditor . document . getText ( ) , "utf8" ) ;
21- const filePath = textEditor . document . fileName ;
22- const formattedText = execSync ( `cd ${ rootPath } && ${ formatter } --name=${ filePath } ${ tempFileName } ` ) . toString ( ) ;
23- fs . unlinkSync ( tempFileName ) ;
20+ if ( textEditor ) {
21+ const tempFileName = `/tmp/vscode-reasonml-${ uuidv4 ( ) } .ml` ;
22+ fs . writeFileSync ( tempFileName , textEditor . document . getText ( ) , "utf8" ) ;
23+ const filePath = textEditor . document . fileName ;
24+ const formattedText = execSync (
25+ `cd ${ rootPath } && ${ formatter } --name=${ filePath } ${ tempFileName } ` ,
26+ ) . toString ( ) ;
27+ fs . unlinkSync ( tempFileName ) ;
2428
25- const textRange = getFullTextRange ( textEditor ) ;
26- return [ vscode . TextEdit . replace ( textRange , formattedText ) ] ;
27- } else {
28- return [ ] ;
29- }
29+ const textRange = getFullTextRange ( textEditor ) ;
30+ return [ vscode . TextEdit . replace ( textRange , formattedText ) ] ;
31+ } else {
32+ return [ ] ;
33+ }
34+ } ,
3035 } ,
31- } ) ;
36+ ) ;
3237}
0 commit comments