Skip to content

Ws symbols #519

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

Closed
wants to merge 5 commits into from
Closed
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
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ jobs:
# https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#example-including-additional-values-into-combinations
include:
- os: macos-latest
build: eval $(opam env) && cd analysis && make test
build: eval $(opam env) && cd analysis && make test -j
artifact-folder: darwin
- os: ubuntu-18.04
build: eval $(opam env) && cd analysis && make test
build: eval $(opam env) && cd analysis && make test -j
artifact-folder: linux
- os: windows-latest
build: "cd analysis && & $env:CYGWIN_ROOT\\bin\\ocaml-env exec -- make test"
build: "cd analysis && & $env:CYGWIN_ROOT\\bin\\ocaml-env exec -- make test -j"
artifact-folder: win32

runs-on: ${{matrix.os}}
Expand Down
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
out
node_modules
examples/*/lib
analysis/tests/lib
analysis/tests/.bsb.lock
analysis/tests/**/*/lib
analysis/tests/*/.bsb.lock
analysis/_build
analysis/tests/.merlin
analysis/tests/**/*/.merlin
analysis/rescript-editor-analysis.exe

2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

#### :rocket: New Feature

- Add workspace Symbols
- Inlay Hints (experimetal). `rescript.settings.inlayHints.enable: true`

## v1.4.2

#### :bug: Bug Fix
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ The plugin activates on `.res` and `.resi` files. If you've already got Reason-L
- Autocomplete.
- Find references.
- Rename.
- Workspace Symbols
- Snippets to ease a few syntaxes:
- `external` features such as `@bs.module` and `@bs.val`
- `try`, `for`, etc.
Expand Down
8 changes: 7 additions & 1 deletion analysis/src/Cli.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ API examples:
./rescript-editor-analysis.exe definition src/MyFile.res 9 3
./rescript-editor-analysis.exe typeDefinition src/MyFile.res 9 3
./rescript-editor-analysis.exe documentSymbol src/Foo.res
./rescript-editor-analysis.exe workspaceSymbols src/
./rescript-editor-analysis.exe hover src/MyFile.res 10 2
./rescript-editor-analysis.exe references src/MyFile.res 10 2
./rescript-editor-analysis.exe rename src/MyFile.res 10 2 foo
Expand Down Expand Up @@ -38,6 +39,10 @@ Options:

./rescript-editor-analysis.exe documentSymbol src/MyFile.res

workspaceSymbols: get all symbols in directory src/

./rescript-editor-analysis.exe workspaceSymbols src/

hover: get inferred type for MyFile.res at line 10 column 2:

./rescript-editor-analysis.exe hover src/MyFile.res 10 2
Expand Down Expand Up @@ -89,7 +94,8 @@ let main () =
Commands.typeDefinition ~path
~pos:(int_of_string line, int_of_string col)
~debug:false
| [_; "documentSymbol"; path] -> DocumentSymbol.command ~path
| [_; "documentSymbol"; path] -> Commands.documentSymbol ~path
| [_; "workspaceSymbols"; dir] -> Commands.workspaceSymbols ~dir
| [_; "hover"; path; line; col; currentFile] ->
Commands.hover ~path
~pos:(int_of_string line, int_of_string col)
Expand Down
21 changes: 19 additions & 2 deletions analysis/src/Commands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,27 @@ let format ~path =
let diagnosticSyntax ~path =
print_endline (Diagnostics.document_syntax ~path |> Protocol.array)

let documentSymbol ~path =
Symbols.document ~path
|> List.map Protocol.stringifyDocumentSymbolItem
|> Protocol.array |> print_endline

let workspaceSymbols ~dir =
print_endline
(match Symbols.workspace ~dir with
| Some symbol -> symbol
| None -> Protocol.null)

let test ~path =
Uri.stripPath := true;
match Files.readFile path with
| None -> assert false
| None -> (
(* Test for workspaces/directory *)
let nameTest = Filename.basename(path) in
match nameTest with
| "workspaceSymbols" -> workspaceSymbols ~dir:path
| _ -> assert false
)
| Some text ->
let lines = text |> String.split_on_char '\n' in
let processLine i line =
Expand Down Expand Up @@ -323,7 +340,7 @@ let test ~path =
DceCommand.command ()
| "doc" ->
print_endline ("DocumentSymbol " ^ path);
DocumentSymbol.command ~path
documentSymbol ~path
| "hig" ->
print_endline ("Highlight " ^ path);
SemanticTokens.command ~debug:true
Expand Down
57 changes: 40 additions & 17 deletions analysis/src/DocumentSymbol.ml → analysis/src/Symbols.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ let kindNumber = function
| EnumMember -> 22
| TypeParameter -> 26

