|
| 1 | +open Grain; |
| 2 | +open Compile; |
| 3 | +open Grain_parsing; |
| 4 | +open Grain_utils; |
| 5 | +open Grain_typed; |
| 6 | +open Grain_diagnostics; |
| 7 | +open Sourcetree; |
| 8 | +open Lsp_types; |
| 9 | + |
| 10 | +// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#definitionParams |
| 11 | +module RequestParams = { |
| 12 | + [@deriving yojson({strict: false})] |
| 13 | + type code_action_context = {diagnostics: list(Protocol.diagnostic)}; |
| 14 | + |
| 15 | + [@deriving yojson({strict: false})] |
| 16 | + type t = { |
| 17 | + [@key "textDocument"] |
| 18 | + text_document: Protocol.text_document_identifier, |
| 19 | + range: Protocol.range, |
| 20 | + context: code_action_context, |
| 21 | + }; |
| 22 | +}; |
| 23 | + |
| 24 | +// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#locationLink |
| 25 | +module ResponseResult = { |
| 26 | + [@deriving yojson] |
| 27 | + type code_action = { |
| 28 | + title: string, |
| 29 | + kind: string, |
| 30 | + edit: Protocol.workspace_edit, |
| 31 | + }; |
| 32 | + |
| 33 | + [@deriving yojson] |
| 34 | + type t = option(list(code_action)); |
| 35 | +}; |
| 36 | + |
| 37 | +let send_no_result = (~id: Protocol.message_id) => { |
| 38 | + Protocol.response(~id, `Null); |
| 39 | +}; |
| 40 | + |
| 41 | +let explicit_type_annotation = (range, uri, type_str) => { |
| 42 | + ResponseResult.{ |
| 43 | + title: "Annotate type", |
| 44 | + kind: "annotate-type", |
| 45 | + edit: { |
| 46 | + document_changes: [ |
| 47 | + { |
| 48 | + text_document: { |
| 49 | + uri, |
| 50 | + version: None, |
| 51 | + }, |
| 52 | + edits: [{range, new_text: ": " ++ type_str}], |
| 53 | + }, |
| 54 | + ], |
| 55 | + }, |
| 56 | + }; |
| 57 | +}; |
| 58 | + |
| 59 | +let send_code_actions = |
| 60 | + (id: Protocol.message_id, code_actions: list(ResponseResult.code_action)) => { |
| 61 | + Protocol.response(~id, ResponseResult.to_yojson(Some(code_actions))); |
| 62 | +}; |
| 63 | + |
| 64 | +let process_explicit_type_annotation = (uri, results: list(Sourcetree.node)) => { |
| 65 | + switch (results) { |
| 66 | + | [Pattern({pattern})] |
| 67 | + | [ |
| 68 | + Pattern({pattern: {pat_desc: TPatAlias({pat_desc: TPatAny}, _, _)}}), |
| 69 | + Pattern({pattern}), |
| 70 | + ..._, |
| 71 | + ] |
| 72 | + when pattern.pat_extra == [] => |
| 73 | + let loc = {...pattern.pat_loc, loc_start: pattern.pat_loc.loc_end}; |
| 74 | + let type_str = Printtyp.string_of_type_scheme(pattern.pat_type); |
| 75 | + Some(explicit_type_annotation(Utils.loc_to_range(loc), uri, type_str)); |
| 76 | + | _ => None |
| 77 | + }; |
| 78 | +}; |
| 79 | + |
| 80 | +let process = |
| 81 | + ( |
| 82 | + ~id: Protocol.message_id, |
| 83 | + ~compiled_code: Hashtbl.t(Protocol.uri, Lsp_types.code), |
| 84 | + ~documents: Hashtbl.t(Protocol.uri, string), |
| 85 | + params: RequestParams.t, |
| 86 | + ) => { |
| 87 | + switch (Hashtbl.find_opt(compiled_code, params.text_document.uri)) { |
| 88 | + | None => send_no_result(~id) |
| 89 | + | Some({program, sourcetree}) => |
| 90 | + let results = Sourcetree.query(params.range.range_start, sourcetree); |
| 91 | + |
| 92 | + let code_actions = |
| 93 | + List.filter_map( |
| 94 | + x => x, |
| 95 | + [ |
| 96 | + process_explicit_type_annotation(params.text_document.uri, results), |
| 97 | + ], |
| 98 | + ); |
| 99 | + |
| 100 | + switch (code_actions) { |
| 101 | + | [] => send_no_result(~id) |
| 102 | + | _ => send_code_actions(id, code_actions) |
| 103 | + }; |
| 104 | + }; |
| 105 | +}; |
0 commit comments