Skip to content

Commit cd00b98

Browse files
committed
OCaml: analysis commands, completions, hints, llm_index, syntax driver
1 parent f55ab2f commit cd00b98

31 files changed

Lines changed: 1426 additions & 189 deletions

analysis/bin/main.ml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,24 @@ let main () =
132132
Cache.deleteCache (Cache.targetFileFromLibBs libBs);
133133
print_endline "\"OK\"")
134134
| _ -> print_endline "\"ERR: Did not find root \"")
135+
| _ :: "rewatch" :: rewatchArgs -> (
136+
match rewatchArgs with
137+
| ["completion"] -> CommandsRewatch.completion ()
138+
| ["completionResolve"] -> CommandsRewatch.completionResolve ()
139+
| ["hover"] -> CommandsRewatch.hover ()
140+
| ["definition"] -> CommandsRewatch.definition ()
141+
| ["typeDefinition"] -> CommandsRewatch.typeDefinition ()
142+
| ["references"] -> CommandsRewatch.references ()
143+
| ["documentSymbol"] -> CommandsRewatch.documentSymbol ()
144+
| ["prepareRename"] -> CommandsRewatch.prepareRename ()
145+
| ["rename"] -> CommandsRewatch.rename ()
146+
| ["signatureHelp"] -> CommandsRewatch.signatureHelp ()
147+
| ["codeLens"] -> CommandsRewatch.codeLens ()
148+
| ["inlayHint"] -> CommandsRewatch.inlayHint ()
149+
| ["semanticTokens"] -> CommandsRewatch.semanticTokens ()
150+
| ["codeAction"] -> CommandsRewatch.codeAction ()
151+
| ["llmIndex"] -> CommandsRewatch.llmIndex ()
152+
| _ -> prerr_endline "Unknown rewatch subcommand")
135153
| [_; "completion"; path; line; col; currentFile] ->
136154
printHeaderInfo path line col;
137155
Commands.completion ~debug ~path
@@ -147,7 +165,13 @@ let main () =
147165
Commands.typeDefinition ~path
148166
~pos:(int_of_string line, int_of_string col)
149167
~debug
150-
| [_; "documentSymbol"; path] -> DocumentSymbol.command ~path
168+
| [_; "documentSymbol"; path] ->
169+
let source =
170+
match Files.readFile path with
171+
| Some s -> s
172+
| None -> ""
173+
in
174+
print_endline (DocumentSymbol.command ~path ~source)
151175
| [_; "hover"; path; line; col; currentFile; supportsMarkdownLinks] ->
152176
Commands.hover ~path
153177
~pos:(int_of_string line, int_of_string col)

analysis/reanalyze/src/Paths.ml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,23 @@ type cmt_scan_entry = {
215215
216216
If missing, returns the empty list and callers should fall back to legacy behavior. *)
217217

218+
let newerFile a b =
219+
match (Sys.file_exists a, Sys.file_exists b) with
220+
| true, true ->
221+
let mtime_a = (Unix.stat a).st_mtime in
222+
let mtime_b = (Unix.stat b).st_mtime in
223+
if mtime_b > mtime_a then b else a
224+
| true, false -> a
225+
| false, true -> b
226+
| false, false -> a
227+
218228
let readCmtScan () =
219229
let sourceDirsFile =
220-
["lib"; "bs"; ".sourcedirs.json"]
221-
|> List.fold_left Filename.concat runConfig.bsbProjectRoot
230+
newerFile
231+
(["lib"; "bs"; ".sourcedirs.json"]
232+
|> List.fold_left Filename.concat runConfig.bsbProjectRoot)
233+
(["lib"; "lsp"; ".sourcedirs.json"]
234+
|> List.fold_left Filename.concat runConfig.bsbProjectRoot)
222235
in
223236
let entries = ref [] in
224237
let read_entry (json : Ext_json_types.t) =

analysis/src/BuildSystem.ml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,17 @@ let getRuntimeDir rootPath =
4040
None))
4141
| true -> Some rootPath
4242

43-
let getLibBs path = Files.ifExists (path /+ "lib" /+ "bs")
43+
let getLibBs path =
44+
let bs = path /+ "lib" /+ "bs" in
45+
let lsp = path /+ "lib" /+ "lsp" in
46+
let sourcedirs dir = dir /+ ".sourcedirs.json" in
47+
match (Sys.file_exists (sourcedirs bs), Sys.file_exists (sourcedirs lsp)) with
48+
| true, true ->
49+
let mtime f = (Unix.stat f).st_mtime in
50+
if mtime (sourcedirs lsp) > mtime (sourcedirs bs) then Some lsp else Some bs
51+
| true, false -> Some bs
52+
| false, true -> Some lsp
53+
| false, false -> Files.ifExists bs
4454

4555
let getStdlib base =
4656
match getRuntimeDir base with

analysis/src/Cmt.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ let loadFullCmtFromPath ~path =
5252
let uri = Uri.fromPath path in
5353
fullFromUri ~uri
5454

55+
let loadFullCmtWithPackage ~path ~package =
56+
let uri = Uri.fromPath path in
57+
let pathStr = Uri.toPath uri in
58+
let moduleName =
59+
BuildSystem.namespacedName package.namespace (FindFiles.getName pathStr)
60+
in
61+
match Hashtbl.find_opt package.pathsForModule moduleName with
62+
| Some paths ->
63+
let cmt = getCmtPath ~uri paths in
64+
fullForCmt ~moduleName ~package ~uri cmt
65+
| None ->
66+
prerr_endline ("can't find module " ^ moduleName);
67+
None
68+
5569
let loadCmtInfosFromPath ~path =
5670
let uri = Uri.fromPath path in
5771
match Packages.getPackage ~uri with

analysis/src/Commands.ml

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,21 @@ let hover ~path ~pos ~currentFile ~debug ~supportsMarkdownLinks =
102102

103103
let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
104104
let result =
105-
match
106-
SignatureHelp.signatureHelp ~path ~pos ~currentFile ~debug
107-
~allowForConstructorPayloads
108-
with
109-
| None ->
105+
match Files.readFile currentFile with
106+
| None | Some "" ->
110107
{Protocol.signatures = []; activeSignature = None; activeParameter = None}
111-
| Some res -> res
108+
| Some text -> (
109+
match
110+
SignatureHelp.signatureHelp ~path ~pos ~currentFile ~text ~debug
111+
~allowForConstructorPayloads ()
112+
with
113+
| None ->
114+
{
115+
Protocol.signatures = [];
116+
activeSignature = None;
117+
activeParameter = None;
118+
}
119+
| Some res -> res)
112120
in
113121
print_endline (Protocol.stringifySignatureHelp result)
114122

@@ -402,7 +410,7 @@ let test ~path =
402410
DceCommand.command ()
403411
| "doc" ->
404412
print_endline ("DocumentSymbol " ^ path);
405-
DocumentSymbol.command ~path
413+
print_endline (DocumentSymbol.command ~path ~source:text)
406414
| "hig" ->
407415
print_endline ("Highlight " ^ path);
408416
SemanticTokens.command ~debug:true

0 commit comments

Comments
 (0)