This repository was archived by the owner on Apr 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Clean up dumpLocations a bit #115
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,49 @@ | ||
let posOfLexing {Lexing.pos_lnum; pos_cnum; pos_bol} = | ||
Json.Object | ||
[ | ||
("line", Json.Number (float_of_int (pos_lnum - 1))); | ||
("character", Json.Number (float_of_int (pos_cnum - pos_bol))); | ||
] | ||
|
||
let rangeOfLoc {Location.loc_start; loc_end} = | ||
Json.Object [("start", posOfLexing loc_start); ("end", posOfLexing loc_end)] | ||
|
||
let dumpLocations state ~package ~file ~extra ~selectPos uri = | ||
let dumpLocations state ~package ~file ~extra = | ||
let locations = | ||
extra.SharedTypes.locations | ||
|> List.filter (fun (l, _) -> not l.Location.loc_ghost) | ||
in | ||
let locations = | ||
match selectPos with | ||
| Some (line, col) -> ( | ||
let pos = Utils.protocolLineColToCmtLoc ~line ~col in | ||
match References.locForPos ~extra:{extra with locations} pos with | ||
| None -> [] | ||
| Some l -> [l]) | ||
| None -> locations | ||
in | ||
let locationsInfo = | ||
locations | ||
|> Utils.filterMap (fun ((location : Location.t), loc) -> | ||
let locIsModule = | ||
match loc with | ||
| SharedTypes.LModule _ | TopLevelModule _ -> true | ||
| TypeDefinition _ | Typed _ | Constant _ | Explanation _ -> false | ||
in | ||
let hoverText = | ||
Hover.newHover ~file | ||
~getModule:(State.fileForModule state ~package) | ||
loc | ||
in | ||
let hover = | ||
match hoverText with | ||
| None -> [] | ||
| Some s -> [("hover", Json.String s)] | ||
in | ||
let uriLocOpt = | ||
References.definitionForLoc ~pathsForModule:package.pathsForModule | ||
~file ~getUri:(State.fileForUri state) | ||
~getModule:(State.fileForModule state ~package) | ||
loc | ||
in | ||
let def, skipZero = | ||
match uriLocOpt with | ||
| None -> ([], false) | ||
| Some (uri2, loc) -> | ||
let uriIsCurrentFile = uri = uri2 in | ||
let posIsZero {Lexing.pos_lnum; pos_bol; pos_cnum} = | ||
pos_lnum = 1 && pos_cnum - pos_bol = 0 | ||
in | ||
(* Skip if range is all zero, unless it's a module *) | ||
let skipZero = | ||
(not locIsModule) && posIsZero loc.loc_start | ||
&& posIsZero loc.loc_end | ||
in | ||
let range = ("range", rangeOfLoc loc) in | ||
( [ | ||
( "definition", | ||
Json.Object | ||
(match uriIsCurrentFile with | ||
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. Removed this check too |
||
| true -> [range] | ||
| false -> | ||
[("uri", Json.String (Uri2.toString uri2)); range]) ); | ||
], | ||
skipZero ) | ||
in | ||
let skip = skipZero || (hover = [] && def = []) in | ||
match skip with | ||
| true -> None | ||
| false -> | ||
Some (Json.Object ([("range", rangeOfLoc location)] @ hover @ def))) | ||
in | ||
Json.stringify (Json.Array locationsInfo) | ||
|
||
(* Split (line,char) from filepath:line:char *) | ||
let splitLineChar pathWithPos = | ||
let mkPos line char = Some (line |> int_of_string, char |> int_of_string) in | ||
match pathWithPos |> String.split_on_char ':' with | ||
| [filePath; line; char] -> (filePath, mkPos line char) | ||
| [drive; rest; line; char] -> | ||
(* c:\... on Windows *) | ||
(drive ^ ":" ^ rest, mkPos line char) | ||
| _ -> (pathWithPos, None) | ||
locations | ||
|> List.map (fun ((location : Location.t), loc) -> | ||
let hoverText = | ||
Hover.newHover ~file | ||
~getModule:(State.fileForModule state ~package) | ||
loc | ||
in | ||
let hover = | ||
match hoverText with None -> "" | Some s -> String.escaped s | ||
in | ||
let uriLocOpt = | ||
References.definitionForLoc ~pathsForModule:package.pathsForModule | ||
~file ~getUri:(State.fileForUri state) | ||
~getModule:(State.fileForModule state ~package) | ||
loc | ||
in | ||
let def = | ||
match uriLocOpt with | ||
| None -> Protocol.null | ||
| Some (uri2, loc) -> | ||
Protocol.stringifyLocation | ||
{uri = Uri2.toString uri2; range = Utils.cmtLocToRange loc} | ||
in | ||
Protocol.stringifyRange (Utils.cmtLocToRange location) | ||
^ "\n Hover: " ^ hover ^ "\n Definition: " ^ def) | ||
|> String.concat "\n\n" | ||
|
||
let dump files = | ||
Shared.cacheTypeToString := true; | ||
let state = TopTypes.empty () in | ||
files | ||
|> List.iter (fun pathWithPos -> | ||
let filePath, selectPos = pathWithPos |> splitLineChar in | ||
let filePath = Files.maybeConcat (Unix.getcwd ()) filePath in | ||
|> List.iter (fun path -> | ||
let filePath = Files.maybeConcat (Unix.getcwd ()) path in | ||
let uri = Uri2.fromPath filePath in | ||
let result = | ||
match State.getFullFromCmt ~state ~uri with | ||
| Error message -> | ||
prerr_endline message; | ||
"[]" | ||
| Ok (package, {file; extra}) -> | ||
dumpLocations state ~package ~file ~extra ~selectPos uri | ||
dumpLocations state ~package ~file ~extra | ||
in | ||
print_endline result) | ||
|
||
|
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.
Removed skipZero. Didn't make a different on my test file and not sure if important enough
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 needed for correctness, with PPXs etc.
But, for debugging it might even be better to remove skipZero so you see the raw stuff not the curated one.