From b0b45981f76cab153c58b72abd44190410c99188 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Tue, 14 Nov 2023 15:17:28 +0100 Subject: [PATCH] More robust handling of namespaces in pipe completion (#850) * more robust handling of namespaces in pipe completion * changelog --- CHANGELOG.md | 4 ++++ analysis/src/CompletionBackEnd.ml | 6 +----- analysis/src/Utils.ml | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fce661349..561a89ae5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ ## master +#### :bug: Bug Fix + +- More robust handling of namespaces in pipe completions. https://github.com/rescript-lang/rescript-vscode/pull/850 + ## 1.24.0 #### :bug: Bug Fix diff --git a/analysis/src/CompletionBackEnd.ml b/analysis/src/CompletionBackEnd.ml index d82c3e2b6..690308ce2 100644 --- a/analysis/src/CompletionBackEnd.ml +++ b/analysis/src/CompletionBackEnd.ml @@ -943,10 +943,6 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact | [_], _ -> Some modulePath | s :: inner, first :: restPath when s = first -> removeRawOpen inner restPath - | s :: inner, first :: restPath - when String.contains first '-' && Utils.startsWith first s -> - (* This handles namespaced modules, which have their namespace appended after a '-' *) - removeRawOpen inner restPath | _ -> None in let rec removeRawOpens rawOpens modulePath = @@ -959,7 +955,7 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact | [] -> modulePath in let completionPathMinusOpens = - completionPath + completionPath |> Utils.flattenAnyNamespaceInPath |> removeRawOpens package.opens |> removeRawOpens rawOpens |> String.concat "." in diff --git a/analysis/src/Utils.ml b/analysis/src/Utils.ml index 17a9d258d..6dce76e26 100644 --- a/analysis/src/Utils.ml +++ b/analysis/src/Utils.ml @@ -228,3 +228,18 @@ let fileNameHasUnallowedChars s = ignore (Str.search_forward regexp s 0); true with Not_found -> false + +(* Flattens any namespace in the provided path. + Example: + Globals-RescriptBun.URL.t (which is an illegal path because of the namespace) becomes: + RescriptBun.Globals.URL.t +*) +let rec flattenAnyNamespaceInPath path = + match path with + | [] -> [] + | head :: tail -> + if String.contains head '-' then + let parts = String.split_on_char '-' head in + (* Namespaces are in reverse order, so "URL-RescriptBun" where RescriptBun is the namespace. *) + (parts |> List.rev) @ flattenAnyNamespaceInPath tail + else head :: flattenAnyNamespaceInPath tail