|
1 | 1 | defmodule NextLS do
|
2 |
| - @moduledoc """ |
3 |
| - Documentation for `NextLS`. |
4 |
| - """ |
| 2 | + @moduledoc false |
| 3 | + use GenLSP |
5 | 4 |
|
6 |
| - @doc """ |
7 |
| - Hello world. |
| 5 | + alias GenLSP.ErrorResponse |
8 | 6 |
|
9 |
| - ## Examples |
| 7 | + alias GenLSP.Enumerations.{ |
| 8 | + ErrorCodes, |
| 9 | + TextDocumentSyncKind |
| 10 | + } |
10 | 11 |
|
11 |
| - iex> NextLS.hello() |
12 |
| - :world |
| 12 | + alias GenLSP.Notifications.{ |
| 13 | + Exit, |
| 14 | + Initialized, |
| 15 | + TextDocumentDidChange, |
| 16 | + TextDocumentDidOpen, |
| 17 | + TextDocumentDidSave |
| 18 | + } |
13 | 19 |
|
14 |
| - """ |
15 |
| - def hello do |
16 |
| - :world |
| 20 | + alias GenLSP.Requests.{Initialize, Shutdown} |
| 21 | + |
| 22 | + alias GenLSP.Structures.{ |
| 23 | + DidOpenTextDocumentParams, |
| 24 | + InitializeParams, |
| 25 | + InitializeResult, |
| 26 | + SaveOptions, |
| 27 | + ServerCapabilities, |
| 28 | + TextDocumentItem, |
| 29 | + TextDocumentSyncOptions |
| 30 | + } |
| 31 | + |
| 32 | + def start_link(args) do |
| 33 | + {args, opts} = Keyword.split(args, [:task_supervisor, :runtime_supervisor]) |
| 34 | + |
| 35 | + GenLSP.start_link(__MODULE__, args, opts) |
| 36 | + end |
| 37 | + |
| 38 | + @impl true |
| 39 | + def init(lsp, args) do |
| 40 | + task_supervisor = Keyword.fetch!(args, :task_supervisor) |
| 41 | + runtime_supervisor = Keyword.fetch!(args, :runtime_supervisor) |
| 42 | + |
| 43 | + {:ok, |
| 44 | + assign(lsp, |
| 45 | + exit_code: 1, |
| 46 | + documents: %{}, |
| 47 | + refresh_refs: %{}, |
| 48 | + task_supervisor: task_supervisor, |
| 49 | + runtime_supervisor: runtime_supervisor, |
| 50 | + runtime_task: nil, |
| 51 | + ready: false |
| 52 | + )} |
| 53 | + end |
| 54 | + |
| 55 | + @impl true |
| 56 | + def handle_request( |
| 57 | + %Initialize{params: %InitializeParams{root_uri: root_uri}}, |
| 58 | + lsp |
| 59 | + ) do |
| 60 | + {:reply, |
| 61 | + %InitializeResult{ |
| 62 | + capabilities: %ServerCapabilities{ |
| 63 | + text_document_sync: %TextDocumentSyncOptions{ |
| 64 | + open_close: true, |
| 65 | + save: %SaveOptions{include_text: true}, |
| 66 | + change: TextDocumentSyncKind.full() |
| 67 | + } |
| 68 | + }, |
| 69 | + server_info: %{name: "NextLS"} |
| 70 | + }, assign(lsp, root_uri: root_uri)} |
| 71 | + end |
| 72 | + |
| 73 | + def handle_request(%Shutdown{}, lsp) do |
| 74 | + {:reply, nil, assign(lsp, exit_code: 0)} |
| 75 | + end |
| 76 | + |
| 77 | + def handle_request(%{method: method}, lsp) do |
| 78 | + GenLSP.warning(lsp, "[NextLS] Method Not Found: #{method}") |
| 79 | + |
| 80 | + {:reply, |
| 81 | + %ErrorResponse{ |
| 82 | + code: ErrorCodes.method_not_found(), |
| 83 | + message: "Method Not Found: #{method}" |
| 84 | + }, lsp} |
| 85 | + end |
| 86 | + |
| 87 | + @impl true |
| 88 | + def handle_notification(%Initialized{}, lsp) do |
| 89 | + GenLSP.log(lsp, "[NextLS] LSP Initialized!") |
| 90 | + |
| 91 | + working_dir = URI.parse(lsp.assigns.root_uri).path |
| 92 | + |
| 93 | + GenLSP.log(lsp, "[NextLS] Booting runime...") |
| 94 | + |
| 95 | + {:ok, runtime} = |
| 96 | + DynamicSupervisor.start_child( |
| 97 | + lsp.assigns.runtime_supervisor, |
| 98 | + {NextLS.Runtime, working_dir: working_dir, parent: self()} |
| 99 | + ) |
| 100 | + |
| 101 | + Process.monitor(runtime) |
| 102 | + |
| 103 | + lsp = assign(lsp, runtime: runtime) |
| 104 | + |
| 105 | + task = |
| 106 | + Task.Supervisor.async_nolink(lsp.assigns.task_supervisor, fn -> |
| 107 | + with false <- |
| 108 | + wait_until(fn -> |
| 109 | + NextLS.Runtime.ready?(runtime) |
| 110 | + end) do |
| 111 | + GenLSP.error(lsp, "Failed to start runtime") |
| 112 | + raise "Failed to boot runtime" |
| 113 | + end |
| 114 | + |
| 115 | + GenLSP.log(lsp, "[NextLS] Runtime ready...") |
| 116 | + |
| 117 | + :ready |
| 118 | + end) |
| 119 | + |
| 120 | + {:noreply, assign(lsp, runtime_task: task)} |
| 121 | + end |
| 122 | + |
| 123 | + def handle_notification(%TextDocumentDidSave{}, %{assigns: %{ready: false}} = lsp) do |
| 124 | + {:noreply, lsp} |
| 125 | + end |
| 126 | + |
| 127 | + def handle_notification( |
| 128 | + %TextDocumentDidSave{ |
| 129 | + params: %GenLSP.Structures.DidSaveTextDocumentParams{ |
| 130 | + text: text, |
| 131 | + text_document: %{uri: uri} |
| 132 | + } |
| 133 | + }, |
| 134 | + %{assigns: %{ready: true}} = lsp |
| 135 | + ) do |
| 136 | + {:noreply, lsp |> then(&put_in(&1.assigns.documents[uri], String.split(text, "\n")))} |
| 137 | + end |
| 138 | + |
| 139 | + def handle_notification(%TextDocumentDidChange{}, %{assigns: %{ready: false}} = lsp) do |
| 140 | + {:noreply, lsp} |
| 141 | + end |
| 142 | + |
| 143 | + def handle_notification(%TextDocumentDidChange{}, lsp) do |
| 144 | + for task <- Task.Supervisor.children(lsp.assigns.task_supervisor), |
| 145 | + task != lsp.assigns.runtime_task do |
| 146 | + Process.exit(task, :kill) |
| 147 | + end |
| 148 | + |
| 149 | + {:noreply, lsp} |
| 150 | + end |
| 151 | + |
| 152 | + def handle_notification( |
| 153 | + %TextDocumentDidOpen{ |
| 154 | + params: %DidOpenTextDocumentParams{ |
| 155 | + text_document: %TextDocumentItem{text: text, uri: uri} |
| 156 | + } |
| 157 | + }, |
| 158 | + lsp |
| 159 | + ) do |
| 160 | + {:noreply, put_in(lsp.assigns.documents[uri], String.split(text, "\n"))} |
| 161 | + end |
| 162 | + |
| 163 | + def handle_notification(%Exit{}, lsp) do |
| 164 | + System.halt(lsp.assigns.exit_code) |
| 165 | + |
| 166 | + {:noreply, lsp} |
| 167 | + end |
| 168 | + |
| 169 | + def handle_notification(_notification, lsp) do |
| 170 | + {:noreply, lsp} |
| 171 | + end |
| 172 | + |
| 173 | + def handle_info({ref, resp}, %{assigns: %{refresh_refs: refs}} = lsp) |
| 174 | + when is_map_key(refs, ref) do |
| 175 | + Process.demonitor(ref, [:flush]) |
| 176 | + {_token, refs} = Map.pop(refs, ref) |
| 177 | + |
| 178 | + lsp = |
| 179 | + case resp do |
| 180 | + :ready -> |
| 181 | + assign(lsp, ready: true) |
| 182 | + |
| 183 | + _ -> |
| 184 | + lsp |
| 185 | + end |
| 186 | + |
| 187 | + {:noreply, assign(lsp, refresh_refs: refs)} |
| 188 | + end |
| 189 | + |
| 190 | + def handle_info( |
| 191 | + {:DOWN, ref, :process, _pid, _reason}, |
| 192 | + %{assigns: %{refresh_refs: refs}} = lsp |
| 193 | + ) |
| 194 | + when is_map_key(refs, ref) do |
| 195 | + {_token, refs} = Map.pop(refs, ref) |
| 196 | + |
| 197 | + {:noreply, assign(lsp, refresh_refs: refs)} |
| 198 | + end |
| 199 | + |
| 200 | + def handle_info( |
| 201 | + {:DOWN, _ref, :process, runtime, _reason}, |
| 202 | + %{assigns: %{runtime: runtime}} = lsp |
| 203 | + ) do |
| 204 | + GenLSP.error(lsp, "[NextLS] The runtime has crashed") |
| 205 | + |
| 206 | + {:noreply, assign(lsp, runtime: nil)} |
| 207 | + end |
| 208 | + |
| 209 | + def handle_info({:log, message}, lsp) do |
| 210 | + GenLSP.log(lsp, String.trim(message)) |
| 211 | + |
| 212 | + {:noreply, lsp} |
| 213 | + end |
| 214 | + |
| 215 | + def handle_info(_, lsp) do |
| 216 | + {:noreply, lsp} |
| 217 | + end |
| 218 | + |
| 219 | + defp wait_until(cb) do |
| 220 | + wait_until(120, cb) |
| 221 | + end |
| 222 | + |
| 223 | + defp wait_until(0, _cb) do |
| 224 | + false |
| 225 | + end |
| 226 | + |
| 227 | + defp wait_until(n, cb) do |
| 228 | + if cb.() do |
| 229 | + true |
| 230 | + else |
| 231 | + Process.sleep(1000) |
| 232 | + wait_until(n - 1, cb) |
| 233 | + end |
17 | 234 | end
|
18 | 235 | end
|
0 commit comments