Skip to content

Commit b5c13b0

Browse files
committed
add workspaceSymbols
1 parent 35ac5f2 commit b5c13b0

File tree

101 files changed

+714
-399
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+714
-399
lines changed

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
out
33
node_modules
44
examples/*/lib
5-
analysis/tests/lib
6-
analysis/tests/.bsb.lock
5+
analysis/tests/**/*/lib
6+
analysis/tests/*/.bsb.lock
77
analysis/_build
8-
analysis/tests/.merlin
8+
analysis/tests/**/*/.merlin
99
analysis/rescript-editor-analysis.exe
1010

CHANGELOG.md

Lines changed: 2 additions & 0 deletions

README.md

Lines changed: 1 addition & 0 deletions

analysis/src/Cli.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ API examples:
77
./rescript-editor-analysis.exe definition src/MyFile.res 9 3
88
./rescript-editor-analysis.exe typeDefinition src/MyFile.res 9 3
99
./rescript-editor-analysis.exe documentSymbol src/Foo.res
10+
./rescript-editor-analysis.exe workspaceSymbols src/
1011
./rescript-editor-analysis.exe hover src/MyFile.res 10 2
1112
./rescript-editor-analysis.exe references src/MyFile.res 10 2
1213
./rescript-editor-analysis.exe rename src/MyFile.res 10 2 foo
@@ -38,6 +39,10 @@ Options:
3839

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

42+
workspaceSymbols: get all symbols in directory src/
43+
44+
./rescript-editor-analysis.exe workspaceSymbols src/
45+
4146
hover: get inferred type for MyFile.res at line 10 column 2:
4247

4348
./rescript-editor-analysis.exe hover src/MyFile.res 10 2
@@ -89,7 +94,8 @@ let main () =
8994
Commands.typeDefinition ~path
9095
~pos:(int_of_string line, int_of_string col)
9196
~debug:false
92-
| [_; "documentSymbol"; path] -> DocumentSymbol.command ~path
97+
| [_; "documentSymbol"; path] -> Commands.documentSymbol ~path
98+
| [_; "workspaceSymbols"; dir] -> Commands.workspaceSymbols ~dir
9399
| [_; "hover"; path; line; col; currentFile] ->
94100
Commands.hover ~path
95101
~pos:(int_of_string line, int_of_string col)

analysis/src/Commands.ml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,27 @@ let format ~path =
264264
let diagnosticSyntax ~path =
265265
print_endline (Diagnostics.document_syntax ~path |> Protocol.array)
266266

267+
let documentSymbol ~path =
268+
Symbols.document ~path
269+
|> List.map Protocol.stringifyDocumentSymbolItem
270+
|> Protocol.array |> print_endline
271+
272+
let workspaceSymbols ~dir =
273+
print_endline
274+
(match Symbols.workspace ~dir with
275+
| Some symbol -> symbol
276+
| None -> Protocol.null)
277+
267278
let test ~path =
268279
Uri.stripPath := true;
269280
match Files.readFile path with
270-
| None -> assert false
281+
| None -> (
282+
(* Test for workspaces/directory *)
283+
let nameTest = Filename.basename(path) in
284+
match nameTest with
285+
| "workspaceSymbols" -> workspaceSymbols ~dir:path
286+
| _ -> assert false
287+
)
271288
| Some text ->
272289
let lines = text |> String.split_on_char '\n' in
273290
let processLine i line =
@@ -323,7 +340,7 @@ let test ~path =
323340
DceCommand.command ()
324341
| "doc" ->
325342
print_endline ("DocumentSymbol " ^ path);
326-
DocumentSymbol.command ~path
343+
documentSymbol ~path
327344
| "hig" ->
328345
print_endline ("Highlight " ^ path);
329346
SemanticTokens.command ~debug:true
Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ let kindNumber = function
2424
| EnumMember -> 22
2525
| TypeParameter -> 26
2626

27-
let command ~path =
27+
let document ~path =
2828
let symbols = ref [] in
2929
let rec exprKind (exp : Parsetree.expression) =
3030
match exp.pexp_desc with
@@ -134,19 +134,42 @@ let command ~path =
134134
let parser = Res_driver.parsingEngine.parseInterface ~forPrinter:false in
135135
let {Res_driver.parsetree = signature} = parser ~filename:path in
136136
iterator.signature iterator signature |> ignore);
137-
let result =
138-
!symbols
139-
|> List.rev_map (fun (name, loc, kind) ->
140-
Protocol.stringifyDocumentSymbolItem
141-
{
142-
name;
143-
location =
144-
{
145-
uri = Uri.toString (Uri.fromPath path);
146-
range = Utils.cmtLocToRange loc;
147-
};
148-
kind = kindNumber kind;
149-
})
150-
|> String.concat ",\n"
151-
in
152-
print_endline ("[\n" ^ result ^ "\n]")
137+
!symbols
138+
|> List.rev_map (fun (name, loc, kind) ->
139+
let symbol : Protocol.documentSymbolItem =
140+
{
141+
name;
142+
location =
143+
{
144+
uri = Uri.toString (Uri.fromPath path);
145+
range = Utils.cmtLocToRange loc;
146+
};
147+
kind = kindNumber kind;
148+
}
149+
in
150+
symbol)
151+
152+
let workspace ~dir =
153+
let open FindFiles in
154+
let bsconfig = dir /+ "bsconfig.json" in
155+
match Files.readFile bsconfig with
156+
| None -> None
157+
| Some text -> (
158+
match Json.parse text with
159+
| None -> None
160+
| Some inner ->
161+
let sourceDirectories =
162+
getSourceDirectories ~includeDev:false ~baseDir:dir inner
163+
in
164+
let result =
165+
sourceDirectories
166+
|> List.map (fun srcDir ->
167+
Files.readDirectory (dir /+ srcDir)
168+
|> List.map (fun path -> dir /+ srcDir /+ path)
169+
|> List.filter isSourceFile |> filterDuplicates)
170+
|> List.flatten
171+
|> List.map (fun path ->
172+
document ~path |> List.map Protocol.stringifyDocumentSymbolItem)
173+
|> List.flatten |> Protocol.array
174+
in
175+
Some result)

analysis/tests/Makefile

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
SHELL = /bin/bash
22

3-
node_modules/.bin/rescript:
4-
npm install
5-
6-
build: node_modules/.bin/rescript
7-
node_modules/.bin/rescript
3+
build:
4+
make -C document build
5+
make -C workspaces build
86

97
test: build
108
./test.sh
119

1210
clean:
13-
rm -r node_modules lib
11+
make -C document clean
12+
make -C workspaces clean
1413

1514
.DEFAULT_GOAL := test
1615

analysis/tests/document/Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
SHELL = /bin/bash
2+
3+
node_modules/.bin/rescript:
4+
npm install
5+
6+
build: node_modules/.bin/rescript
7+
node_modules/.bin/rescript
8+
9+
clean:
10+
rm -r node_modules lib
11+
12+
.PHONY: clean
File renamed without changes.

0 commit comments

Comments
 (0)