let command ~path =
let document ~path =
let symbols = ref [] in
let rec exprKind (exp : Parsetree.expression) =
match exp.pexp_desc with
Expand Down Expand Up @@ -134,19 +134,42 @@ let command ~path =
let parser = Res_driver.parsingEngine.parseInterface ~forPrinter:false in
let {Res_driver.parsetree = signature} = parser ~filename:path in
iterator.signature iterator signature |> ignore);
let result =
!symbols
|> List.rev_map (fun (name, loc, kind) ->
Protocol.stringifyDocumentSymbolItem
{
name;
location =
{
uri = Uri.toString (Uri.fromPath path);
range = Utils.cmtLocToRange loc;
};
kind = kindNumber kind;
})
|> String.concat ",\n"
in
print_endline ("[\n" ^ result ^ "\n]")
!symbols
|> List.rev_map (fun (name, loc, kind) ->
let symbol : Protocol.documentSymbolItem =
{
name;
location =
{
uri = Uri.toString (Uri.fromPath path);
range = Utils.cmtLocToRange loc;
};
kind = kindNumber kind;
}
in
symbol)

let workspace ~dir =
let open FindFiles in
let bsconfig = dir /+ "bsconfig.json" in
match Files.readFile bsconfig with
| None -> None
| Some text -> (
match Json.parse text with
| None -> None
| Some inner ->
let sourceDirectories =
getSourceDirectories ~includeDev:false ~baseDir:dir inner
in
let result =
sourceDirectories
|> List.map (fun srcDir ->
Files.readDirectory (dir /+ srcDir)
|> List.map (fun path -> dir /+ srcDir /+ path)
|> List.filter isSourceFile |> filterDuplicates)
|> List.flatten
|> List.map (fun path ->
document ~path |> List.map Protocol.stringifyDocumentSymbolItem)
|> List.flatten |> Protocol.array
in
Some result)
13 changes: 8 additions & 5 deletions analysis/tests/Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
SHELL = /bin/bash

node_modules/.bin/rescript:
npm install
buildDocument:
make -C document build

build: node_modules/.bin/rescript
node_modules/.bin/rescript
buildWorkspaceSymbols:
make -C workspaceSymbols build

build: buildDocument buildWorkspaceSymbols

test: build
./test.sh

clean:
rm -r node_modules lib
make -C document clean
make -C workspaceSymbols clean

.DEFAULT_GOAL := test

Expand Down
12 changes: 12 additions & 0 deletions analysis/tests/document/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
SHELL = /bin/bash

node_modules/.bin/rescript:
npm install

build: node_modules/.bin/rescript
node_modules/.bin/rescript

clean:
rm -r node_modules lib

.PHONY: clean
File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Hover src/Auto.res 2:13
Hover document/src/Auto.res 2:13
{"contents": "```rescript\n(Belt.List.t<'a>, 'a => 'b) => Belt.List.t<'b>\n```\n\n\n Returns a new list with `f` applied to each element of `someList`.\n\n ```res example\n list{1, 2}->Belt.List.map(x => x + 1) // list{3, 4}\n ```\n"}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Complete src/CompletePrioritize1.res 5:6
Complete document/src/CompletePrioritize1.res 5:6
posCursor:[5:6] posNoWhite:[5:5] Found expr:[5:3->0:-1]
Completable: Cpath Value[a]->
[{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Complete src/CompletePrioritize2.res 9:7
Complete document/src/CompletePrioritize2.res 9:7
posCursor:[9:7] posNoWhite:[9:6] Found expr:[9:3->0:-1]
Completable: Cpath Value[ax]->
[{
Expand All @@ -9,7 +9,7 @@ Completable: Cpath Value[ax]->
"documentation": null
}]

Complete src/CompletePrioritize2.res 12:5
Complete document/src/CompletePrioritize2.res 12:5
posCursor:[12:5] posNoWhite:[12:4] Found expr:[12:3->12:5]
Pexp_ident ax:[12:3->12:5]
Completable: Cpath Value[ax]
Expand Down
Loading