lsp

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 16 Imported by: 1

Documentation

Overview

Package lsp provides a client implementation for the Language Server Protocol (LSP).

Index

Constants

View Source
const (
	MethodInitialize                         = "initialize"
	MethodInitialized                        = "initialized"
	MethodShutdown                           = "shutdown"
	MethodExit                               = "exit"
	MethodTextDocumentDidOpen                = "textDocument/didOpen"
	MethodTextDocumentDidChange              = "textDocument/didChange"
	MethodTextDocumentDidSave                = "textDocument/didSave"
	MethodTextDocumentDidClose               = "textDocument/didClose"
	MethodTextDocumentCompletion             = "textDocument/completion"
	MethodTextDocumentHover                  = "textDocument/hover"
	MethodTextDocumentDefinition             = "textDocument/definition"
	MethodTextDocumentReferences             = "textDocument/references"
	MethodTextDocumentDiagnostic             = "textDocument/publishDiagnostics"
	MethodWorkspaceConfiguration             = "workspace/configuration"
	MethodWorkspaceDidChangeConfiguration    = "workspace/didChangeConfiguration"
	MethodWorkspaceDidChangeWorkspaceFolders = "workspace/didChangeWorkspaceFolders"
	MethodWorkspaceDidChangeWatchedFiles     = "workspace/didChangeWatchedFiles"
)

LSP method constants.

Variables

This section is empty.

Functions

func CreateFullDocumentChange

func CreateFullDocumentChange(content string) []protocol.TextDocumentContentChangeEvent

CreateFullDocumentChange creates a change event for full document sync.

func DetectLanguage

func DetectLanguage(path string) protocol.LanguageKind

DetectLanguage detects the language of a given file path.

func FilePathToURI deprecated

func FilePathToURI(path string) string

FilePathToURI converts a file path to a file URI.

Deprecated: use protocol.URIFromPath.

func URIToFilePath

func URIToFilePath(uri string) string

URIToFilePath converts a file URI to a file path.

Types

type Client

type Client struct {
	ID   string
	Name string
	// contains filtered or unexported fields
}

Client represents an LSP client connection to a language server.

func NewClient

func NewClient(config ClientConfig) (*Client, error)

NewClient creates a new LSP client with the given configuration.

func (*Client) Exit

func (c *Client) Exit() error

Exit sends an exit notification to the language server.

func (*Client) FindReferences

func (c *Client) FindReferences(ctx context.Context, filepath string, line, character int, includeDeclaration bool) ([]protocol.Location, error)

FindReferences finds all references to the symbol at the given position.

func (*Client) GetCapabilities

func (c *Client) GetCapabilities() protocol.ServerCapabilities

GetCapabilities returns the server capabilities.

func (*Client) Initialize

func (c *Client) Initialize(ctx context.Context, enableSnippets bool) error

Initialize sends the initialize request to the language server.

func (*Client) IsInitialized

func (c *Client) IsInitialized() bool

IsInitialized returns whether the client has been initialized.

func (*Client) IsRunning

func (c *Client) IsRunning() bool

IsRunning returns whether the client connection is still active.

func (*Client) Kill

func (c *Client) Kill()

Kill cancels the connection context.

func (*Client) NotifyDidChangeTextDocument

func (c *Client) NotifyDidChangeTextDocument(ctx context.Context, uri string, version int, changes []protocol.TextDocumentContentChangeEvent) error

NotifyDidChangeTextDocument notifies the server that a document was changed.

func (*Client) NotifyDidChangeWatchedFiles

func (c *Client) NotifyDidChangeWatchedFiles(ctx context.Context, changes []protocol.FileEvent) error

NotifyDidChangeWatchedFiles notifies the server that watched files have changed.

func (*Client) NotifyDidCloseTextDocument

func (c *Client) NotifyDidCloseTextDocument(ctx context.Context, uri string) error

NotifyDidCloseTextDocument notifies the server that a document was closeed.

func (*Client) NotifyDidOpenTextDocument

func (c *Client) NotifyDidOpenTextDocument(ctx context.Context, uri string, languageID string, version int, text string) error

NotifyDidOpenTextDocument notifies the server that a document was opened.

