···88open Forester_core
99open Forester_compiler
1010open Forester_frontend
1111+open Forester_search
1212+open State.Syntax
11131214module L = Lsp.Types
1313-(* module F = Analysis.F *)
1414-(* module PT = Analysis.PT *)
1515module T = Types
16161717-let compute
1818- ({position;
1919- textDocument;
2020- _
2121- }:
2222- L.HoverParams.t
2323- )
2424- : L.Hover.t option
2525- =
1717+let (let*) = Option.bind
1818+1919+let compute ({position; textDocument; _}: L.HoverParams.t) =
2620 let Lsp_state.{forest; _} = Lsp_state.get () in
2721 let render =
2822 Plain_text_client.string_of_content
···3125 in
3226 let config = forest.config in
3327 let host = config.host in
3434- let content =
3535- match State.get_code
3636- forest
3737- (URI_scheme.lsp_uri_to_uri ~host: forest.config.host textDocument.uri) with
3838- | None -> "code of current tree is not stored. this is a bug"
2828+ let uri = URI_scheme.lsp_uri_to_uri ~host: forest.config.host textDocument.uri in
2929+ let* content =
3030+ match forest.={uri} with
3131+ | None -> Reporter.fatal Internal_error ~extra_remarks: [Asai.Diagnostic.loctextf "%a is not in the index" URI.pp uri]
3932 | Some tree ->
4040- (* TODO: use node_at and provide hover for things other than links.*)
4141- match Analysis.addr_at ~position tree.nodes with
4242- | None -> Format.asprintf "character: %i, line: %i." position.character position.line;
4343- | Some Range.{value = addr_at_cursor; _} ->
4444- let uri_under_cursor = URI_scheme.user_uri ~host addr_at_cursor in
4545- match State.get_article uri_under_cursor forest with
4646- | None ->
4747- Format.asprintf "Could not get article %a." URI.pp uri_under_cursor
4848- | Some {mainmatter; frontmatter; _} ->
4949- let main = render mainmatter in
5050- if main = "" then (Format.asprintf "%a" T.(pp_frontmatter pp_content) frontmatter)
5151- else main
3333+ match Tree.to_code tree with
3434+ | None -> None
3535+ | Some {nodes; _} ->
3636+ match Analysis.node_at ~position nodes with
3737+ | None -> None
3838+ | Some node ->
3939+ let tree_under_cursor =
4040+ let* {value = addr; _} = Analysis.extract_addr node in
4141+ let uri_under_cursor = URI_scheme.user_uri ~host addr in
4242+ State.get_article uri_under_cursor forest
4343+ in
4444+ match tree_under_cursor with
4545+ | Some article -> Some (render article.mainmatter)
4646+ | None ->
4747+ let* doc = Tree.to_doc tree in
4848+ let* search_term = Analysis.word_at ~position doc in
4949+ let results = List.map snd @@ Index.search forest.search_index search_term in
5050+ Some Format.(asprintf "Relevant results:@.%a@." (pp_print_list ~pp_sep: (fun out () -> fprintf out "@.") URI.pp) results)
5251 in
5352 Some
5453 (