Skip to content

Commit ecb1bba

Browse files
authored
Refactor analysis to decouple I/O from core logic (#8426)
* Refactor analysis to decouple I/O from core logic Splits `Commands.ml` into a pure layer that returns OCaml values (option, list, typed records like Protocol.hover, Protocol.signatureHelp, Protocol.completionItem) and a new `analysis/src/Cli.ml` that does the stringify-and-print step. `analysis/bin/main.ml` now dispatches to `Cli.*`, while the LSP server consumes `Commands.*` directly. Makes the parsers accept source strings: `res_driver` gains `parse_interface_from_source` alongside the existing `parse_implementation_from_source` * Rename `tokenModifiersString` to `tokenModifiers` with correct type * update CHANGELOG.md * Fix format range to use accurate line/character positions The previous implementation used the source byte length as both line and character values for the end of the format range, which was incorrect. Replace it with a helper that computes the actual final line index and character offset by splitting on newlines. Signed-Off-By: Your Name <email> * Refactor token debugger to use typed input variant --------- Signed-off-by: Your Name <email>
1 parent de28ae6 commit ecb1bba

20 files changed

Lines changed: 819 additions & 529 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
- Build system: Add OpenTelemetry tracing support for cli commands. https://github.com/rescript-lang/rescript/pull/8370
4141
- Use a single vendored @rescript/react package across the repo. https://github.com/rescript-lang/rescript/pull/7525
4242
- Improve deprecated attribute extraction and support record form. https://github.com/rescript-lang/rescript/pull/8396
43+
- Refactor analysis to decouple I/O from core logic. https://github.com/rescript-lang/rescript/pull/8426
4344

4445
#### :house: Internal
4546

analysis/bin/main.ml

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -134,22 +134,18 @@ let main () =
134134
| _ -> print_endline "\"ERR: Did not find root \"")
135135
| [_; "completion"; path; line; col; currentFile] ->
136136
printHeaderInfo path line col;
137-
Commands.completion ~debug ~path
137+
Cli.completion ~debug ~path
138138
~pos:(int_of_string line, int_of_string col)
139139
~currentFile
140140
| [_; "completionResolve"; path; modulePath] ->
141-
Commands.completionResolve ~path ~modulePath
141+
Cli.completionResolve ~path ~modulePath
142142
| [_; "definition"; path; line; col] ->
143-
Commands.definition ~path
144-
~pos:(int_of_string line, int_of_string col)
145-
~debug
143+
Cli.definition ~path ~pos:(int_of_string line, int_of_string col) ~debug
146144
| [_; "typeDefinition"; path; line; col] ->
147-
Commands.typeDefinition ~path
148-
~pos:(int_of_string line, int_of_string col)
149-
~debug
145+
Cli.typeDefinition ~path ~pos:(int_of_string line, int_of_string col) ~debug
150146
| [_; "documentSymbol"; path] -> DocumentSymbol.command ~path
151147
| [_; "hover"; path; line; col; currentFile; supportsMarkdownLinks] ->
152-
Commands.hover ~path
148+
Cli.hover ~path
153149
~pos:(int_of_string line, int_of_string col)
154150
~currentFile ~debug
155151
~supportsMarkdownLinks:
@@ -159,21 +155,21 @@ let main () =
159155
| [
160156
_; "signatureHelp"; path; line; col; currentFile; allowForConstructorPayloads;
161157
] ->
162-
Commands.signatureHelp ~path
158+
Cli.signatureHelp ~path
163159
~pos:(int_of_string line, int_of_string col)
164160
~currentFile ~debug
165161
~allowForConstructorPayloads:
166162
(match allowForConstructorPayloads with
167163
| "true" -> true
168164
| _ -> false)
169165
| [_; "inlayHint"; path; line_start; line_end; maxLength] ->
170-
Commands.inlayhint ~path
166+
Cli.inlayhint ~path
171167
~pos:(int_of_string line_start, int_of_string line_end)
172168
~maxLength ~debug
173-
| [_; "codeLens"; path] -> Commands.codeLens ~path ~debug
169+
| [_; "codeLens"; path] -> Cli.codeLens ~path ~debug
174170
| [_; "codeAction"; path; startLine; startCol; endLine; endCol; currentFile]
175171
->
176-
Commands.codeAction ~path
172+
Cli.codeAction ~path
177173
~startPos:(int_of_string startLine, int_of_string startCol)
178174
~endPos:(int_of_string endLine, int_of_string endCol)
179175
~currentFile ~debug
@@ -183,34 +179,29 @@ let main () =
183179
| "add-missing-cases" -> Codemod.AddMissingCases
184180
| _ -> raise (Failure "unsupported type")
185181
in
182+
let source = Files.readFile path |> Option.value ~default:"" in
186183
let res =
187-
Codemod.transform ~path
184+
Codemod.transform ~source
188185
~pos:(int_of_string line, int_of_string col)
189186
~debug ~typ ~hint
190187
|> Json.escape
191188
in
192189
Printf.printf "\"%s\"" res
193-
| [_; "diagnosticSyntax"; path] -> Commands.diagnosticSyntax ~path
190+
| [_; "diagnosticSyntax"; path] -> Cli.diagnosticSyntax ~path
194191
| [_; "references"; path; line; col] ->
195-
Commands.references ~path
196-
~pos:(int_of_string line, int_of_string col)
197-
~debug
192+
Cli.references ~path ~pos:(int_of_string line, int_of_string col) ~debug
198193
| [_; "prepareRename"; path; line; col] ->
199-
Commands.prepareRename ~path
200-
~pos:(int_of_string line, int_of_string col)
201-
~debug
194+
Cli.prepareRename ~path ~pos:(int_of_string line, int_of_string col) ~debug
202195
| [_; "rename"; path; line; col; newName] ->
203-
Commands.rename ~path
196+
Cli.rename ~path
204197
~pos:(int_of_string line, int_of_string col)
205198
~newName ~debug
206-
| [_; "semanticTokens"; currentFile] ->
207-
SemanticTokens.semanticTokens ~currentFile
199+
| [_; "semanticTokens"; currentFile] -> Cli.semanticTokens ~path:currentFile
208200
| [_; "createInterface"; path; cmiFile] ->
209201
Printf.printf "\"%s\""
210202
(Json.escape (CreateInterface.command ~path ~cmiFile))
211-
| [_; "format"; path] ->
212-
Printf.printf "\"%s\"" (Json.escape (Commands.format ~path))
213-
| [_; "test"; path] -> Commands.test ~path
203+
| [_; "format"; path] -> Cli.format ~path
204+
| [_; "test"; path] -> Cli.test ~path
214205
| [_; "cmt"; rescript_json; cmt_path] -> CmtViewer.dump rescript_json cmt_path
215206
| args when List.mem "-h" args || List.mem "--help" args -> prerr_endline help
216207
| _ ->

0 commit comments

Comments
 (0)