lsp

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 27, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArrayBuffer

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

ArrayBuffer is the simplest implementation of Buffer, using a byte slice for storage. It is optimized for reads. Insertions and deletions are O(n) due to slice copying.

This implementation is not recommended and is mainly used as a testing benchmark baseline for smarter buffer implementations.

func (*ArrayBuffer) Bytes

func (a *ArrayBuffer) Bytes() []byte

Bytes implements Buffer.

func (*ArrayBuffer) Delete

func (a *ArrayBuffer) Delete(start, end int)

Delete implements Buffer.

func (*ArrayBuffer) Len

func (a *ArrayBuffer) Len() int

Len implements Buffer.

func (*ArrayBuffer) ReadAt

func (a *ArrayBuffer) ReadAt(p []byte, off int64) (n int, err error)

ReadAt implements Buffer.

func (*ArrayBuffer) Reset

func (a *ArrayBuffer) Reset(b []byte)

Reset implements Buffer.

func (*ArrayBuffer) WriteAt

func (a *ArrayBuffer) WriteAt(p []byte, off int64) (n int, err error)

WriteAt implements Buffer.

type Buffer

type Buffer interface {
	io.ReaderAt
	io.WriterAt
	// Reset reinitializes the buffer with the given text.
	Reset(b []byte)
	// Bytes returns the full content of the buffer as a byte slice.
	Bytes() []byte
	// Delete deletes a range of bytes from the buffer.
	Delete(start, end int)
	// Len returns the number of bytes in the buffer.
	Len() int
}

Buffer implements large text storage with methods for random access.

type BufferType

type BufferType int

BufferType represents a buffer implementation.

const (
	// BufferTypeDefault chooses a buffer for you.
	BufferTypeDefault BufferType = iota
	// BufferTypeGap uses a gap buffer for amortized O(1) insertions and
	// deletions and fast reads, at the cost of poorer random access.
	BufferTypeGap
	// BufferTypeRope uses a rope data structure for efficient random
	// access and insertions/deletions in large documents.
	BufferTypeRope
)

type CompletionItem

type CompletionItem struct {
	Label         string             `json:"label"`
	Kind          CompletionItemKind `json:"kind,omitempty"`
	Detail        string             `json:"detail,omitempty"`
	Documentation *MarkupContent     `json:"documentation,omitempty"`
}

https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#completionItem

type CompletionItemKind

type CompletionItemKind int

https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#completionItemKind

const (
	CompletionText          CompletionItemKind = 1
	CompletionMethod        CompletionItemKind = 2
	CompletionFunction      CompletionItemKind = 3
	CompletionConstructor   CompletionItemKind = 4
	CompletionField         CompletionItemKind = 5
	CompletionVariable      CompletionItemKind = 6
	CompletionClass         CompletionItemKind = 7
	CompletionInterface     CompletionItemKind = 8
	CompletionModule        CompletionItemKind = 9
	CompletionProperty      CompletionItemKind = 10
	CompletionUnit          CompletionItemKind = 11
	CompletionValue         CompletionItemKind = 12
	CompletionEnum          CompletionItemKind = 13
	CompletionKeyword       CompletionItemKind = 14
	CompletionSnippet       CompletionItemKind = 15
	CompletionColor         CompletionItemKind = 16
	CompletionFile          CompletionItemKind = 17
	CompletionReference     CompletionItemKind = 18
	CompletionFolder        CompletionItemKind = 19
	CompletionEnumMember    CompletionItemKind = 20
	CompletionConstant      CompletionItemKind = 21
	CompletionStruct        CompletionItemKind = 22
	CompletionEvent         CompletionItemKind = 23
	CompletionOperator      CompletionItemKind = 24
	CompletionTypeParameter CompletionItemKind = 25
)

https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#completionItemKind

type CompletionList

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

https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#completionList

type Document

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

Document represents a text document with methods to manipulate its content.

func NewDocument

func NewDocument(text []byte, buf Buffer) *Document

NewDocument creates a new Document with the given initial text and buffer. If buf is nil, a GapBuffer is used.

func (*Document) ApplyChange

func (d *Document) ApplyChange(change TextDocumentContentChangeEvent) error

ApplyChange applies a content change to the document.

func (*Document) Bytes

func (d *Document) Bytes() []byte

Bytes returns the full content of the document.

func (*Document) Len

func (d *Document) Len() int

Len returns the number of bytes in the document.

func (*Document) PositionToOffset

func (d *Document) PositionToOffset(pos Position) (int, error)

PositionToOffset converts a Position (line and character) to a byte offset in the document. It correctly handles UTF-16 character widths.

func (*Document) ReadAt

func (d *Document) ReadAt(p []byte, off int64) (n int, err error)

ReadAt implements io.ReaderAt.

func (*Document) Reset

func (d *Document) Reset(text []byte)

Reset reinitializes the document with the given text.

type DocumentSyncHandler

type DocumentSyncHandler interface {
	DidOpenTextDocument(ctx context.Context, params DidOpenTextDocumentParams) error
	DidCloseTextDocument(ctx context.Context, params DidCloseTextDocumentParams) error
	DidChangeTextDocument(ctx context.Context, params DidChangeTextDocumentParams) error
}

