Skip to content

Commit 7592d76

Browse files
authored
Add more info about analysis tool (#7209)
* Add more info about analysis tool * Update local repo install instruction
1 parent ad568e0 commit 7592d76

File tree

1 file changed

+80
-6
lines changed

1 file changed

+80
-6
lines changed

analysis/README.md

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,89 @@ See main CONTRIBUTING.md's repo structure. Additionally, `examples/` is a conven
1212

1313
## Usage
1414

15-
At root:
16-
```sh
17-
./rescript-editor-analysis.exe --help
18-
19-
# or
20-
15+
```shell
2116
dune exec -- rescript-editor-analysis --help
2217
```
2318

2419
## History
2520

2621
This project is based on a fork of [Reason Language Server](https://github.com/jaredly/reason-language-server).
22+
23+
## Tests
24+
25+
### Prerequisites
26+
27+
- Ensure the compiler is built (`make build` in the repository root).
28+
- Ensure the library is built (`make lib` in the repository root).
29+
30+
### Running the Tests
31+
32+
Run `make test` in `tests/analysis_tests/tests`.
33+
34+
### Key Concept
35+
36+
The tests in the `tests/analysis_tests/tests` folder are based on the `dune exec -- rescript-editor-analysis test` command. This special subcommand processes a file and executes specific editor analysis functionality based on special syntax found in code comments.
37+
38+
Consider the following code:
39+
40+
```res
41+
let a = 5
42+
// a.
43+
// ^com
44+
```
45+
46+
After building the ReScript project (**⚠️ this is a requirement**), you can execute `dune exec -- rescript-editor-analysis test Sample.res`, and completion will be performed for the cursor position indicated by `^`. The `com` directive requests completion. To see other commands, check out the pattern match in the `test` function in [Commands.ml](./src/Commands.ml).
47+
48+
> [!WARNING]
49+
> Ensure there are no spaces in the code comments, as the commands are captured by a regular expression that expects spaces and not tabs!
50+
51+
Here’s how it works: once a command is found in a comment, a copy of the source file is created inside a temporary directory, where the line above `^com` is uncommented. The corresponding analysis functionality is then processed, typically with `~debug:true`. With debug enabled, code paths like
52+
53+
```ml
54+
if Debug.verbose () then
55+
print_endline "[complete_typed_value]--> Tfunction #other";
56+
```
57+
58+
will print to stdout. This is helpful for observing what happens during the analysis.
59+
60+
When you run `make test` (from the `tests/analysis_tests` folder), `dune exec -- rescript-editor-analysis test <file>` will be executed for each `*.res` file in `analysis/tests/src`. The stdout will be compared to the corresponding `analysis/tests/src/expected` file. If `git diff` indicates changes, `make test` will fail, as these differences might be unintentional.
61+
62+
## Testing on Your Own Projects
63+
64+
To use a local version of `rescript-editor-analysis`, the targeted project needs to be compiled with the local compiler.
65+
66+
Install your local ReScript with `npm i /path/to/your-local-rescript-repo`.
67+
Reinstall the dependencies and run `npx rescript` in your project. This ensures the project is compiled with the same compiler version that the `rescript-editor-analysis` will process.
68+
69+
## Debugging
70+
71+
It is possible to debug `analysis` via [ocamlearlybird](https://github.com/hackwaly/ocamlearlybird).
72+
73+
1. Install `opam install earlybird`.
74+
2. Install the [earlybird extension](https://marketplace.visualstudio.com/items?itemName=hackwaly.ocamlearlybird).
75+
3. Create a launch configuration (`.vscode/launch.json`):
76+
77+
```json
78+
{
79+
"version": "0.2.0",
80+
"configurations": [
81+
{
82+
"name": "Debug analysis",
83+
"type": "ocaml.earlybird",
84+
"request": "launch",
85+
"program": "${workspaceFolder}/_build/default/analysis/bin/main.bc",
86+
"stopOnEntry": true,
87+
"cwd": "/projects/your-project",
88+
"env": {
89+
"CAML_LD_LIBRARY_PATH": "${workspaceFolder}/_build/default/compiler/ext"
90+
},
91+
"arguments": [
92+
"test",
93+
"src/Main.res"
94+
]
95+
}
96+
]
97+
}
98+
```
99+
100+
The `CAML_LD_LIBRARY_PATH` environment variable is required to tell OCaml where `dllext_stubs.so` can be loaded from.

0 commit comments

Comments
 (0)