package forester
A tool for tending mathematical forests
Install
Dune Dependency
Authors
Maintainers
Sources
5.0.tar.gz
md5=24f4aed96a8b8af33aba13fba66f1b37
sha512=d36b896aca11858bb4a00fc704c16cc27a1f197bdb3e479d6132fd70f70d67d7158096285cb0b6fb00db14417f0f822cc27fe65d82f0971e42378fd8271ce573
doc/src/forester.language_server/Completion.ml.html
Source file Completion.ml
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
(* * SPDX-FileCopyrightText: 2024 The Forester Project Contributors AND The RedPRL Development Team * * SPDX-License-Identifier: GPL-3.0-or-later OR Apache-2.0 WITH LLVM-exception * *) open Forester_prelude open Forester_core open Forester_compiler open Forester_frontend open State.Syntax open struct module T = Types module L = Lsp.Types end type completion = | Addrs | New_addr | Assets | Visible [@@deriving show] module S = Set.Make(struct type t = completion let compare = compare end) type completion_kind = { text: string -> completion option; code: Code.node Asai.Range.located Analysis.Context.t -> completion option; syn: Syn.node Asai.Range.located Analysis.Context.t -> completion option; } let subtree_completion : completion_kind = let text word_before = if Str.(string_match (regexp {|.*subtree.*|}) word_before 0) then Some New_addr else None in let code (context : _ Analysis.Context.t) = match context with | Prev (Asai.Range.{value = Code.Subtree (_, _); _}, _) | Prev (_, Asai.Range.{value = Code.Subtree (_, _); _}) | Parent {value = Code.Subtree (_, _); _} -> Some New_addr | Parent _ | Prev (_, _) | Top _ -> None in let syn (context : Syn.node Range.located Analysis.Context.t) = match context with | (Top {value = Subtree _; _}) -> Some New_addr | (Prev (_, {value = Subtree _; _;})) -> Some New_addr | _ -> None in {text; code; syn} let asset_completion : completion_kind = let text word_before = if Str.(string_match (regexp {|.*route-asset.*|}) word_before 0) then Some Assets else None in let code (context : _ Analysis.Context.t) = match context with | Prev (Asai.Range.{value = Code.Ident ["route-asset"]; _}, _) | Prev (_, Asai.Range.{value = Code.Ident ["route-asset"]; _}) -> Some Assets | Prev (_, _) | Parent _ | Top _ -> None in let syn (context : Syn.node Range.located Analysis.Context.t) = match context with | (Top {value = Route_asset; _}) -> Some Assets | (Prev (_, {value = Route_asset; _;})) -> Some Assets | _ -> None in {text; code; syn} let uri_completion : completion_kind = let text word_before = if Str.(string_match (regexp {|.*]($|}) word_before 0) || Str.(string_match (regexp {|.*\[\[$|}) word_before 0) || Str.(string_match (regexp {|.*transclude.*|}) word_before 0) then Some Addrs else None in let code (context : _ Analysis.Context.t) = match context with | Prev (_, _) | Parent _ | Top _ -> None in let syn (context : Syn.node Range.located Analysis.Context.t) = match context with | _ -> None in {text; code; syn} let new_uri_completion : completion_kind = let text context = if Str.(string_match (regexp {|.*subtree\[.*|}) context 0) then Some New_addr else None in let code context = Option.map (Fun.const New_addr) (uri_completion.code context) in let syn context = Option.map (Fun.const New_addr) (uri_completion.syn context) in {text; code; syn} let completion_types (t : Tree.t) ~position = let completions : completion_kind list = [ uri_completion; asset_completion; subtree_completion; new_uri_completion; ] in let code_opt = Tree.to_code t in let syn_opt = Tree.to_syn t in let doc_opt = Tree.to_doc t in let text_context = Option.bind doc_opt @@ Analysis.word_before ~position in Logs.debug (fun m -> m "Text_context: %a" Format.(pp_print_option pp_print_string) text_context); let code_context = let enclosing_group = Analysis.get_enclosing_code_group in let@ position = Option.bind @@ Analysis.enclosing_group_start ~enclosing_group ~position t in let@ code = Option.bind code_opt in Analysis.parent_or_prev_at_code ~position code.nodes in let syn_context = let enclosing_group = Analysis.get_enclosing_syn_group in let@ position = Option.bind @@ Analysis.enclosing_group_start ~enclosing_group ~position t in let@ syn = Option.bind syn_opt in Analysis.parent_or_prev_at_syn ~position syn.nodes in completions |> List.fold_left begin fun acc completion_kind -> (* let module Kind = (val completion_kind : CompletionKind) in *) let compl = List.fold_left begin fun acc a -> match a with | None -> acc | Some compl -> S.add compl acc end S.empty [ Option.bind text_context completion_kind.text; Option.bind code_context completion_kind.code; Option.bind syn_context completion_kind.syn; ] in S.union compl acc end S.empty let kind : Syn.node -> L.CompletionItemKind.t option = function | Fun (_, _) -> Some Function | Text _ | Verbatim _ -> Some Text | Meta -> Some Field | Route_asset -> Some File | Var _ -> Some Variable | Prim _ | Transclude | Embed_tex | Title | Parent | Taxon | Attribution (_, _) | Tag _ | Date | Number -> Some Keyword | Ref -> Some Reference | Group (_, _) | Math (_, _) | Link _ | Subtree (_, _) | Sym _ | Put (_, _, _) | Default (_, _, _) | Get _ | Xml_tag (_, _, _) | TeX_cs _ | Unresolved_ident _ | Object _ | Patch _ | Call (_, _) | Results_of_query | Dx_sequent (_, _) | Dx_query (_, _, _) | Dx_prop (_, _) | Dx_var _ | Dx_const (_, _) | Dx_execute | Syndicate_current_tree_as_atom_feed | Syndicate_query_as_json_blob | Current_tree -> None let insert_text path = String.concat "/" path let asset_completions ~(config : Config.t) () = let asset_dirs = config.assets in let paths = List.of_seq @@ Hashtbl.to_seq_keys Asset_router.router in let@ asset_path = List.filter_map @~ paths in let@ insertText = Option.map @~ List.find_map (fun dir -> let dir_path = Unix.realpath dir in if String.starts_with ~prefix: dir_path asset_path then try Some ( String.sub asset_path (String.length dir_path - String.length dir) (String.length asset_path - String.length dir_path + String.length dir) ) with | _ -> None else None ) asset_dirs in L.CompletionItem.create ~label: insertText ~kind: File ~insertText () let make (path, (data, _): Yuujinchou.Trie.path * (Resolver.P.data * Asai.Range.t option)) = match data with | Term syn -> (* NOTE: Eventually we want to analyse the syntax so that, for example, you can tab through the snippet for a function of arity n*) let kind = kind (List.hd syn).value in let insertText = insert_text path in Some ( L.CompletionItem.create ?kind ~insertText ~label: (String.concat "/" path) () ) | Xmlns _ -> assert false (* These are useful completion items that are handled during parsing, not during expansion and are thus not "builtins"*) module Syntax = struct let verb = let insertText = Lsp.Snippet.( let open O in to_string @@ "\\startverb<<|\n" @+ tabstop 1 +@ "\n<<|" ) in L.CompletionItem.create ~insertTextFormat: Snippet ~insertText ~label: "startverb" ~documentation: (`String "Create a verbatim block") () end (* Incomplete. The idea here is to create clever snippets for specific syntax items. *) let syntax_completions = [ ("startverb", "startverb"); ("scope", "scope"); ("put", "put"); ("put?", "put?"); ("get", "get"); ("import", "import"); ("export", "export"); ("namespace", "namespace"); ("open", "open"); ("def", "def"); ("alloc", "alloc"); ("let", "let"); ("fun", "fun"); ("subtree", "subtree"); ("object", "object"); ("patch", "patch"); ("call", "call"); ("datalog", "datalog"); ("xmlns", "xmlns"); ] |> List.map (fun (insertText, label) -> L.CompletionItem.create ~insertText ~label () ) let compute ({context; position; textDocument = {uri}; _;}: L.CompletionParams.t) = Logs.debug (fun m -> m "when computing completions for %s" (Lsp.Uri.to_string uri)); let triggerCharacter = match context with | Some {triggerCharacter; _} -> triggerCharacter | None -> None in let Lsp_state.{forest; _} = Lsp_state.get () in let config = forest.config in let base = config.url in let uri = URI_scheme.lsp_uri_to_uri ~base uri in let tree = forest.={uri} in match tree with | None -> Reporter.fatal Internal_error ~backtrace: (Bwd.of_list [Asai.Diagnostic.loctextf "when computing completions for %a" URI.pp uri]) ~extra_remarks: [Asai.Diagnostic.loctextf "%a was not found in the index" URI.pp uri] | Some tree -> let code = Tree.to_code tree in Logs.debug (fun m -> m "Received completion request at %s" (Yojson.Safe.to_string (L.Position.yojson_of_t position))); Logs.debug (fun m -> m "phase is %s" (Tree.show_phase tree)); let completion_types = completion_types ~position tree in Logs.debug (fun m -> m "computed completion types: %a" (Format.pp_print_list pp_completion) (S.to_list completion_types)); let items = let@ completion = List.concat_map @~ S.to_list completion_types in match completion with | Addrs -> List.of_seq @@ let@ uri, tree = Seq.map @~ URI.Tbl.to_seq forest.index in let frontmatter = Tree.get_frontmatter tree in let title = Option.map (State.get_expanded_title @~ forest) frontmatter in let render = Plain_text_client.string_of_content ~forest in let documentation = let taxon = Option.bind frontmatter (fun fm -> fm.taxon) in let content = Format.asprintf {|%s\n %s\n |} (Option.fold ~none: "" ~some: (fun s -> Format.asprintf "# %s" (render s)) title) (Option.fold ~none: "" ~some: (fun s -> Format.asprintf "taxon: %s" (render s)) taxon) in Some (`String content) in let insertText = (* TODO if host = current_host insert shortform else insert fully qualified uri*) match triggerCharacter with | Some "{" -> URI_scheme.name uri ^ "}" | Some "(" -> URI_scheme.name uri ^ ")" | Some "[" -> URI_scheme.name uri ^ "]" | _ -> URI_scheme.name uri in let title_text = Option.map render title in let filterText = Option.fold ~none: insertText ~some: (fun s -> insertText ^ " " ^ s) title_text in L.CompletionItem.create ?documentation ~label: (Format.(asprintf "%a (%s)" (pp_print_option pp_print_string) title_text (URI_scheme.name uri))) ~insertText ~filterText () | New_addr -> let next mode = URI_util.next_uri ~prefix: None ~mode ~forest in [ L.CompletionItem.create ~label: "random" ~insertText: (next `Random) (); L.CompletionItem.create ~label: "sequential" ~insertText: (next `Sequential) () ] | Assets -> asset_completions ~config () | Visible -> begin match code with | None -> List.append syntax_completions @@ let@ path, _ = List.map @~ List.of_seq @@ Trie.to_seq Expand.initial_visible_trie in L.CompletionItem.create ~insertText: "todo" ~label: (String.concat "/" path) () | Some {nodes; _} -> Analysis.get_visible ~position ~forest nodes |> Trie.to_seq |> List.of_seq |> List.filter_map make |> List.append syntax_completions end in Logs.debug (fun m -> m "items: %d" (List.length items)); Option.some @@ `CompletionList (L.CompletionList.create ~isIncomplete: false ~items ())
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>