Skip to content

Commit cc2dc53

Browse files
committed
handle scope
1 parent 1e5c1aa commit cc2dc53

File tree

3 files changed

+89
-4
lines changed

3 files changed

+89
-4
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,8 @@ let getPipeCompletions ~env ~full ~identifierLoc ?posOfDot ~debug
704704
in
705705
let completionPath =
706706
match (completeAsBuiltin, typePath) with
707-
| Some completionPathForBuiltin, _ -> Some completionPathForBuiltin
707+
| Some completionPathForBuiltin, _ ->
708+
Some (false, completionPathForBuiltin)
708709
| _, Some p -> (
709710
(* If this isn't a builtin, but we have a path, we try to resolve the
710711
module path relative to the env we're completing from. This ensures that
@@ -714,17 +715,32 @@ let getPipeCompletions ~env ~full ~identifierLoc ?posOfDot ~debug
714715
TypeUtils.getModulePathRelativeToEnv ~debug
715716
~env:envCompletionIsMadeFrom ~envFromItem:env (Utils.expandPath p)
716717
with
717-
| None -> Some [env.file.moduleName]
718-
| Some p -> Some p)
718+
| None -> Some (true, [env.file.moduleName])
719+
| Some p -> Some (false, p))
719720
| _ -> None
720721
in
721722
match completionPath with
722723
| None -> []
723-
| Some completionPath ->
724+
| Some (isFromCurrentModule, completionPath) ->
724725
completionsForPipeFromCompletionPath ~envCompletionIsMadeFrom ~opens
725726
~pos ~scope ~debug ~prefix ~env ~rawOpens ~full completionPath
726727
|> TypeUtils.filterPipeableFunctions ~env ~full
727728
~synthetic:mainCompletionsAreSynthetic ~targetTypeId:mainTypeId
729+
|> List.filter (fun (c : Completion.t) ->
730+
(* If we're completing from the current module then we need to care about scope.
731+
This is automatically taken care of in other cases. *)
732+
if isFromCurrentModule then
733+
match c.kind with
734+
| Value _ ->
735+
scope
736+
|> List.find_opt (fun (item : ScopeTypes.item) ->
737+
match item with
738+
| Value (scopeItemName, _, _, _) ->
739+
scopeItemName = c.name
740+
| _ -> false)
741+
|> Option.is_some
742+
| _ -> false
743+
else true)
728744
in
729745
(* Extra completions can be drawn from the @editor.completeFrom attribute. Here we
730746
find and add those completions as well. *)

analysis/tests/src/DotPipeCompletionSpec.res

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,11 @@ let str = "hello"
9797

9898
// str->Js.String2.toLowerCase->Js.String2.toUpperCase.toLowerC
9999
// ^com
100+
101+
let cc = (t: typeOutsideModule) => {
102+
// t.
103+
// ^com
104+
t
105+
}
106+
107+
let outOfScope = (t: typeOutsideModule) => t

analysis/tests/src/expected/DotPipeCompletionSpec.res.txt

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,3 +467,64 @@ Path Js.String2.toLowerC
467467
}]
468468
}]
469469

470+
Complete src/DotPipeCompletionSpec.res 101:7
471+
posCursor:[101:7] posNoWhite:[101:6] Found expr:[100:9->104:1]
472+
posCursor:[101:7] posNoWhite:[101:6] Found expr:[100:10->104:1]
473+
posCursor:[101:7] posNoWhite:[101:6] Found expr:[101:5->103:3]
474+
Pexp_field [101:5->101:6] t:[103:2->103:3]
475+
Completable: Cpath Value[t].""
476+
Package opens Pervasives.JsxModules.place holder
477+
Resolved opens 1 pervasives
478+
ContextPath Value[t].""
479+
ContextPath Value[t]
480+
Path t
481+
ContextPath Value[t]->
482+
ContextPath Value[t]
483+
Path t
484+
CPPipe pathFromEnv: found:true
485+
Path DotPipeCompletionSpec.
486+
Path DotPipeCompletionSpec.SomeOtherModule.
487+
[{
488+
"label": "nname",
489+
"kind": 5,
490+
"tags": [],
491+
"detail": "string",
492+
"documentation": {"kind": "markdown", "value": "```rescript\nnname: string\n```\n\n```rescript\ntype typeOutsideModule = {nname: string}\n```"}
493+
}, {
494+
"label": "->doWithTypeOutsideModule",
495+
"kind": 12,
496+
"tags": [],
497+
"detail": "typeOutsideModule => string",
498+
"documentation": null,
499+
"sortText": "doWithTypeOutsideModule",
500+
"insertText": "->doWithTypeOutsideModule",
501+
"additionalTextEdits": [{
502+
"range": {"start": {"line": 101, "character": 6}, "end": {"line": 101, "character": 7}},
503+
"newText": ""
504+
}]
505+
}, {
506+
"label": "->SomeOtherModule.getNName",
507+
"kind": 12,
508+
"tags": [],
509+
"detail": "t => string",
510+
"documentation": null,
511+
"sortText": "getNName",
512+
"insertText": "->SomeOtherModule.getNName",
513+
"additionalTextEdits": [{
514+
"range": {"start": {"line": 101, "character": 6}, "end": {"line": 101, "character": 7}},
515+
"newText": ""
516+
}]
517+
}, {
518+
"label": "->SomeOtherModule.getNName2",
519+
"kind": 12,
520+
"tags": [],
521+
"detail": "typeOutsideModule => string",
522+
"documentation": null,
523+
"sortText": "getNName2",
524+
"insertText": "->SomeOtherModule.getNName2",
525+
"additionalTextEdits": [{
526+
"range": {"start": {"line": 101, "character": 6}, "end": {"line": 101, "character": 7}},
527+
"newText": ""
528+
}]
529+
}]
530+

0 commit comments

Comments
 (0)