Skip to content

Commit b460a86

Browse files
committed
Generalize support for hover
We are still missing some cases (tactics, local context, notations).
1 parent c4451c1 commit b460a86

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

language-server/dm/documentManager.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ let get_proof st pos =
274274
let get_context st pos =
275275
let loc = RawDocument.loc_of_position (Document.raw_document st.document) pos in
276276
match Document.find_sentence_before st.document loc with
277-
| None -> None
277+
| None -> Some (ExecutionManager.get_initial_context st.execution_state)
278278
| Some sentence ->
279279
ExecutionManager.get_context st.execution_state sentence.id
280280

language-server/dm/executionManager.ml

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -571,24 +571,30 @@ let rec invalidate schedule id st =
571571
let deps = Scheduler.dependents schedule id in
572572
Stateid.Set.fold (invalidate schedule) deps { st with of_sentence }
573573

574-
let get_context st id =
575-
match find_fulfilled_opt id st.of_sentence with
576-
| None -> log "Cannot find state for get_context"; None
577-
| Some (Error _) -> log "Context requested in error state"; None
578-
| Some (Success None) -> log "Context requested in a remotely checked state"; None
579-
| Some (Success (Some { interp = st })) ->
574+
let context_of_state st =
580575
Vernacstate.Interp.unfreeze_interp_state st;
581576
begin match st.lemmas with
582577
| None ->
583578
let env = Global.env () in
584579
let sigma = Evd.from_env env in
585-
Some (sigma, env)
580+
sigma, env
586581
| Some lemmas ->
587582
let open Declare in
588583
let open Vernacstate in
589-
lemmas |> LemmaStack.with_top ~f:Proof.get_current_context |> Option.make
584+
lemmas |> LemmaStack.with_top ~f:Proof.get_current_context
590585
end
591586

587+
let get_context st id =
588+
match find_fulfilled_opt id st.of_sentence with
589+
| None -> log "Cannot find state for get_context"; None
590+
| Some (Error _) -> log "Context requested in error state"; None
591+
| Some (Success None) -> log "Context requested in a remotely checked state"; None
592+
| Some (Success (Some { interp = st })) ->
593+
Some (context_of_state st)
594+
595+
let get_initial_context st =
596+
context_of_state st.initial.Vernacstate.interp
597+
592598
let get_vernac_state st id =
593599
match find_fulfilled_opt id st.of_sentence with
594600
| None -> log "Cannot find state for get_context"; None

language-server/dm/executionManager.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ val is_executed : state -> sentence_id -> bool
5252
val is_remotely_executed : state -> sentence_id -> bool
5353

5454
val get_context : state -> sentence_id -> (Evd.evar_map * Environ.env) option
55+
val get_initial_context : state -> Evd.evar_map * Environ.env
5556

5657
(** Returns the vernac state after the sentence *)
5758
val get_vernac_state : state -> sentence_id -> Vernacstate.t option

language-server/language/hover.ml

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,19 @@ let ref_info env _sigma ref udecl =
222222
let impargs = List.map Impargs.binding_kind_of_status impargs in
223223
let typ = pr_ltype_env env sigma ~impargs typ in
224224
let path = Libnames.pr_path (Nametab.path_of_global ref) ++ canonical_name_info ref in
225-
[md_code_block @@ compactify @@ Pp.string_of_ppcmds typ
226-
; "**Args:** " ^ (Pp.string_of_ppcmds (print_arguments ref))
227-
; Format.sprintf "**%s** %s" (pr_globref_kind ref) (Pp.string_of_ppcmds path)
228-
]
225+
let args = print_arguments ref in
226+
let args = if Pp.ismt args then [] else ["**Args:** " ^ (Pp.string_of_ppcmds (print_arguments ref))] in
227+
[md_code_block @@ compactify @@ Pp.string_of_ppcmds typ]@args@
228+
[Format.sprintf "**%s** %s" (pr_globref_kind ref) (Pp.string_of_ppcmds path)]
229+
230+
let mod_info mp =
231+
[md_code_block @@ Format.sprintf "Module %s" (DirPath.to_string (Nametab.dirpath_of_module mp))]
232+
233+
let modtype_info mp =
234+
[md_code_block @@ Format.sprintf "Module Type %s" (Libnames.string_of_path (Nametab.path_of_modtype mp))]
235+
236+
let syndef_info kn =
237+
[md_code_block @@ Format.sprintf "Notation %s" (Libnames.string_of_path (Nametab.path_of_abbreviation kn))]
229238

230239
let get_hover_contents env sigma ref_or_by_not =
231240
match ref_or_by_not.CAst.v with
@@ -234,6 +243,19 @@ let get_hover_contents env sigma ref_or_by_not =
234243
let udecl = None in
235244
let ref = Nametab.locate qid in
236245
Some (ref_info env sigma ref udecl)
237-
with Not_found -> None
246+
with Not_found ->
247+
try
248+
let kn = Nametab.locate_abbreviation qid in
249+
Some (syndef_info kn)
250+
with Not_found ->
251+
try
252+
let mp = Nametab.locate_module qid in
253+
Some (mod_info mp)
254+
with Not_found ->
255+
try
256+
let mp = Nametab.locate_modtype qid in
257+
Some (modtype_info mp)
258+
with Not_found ->
259+
None
238260
end
239261
| Constrexpr.ByNotation (_ntn,_sc) -> assert false

0 commit comments

Comments
 (0)