-
Notifications
You must be signed in to change notification settings - Fork 469
Complete functions from included module #7515
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
Merged
Merged
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
1827920
WIP find answer in stamps
nojaf a9e37c0
Refactor included values
nojaf 6b80247
Fix check in processLocalValue
nojaf 43b15e8
Clean up dump logs
nojaf 5c82b2f
Include values without pipe completion as well
nojaf 8fdc591
Introduce include scope
nojaf f849598
Check if include ends with path
nojaf 7b87320
Deal with namespace in modulePathFromEnv
nojaf 88cd2bc
Simplify completions from current module
nojaf 8883bfd
Also iter includes in findLocalCompletionsForValues
nojaf 978811d
Add changelog entry
nojaf aff92c5
Merge branch 'master' into pipe-include-module-two
nojaf d17e55e
Merge branch 'master' into pipe-include-module-two
nojaf c4bdd03
Remove unused function
nojaf 6aa9d71
Dump structure
nojaf feb9b1e
Print scope
nojaf fa55ece
IncludePstr_include( Pmod_ident)
nojaf 9f66dd2
Remove path to_string
nojaf f782f32
Don't let exported override included
nojaf 4a578bf
Use Path.name
nojaf 0359c7f
Remove duplicate check
nojaf 1518f6d
Store included values in separate table
nojaf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
let filter_by_cursor cursor (loc : Warnings.loc) : bool = | ||
match cursor with | ||
| None -> true | ||
| Some (line, col) -> | ||
let start = loc.loc_start and end_ = loc.loc_end in | ||
let line_in = start.pos_lnum <= line && line <= end_.pos_lnum in | ||
let col_in = | ||
if start.pos_lnum = end_.pos_lnum then | ||
start.pos_cnum - start.pos_bol <= col | ||
&& col <= end_.pos_cnum - end_.pos_bol | ||
else if line = start.pos_lnum then col >= start.pos_cnum - start.pos_bol | ||
else if line = end_.pos_lnum then col <= end_.pos_cnum - end_.pos_bol | ||
else true | ||
in | ||
line_in && col_in | ||
|
||
type filter = Cursor of (int * int) | Loc of Loc.t | ||
|
||
let dump ?filter rescript_json cmt_path = | ||
let uri = Uri.fromPath (Filename.remove_extension cmt_path ^ ".res") in | ||
let package = | ||
let uri = Uri.fromPath rescript_json in | ||
Packages.getPackage ~uri |> Option.get | ||
in | ||
let moduleName = | ||
BuildSystem.namespacedName package.namespace (FindFiles.getName cmt_path) | ||
in | ||
match Cmt.fullForCmt ~moduleName ~package ~uri cmt_path with | ||
| None -> failwith (Format.sprintf "Could not load cmt for %s" cmt_path) | ||
| Some full -> | ||
let open SharedTypes in | ||
let open SharedTypes.Stamps in | ||
let applyFilter = | ||
match filter with | ||
| None -> fun _ -> true | ||
| Some (Cursor cursor) -> Loc.hasPos ~pos:cursor | ||
| Some (Loc loc) -> Loc.isInside loc | ||
in | ||
(match filter with | ||
| None -> () | ||
| Some (Cursor (line, col)) -> | ||
Printf.printf "Filtering by cursor %d,%d\n" line col | ||
| Some (Loc loc) -> Printf.printf "Filtering by loc %s\n" (Loc.toString loc)); | ||
|
||
Printf.printf "file moduleName: %s\n\n" full.file.moduleName; | ||
|
||
let stamps = | ||
full.file.stamps |> getEntries | ||
|> List.filter (fun (_, stamp) -> applyFilter (locOfKind stamp)) | ||
in | ||
|
||
let total_stamps = List.length stamps in | ||
Printf.printf "Found %d stamps:\n%s" total_stamps | ||
(if total_stamps > 0 then "\n" else ""); | ||
|
||
stamps | ||
|> List.sort (fun (_, a) (_, b) -> | ||
let aLoc = locOfKind a in | ||
let bLoc = locOfKind b in | ||
match compare aLoc.loc_start.pos_lnum bLoc.loc_start.pos_lnum with | ||
| 0 -> compare aLoc.loc_start.pos_cnum bLoc.loc_start.pos_cnum | ||
| c -> c) | ||
|> List.iter (fun (stamp, kind) -> | ||
match kind with | ||
| KType t -> | ||
Printf.printf "%d ktype %s\n" stamp | ||
(Warnings.loc_to_string t.extentLoc) | ||
| KValue t -> | ||
Printf.printf "%d kvalue %s\n" stamp | ||
(Warnings.loc_to_string t.extentLoc) | ||
| KModule t -> | ||
Printf.printf "%d kmodule %s\n" stamp | ||
(Warnings.loc_to_string t.extentLoc) | ||
| KConstructor t -> | ||
Printf.printf "%d kconstructor %s\n" stamp | ||
(Warnings.loc_to_string t.extentLoc)); | ||
|
||
(* dump the structure *) | ||
let rec dump_structure indent (structure : Module.structure) = | ||
if indent > 0 then Printf.printf "%s" (String.make indent ' '); | ||
Printf.printf "Structure %s:\n" structure.name; | ||
structure.items |> List.iter (dump_structure_item (indent + 2)) | ||
and dump_structure_item indent item = | ||
if indent > 0 then Printf.printf "%s" (String.make indent ' '); | ||
let open Module in | ||
match item.kind with | ||
| Value _typedExpr -> | ||
Printf.printf "Value %s %s\n" item.name | ||
(Warnings.loc_to_string item.loc) | ||
| Type _ -> | ||
Printf.printf "Type %s %s\n" item.name (Warnings.loc_to_string item.loc) | ||
| Module {type_ = m} -> | ||
Printf.printf "Module %s %s\n" item.name | ||
(Warnings.loc_to_string item.loc); | ||
dump_module indent m | ||
and dump_module indent (module_ : Module.t) = | ||
match module_ with | ||
| Ident path -> Printf.printf "Module (Ident) %s\n" (Path.to_string path) | ||
| Structure structure -> dump_structure indent structure | ||
| Constraint (m1, m2) -> | ||
dump_module indent m1; | ||
dump_module indent m2 | ||
in | ||
|
||
print_newline (); | ||
dump_structure 0 full.file.structure; | ||
|
||
(* Dump all locItems (typed nodes) *) | ||
let locItems = | ||
match full.extra with | ||
| {locItems} -> | ||
locItems |> List.filter (fun locItem -> applyFilter locItem.loc) | ||
in | ||
|
||
Printf.printf "\nFound %d locItems (typed nodes):\n\n" | ||
(List.length locItems); | ||
|
||
locItems | ||
|> List.sort (fun a b -> | ||
let aLoc = a.loc.Location.loc_start in | ||
let bLoc = b.loc.Location.loc_start in | ||
match compare aLoc.pos_lnum bLoc.pos_lnum with | ||
| 0 -> compare aLoc.pos_cnum bLoc.pos_cnum | ||
| c -> c) | ||
|> List.iter (fun {loc; locType} -> | ||
let locStr = Warnings.loc_to_string loc in | ||
let kindStr = SharedTypes.locTypeToString locType in | ||
Printf.printf "%s %s\n" locStr kindStr) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -451,6 +451,52 @@ let processLocalModule name loc ~prefix ~exact ~env | |
(Printf.sprintf "Completion Module Not Found %s loc:%s\n" name | ||
(Loc.toString loc)) | ||
|
||
let processLocalInclude includePath _loc ~prefix ~exact ~(env : QueryEnv.t) | ||
~(localTables : LocalTables.t) = | ||
(* process only values for now *) | ||
localTables.valueTable | ||
|> Hashtbl.iter (fun (name, _) (declared : Types.type_expr Declared.t) -> | ||
(* We check all the values if their origin is the same as the include path. *) | ||
match declared.modulePath with | ||
| ModulePath.ExportedModule {name = exportedName} | ||
when exportedName = includePath -> | ||
if Utils.checkName name ~prefix ~exact then | ||
if not (Hashtbl.mem localTables.namesUsed name) then ( | ||
Hashtbl.add localTables.namesUsed name (); | ||
localTables.resultRev <- | ||
{ | ||
(Completion.create declared.name.txt ~env | ||
~kind:(Value declared.item)) | ||
with | ||
deprecated = declared.deprecated; | ||
docstring = declared.docstring; | ||
synthetic = true; | ||
} | ||
:: localTables.resultRev) | ||
| ModulePath.IncludedModule (source, _) -> | ||
let source_module_path = | ||
match Path.flatten source with | ||
| `Contains_apply -> "" | ||
| `Ok (ident, path) -> ident.name :: path |> String.concat "." | ||
in | ||
|
||
if String.ends_with ~suffix:includePath source_module_path then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure how we could entirely match this. |
||
(* If this is the case we perform a similar check for the prefix *) | ||
if Utils.checkName name ~prefix ~exact then | ||
if not (Hashtbl.mem localTables.namesUsed name) then ( | ||
Hashtbl.add localTables.namesUsed name (); | ||
localTables.resultRev <- | ||
{ | ||
(Completion.create declared.name.txt ~env | ||
~kind:(Value declared.item)) | ||
with | ||
deprecated = declared.deprecated; | ||
docstring = declared.docstring; | ||
synthetic = true; | ||
} | ||
:: localTables.resultRev) | ||
| _ -> ()) | ||
|
||
let getItemsFromOpens ~opens ~localTables ~prefix ~exact ~completionContext = | ||
opens | ||
|> List.fold_left | ||
|
@@ -467,6 +513,7 @@ let findLocalCompletionsForValuesAndConstructors ~(localTables : LocalTables.t) | |
localTables |> LocalTables.populateValues ~env; | ||
localTables |> LocalTables.populateConstructors ~env; | ||
localTables |> LocalTables.populateModules ~env; | ||
|
||
scope | ||
|> Scope.iterValuesBeforeFirstOpen | ||
(processLocalValue ~prefix ~exact ~env ~localTables); | ||
|
@@ -491,6 +538,10 @@ let findLocalCompletionsForValuesAndConstructors ~(localTables : LocalTables.t) | |
scope | ||
|> Scope.iterModulesAfterFirstOpen | ||
(processLocalModule ~prefix ~exact ~env ~localTables); | ||
|
||
scope | ||
|> Scope.iterIncludes (processLocalInclude ~prefix ~exact ~env ~localTables); | ||
|
||
List.rev_append localTables.resultRev valuesFromOpens | ||
|
||
let findLocalCompletionsForValues ~(localTables : LocalTables.t) ~env ~prefix | ||
|
@@ -515,6 +566,10 @@ let findLocalCompletionsForValues ~(localTables : LocalTables.t) ~env ~prefix | |
scope | ||
|> Scope.iterModulesAfterFirstOpen | ||
(processLocalModule ~prefix ~exact ~env ~localTables); | ||
|
||
scope | ||
|> Scope.iterIncludes (processLocalInclude ~prefix ~exact ~env ~localTables); | ||
|
||
List.rev_append localTables.resultRev valuesFromOpens | ||
|
||
let findLocalCompletionsForTypes ~(localTables : LocalTables.t) ~env ~prefix | ||
|
@@ -1049,6 +1104,8 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact | |
| None -> []) | ||
| CPPipe {contextPath = cp; id = prefix; lhsLoc; inJsx; synthetic} -> ( | ||
if Debug.verbose () then print_endline "[ctx_path]--> CPPipe"; | ||
(* The environment at the cursor is the environment we're completing from. *) | ||
let env_at_cursor = env in | ||
match | ||
cp | ||
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env | ||
|
@@ -1175,8 +1232,8 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact | |
in | ||
(* Add completions from the current module. *) | ||
let currentModuleCompletions = | ||
completionsForPipeFromCompletionPath ~envCompletionIsMadeFrom | ||
~opens:[] ~pos ~scope ~debug ~prefix ~env ~rawOpens ~full [] | ||
getCompletionsForPath ~debug ~completionContext:Value ~exact:false | ||
~opens:[] ~full ~pos ~env:env_at_cursor ~scope [prefix] | ||
|> TypeUtils.filterPipeableFunctions ~synthetic:true ~env ~full | ||
~targetTypeId:mainTypeId | ||
in | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cristianoc, this part was a bit weird.
When I have only:
in the test file, it it not needed.
Having the entire
tests/analysis_tests/tests/src/IncludeModuleCompletion.res
made the matchModulePath.ExportedModule
case necessary.I can't explain this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's indeed almost unexplainable.
Turns out that when the value table is populated, each value declared in
env
is processed, and the pair (name, position
) updated in the table.Included values exist alongside the original values. So there are two values
lex
: one fromM
with module pathExportedModule
, and one frominclude M
with module pathIncludedModule
.The last second one processed overwrites the first one.
And the order seems kind of arbitrary and changes with small changes to the source file.
Interestingly, when including values from other files, since we use the pair (
name, position
), there could be accidental clash in theposition
, which is just a pair of numbers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nojaf if included values go to a separate table in
LocalTables.ml
, is it going to work? Or is there a need for them to be in the value table?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, that is an interesting find. Would have never figured that one out, thanks!
I've added a check not to override under very specific conditions.
@cristianoc is this the way to go here?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, they could just be a list, as the location and name are never used, except the name for filtering at the end?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A separate table could work, so a new one based on
ModulePath.IncludedModule _
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cristianoc a separate table works fine.