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

feat: adding sections and modules in outlines #1059

Merged
merged 3 commits into from
Mar 5, 2025
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
32 changes: 20 additions & 12 deletions language-server/dm/document.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ module LM = Map.Make (Int)
module SM = Map.Make (Stateid)

type proof_block_type =
| TheoremKind of Decls.theorem_kind
| DefinitionType of Decls.definition_object_kind
| InductiveType of Vernacexpr.inductive_kind
| TheoremKind
| DefinitionType
| InductiveType
| BeginSection
| BeginModule
| End
| Other

type proof_step = {
Expand Down Expand Up @@ -188,10 +191,10 @@ let record_outline document id (ast : Synterp.vernac_control_entry) classif (out
| VernacSynterp _ -> None
| VernacSynPure pure ->
match pure with
| Vernacexpr.VernacStartTheoremProof (kind, _) -> Some (TheoremKind kind)
| Vernacexpr.VernacDefinition ((_, def), _, _) -> Some (DefinitionType def)
| Vernacexpr.VernacFixpoint (_, _) -> Some (DefinitionType Decls.Fixpoint)
| Vernacexpr.VernacCoFixpoint (_, _) -> Some (DefinitionType Decls.CoFixpoint)
| Vernacexpr.VernacStartTheoremProof _ -> Some TheoremKind
| Vernacexpr.VernacDefinition _ -> Some DefinitionType
| Vernacexpr.VernacFixpoint _ -> Some DefinitionType
| Vernacexpr.VernacCoFixpoint _ -> Some DefinitionType
| _ -> None
in
let name = match names with
Expand All @@ -210,14 +213,19 @@ let record_outline document id (ast : Synterp.vernac_control_entry) classif (out
let vernac_gen_expr = ast.v.expr in
let type_, statement = match vernac_gen_expr with
| VernacSynterp (Synterp.EVernacExtend _) when names <> [] -> Some Other, "external"
| VernacSynterp (Synterp.EVernacBeginSection _) -> log (fun () -> Format.sprintf "BEGIN SECTION %s" (string_of_id document id)); Some BeginSection, ""
| VernacSynterp (Synterp.EVernacDeclareModuleType _) -> log (fun () -> Format.sprintf "BEGIN MODULE %s" (string_of_id document id)); Some BeginModule, ""
| VernacSynterp (Synterp.EVernacDefineModule _) -> log (fun () -> Format.sprintf "BEGIN MODULE %s" (string_of_id document id)); Some BeginModule, ""
| VernacSynterp (Synterp.EVernacDeclareModule _) -> log (fun () -> Format.sprintf "BEGIN MODULE %s" (string_of_id document id)); Some BeginModule, ""
| VernacSynterp (Synterp.EVernacEndSegment _) -> log (fun () -> Format.sprintf "END SEGMENT"); Some End, ""
| VernacSynterp _ -> None, ""
| VernacSynPure pure ->
match pure with
| Vernacexpr.VernacStartTheoremProof (kind, _) -> Some (TheoremKind kind), string_of_id document id
| Vernacexpr.VernacDefinition ((_, def), _, _) -> Some (DefinitionType def), string_of_id document id
| Vernacexpr.VernacInductive (kind, _) -> Some (InductiveType kind), string_of_id document id
| Vernacexpr.VernacFixpoint (_, _) -> Some (DefinitionType Decls.Fixpoint), string_of_id document id
| Vernacexpr.VernacCoFixpoint (_, _) -> Some (DefinitionType Decls.CoFixpoint), string_of_id document id
| Vernacexpr.VernacStartTheoremProof _ -> Some TheoremKind, string_of_id document id
| Vernacexpr.VernacDefinition _ -> Some DefinitionType, string_of_id document id
| Vernacexpr.VernacInductive _ -> Some InductiveType, string_of_id document id
| Vernacexpr.VernacFixpoint _ -> Some DefinitionType, string_of_id document id
| Vernacexpr.VernacCoFixpoint _ -> Some DefinitionType, string_of_id document id
| _ -> None, ""
in
let name = match names with
Expand Down
9 changes: 6 additions & 3 deletions language-server/dm/document.mli
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ open Lsp.Types
type document

type proof_block_type =
| TheoremKind of Decls.theorem_kind
| DefinitionType of Decls.definition_object_kind
| InductiveType of Vernacexpr.inductive_kind
| TheoremKind
| DefinitionType
| InductiveType
| BeginSection
| BeginModule
| End
| Other

type proof_step = {
Expand Down
71 changes: 58 additions & 13 deletions language-server/dm/documentManager.ml
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ let get_document_proofs st =
let outline = Document.outline st.document in
let is_theorem Document.{ type_ } =
match type_ with
| TheoremKind _ -> true
| TheoremKind -> true
| _ -> false
in
let mk_proof_block Document.{statement; proof; range } =
Expand All @@ -351,19 +351,64 @@ let get_document_proofs st =
let proofs, _ = List.partition is_theorem outline in
List.map mk_proof_block proofs

let get_document_symbols st =
let outline = Document.outline st.document in
let to_document_symbol elem =
let Document.{name; statement; range; type_} = elem in
let kind = begin match type_ with
| TheoremKind _ -> SymbolKind.Function
| DefinitionType _ -> SymbolKind.Variable
| InductiveType _ -> SymbolKind.Struct
| Other -> SymbolKind.Null
end in
DocumentSymbol.{name; detail=(Some statement); kind; range; selectionRange=range; children=None; deprecated=None; tags=None;}
let to_document_symbol elem =
let Document.{name; statement; range; type_} = elem in
let kind = begin match type_ with
| TheoremKind -> SymbolKind.Function
| DefinitionType -> SymbolKind.Variable
| InductiveType -> SymbolKind.Struct
| Other -> SymbolKind.Null
| BeginSection | BeginModule -> SymbolKind.Class
| End -> SymbolKind.Null
end in
DocumentSymbol.{name; detail=(Some statement); kind; range; selectionRange=range; children=None; deprecated=None; tags=None;}

let rec get_document_symbols outline (sec_or_m: DocumentSymbol.t list) symbols =
let add_child (s_father: DocumentSymbol.t) s_child =
let children = match s_father.children with
| None -> Some [s_child]
| Some l -> Some (l @ [s_child])
in
{s_father with children}
in
let record_in_outline outline symbol sec_or_m =
match sec_or_m with
| [] ->
let symbols = symbols @ [symbol] in
get_document_symbols outline sec_or_m symbols
| s :: l ->
let s = add_child s symbol in
get_document_symbols outline (s::l) symbols
in
List.map to_document_symbol outline
match outline with
| [] -> symbols
| e :: l ->
let Document.{type_} = e in
match type_ with
| TheoremKind | DefinitionType | InductiveType | Other ->
let symbol = to_document_symbol e in
record_in_outline l symbol sec_or_m
| BeginSection ->
let symbol = to_document_symbol e in
get_document_symbols l (symbol :: sec_or_m) symbols
| BeginModule ->
let symbol = to_document_symbol e in
get_document_symbols l (symbol :: sec_or_m) symbols
| End ->
match sec_or_m with
| [] -> log(fun () -> "Trying to end a module or section with no begin"); get_document_symbols l [] symbols
| symbol :: s_l ->
match s_l with
| [] ->
get_document_symbols l s_l (symbols @ [symbol])
| s_parent :: s_l ->
let s = add_child s_parent symbol in
get_document_symbols l (s :: s_l) symbols


let get_document_symbols st =
let outline = List.rev @@ Document.outline st.document in
get_document_symbols outline [] []

let interpret_to st id check_mode =
let observe_id = (Id id) in
Expand Down
Loading