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
Expand file tree
/
Copy pathEditorSupportCommands.ml
More file actions
132 lines (128 loc) · 4.46 KB
/
EditorSupportCommands.ml
File metadata and controls
132 lines (128 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
let dumpLocations state ~package ~file ~extra ~selectPos uri =
let locations =
extra.SharedTypes.locations
|> List.filter (fun (l, _) -> not l.Location.loc_ghost)
in
let locations =
match selectPos with
| Some pos -> (
let pos = Utils.cmtLocFromVscode pos in
match References.locForPos ~extra:{extra with locations} pos with
| None -> []
| Some l -> [l] )
| None -> locations
in
let dedupTable = Hashtbl.create 1 in
let dedupHover hover i =
let isCandidate = String.length hover > 10 in
if isCandidate then (
match Hashtbl.find_opt dedupTable hover with
| Some n -> Json.String ("#" ^ string_of_int n)
| None ->
Hashtbl.replace dedupTable hover i;
Json.String hover )
else Json.String hover
in
let locationsInfo =
locations
|> Utils.filterMapIndex (fun i ((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", dedupHover s i)]
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) && loc.loc_start |> posIsZero
&& loc.loc_end |> posIsZero
in
let range = ("range", Protocol.rangeOfLoc loc) in
(
[
("definition",
Json.Object
(match uriIsCurrentFile with
| 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", Protocol.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)
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
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
in
print_endline result)
let autocomplete ~currentFile ~full ~package ~pos ~state =
let maybeText = Files.readFile currentFile in
NewCompletions.computeCompletions ~full ~maybeText ~package ~pos ~state
|> List.map Protocol.stringifyCompletionItem
|> Protocol.array
let complete ~pathWithPos ~currentFile =
let state = TopTypes.empty () in
match pathWithPos |> splitLineChar with
| filePath, Some pos ->
let filePath = Files.maybeConcat (Unix.getcwd ()) filePath in
let uri = Uri2.fromPath filePath in
let result =
match State.getFullFromCmt ~state ~uri with
| Error message ->
prerr_endline message;
"[]"
| Ok (package, full) ->
autocomplete ~currentFile ~full ~package ~pos ~state
in
print_endline result
| _ -> ()