Skip to content

package protocol

import "github.com/cloudboss/unobin/pkg/lsp/protocol"

Constants

const (
    ErrorCodeParseError     = -32700
    ErrorCodeInvalidRequest = -32600
    ErrorCodeMethodNotFound = -32601
    ErrorCodeInvalidParams  = -32602
    ErrorCodeInternalError  = -32603
    ErrorCodeRequestCancel  = -32800
)

Functions

func ReadMessage

func ReadMessage(r io.Reader) ([]byte, error)

ReadMessage reads one Content-Length-framed JSON-RPC message body.

func WriteMessage

func WriteMessage(w io.Writer, body []byte) error

WriteMessage writes one Content-Length-framed JSON-RPC message body.

Types

type ClientCapabilities

type ClientCapabilities struct{}

ClientCapabilities is present for initialize compatibility.

type CompletionItem

type CompletionItem struct {
    Label      string             `json:"label"`
    Kind       CompletionItemKind `json:"kind,omitempty"`
    FilterText string             `json:"filterText,omitempty"`
    TextEdit   *TextEdit          `json:"textEdit,omitempty"`
}

CompletionItem is one completion candidate.

type CompletionItemKind

type CompletionItemKind int

CompletionItemKind is an LSP completion item kind.

type CompletionList

type CompletionList struct {
    IsIncomplete bool             `json:"isIncomplete"`
    Items        []CompletionItem `json:"items"`
}

CompletionList is an LSP completion response.

type CompletionOptions

type CompletionOptions struct {
    TriggerCharacters []string `json:"triggerCharacters,omitempty"`
}

CompletionOptions configures completion requests.

type CompletionParams

type CompletionParams struct {
    TextDocument TextDocumentIdentifier `json:"textDocument"`
    Position     Position               `json:"position"`
}

CompletionParams is a completion request.

type DefinitionParams

type DefinitionParams struct {
    TextDocument TextDocumentIdentifier `json:"textDocument"`
    Position     Position               `json:"position"`
}

DefinitionParams is a go-to-definition request.

type Diagnostic

type Diagnostic struct {
    Range    Range              `json:"range"`
    Severity DiagnosticSeverity `json:"severity,omitempty"`
    Source   string             `json:"source,omitempty"`
    Message  string             `json:"message"`
}

Diagnostic is an LSP diagnostic.

type DiagnosticSeverity

type DiagnosticSeverity int

DiagnosticSeverity is an LSP diagnostic severity.

type DidChangeTextDocumentParams

type DidChangeTextDocumentParams struct {
    TextDocument   VersionedTextDocumentIdentifier  `json:"textDocument"`
    ContentChanges []TextDocumentContentChangeEvent `json:"contentChanges"`
}

DidChangeTextDocumentParams is sent when a text document changes.

type DidChangeWatchedFilesParams

type DidChangeWatchedFilesParams struct {
    Changes []FileEvent `json:"changes"`
}

DidChangeWatchedFilesParams is sent when watched files change.

type DidCloseTextDocumentParams

type DidCloseTextDocumentParams struct {
    TextDocument TextDocumentIdentifier `json:"textDocument"`
}

DidCloseTextDocumentParams is sent when a text document closes.

type DidOpenTextDocumentParams

type DidOpenTextDocumentParams struct {
    TextDocument TextDocumentItem `json:"textDocument"`
}

DidOpenTextDocumentParams is sent when a text document opens.

type DidSaveTextDocumentParams

type DidSaveTextDocumentParams struct {
    TextDocument TextDocumentIdentifier `json:"textDocument"`
    Text         string                 `json:"text,omitempty"`
}

DidSaveTextDocumentParams is sent when a text document is saved.

type DocumentFormattingParams

type DocumentFormattingParams struct {
    TextDocument TextDocumentIdentifier `json:"textDocument"`
    Options      FormattingOptions      `json:"options"`
}

DocumentFormattingParams is a whole-document formatting request.

type DocumentSymbol

type DocumentSymbol struct {
    Name           string           `json:"name"`
    Kind           SymbolKind       `json:"kind"`
    Range          Range            `json:"range"`
    SelectionRange Range            `json:"selectionRange"`
    Children       []DocumentSymbol `json:"children,omitempty"`
}

DocumentSymbol is a document-local symbol.

type DocumentSymbolParams

