Skip to content

Tools: enable doc extraction from compiler #868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions analysis/src/BuildSystem.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ let namespacedName namespace name =
let ( /+ ) = Filename.concat

let getBsPlatformDir rootPath =
let result =
ModuleResolution.resolveNodeModulePath ~startPath:rootPath "rescript"
in
match result with
| Some path -> Some path
| None ->
let message = "rescript could not be found" in
Log.log message;
None
match !Cfg.isDocGenFromCompiler with
| false -> (
let result =
ModuleResolution.resolveNodeModulePath ~startPath:rootPath "rescript"
in
match result with
| Some path -> Some path
| None ->
let message = "rescript could not be found" in
Log.log message;
None)
| true -> Some rootPath

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

Expand Down
2 changes: 2 additions & 0 deletions analysis/src/Cfg.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
let supportsSnippets = ref false

let debugFollowCtxPath = ref false

let isDocGenFromCompiler = ref false
8 changes: 7 additions & 1 deletion analysis/src/Cli.ml
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,13 @@ let main () =
~pos:(int_of_string line_start, int_of_string line_end)
~maxLength ~debug
| [_; "codeLens"; path] -> Commands.codeLens ~path ~debug
| [_; "extractDocs"; path] -> DocExtraction.extractDocs ~path ~debug
| [_; "extractDocs"; path] ->

let () = match Sys.getenv_opt "FROM_COMPILER" with
| Some("true") -> Cfg.isDocGenFromCompiler := true
| _ -> () in

DocExtraction.extractDocs ~path ~debug
| [_; "codeAction"; path; startLine; startCol; endLine; endCol; currentFile]
->
Commands.codeAction ~path
Expand Down
5 changes: 4 additions & 1 deletion analysis/src/Packages.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ let newBsPackage ~rootPath =
let bsconfigJson = Filename.concat rootPath "bsconfig.json" in

let parseRaw raw =
let libBs = BuildSystem.getLibBs rootPath in
let libBs = match !Cfg.isDocGenFromCompiler with
| true -> BuildSystem.getStdlib rootPath
| false -> BuildSystem.getLibBs rootPath
in
match Json.parse raw with
| Some config -> (
match FindFiles.findDependencyFiles rootPath config with
Expand Down
5 changes: 5 additions & 0 deletions tools/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

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

#### :house: [Internal]

- Add env var `FROM_COMPILER` to extract docstrings from compiler repo. https://github.com/rescript-lang/rescript-vscode/pull/868

#### :bug: Bug Fix

- Fix tagged variant for `Module` and add attr to interface files. https://github.com/rescript-lang/rescript-vscode/pull/866
- 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
25 changes: 10 additions & 15 deletions tools/src/Cli.res
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ module Buffer = {
@send external toString: t => string = "toString"
}

type processEnvOptions = {stdio?: string}
type spawnSyncResult = {
stdout: Buffer.t,
stderr: Buffer.t,
status: Js.Null.t<int>,
}

@module("child_process")
external spawnSync: (string, array<string>) => spawnSyncResult = "spawnSync"
external spawnSync: (string, array<string>, option<processEnvOptions>) => spawnSyncResult =
"spawnSync"

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

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

switch spawn.status->Js.Null.toOption {
| Some(code) if code !== 0 => logAndExit(~log=spawn.stderr->Buffer.toString, ~code)
| Some(code) => logAndExit(~log=spawn.stdout->Buffer.toString, ~code)
| None =>
logAndExit(
~log=`error: unexpected error to run reanalyze with arguments: ${args->Js.Array2.joinWith(
" ",
)}`,
~code=1,
)
| Some(code) => exit(code)
| None => ()
}
| list{"-h" | "--help"} => logAndExit(~log=help, ~code=0)
| list{"-v" | "--version"} =>
Expand Down