DocumentSyncHandler defines methods for handling document synchronization.

type ErrorCode

type ErrorCode int

https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#errorCodes

const (
	CodeParseError     ErrorCode = -32700
	CodeInvalidRequest ErrorCode = -32600
	CodeMethodNotFound ErrorCode = -32601
	CodeInvalidParams  ErrorCode = -32602
	CodeInternalError  ErrorCode = -32603
)

LSP error codes.

type Filesystem

type Filesystem struct {
	Documents  map[string]*Document
	BufferType BufferType
}

Filesystem can be embedded into handlers in order to implement the basic document sync methods of the LSP.

func (*Filesystem) DidChangeTextDocument

func (f *Filesystem) DidChangeTextDocument(_ context.Context, params DidChangeTextDocumentParams) error

DidChangeTextDocument implements lsp.Handler.

func (*Filesystem) DidCloseTextDocument

func (f *Filesystem) DidCloseTextDocument(_ context.Context, params DidCloseTextDocumentParams) error

DidCloseTextDocument implements lsp.Handler.

func (*Filesystem) DidOpenTextDocument

func (f *Filesystem) DidOpenTextDocument(_ context.Context, params DidOpenTextDocumentParams) error

DidOpenTextDocument implements DocumentSyncHandler.

type GapBuffer

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

GapBuffer implements a gap buffer for amortized O(1) insertions and deletions at the cursor position and O(n) for random access. Its reads are fast compared to RopeBuffer.

func (*GapBuffer) Bytes

func (g *GapBuffer) Bytes() []byte

Bytes implements Buffer.

func (*GapBuffer) Delete

func (g *GapBuffer) Delete(start, end int)

Delete implements Buffer.

func (*GapBuffer) Len

func (g *GapBuffer) Len() int

Len implements Buffer.

func (*GapBuffer) ReadAt

func (g *GapBuffer) ReadAt(p []byte, off int64) (n int, err error)

ReadAt implements Buffer.

func (*GapBuffer) Reset

func (g *GapBuffer) Reset(b []byte)

Reset implements Buffer.

func (*GapBuffer) WriteAt

func (g *GapBuffer) WriteAt(p []byte, off int64) (n int, err error)

WriteAt implements Buffer.

type Handler

type Handler interface {
	DocumentSyncHandler
	Initialize(ctx context.Context, clientCapabilities ClientCapabilities) (*ServerCapabilities, error)
	Completion(ctx context.Context, params CompletionParams) (*CompletionList, error)
}

Handler provides the logic for handling LSP requests and notifications.

type Position

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

https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position

func (Position) String

func (p Position) String() string

type Range

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

https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#range

func (Range) String

func (r Range) String() string

type ResponseError

type ResponseError struct {
	Code          ErrorCode `json:"code"`
	Message       string    `json:"message"`
	InternalError error     `json:"-"`
}

https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#responseError

func (*ResponseError) Error

func (e *ResponseError) Error() string

func (*ResponseError) Unwrap

func (e *ResponseError) Unwrap() error

type RopeBuffer

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

RopeBuffer implements Buffer using a rope data structure. This is best at scale, for large documents with frequent random insertions and deletions.

func (*RopeBuffer) Bytes

func (r *RopeBuffer) Bytes() []byte

Bytes implements Buffer.

func (*RopeBuffer) Delete

func (r *RopeBuffer) Delete(start, end int)

Delete implements Buffer.

func (*RopeBuffer) Len

func (r *RopeBuffer) Len() int

Len implements Buffer.

func (*RopeBuffer) ReadAt

func (r *RopeBuffer) ReadAt(p []byte, off int64) (n int, err error)

ReadAt implements Buffer.

func (*RopeBuffer) Reset

func (r *RopeBuffer) Reset(b []byte)

Reset implements Buffer.

func (*RopeBuffer) WriteAt

func (r *RopeBuffer) WriteAt(p []byte, off int64) (n int, err error)

WriteAt implements Buffer.

type Server

type Server struct {
	Stdin   io.Reader
	Stdout  io.Writer
	Info    ServerInfo
	Handler Handler
}

Server manages the LSP server lifecycle and dispatching requests and notifications to a handler.

func (*Server) Serve

func (s *Server) Serve() error

Serve runs the LSP server. It blocks until the client receives an "exit".

type ServerCapabilities

type ServerCapabilities struct {
	TextDocumentSync      *TextDocumentSyncOptions `json:"textDocumentSync,omitempty"`
	CompletionProvider    *CompletionOptions       `json:"completionProvider,omitempty"`
	HoverProvider         bool                     `json:"hoverProvider,omitempty"`
	DefinitionProvider    bool                     `json:"definitionProvider,omitempty"`
	ReferencesProvider    bool                     `json:"referencesProvider,omitempty"`
	SignatureHelpProvider bool                     `json:"signatureHelpProvider,omitempty"`
}

https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#serverCapabilities

type TextDocumentContentChangeEvent

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

https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocumentContentChangeEvent

func (TextDocumentContentChangeEvent) String

type TextDocumentSyncOptions

type TextDocumentSyncOptions struct {
	OpenClose bool                 `json:"openClose,omitempty"`
	Change    TextDocumentSyncKind `json:"change"`
}

https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocumentSyncOptions

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL