Skip to content

Commit 3817c6c

Browse files
authored
Tools: enable doc extraction from compiler (#868)
* enable doc extraction from compiler * update CHANGELOG.md
1 parent 04ff9aa commit 3817c6c

File tree

6 files changed

+40
-26
lines changed

6 files changed

+40
-26
lines changed

analysis/src/BuildSystem.ml

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ let namespacedName namespace name =
66
let ( /+ ) = Filename.concat
77

88
let getBsPlatformDir rootPath =
9-
let result =
10-
ModuleResolution.resolveNodeModulePath ~startPath:rootPath "rescript"
11-
in
12-
match result with
13-
| Some path -> Some path
14-
| None ->
15-
let message = "rescript could not be found" in
16-
Log.log message;
17-
None
9+
match !Cfg.isDocGenFromCompiler with
10+
| false -> (
11+
let result =
12+
ModuleResolution.resolveNodeModulePath ~startPath:rootPath "rescript"
13+
in
14+
match result with
15+
| Some path -> Some path
16+
| None ->
17+
let message = "rescript could not be found" in
18+
Log.log message;
19+
None)
20+
| true -> Some rootPath
1821

1922
let getLibBs root = Files.ifExists (root /+ "lib" /+ "bs")
2023

analysis/src/Cfg.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
let supportsSnippets = ref false
22

33
let debugFollowCtxPath = ref false
4+
5+
let isDocGenFromCompiler = ref false

analysis/src/Cli.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,13 @@ let main () =
143143
~pos:(int_of_string line_start, int_of_string line_end)
144144
~maxLength ~debug
145145
| [_; "codeLens"; path] -> Commands.codeLens ~path ~debug
146-
| [_; "extractDocs"; path] -> DocExtraction.extractDocs ~path ~debug
146+
| [_; "extractDocs"; path] ->
147+
148+
let () = match Sys.getenv_opt "FROM_COMPILER" with
149+
| Some("true") -> Cfg.isDocGenFromCompiler := true
150+
| _ -> () in
151+
152+
DocExtraction.extractDocs ~path ~debug
147153
| [_; "codeAction"; path; startLine; startCol; endLine; endCol; currentFile]
148154
->
149155
Commands.codeAction ~path

analysis/src/Packages.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ let newBsPackage ~rootPath =
3232
let bsconfigJson = Filename.concat rootPath "bsconfig.json" in
3333

3434
let parseRaw raw =
35-
let libBs = BuildSystem.getLibBs rootPath in
35+
let libBs = match !Cfg.isDocGenFromCompiler with
36+
| true -> BuildSystem.getStdlib rootPath
37+
| false -> BuildSystem.getLibBs rootPath
38+
in
3639
match Json.parse raw with
3740
| Some config -> (
3841
match FindFiles.findDependencyFiles rootPath config with

tools/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
- Expose more `decode` functions. https://github.com/rescript-lang/rescript-vscode/pull/866
1818

19+
#### :house: [Internal]
20+
21+
- Add env var `FROM_COMPILER` to extract docstrings from compiler repo. https://github.com/rescript-lang/rescript-vscode/pull/868
22+
1923
#### :bug: Bug Fix
2024

2125
- Fix tagged variant for `Module` and add attr to interface files. https://github.com/rescript-lang/rescript-vscode/pull/866
26+
- Fix output truncate when run `rescript-tools doc path/to/file.res` in a separate process. https://github.com/rescript-lang/rescript-vscode/pull/868

tools/src/Cli.res

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ module Buffer = {
1111
@send external toString: t => string = "toString"
1212
}
1313

14+
type processEnvOptions = {stdio?: string}
1415
type spawnSyncResult = {
1516
stdout: Buffer.t,
1617
stderr: Buffer.t,
1718
status: Js.Null.t<int>,
1819
}
20+
1921
@module("child_process")
20-
external spawnSync: (string, array<string>) => spawnSyncResult = "spawnSync"
22+
external spawnSync: (string, array<string>, option<processEnvOptions>) => spawnSyncResult =
23+
"spawnSync"
2124

2225
@val @scope("process")
2326
external exit: int => unit = "exit"
@@ -68,29 +71,21 @@ switch args->Belt.List.fromArray {
6871
switch rest {
6972
| list{"-h" | "--help"} => logAndExit(~log=docHelp, ~code=0)
7073
| list{filePath} =>
71-
let spawn = spawnSync(analysisProdPath, ["extractDocs", filePath])
74+
let spawn = spawnSync(analysisProdPath, ["extractDocs", filePath], Some({stdio: "inherit"}))
7275

7376
switch spawn.status->Js.Null.toOption {
74-
| Some(code) if code !== 0 => logAndExit(~log=spawn.stderr->Buffer.toString, ~code)
75-
| Some(code) => logAndExit(~log=spawn.stdout->Buffer.toString, ~code)
76-
| None => logAndExit(~log=`error: unexpected error to extract docs for ${filePath}`, ~code=1)
77+
| Some(code) => exit(code)
78+
| None => ()
7779
}
7880
| _ => logAndExit(~log=docHelp, ~code=1)
7981
}
8082
| list{"reanalyze", ...rest} =>
8183
let args = ["reanalyze"]->Js.Array2.concat(Belt.List.toArray(rest))
82-
let spawn = spawnSync(analysisProdPath, args)
84+
let spawn = spawnSync(analysisProdPath, args, Some({stdio: "inherit"}))
8385

8486
switch spawn.status->Js.Null.toOption {
85-
| Some(code) if code !== 0 => logAndExit(~log=spawn.stderr->Buffer.toString, ~code)
86-
| Some(code) => logAndExit(~log=spawn.stdout->Buffer.toString, ~code)
87-
| None =>
88-
logAndExit(
89-
~log=`error: unexpected error to run reanalyze with arguments: ${args->Js.Array2.joinWith(
90-
" ",
91-
)}`,
92-
~code=1,
93-
)
87+
| Some(code) => exit(code)
88+
| None => ()
9489
}
9590
| list{"-h" | "--help"} => logAndExit(~log=help, ~code=0)
9691
| list{"-v" | "--version"} =>

0 commit comments

Comments
 (0)