Skip to content

Commit 59c9138

Browse files
authored
Merge pull request #256 from rgrinberg/destruct-own-module
Move destruct to own module
2 parents 2d1e7c0 + b0673ec commit 59c9138

File tree

3 files changed

+52
-40
lines changed

3 files changed

+52
-40
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
open Import
2+
3+
let action_kind = "destruct"
4+
5+
let code_action_of_case_analysis uri (loc, newText) =
6+
let edit : WorkspaceEdit.t =
7+
let textedit : TextEdit.t = { range = Range.of_loc loc; newText } in
8+
let uri = Uri.to_string uri in
9+
WorkspaceEdit.create ~changes:[ (uri, [ textedit ]) ] ()
10+
in
11+
let title = String.capitalize_ascii action_kind in
12+
CodeAction.create ~title ~kind:(CodeActionKind.Other action_kind) ~edit
13+
~isPreferred:false ()
14+
15+
let code_action doc (params : CodeActionParams.t) =
16+
let uri = Uri.t_of_yojson (`String params.textDocument.uri) in
17+
match Document.kind doc with
18+
| Intf -> Fiber.return (Ok None)
19+
| Impl -> (
20+
let command =
21+
let start = Position.logical params.range.start in
22+
let finish = Position.logical params.range.end_ in
23+
Query_protocol.Case_analysis (start, finish)
24+
in
25+
let open Fiber.O in
26+
let+ res = Document.dispatch doc command in
27+
match res with
28+
| Ok res -> Ok (Some (code_action_of_case_analysis uri res))
29+
| Error
30+
( Destruct.Wrong_parent _ | Query_commands.No_nodes
31+
| Destruct.Not_allowed _ | Destruct.Useless_refine
32+
| Destruct.Nothing_to_do ) ->
33+
Ok None
34+
| Error exn -> Error (Jsonrpc.Response.Error.of_exn exn) )
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
open Import
2+
3+
val action_kind : string
4+
5+
val code_action :
6+
Document.t
7+
-> CodeActionParams.t
8+
-> (CodeAction.t option, Jsonrpc.Response.Error.t) Fiber.Result.t

ocaml-lsp-server/src/ocaml_lsp_server.ml

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,10 @@ let not_supported () =
1212
@@ Error
1313
(make_error ~code:InternalError ~message:"Request not supported yet!" ())
1414

15-
module Action = struct
16-
let destruct = "destruct"
17-
end
18-
1915
let initialize_info : InitializeResult.t =
2016
let codeActionProvider =
21-
`CodeActionOptions
22-
(CodeActionOptions.create ~codeActionKinds:[ Other Action.destruct ] ())
17+
let codeActionKinds = [ CodeActionKind.Other Destruct_lsp.action_kind ] in
18+
`CodeActionOptions (CodeActionOptions.create ~codeActionKinds ())
2319
in
2420
let textDocumentSync =
2521
`TextDocumentSyncOptions
@@ -157,49 +153,23 @@ let on_initialize rpc =
157153
Logger.register_consumer log_consumer;
158154
initialize_info
159155

160-
let code_action_of_case_analysis uri (loc, newText) =
161-
let edit : WorkspaceEdit.t =
162-
let textedit : TextEdit.t = { range = Range.of_loc loc; newText } in
163-
let uri = Uri.to_string uri in
164-
WorkspaceEdit.create ~changes:[ (uri, [ textedit ]) ] ()
165-
in
166-
let title = String.capitalize_ascii Action.destruct in
167-
CodeAction.create ~title ~kind:(CodeActionKind.Other Action.destruct) ~edit
168-
~isPreferred:false ()
169-
170156
let code_action server (params : CodeActionParams.t) =
171157
let state : State.t = Server.state server in
172158
let store = state.store in
173159
match params.context.only with
174-
| Some set when not (List.mem (CodeActionKind.Other Action.destruct) ~set) ->
160+
| Some set
161+
when not (List.mem (CodeActionKind.Other Destruct_lsp.action_kind) ~set) ->
175162
Fiber.return (Ok (None, state))
176163
| Some _
177-
| None -> (
164+
| None ->
178165
let open Fiber.Result.O in
179166
let uri = Uri.t_of_yojson (`String params.textDocument.uri) in
180167
let* doc = Fiber.return (Document_store.get store uri) in
181-
match Document.kind doc with
182-
| Intf -> Fiber.return (Ok (None, state))
183-
| Impl ->
184-
let command =
185-
let start = Position.logical params.range.start in
186-
let finish = Position.logical params.range.end_ in
187-
Query_protocol.Case_analysis (start, finish)
188-
in
189-
let+ result =
190-
let open Fiber.O in
191-
let+ res = Document.dispatch doc command in
192-
match res with
193-
| Ok res ->
194-
Ok (Some [ `CodeAction (code_action_of_case_analysis uri res) ])
195-
| Error
196-
( Destruct.Wrong_parent _ | Query_commands.No_nodes
197-
| Destruct.Not_allowed _ | Destruct.Useless_refine
198-
| Destruct.Nothing_to_do ) ->
199-
Ok (Some [])
200-
| Error exn -> raise exn
201-
in
202-
(result, state) )
168+
let+ action = Destruct_lsp.code_action doc params in
169+
let action =
170+
Option.map action ~f:(fun destruct -> [ `CodeAction destruct ])
171+
in
172+
(action, state)
203173

204174
module Formatter = struct
205175
let jsonrpc_error (e : Fmt.error) =

0 commit comments

Comments
 (0)