package forester

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file Code_action.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
(*
 * 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_compiler

open struct module L = Lsp.Types end

let resolve (params : L.CodeAction.t) = params

let next_addrs ~(forest : State.t) prefix =
  URI_util.next_uri ~prefix ~mode: `Sequential ~forest, URI_util.next_uri ~prefix ~mode: `Random ~forest

let create_tree_edit ~range ~uri addr dir =
  L.WorkspaceEdit.create
    ~documentChanges: [
      `CreateFile
        (
          L.CreateFile.create
            ~uri: (Lsp.Uri.of_path (Format.asprintf "%s/%s.tree" dir addr))
            ()
        );
      `TextDocumentEdit
        (
          L.TextDocumentEdit.create
            ~textDocument: {uri; version = None}
            ~edits: [`TextEdit {newText = Format.asprintf "\\transclude{%s}" addr; range}]
        )
    ]
    ()

let compute (L.CodeActionParams.{range; textDocument = {uri}; _;}) : L.CodeActionResult.t =
  let Lsp_state.{forest; _} = Lsp_state.get () in
  let actions =
    let next_sequential, next_random = next_addrs ~forest None in
    match forest.config.trees with
    | [] -> []
    | dir :: _ ->
      let sequential =
        L.CodeAction.create
          ~title: (Format.asprintf "create new tree (sequential address)")
          ~kind: (L.CodeActionKind.Other "new tree")
          ~edit: (create_tree_edit ~range ~uri next_sequential dir)
          ()
      in
      let random =
        L.CodeAction.create
          ~title: (Format.asprintf "create new tree (random address)")
          ~kind: (L.CodeActionKind.Other "new tree")
          ~edit: (create_tree_edit ~range ~uri next_random dir)
          ()
      in
      [`CodeAction sequential; `CodeAction random]
  in
  Some actions
OCaml

Innovation. Community. Security.