GenLSP behaviour (gen_lsp v0.0.9)
GenLSP is an OTP behaviour for building processes that implement the Language Server Protocol.
example
Example
Here is an example of a Credo language server.
defmodule Credo.Lsp do
@moduledoc """
LSP implementation for Credo.
"""
use GenLSP
alias GenLSP.Enumerations.TextDocumentSyncKind
alias GenLSP.Notifications.{
Exit,
Initialized,
TextDocumentDidChange,
TextDocumentDidClose,
TextDocumentDidOpen,
TextDocumentDidSave
}
alias GenLSP.Requests.{Initialize, Shutdown}
alias GenLSP.Structures.{
InitializeParams,
InitializeResult,
SaveOptions,
ServerCapabilities,
TextDocumentSyncOptions
}
alias Credo.Lsp.Cache, as: Diagnostics
def start_link(args) do
GenLSP.start_link(__MODULE__, args, [])
end
@impl true
def init(lsp, args) do
cache = Keyword.fetch!(args, :cache)
{:ok, assign(lsp, exit_code: 1, cache: cache)}
end
@impl true
def handle_request(%Initialize{params: %InitializeParams{root_uri: root_uri}}, lsp) do
{:reply,
%InitializeResult{
capabilities: %ServerCapabilities{
text_document_sync: %TextDocumentSyncOptions{
open_close: true,
save: %SaveOptions{include_text: true},
change: TextDocumentSyncKind.full()
}
},
server_info: %{name: "Credo"}
}, assign(lsp, root_uri: root_uri)}
end
def handle_request(%Shutdown{}, lsp) do
{:noreply, assign(lsp, exit_code: 0)}
end
@impl true
def handle_notification(%Initialized{}, lsp) do
GenLSP.log(lsp, :log, "[Credo] LSP Initialized!")
Diagnostics.refresh(lsp.assigns.cache, lsp)
Diagnostics.publish(lsp.assigns.cache, lsp)
{:noreply, lsp}
end
def handle_notification(%TextDocumentDidSave{}, lsp) do
Task.start_link(fn ->
Diagnostics.clear(lsp.assigns.cache)
Diagnostics.refresh(lsp.assigns.cache, lsp)
Diagnostics.publish(lsp.assigns.cache, lsp)
end)
{:noreply, lsp}
end
def handle_notification(%TextDocumentDidChange{}, lsp) do
Task.start_link(fn ->
Diagnostics.clear(lsp.assigns.cache)
Diagnostics.publish(lsp.assigns.cache, lsp)
end)
{:noreply, lsp}
end
def handle_notification(%note{}, lsp)
when note in [TextDocumentDidOpen, TextDocumentDidClose] do
{:noreply, lsp}
end
def handle_notification(%Exit{}, lsp) do
System.halt(lsp.assigns.exit_code)
{:noreply, lsp}
end
def handle_notification(_thing, lsp) do
{:noreply, lsp}
end
end
defmodule Credo.Lsp.Cache do
@moduledoc """
Cache for Credo diagnostics.
"""
use Agent
alias GenLSP.Structures.{
Diagnostic,
Position,
PublishDiagnosticsParams,
Range
}
alias GenLSP.Notifications.TextDocumentPublishDiagnostics
def start_link(_) do
Agent.start_link(fn -> Map.new() end)
end
def refresh(cache, lsp) do
dir = URI.new!(lsp.assigns.root_uri).path
issues = Credo.Execution.get_issues(Credo.run(["--strict", "--all", "#{dir}/**/*.ex"]))
GenLSP.log(lsp, :info, "[Credo] Found #{Enum.count(issues)} issues")
for issue <- issues do
diagnostic = %Diagnostic{
range: %Range{
start: %Position{line: issue.line_no - 1, character: issue.column || 0},
end: %Position{line: issue.line_no, character: 0}
},
severity: category_to_severity(issue.category),
message: """
#{issue.message}
## Explanation
#{issue.check.explanations()[:check]}
"""
}
put(cache, Path.absname(issue.filename), diagnostic)
end
end
def get(cache) do
Agent.get(cache, & &1)
end
def put(cache, filename, diagnostic) do
Agent.update(cache, fn cache ->
Map.update(cache, Path.absname(filename), [diagnostic], fn v ->
[diagnostic | v]
end)
end)
end
def clear(cache) do
Agent.update(cache, fn cache ->
for {k, _} <- cache, into: Map.new() do
{k, []}
end
end)
end
def publish(cache, lsp) do
for {file, diagnostics} <- get(cache) do
GenLSP.notify(lsp, %TextDocumentPublishDiagnostics{
params: %PublishDiagnosticsParams{
uri: "file://#{file}",
diagnostics: diagnostics
}
})
end
end
def category_to_severity(:refactor), do: 1
def category_to_severity(:warning), do: 2
def category_to_severity(:design), do: 3
def category_to_severity(:consistency), do: 4
def category_to_severity(:readability), do: 4
end
Link to this section Summary
Callbacks
The callback responsible for handling normal messages.
The callback responsible for handling notifications from the client.
The callback responsible for handling requests from the client.
The callback responsible for initializing the process.
Link to this section Callbacks
handle_info(message, state)
@callback handle_info(message :: any(), state) :: {:noreply, state} when state: GenLSP.LSP.t()
The callback responsible for handling normal messages.
Receives the notification struct as the first argument and the LSP token GenLSP.LSP.t/0
as the second.
usage
Usage
@impl true
def handle_info(message, lsp) do
# handle the message
{:noreply, lsp}
end
handle_notification(notification, state)
@callback handle_notification(notification :: term(), state) :: {:noreply, state} when state: GenLSP.LSP.t()
The callback responsible for handling notifications from the client.
Receives the notification struct as the first argument and the LSP token GenLSP.LSP.t/0
as the second.
usage
Usage
@impl true
def handle_notification(%Initialized{}, lsp) do
# handle the notification
{:noreply, lsp}
end
handle_request(request, state)
@callback handle_request(request :: term(), state) :: {:reply, reply :: term(), state} | {:noreply, state} when state: GenLSP.LSP.t()
The callback responsible for handling requests from the client.
Receives the request struct as the first argument and the LSP token GenLSP.LSP.t/0
as the second.
usage
Usage
@impl true
def handle_request(%Initialize{params: %InitializeParams{root_uri: root_uri}}, lsp) do
{:reply,
%InitializeResult{
capabilities: %ServerCapabilities{
text_document_sync: %TextDocumentSyncOptions{
open_close: true,
save: %SaveOptions{include_text: true},
change: TextDocumentSyncKind.full()
}
},
server_info: %{name: "MyLSP"}
}, assign(lsp, root_uri: root_uri)}
end
init(lsp, init_arg)
@callback init(lsp :: GenLSP.LSP.t(), init_arg :: term()) :: {:ok, GenLSP.LSP.t()}
The callback responsible for initializing the process.
Receives the GenLSP.LSP.t/0
token as the first argument and the arguments that were passed to GenLSP.start_link/3
as the second.
usage
Usage
@impl true
def init(lsp, args) do
some_arg = Keyword.fetch!(args, :some_arg)
{:ok, assign(lsp, static_assign: :some_assign, some_arg: some_arg)}
end
Link to this section Functions
log(lsp, level, message)
@spec log(GenLSP.LSP.t(), :error | :warning | :info | :log, String.t()) :: :ok
Send a window/logMessage
notification to the client.
usage
Usage
GenLSP.log(lsp, :warning, "Failed to compiled!")
notify(map, notification)
@spec notify(GenLSP.LSP.t(), notification :: any()) :: :ok
Sends a notification to the client from the LSP process.
usage
Usage
GenLSP.notify(lsp, %TextDocumentPublishDiagnostics{
params: %PublishDiagnosticsParams{
uri: "file://#{file}",
diagnostics: diagnostics
}
})
notify_server(pid, notification)
Sends a notification from the client to the LSP process.
Generally used by the GenLSP.Communication.Adapter
implementation to forward messages from the buffer to the LSP process.
You shouldn't need to use this to implement a language server.
request_server(pid, request)
Sends a request from the client to the LSP process.
Generally used by the GenLSP.Communication.Adapter
implementation to forward messages from the buffer to the LSP process.
You shouldn't need to use this to implement a language server.
start_link(module, init_args, opts)
Starts a GenLSP
process that is linked to the current process.
options
Options
:buffer
- Required. Thepid/0
or name of theGenLSP.Buffer
process.:name
(atom/0
) - Used for name registration as described in the "Name registration" section in the documentation forGenServer
.