func (*Client) NotifyWorkspaceDidChangeConfiguration

func (c *Client) NotifyWorkspaceDidChangeConfiguration(ctx context.Context, settings any) error

NotifyWorkspaceDidChangeConfiguration notifies the server that the workspace configuration has changed.

func (*Client) RegisterHandler

func (c *Client) RegisterHandler(method string, handler transport.Handler)

RegisterHandler registers a handler for server-initiated requests.

func (*Client) RegisterNotificationHandler

func (c *Client) RegisterNotificationHandler(method string, handler transport.NotificationHandler)

RegisterNotificationHandler registers a handler for server-initiated notifications.

func (*Client) RequestCompletion

func (c *Client) RequestCompletion(ctx context.Context, uri string, position protocol.Position) (*protocol.CompletionList, error)

RequestCompletion requests completion items at the given position.

func (*Client) RequestHover

func (c *Client) RequestHover(ctx context.Context, uri string, position protocol.Position) (*protocol.Hover, error)

RequestHover requests hover information at the given position.

func (*Client) Shutdown

func (c *Client) Shutdown(ctx context.Context) error

Shutdown sends a shutdown request to the language server.

type ClientConfig

type ClientConfig struct {
	Command          string
	Args             []string
	RootURI          string
	WorkspaceFolders []protocol.WorkspaceFolder
	InitOptions      map[string]any
	Settings         map[string]any
	Environment      map[string]string
	Timeout          time.Duration
}

ClientConfig represents the configuration for creating a new LSP client.

type Document

type Document struct {
	URI        string
	LanguageID string
	Version    int
	Content    string
}

Document represents an open text document.

type Message

type Message struct {
	JSONRPC string          `json:"jsonrpc"`
	ID      int32           `json:"id,omitempty"`
	Method  string          `json:"method,omitempty"`
	Params  json.RawMessage `json:"params,omitempty"`
	Result  json.RawMessage `json:"result,omitempty"`
	Error   *ResponseError  `json:"error,omitempty"`
}

Message represents a JSON-RPC 2.0 message.

func NewNotification

func NewNotification(method string, params any) (*Message, error)

NewNotification creates a new JSON-RPC 2.0 notification message.

func NewRequest

func NewRequest(id int32, method string, params any) (*Message, error)

NewRequest creates a new JSON-RPC 2.0 request message.

type OffsetEncoding

type OffsetEncoding int

OffsetEncoding represents the character encoding used for text document offsets.

const (
	// UTF8 encoding - bytes.
	UTF8 OffsetEncoding = iota
	// UTF16 encoding - default for LSP.
	UTF16
	// UTF32 encoding - codepoints.
	UTF32
)

type ResponseError

type ResponseError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

ResponseError represents a JSON-RPC 2.0 error.

type TextDocumentSyncManager

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

TextDocumentSyncManager manages text document synchronization with the language server.

func NewTextDocumentSyncManager

func NewTextDocumentSyncManager(client *Client) *TextDocumentSyncManager

NewTextDocumentSyncManager creates a new text document sync manager.

func (*TextDocumentSyncManager) Change

Change applies changes to an open document.

func (*TextDocumentSyncManager) Close

func (m *TextDocumentSyncManager) Close(uri string) error

Close closes a text document.

func (*TextDocumentSyncManager) GetDocument

func (m *TextDocumentSyncManager) GetDocument(uri string) (*Document, bool)

GetDocument returns the document for the given URI.

func (*TextDocumentSyncManager) Open

func (m *TextDocumentSyncManager) Open(uri, languageID, content string) error

Open opens a new text document.

func (*TextDocumentSyncManager) OpenFile

func (m *TextDocumentSyncManager) OpenFile(filePath, content string) error

OpenFile opens a new text document from a file path.

func (*TextDocumentSyncManager) Save

func (m *TextDocumentSyncManager) Save(uri string, includeText bool) error

Save notifies the server that a document was saved.

Directories

Path Synopsis
Package protocol provides types and functions for the Language Server Protocol (LSP).
Package protocol provides types and functions for the Language Server Protocol (LSP).

Jump to

Keyboard shortcuts

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