This repository was archived by the owner on Mar 21, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +79
-5
lines changed Expand file tree Collapse file tree 5 files changed +79
-5
lines changed Original file line number Diff line number Diff line change 8686 "description" :
8787 " The path to the `env` command which prints the language server environment for debugging editor issues."
8888 },
89+ "reason.path.ocamlformat" : {
90+ "type" : " string" ,
91+ "default" : " ocamlformat" ,
92+ "description" : " The path to the `ocamlformat` binary."
93+ },
8994 "reason.path.ocamlmerlin" : {
9095 "type" : " string" ,
9196 "default" : " ocamlmerlin" ,
96101 "default" : null ,
97102 "description" : " The path to the `ocamlmerlin-lsp` binary."
98103 },
99- "reason.path.ocpindent" : {
100- "type" : " string" ,
101- "default" : " ocp-indent" ,
102- "description" : " The path to the `ocp-indent` binary."
103- },
104104 "reason.path.opam" : {
105105 "type" : " string" ,
106106 "default" : " opam" ,
Original file line number Diff line number Diff line change 22
33import * as vscode from "vscode" ;
44import * as client from "./client" ;
5+ import { register as registerOcamlForamtter } from "./formatters/ocaml" ;
6+ import { register as registerReasonForamtter } from "./formatters/reason" ;
57
68const reasonConfiguration = {
79 indentationRules : {
@@ -93,6 +95,8 @@ const reasonConfiguration = {
9395export async function activate ( context : vscode . ExtensionContext ) {
9496 context . subscriptions . push ( vscode . languages . setLanguageConfiguration ( "reason" , reasonConfiguration ) ) ;
9597 await client . launch ( context ) ;
98+ registerOcamlForamtter ( ) ;
99+ registerReasonForamtter ( ) ;
96100}
97101
98102export function deactivate ( ) {
Original file line number Diff line number Diff line change 1+ import { execSync } from "child_process" ;
2+ import * as path from "path" ;
3+ import * as vscode from "vscode" ;
4+ import { getFullTextRange } from "./utils" ;
5+
6+ export function register ( ) {
7+ const configuration = vscode . workspace . getConfiguration ( "ocaml-reason-format" ) ;
8+ const rootPath = vscode . workspace . rootPath || "" ;
9+
10+ vscode . languages . registerDocumentFormattingEditProvider ( "ocaml" , {
11+ provideDocumentFormattingEdits ( _document : vscode . TextDocument ) : vscode . TextEdit [ ] {
12+ const formatterPath = configuration . get < string | undefined > ( "ocamlformat" ) ;
13+ const formatter = formatterPath ? path . resolve ( rootPath , formatterPath ) : "ocamlformat" ;
14+ const textEditor = vscode . window . activeTextEditor ;
15+
16+ if ( textEditor ) {
17+ const text = textEditor . document . getText ( ) ;
18+ const filePath = textEditor . document . fileName ;
19+ const formattedText = execSync ( `cd ${ rootPath } && ${ formatter } --name=${ filePath } /dev/stdin <<<'${ text } '` , {
20+ cwd : rootPath ,
21+ } ) . toString ( ) ;
22+ const textRange = getFullTextRange ( textEditor ) ;
23+
24+ return [ vscode . TextEdit . replace ( textRange , formattedText ) ] ;
25+ } else {
26+ return [ ] ;
27+ }
28+ } ,
29+ } ) ;
30+ }
Original file line number Diff line number Diff line change 1+ import { execSync } from "child_process" ;
2+ import * as path from "path" ;
3+ import * as vscode from "vscode" ;
4+ import { getFullTextRange } from "./utils" ;
5+
6+ export function register ( ) {
7+ const configuration = vscode . workspace . getConfiguration ( "ocaml-reason-format" ) ;
8+ const rootPath = vscode . workspace . rootPath || "" ;
9+
10+ vscode . languages . registerDocumentFormattingEditProvider ( "reason" , {
11+ provideDocumentFormattingEdits ( _document : vscode . TextDocument ) : vscode . TextEdit [ ] {
12+ const formatterPath = configuration . get < string | undefined > ( "refmt" ) ;
13+ const formatter = formatterPath ? path . resolve ( rootPath , formatterPath ) : "refmt" ;
14+ const textEditor = vscode . window . activeTextEditor ;
15+
16+ if ( textEditor ) {
17+ const text = textEditor . document . getText ( ) ;
18+ const formattedText = execSync ( `${ formatter } <<<'${ text } '` ) . toString ( ) ;
19+ const textRange = getFullTextRange ( textEditor ) ;
20+
21+ return [ vscode . TextEdit . replace ( textRange , formattedText ) ] ;
22+ } else {
23+ return [ ] ;
24+ }
25+ } ,
26+ } ) ;
27+ }
Original file line number Diff line number Diff line change 1+ import * as vscode from "vscode" ;
2+
3+ export function getFullTextRange ( textEditor : vscode . TextEditor ) {
4+ const firstLine = textEditor . document . lineAt ( 0 ) ;
5+ const lastLine = textEditor . document . lineAt ( textEditor . document . lineCount - 1 ) ;
6+
7+ return new vscode . Range (
8+ 0 ,
9+ firstLine . range . start . character ,
10+ textEditor . document . lineCount - 1 ,
11+ lastLine . range . end . character ,
12+ ) ;
13+ }
You can’t perform that action at this time.
0 commit comments