type DocumentSymbolParams struct {
    TextDocument TextDocumentIdentifier `json:"textDocument"`
}

DocumentSymbolParams is a document symbol request.

type FileChangeType

type FileChangeType int

FileChangeType is an LSP watched-file change kind.

type FileEvent

type FileEvent struct {
    URI  string         `json:"uri"`
    Type FileChangeType `json:"type"`
}

FileEvent is one watched-file change.

type FormattingOptions

type FormattingOptions struct {
    TabSize      uint32 `json:"tabSize"`
    InsertSpaces bool   `json:"insertSpaces"`
}

FormattingOptions is present for LSP formatting compatibility.

type Handler

type Handler interface {
    HandleRequest(context.Context, *RequestMessage) (any, *ResponseError)
}

Handler serves JSON-RPC requests.

type HandlerFunc

type HandlerFunc func(context.Context, *RequestMessage) (any, *ResponseError)

HandlerFunc adapts a function to Handler.

func (HandlerFunc) HandleRequest

func (f HandlerFunc) HandleRequest(ctx context.Context, req *RequestMessage) (any, *ResponseError)

HandleRequest calls f.

type Hover

type Hover struct {
    Contents MarkupContent `json:"contents"`
    Range    *Range        `json:"range,omitempty"`
}

Hover is an LSP hover response.

type HoverParams

type HoverParams struct {
    TextDocument TextDocumentIdentifier `json:"textDocument"`
    Position     Position               `json:"position"`
}

HoverParams is a hover request.

type ID

type ID struct {
    // contains filtered or unexported fields
}

ID is a JSON-RPC request id.

func NewNumberID

func NewNumberID(n int64) *ID

NewNumberID returns a numeric JSON-RPC request id.

func NewStringID

func NewStringID(s string) *ID

NewStringID returns a string JSON-RPC request id.

func (ID) MarshalJSON

func (id ID) MarshalJSON() ([]byte, error)

func (ID) Number

func (id ID) Number() (json.Number, bool)

Number returns the id as a number when it was encoded as a number.

func (ID) String

func (id ID) String() (string, bool)

String returns the id as a string when it was encoded as a string.

func (*ID) UnmarshalJSON

func (id *ID) UnmarshalJSON(data []byte) error

type InitializeParams

type InitializeParams struct {
    ProcessID        *int32             `json:"processId,omitempty"`
    RootURI          string             `json:"rootUri,omitempty"`
    Capabilities     ClientCapabilities `json:"capabilities"`
    WorkspaceFolders []WorkspaceFolder  `json:"workspaceFolders,omitempty"`
}

InitializeParams is the subset of LSP initialize params used by Unobin.

type InitializeResult

type InitializeResult struct {
    Capabilities ServerCapabilities `json:"capabilities"`
    ServerInfo   *ServerInfo        `json:"serverInfo,omitempty"`
}

InitializeResult is the LSP initialize response body.

type Location

type Location struct {
    URI   string `json:"uri"`
    Range Range  `json:"range"`
}

Location is an LSP source location.

type MarkupContent

type MarkupContent struct {
    Kind  MarkupKind `json:"kind"`
    Value string     `json:"value"`
}

MarkupContent is hover text with a markup kind.

type MarkupKind

type MarkupKind string

MarkupKind is an LSP markup content kind.

type Position

type Position struct {
    Line      uint32 `json:"line"`
    Character uint32 `json:"character"`
}

Position is a zero-based LSP UTF-16 text position.

type PublishDiagnosticsParams

type PublishDiagnosticsParams struct {
    URI         string       `json:"uri"`
    Version     *int32       `json:"version,omitempty"`
    Diagnostics []Diagnostic `json:"diagnostics"`
}

PublishDiagnosticsParams sends diagnostics for one document.

type Range

type Range struct {
    Start Position `json:"start"`
    End   Position `json:"end"`
}

Range is an LSP text range.

type RequestMessage

type RequestMessage struct {
    JSONRPC string          `json:"jsonrpc"`
    ID      *ID             `json:"id,omitempty"`
    Method  string          `json:"method"`
    Params  json.RawMessage `json:"params,omitempty"`
}

RequestMessage is a JSON-RPC request or notification.

type ResponseError

type ResponseError struct {
    Code    int    `json:"code"`
    Message string `json:"message"`
    Data    any    `json:"data,omitempty"`
}

