Skip to content
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

[hover] Fix universe printing in hover. #839

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
- [serlib] Fix Ltac2 AST piercing bug, add test case that should help
in the future (@ejgallego, @jim-portegies, #821)
- [fleche] [8.20] understand rewrite rules and symbols on document
outline (@ejgallego, @Alizter, #825, fixes: #824)
outline (@ejgallego, @Alizter, #825, fixes #824)
- [fleche] [coq] support `Restart` meta command (@ejgallego,
@Alizter, #828, fixes #827)
- [fleche] [plugins] New plugin example `explain_errors`, that will
Expand All @@ -23,6 +23,8 @@
provides a full working Coq enviroment in `vscode.dev`. The web
worker version is build as an artifact on CI (@ejgallego
@corwin-of-amber, #433)
- [hover] Fix universe and level printing in hover (#839, fixes #835
, @ejgallego , @Alizter)

# coq-lsp 0.2.0: From Green to Blue
-----------------------------------
Expand Down
71 changes: 59 additions & 12 deletions controller/rq_hover.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,28 @@ let build_ind_type mip = Inductive.type_of_inductive mip

type id_info =
| Notation of Pp.t
| Def of (Pp.t * Names.Constant.t option * string option)

let info_of_ind env sigma ((sp, i) : Names.Ind.t) =
| Def of
{ typ : Pp.t (** type of the ide *)
; params : Pp.t (** params that need display next to the name *)
; full_path : Names.Constant.t option
(** full path of the constant, if any, for example
[Stdlib.Lists.map] *)
; file : string option (** filename where the constant is located *)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have access to the location too? (The one goto definition uses). Not relevant for this PR, but it might be useful to include.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, but not sure line number is relevant here, the client can indeed issue the corresponding specific request for location.

}

let print_params env sigma params =
if CList.is_empty params then Pp.mt ()
else Pp.(spc () ++ Printer.pr_rel_context env sigma params ++ brk (1, 2))

let info_of_ind env ((sp, i) : Names.Ind.t) =
let udecl = None in
let mib = Environ.lookup_mind sp env in
let bl =
Printer.universe_binders_with_opt_names
(Declareops.inductive_polymorphic_context mib)
udecl
in
let sigma = Evd.from_ctx (UState.of_names bl) in
let u =
UVars.make_abstract_instance (Declareops.inductive_polymorphic_context mib)
in
Expand All @@ -37,17 +55,34 @@ let info_of_ind env sigma ((sp, i) : Names.Ind.t) =
(Impargs.implicits_of_global (Names.GlobRef.IndRef (sp, i)))
in
let impargs = List.map Impargs.binding_kind_of_status impargs in
Def (Printer.pr_ltype_env ~impargs env_params sigma arity, None, None)
let inst =
if Declareops.inductive_is_polymorphic mib then
Printer.pr_universe_instance sigma u
else Pp.mt ()
in
let params = EConstr.Unsafe.to_rel_context params in
let typ = Printer.pr_ltype_env ~impargs env_params sigma arity in
let params = Pp.(inst ++ print_params env sigma params) in
Def { typ; params; full_path = None; file = None }

let type_of_constant cb = cb.Declarations.const_type

let info_of_const env sigma cr =
let info_of_const env cr =
let udecl = None in
let cdef = Environ.lookup_constant cr env in
let bl =
Printer.universe_binders_with_opt_names
(Environ.constant_context env cr)
udecl
in
let sigma = Evd.from_ctx (UState.of_names bl) in
(* This prints the definition *)
(* let cb = Environ.lookup_constant cr env in *)
(* Option.cata (fun (cb,_univs,_uctx) -> Some cb ) None *)
(* (Global.body_of_constant_body Library.indirect_accessor cb), *)
let typ = type_of_constant cdef in
let univs = Declareops.constant_polymorphic_context cdef in
let inst = UVars.make_abstract_instance univs in
let impargs =
Impargs.select_stronger_impargs
(Impargs.implicits_of_global (Names.GlobRef.ConstRef cr))
Expand All @@ -56,7 +91,12 @@ let info_of_const env sigma cr =
let typ = Printer.pr_ltype_env env sigma ~impargs typ in
let dp = Names.Constant.modpath cr |> Names.ModPath.dp in
let source = Coq.Module.(make dp |> Result.to_option |> Option.map source) in
Def (typ, Some cr, source)
let inst =
if Environ.polymorphic_constant cr env then
Printer.pr_universe_instance sigma inst
else Pp.mt ()
in
Def { typ; params = inst; full_path = Some cr; file = source }

let info_of_var env vr =
let vdef = Environ.lookup_named vr env in
Expand All @@ -73,7 +113,13 @@ let info_of_constructor env cr =
in
ctype

let print_type env sigma x = Def (Printer.pr_ltype_env env sigma x, None, None)
let print_type env sigma x =
Def
{ typ = Printer.pr_ltype_env env sigma x
; params = Pp.mt ()
; full_path = None
; file = None
}

let info_of_id env sigma id =
let qid = Libnames.qualid_of_string id in
Expand All @@ -89,8 +135,8 @@ let info_of_id env sigma id =
let open Names.GlobRef in
(match lid with
| VarRef vr -> info_of_var env vr |> print_type env sigma
| ConstRef cr -> info_of_const env sigma cr
| IndRef ir -> info_of_ind env sigma ir
| ConstRef cr -> info_of_const env cr
| IndRef ir -> info_of_ind env ir
| ConstructRef cr -> info_of_constructor env cr |> print_type env sigma)
|> fun x -> Some x
| Abbrev kn ->
Expand Down Expand Up @@ -128,11 +174,12 @@ let pp_file fmt = function
| Some file -> Format.fprintf fmt " - **in file**: `%s`" file

let pp_typ id = function
| Def (typ, cr, file) ->
| Def { typ; params; full_path; file } ->
let typ = Pp.string_of_ppcmds typ in
let param = Pp.string_of_ppcmds params in
Format.(
asprintf "@[```coq\n%s : %s@\n```@\n@[%a@]@[%a@]@]" id typ pp_cr cr
pp_file file)
asprintf "@[```coq\n%s%s: %s@\n```@\n@[%a@]@[%a@]@]" id param typ pp_cr
full_path pp_file file)
| Notation nt ->
let nt = Pp.string_of_ppcmds nt in
Format.(asprintf "```coq\n%s\n```" nt)
Expand Down
14 changes: 14 additions & 0 deletions examples/print_univs.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
From Coq Require Import Prelude.
Set Printing Universes.
Set Universe Polymorphism.

Definition arr (S: Type) : Type := S.

Print arr.

Inductive foo (M : Type) : Type -> Type :=
bar : M -> Type -> foo M nat.

Print foo.

About foo.