|
| 1 | +open Analysis |
| 2 | + |
| 3 | +let help = |
| 4 | + {| |
| 5 | +**Private CLI For rescript-vscode usage only** |
| 6 | + |
| 7 | +API examples: |
| 8 | + ./rescript-editor-analysis.exe completion src/MyFile.res 0 4 currentContent.res true |
| 9 | + ./rescript-editor-analysis.exe definition src/MyFile.res 9 3 |
| 10 | + ./rescript-editor-analysis.exe typeDefinition src/MyFile.res 9 3 |
| 11 | + ./rescript-editor-analysis.exe documentSymbol src/Foo.res |
| 12 | + ./rescript-editor-analysis.exe hover src/MyFile.res 10 2 true |
| 13 | + ./rescript-editor-analysis.exe references src/MyFile.res 10 2 |
| 14 | + ./rescript-editor-analysis.exe rename src/MyFile.res 10 2 foo |
| 15 | + ./rescript-editor-analysis.exe diagnosticSyntax src/MyFile.res |
| 16 | + ./rescript-editor-analysis.exe inlayHint src/MyFile.res 0 3 25 |
| 17 | + ./rescript-editor-analysis.exe codeLens src/MyFile.res |
| 18 | + |
| 19 | +Dev-time examples: |
| 20 | + ./rescript-editor-analysis.exe dump src/MyFile.res src/MyFile2.res |
| 21 | + ./rescript-editor-analysis.exe test src/MyFile.res |
| 22 | + |
| 23 | +Note: positions are zero-indexed (start at 0 0), following LSP. |
| 24 | +https://microsoft.github.io/language-server-protocol/specification#position |
| 25 | + |
| 26 | +Options: |
| 27 | + completion: compute autocomplete for MyFile.res at line 0 and column 4, |
| 28 | + where MyFile.res is being edited and the editor content is in file current.res. |
| 29 | + |
| 30 | + ./rescript-editor-analysis.exe completion src/MyFile.res 0 4 current.res |
| 31 | + |
| 32 | + definition: get definition for item in MyFile.res at line 10 column 2: |
| 33 | + |
| 34 | + ./rescript-editor-analysis.exe definition src/MyFile.res 10 2 |
| 35 | + |
| 36 | + typeDefinition: get type definition for item in MyFile.res at line 10 column 2: |
| 37 | + |
| 38 | + ./rescript-editor-analysis.exe typeDefinition src/MyFile.res 10 2 |
| 39 | + |
| 40 | + documentSymbol: get all symbols declared in MyFile.res |
| 41 | + |
| 42 | + ./rescript-editor-analysis.exe documentSymbol src/MyFile.res |
| 43 | + |
| 44 | + hover: get inferred type for MyFile.res at line 10 column 2 (supporting markdown links): |
| 45 | + |
| 46 | + ./rescript-editor-analysis.exe hover src/MyFile.res 10 2 true |
| 47 | + |
| 48 | + references: get all references to item in MyFile.res at line 10 column 2: |
| 49 | + |
| 50 | + ./rescript-editor-analysis.exe references src/MyFile.res 10 2 |
| 51 | + |
| 52 | + rename: rename all appearances of item in MyFile.res at line 10 column 2 with foo: |
| 53 | + |
| 54 | + ./rescript-editor-analysis.exe rename src/MyFile.res 10 2 foo |
| 55 | + |
| 56 | + semanticTokens: return token semantic highlighting info for MyFile.res |
| 57 | + |
| 58 | + ./rescript-editor-analysis.exe semanticTokens src/MyFile.res |
| 59 | + |
| 60 | + createInterface: print to stdout the interface file for src/MyFile.res |
| 61 | + |
| 62 | + ./rescript-editor-analysis.exe createInterface src/MyFile.res lib/bs/src/MyFile.cmi |
| 63 | + |
| 64 | + format: print to stdout the formatted version of the provided file |
| 65 | + |
| 66 | + ./rescript-editor-analysis.exe format src/MyFile.res |
| 67 | + |
| 68 | + diagnosticSyntax: print to stdout diagnostic for syntax |
| 69 | + |
| 70 | + ./rescript-editor-analysis.exe diagnosticSyntax src/MyFile.res |
| 71 | + |
| 72 | + inlayHint: get all inlay Hint between line 0 and 3 declared in MyFile.res. Last argument is maximum of character length for inlay hints |
| 73 | + |
| 74 | + ./rescript-editor-analysis.exe inlayHint src/MyFile.res 0 3 25 |
| 75 | + |
| 76 | + codeLens: get all code lens entries for file src/MyFile.res |
| 77 | + |
| 78 | + ./rescript-editor-analysis.exe codeLens src/MyFile.res |
| 79 | + |
| 80 | + signatureHelp: get signature help if available for position at line 10 column 2 in src/MyFile.res |
| 81 | + |
| 82 | + ./rescript-editor-analysis.exe signatureHelp src/MyFile.res 10 2 |
| 83 | + |
| 84 | + test: run tests specified by special comments in file src/MyFile.res |
| 85 | + |
| 86 | + ./rescript-editor-analysis.exe test src/src/MyFile.res |
| 87 | +|} |
| 88 | + |
| 89 | +let main () = |
| 90 | + let args = Array.to_list Sys.argv in |
| 91 | + let debugLevel, args = |
| 92 | + match args with |
| 93 | + | _ :: "debug-dump" :: logLevel :: rest -> |
| 94 | + ( (match logLevel with |
| 95 | + | "verbose" -> Debug.Verbose |
| 96 | + | "regular" -> Regular |
| 97 | + | _ -> Off), |
| 98 | + "dummy" :: rest ) |
| 99 | + | args -> (Off, args) |
| 100 | + in |
| 101 | + Debug.debugLevel := debugLevel; |
| 102 | + let debug = debugLevel <> Debug.Off in |
| 103 | + let printHeaderInfo path line col = |
| 104 | + if debug then |
| 105 | + Printf.printf "Debug level: %s\n%s:%s-%s\n\n" |
| 106 | + (match debugLevel with |
| 107 | + | Debug.Verbose -> "verbose" |
| 108 | + | Regular -> "regular" |
| 109 | + | Off -> "off") |
| 110 | + path line col |
| 111 | + in |
| 112 | + match args with |
| 113 | + | [_; "cache-project"; rootPath] -> ( |
| 114 | + Cfg.readProjectConfigCache := false; |
| 115 | + let uri = Uri.fromPath rootPath in |
| 116 | + match Packages.getPackage ~uri with |
| 117 | + | Some package -> Cache.cacheProject package |
| 118 | + | None -> print_endline "\"ERR\"") |
| 119 | + | [_; "cache-delete"; rootPath] -> ( |
| 120 | + Cfg.readProjectConfigCache := false; |
| 121 | + let uri = Uri.fromPath rootPath in |
| 122 | + match Packages.findRoot ~uri (Hashtbl.create 0) with |
| 123 | + | Some (`Bs rootPath) -> ( |
| 124 | + match BuildSystem.getLibBs rootPath with |
| 125 | + | None -> print_endline "\"ERR\"" |
| 126 | + | Some libBs -> |
| 127 | + Cache.deleteCache (Cache.targetFileFromLibBs libBs); |
| 128 | + print_endline "\"OK\"") |
| 129 | + | _ -> print_endline "\"ERR: Did not find root \"") |
| 130 | + | [_; "completion"; path; line; col; currentFile] -> |
| 131 | + printHeaderInfo path line col; |
| 132 | + Commands.completion ~debug ~path |
| 133 | + ~pos:(int_of_string line, int_of_string col) |
| 134 | + ~currentFile |
| 135 | + | [_; "completionResolve"; path; modulePath] -> |
| 136 | + Commands.completionResolve ~path ~modulePath |
| 137 | + | [_; "definition"; path; line; col] -> |
| 138 | + Commands.definition ~path |
| 139 | + ~pos:(int_of_string line, int_of_string col) |
| 140 | + ~debug |
| 141 | + | [_; "typeDefinition"; path; line; col] -> |
| 142 | + Commands.typeDefinition ~path |
| 143 | + ~pos:(int_of_string line, int_of_string col) |
| 144 | + ~debug |
| 145 | + | [_; "documentSymbol"; path] -> DocumentSymbol.command ~path |
| 146 | + | [_; "hover"; path; line; col; currentFile; supportsMarkdownLinks] -> |
| 147 | + Commands.hover ~path |
| 148 | + ~pos:(int_of_string line, int_of_string col) |
| 149 | + ~currentFile ~debug |
| 150 | + ~supportsMarkdownLinks: |
| 151 | + (match supportsMarkdownLinks with |
| 152 | + | "true" -> true |
| 153 | + | _ -> false) |
| 154 | + | [ |
| 155 | + _; "signatureHelp"; path; line; col; currentFile; allowForConstructorPayloads; |
| 156 | + ] -> |
| 157 | + Commands.signatureHelp ~path |
| 158 | + ~pos:(int_of_string line, int_of_string col) |
| 159 | + ~currentFile ~debug |
| 160 | + ~allowForConstructorPayloads: |
| 161 | + (match allowForConstructorPayloads with |
| 162 | + | "true" -> true |
| 163 | + | _ -> false) |
| 164 | + | [_; "inlayHint"; path; line_start; line_end; maxLength] -> |
| 165 | + Commands.inlayhint ~path |
| 166 | + ~pos:(int_of_string line_start, int_of_string line_end) |
| 167 | + ~maxLength ~debug |
| 168 | + | [_; "codeLens"; path] -> Commands.codeLens ~path ~debug |
| 169 | + | [_; "codeAction"; path; startLine; startCol; endLine; endCol; currentFile] |
| 170 | + -> |
| 171 | + Commands.codeAction ~path |
| 172 | + ~startPos:(int_of_string startLine, int_of_string startCol) |
| 173 | + ~endPos:(int_of_string endLine, int_of_string endCol) |
| 174 | + ~currentFile ~debug |
| 175 | + | [_; "codemod"; path; line; col; typ; hint] -> |
| 176 | + let typ = |
| 177 | + match typ with |
| 178 | + | "add-missing-cases" -> Codemod.AddMissingCases |
| 179 | + | _ -> raise (Failure "unsupported type") |
| 180 | + in |
| 181 | + let res = |
| 182 | + Codemod.transform ~path |
| 183 | + ~pos:(int_of_string line, int_of_string col) |
| 184 | + ~debug ~typ ~hint |
| 185 | + |> Json.escape |
| 186 | + in |
| 187 | + Printf.printf "\"%s\"" res |
| 188 | + | [_; "diagnosticSyntax"; path] -> Commands.diagnosticSyntax ~path |
| 189 | + | _ :: "reanalyze" :: _ -> |
| 190 | + let len = Array.length Sys.argv in |
| 191 | + for i = 1 to len - 2 do |
| 192 | + Sys.argv.(i) <- Sys.argv.(i + 1) |
| 193 | + done; |
| 194 | + Sys.argv.(len - 1) <- ""; |
| 195 | + Reanalyze.cli () |
| 196 | + | [_; "references"; path; line; col] -> |
| 197 | + Commands.references ~path |
| 198 | + ~pos:(int_of_string line, int_of_string col) |
| 199 | + ~debug |
| 200 | + | [_; "rename"; path; line; col; newName] -> |
| 201 | + Commands.rename ~path |
| 202 | + ~pos:(int_of_string line, int_of_string col) |
| 203 | + ~newName ~debug |
| 204 | + | [_; "semanticTokens"; currentFile] -> |
| 205 | + SemanticTokens.semanticTokens ~currentFile |
| 206 | + | [_; "createInterface"; path; cmiFile] -> |
| 207 | + Printf.printf "\"%s\"" |
| 208 | + (Json.escape (CreateInterface.command ~path ~cmiFile)) |
| 209 | + | [_; "format"; path] -> |
| 210 | + Printf.printf "\"%s\"" (Json.escape (Commands.format ~path)) |
| 211 | + | [_; "test"; path] -> Commands.test ~path |
| 212 | + | args when List.mem "-h" args || List.mem "--help" args -> prerr_endline help |
| 213 | + | _ -> |
| 214 | + prerr_endline help; |
| 215 | + exit 1 |
| 216 | +;; |
| 217 | + |
| 218 | +main () |
0 commit comments