ResponseError is a JSON-RPC response error object.

func InternalError

func InternalError(err error) *ResponseError

InternalError returns the standard JSON-RPC internal-error object.

func InvalidParams

func InvalidParams(message string) *ResponseError

InvalidParams returns the standard JSON-RPC invalid-params error.

func MethodNotFound

func MethodNotFound(method string) *ResponseError

MethodNotFound returns the standard JSON-RPC method-not-found error.

type ResponseMessage

type ResponseMessage struct {
    JSONRPC string
    ID      *ID
    Result  any
    Error   *ResponseError
}

ResponseMessage is a JSON-RPC response.

func (ResponseMessage) MarshalJSON

func (r ResponseMessage) MarshalJSON() ([]byte, error)

type Sender

type Sender func(method string, params any) error

Sender writes a server-to-client notification.

type SenderSetter

type SenderSetter interface {
    SetSender(Sender)
}

SenderSetter accepts a server-to-client notification sender.

type Server

type Server struct {
    // contains filtered or unexported fields
}

Server serves JSON-RPC messages over LSP stdio framing.

func NewServer

func NewServer(r io.Reader, w io.Writer, handler Handler) *Server

NewServer returns a JSON-RPC server using LSP stdio framing.

func NewServerWithOptions

func NewServerWithOptions(
    r io.Reader,
    w io.Writer,
    handler Handler,
    options ServerOptions,
) *Server

NewServerWithOptions returns a JSON-RPC server with tracing and logging.

func (*Server) Notify

func (s *Server) Notify(method string, params any) error

Notify writes a server-to-client JSON-RPC notification.

func (*Server) Serve

func (s *Server) Serve(ctx context.Context) error

Serve reads requests until the input stream ends.

type ServerCapabilities

type ServerCapabilities struct {
    TextDocumentSync           TextDocumentSyncKind `json:"textDocumentSync,omitempty"`
    DocumentFormattingProvider bool                 `json:"documentFormattingProvider,omitempty"`
    DefinitionProvider         bool                 `json:"definitionProvider,omitempty"`
    DocumentSymbolProvider     bool                 `json:"documentSymbolProvider,omitempty"`
    CompletionProvider         *CompletionOptions   `json:"completionProvider,omitempty"`
    HoverProvider              bool                 `json:"hoverProvider,omitempty"`
}

ServerCapabilities advertises implemented LSP features.

type ServerInfo

type ServerInfo struct {
    Name    string `json:"name"`
    Version string `json:"version,omitempty"`
}

ServerInfo names the LSP server.

type ServerOptions

type ServerOptions struct {
    Trace io.Writer
    Log   io.Writer
}

ServerOptions configures JSON-RPC tracing and logging.

type Stopper

type Stopper interface {
    StopRequested() bool
}

Stopper reports whether the server should stop reading messages.

type SymbolKind

type SymbolKind int

SymbolKind is an LSP symbol kind.

type TextDocumentContentChangeEvent

type TextDocumentContentChangeEvent struct {
    Range       *Range `json:"range,omitempty"`
    RangeLength *int   `json:"rangeLength,omitempty"`
    Text        string `json:"text"`
}

TextDocumentContentChangeEvent is a full-text or incremental change.

type TextDocumentIdentifier

type TextDocumentIdentifier struct {
    URI string `json:"uri"`
}

TextDocumentIdentifier identifies a text document by URI.

type TextDocumentItem

type TextDocumentItem struct {
    URI        string `json:"uri"`
    LanguageID string `json:"languageId"`
    Version    int32  `json:"version"`
    Text       string `json:"text"`
}

TextDocumentItem is an open text document.

type TextDocumentSyncKind

type TextDocumentSyncKind int

TextDocumentSyncKind is an LSP text sync mode.

type TextEdit

type TextEdit struct {
    Range   Range  `json:"range"`
    NewText string `json:"newText"`
}

TextEdit replaces a range with new text.

type VersionedTextDocumentIdentifier

type VersionedTextDocumentIdentifier struct {
    URI     string `json:"uri"`
    Version int32  `json:"version"`
}

VersionedTextDocumentIdentifier identifies a versioned text document.

type WorkspaceFolder

type WorkspaceFolder struct {
    URI  string `json:"uri"`
    Name string `json:"name"`
}

WorkspaceFolder is an LSP workspace folder.