From 72461a96fdb3c5b1e6939727cc2826f7e2028b0d Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Wed, 7 Feb 2024 20:38:01 +0100 Subject: [PATCH 1/3] enhance module attribute completion some more --- analysis/src/CompletionBackEnd.ml | 4 +- analysis/src/CompletionFrontEnd.ml | 32 +++++--- analysis/tests/src/CompletionAttributes.res | 3 + .../src/expected/CompletionAttributes.res.txt | 73 +++++++++++++++++++ analysis/tests/src/test.json | 0 5 files changed, 99 insertions(+), 13 deletions(-) create mode 100644 analysis/tests/src/test.json diff --git a/analysis/src/CompletionBackEnd.ml b/analysis/src/CompletionBackEnd.ml index 434589448..cc18785e1 100644 --- a/analysis/src/CompletionBackEnd.ml +++ b/analysis/src/CompletionBackEnd.ml @@ -1882,8 +1882,8 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover completable = then None else match Filename.extension fileName with - | ".js" | ".mjs" -> Some ("./" ^ fileName) - | _ -> None) + | ".res" | ".resi" | "" -> None + | _ -> Some ("./" ^ fileName)) with _ -> if debug then print_endline "Could not read relative directory"; [] diff --git a/analysis/src/CompletionFrontEnd.ml b/analysis/src/CompletionFrontEnd.ml index 042aa5006..1c88ed968 100644 --- a/analysis/src/CompletionFrontEnd.ml +++ b/analysis/src/CompletionFrontEnd.ml @@ -832,25 +832,35 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor Pstr_eval ( { pexp_desc = - Pexp_record - ( ( {txt = Lident "from"}, - { - pexp_loc; - pexp_desc = - Pexp_constant (Pconst_string (s, _)); - } ) - :: _, - _ ); + Pexp_record (({txt = Lident "from"}, fromExpr) :: _, _); }, _ ); }; ] - when locHasCursor pexp_loc -> + when locHasCursor fromExpr.pexp_loc + || locIsEmpty fromExpr.pexp_loc + && CompletionExpressions.isExprHole fromExpr -> ( if Debug.verbose () then print_endline "[decoratorCompletion] Found @module with import attributes and \ cursor on \"from\""; - setResult (Completable.CdecoratorPayload (Module s)) + match + ( locHasCursor fromExpr.pexp_loc, + locIsEmpty fromExpr.pexp_loc, + CompletionExpressions.isExprHole fromExpr, + fromExpr ) + with + | true, _, _, {pexp_desc = Pexp_constant (Pconst_string (s, _))} -> + if Debug.verbose () then + print_endline + "[decoratorCompletion] @module `from` payload was string"; + setResult (Completable.CdecoratorPayload (Module s)) + | false, true, true, _ -> + if Debug.verbose () then + print_endline + "[decoratorCompletion] @module `from` payload was expr hole"; + setResult (Completable.CdecoratorPayload (Module "")) + | _ -> ()) | PStr [{pstr_desc = Pstr_eval (expr, _)}] -> ( if Debug.verbose () then print_endline diff --git a/analysis/tests/src/CompletionAttributes.res b/analysis/tests/src/CompletionAttributes.res index cf9e011f0..7c8809bb5 100644 --- a/analysis/tests/src/CompletionAttributes.res +++ b/analysis/tests/src/CompletionAttributes.res @@ -31,3 +31,6 @@ // @module({from: "" }) external doStuff: t = "default" // ^com +// @module({from: }) external doStuff: t = "default" +// ^com + diff --git a/analysis/tests/src/expected/CompletionAttributes.res.txt b/analysis/tests/src/expected/CompletionAttributes.res.txt index 3c8da2d2e..9ac5b04cd 100644 --- a/analysis/tests/src/expected/CompletionAttributes.res.txt +++ b/analysis/tests/src/expected/CompletionAttributes.res.txt @@ -24,6 +24,24 @@ Resolved opens 1 pervasives "tags": [], "detail": "Package", "documentation": null + }, { + "label": "./test.json", + "kind": 4, + "tags": [], + "detail": "Local file", + "documentation": null + }, { + "label": "./TableclothMap.mli", + "kind": 4, + "tags": [], + "detail": "Local file", + "documentation": null + }, { + "label": "./TableclothMap.ml", + "kind": 4, + "tags": [], + "detail": "Local file", + "documentation": null }, { "label": "./tst.js", "kind": 4, @@ -185,6 +203,61 @@ Resolved opens 1 pervasives "tags": [], "detail": "Package", "documentation": null + }, { + "label": "./test.json", + "kind": 4, + "tags": [], + "detail": "Local file", + "documentation": null + }, { + "label": "./TableclothMap.mli", + "kind": 4, + "tags": [], + "detail": "Local file", + "documentation": null + }, { + "label": "./TableclothMap.ml", + "kind": 4, + "tags": [], + "detail": "Local file", + "documentation": null + }, { + "label": "./tst.js", + "kind": 4, + "tags": [], + "detail": "Local file", + "documentation": null + }] + +Complete src/CompletionAttributes.res 33:17 +XXX Not found! +Completable: CdecoratorPayload(module=) +Package opens Pervasives.JsxModules.place holder +Resolved opens 1 pervasives +[{ + "label": "@rescript/react", + "kind": 4, + "tags": [], + "detail": "Package", + "documentation": null + }, { + "label": "./test.json", + "kind": 4, + "tags": [], + "detail": "Local file", + "documentation": null + }, { + "label": "./TableclothMap.mli", + "kind": 4, + "tags": [], + "detail": "Local file", + "documentation": null + }, { + "label": "./TableclothMap.ml", + "kind": 4, + "tags": [], + "detail": "Local file", + "documentation": null }, { "label": "./tst.js", "kind": 4, diff --git a/analysis/tests/src/test.json b/analysis/tests/src/test.json new file mode 100644 index 000000000..e69de29bb From 8f5df6290d469365d7cc72f5a3199e0cd3e8bfcd Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Wed, 7 Feb 2024 20:42:07 +0100 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a69eef61c..ea65f9f4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,8 @@ - Enhance decorator completion. https://github.com/rescript-lang/rescript-vscode/pull/908 - Completion for import attributes in `@module`. https://github.com/rescript-lang/rescript-vscode/pull/913 +- Relax filter for what local files that come up in from and regular string completion in `@module`. https://github.com/rescript-lang/rescript-vscode/pull/918 +- Make from completion trigger for expr hole so we get a nice experience when completing {from: } in `@module`. https://github.com/rescript-lang/rescript-vscode/pull/918 ## 1.38.0 From 0de2d33699b4e02f9cac0e292c2342b77f758462 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Sat, 10 Feb 2024 21:22:35 +0100 Subject: [PATCH 3/3] sort file results --- analysis/src/CompletionBackEnd.ml | 1 + .../tests/src/expected/CompletionAttributes.res.txt | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/analysis/src/CompletionBackEnd.ml b/analysis/src/CompletionBackEnd.ml index cc18785e1..941c5b16c 100644 --- a/analysis/src/CompletionBackEnd.ml +++ b/analysis/src/CompletionBackEnd.ml @@ -1884,6 +1884,7 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover completable = match Filename.extension fileName with | ".res" | ".resi" | "" -> None | _ -> Some ("./" ^ fileName)) + |> List.sort String.compare with _ -> if debug then print_endline "Could not read relative directory"; [] diff --git a/analysis/tests/src/expected/CompletionAttributes.res.txt b/analysis/tests/src/expected/CompletionAttributes.res.txt index 9ac5b04cd..81fa8b918 100644 --- a/analysis/tests/src/expected/CompletionAttributes.res.txt +++ b/analysis/tests/src/expected/CompletionAttributes.res.txt @@ -25,7 +25,7 @@ Resolved opens 1 pervasives "detail": "Package", "documentation": null }, { - "label": "./test.json", + "label": "./TableclothMap.ml", "kind": 4, "tags": [], "detail": "Local file", @@ -37,7 +37,7 @@ Resolved opens 1 pervasives "detail": "Local file", "documentation": null }, { - "label": "./TableclothMap.ml", + "label": "./test.json", "kind": 4, "tags": [], "detail": "Local file", @@ -204,7 +204,7 @@ Resolved opens 1 pervasives "detail": "Package", "documentation": null }, { - "label": "./test.json", + "label": "./TableclothMap.ml", "kind": 4, "tags": [], "detail": "Local file", @@ -216,7 +216,7 @@ Resolved opens 1 pervasives "detail": "Local file", "documentation": null }, { - "label": "./TableclothMap.ml", + "label": "./test.json", "kind": 4, "tags": [], "detail": "Local file", @@ -241,7 +241,7 @@ Resolved opens 1 pervasives "detail": "Package", "documentation": null }, { - "label": "./test.json", + "label": "./TableclothMap.ml", "kind": 4, "tags": [], "detail": "Local file", @@ -253,7 +253,7 @@ Resolved opens 1 pervasives "detail": "Local file", "documentation": null }, { - "label": "./TableclothMap.ml", + "label": "./test.json", "kind": 4, "tags": [], "detail": "Local file",