lsp

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2024 License: MIT Imports: 11 Imported by: 0

README

LSP

Language server protocol library/framework for Golang 1.21+

This project is in early development. The API is subject to change.

This package simplifies the process of creating a language server. It's as easy as implementing the language-specific handlers.

I created this package as a library for myself, so that I can quickly implement custom IDE features whenever I have some need. I hope you find it useful too!

The root lsp package provides a high-level framework for creating a language server. It includes a Server that handles communication with the client and delegates the actual language server implementation using handlers.

For users who need more flexibility, the low-level jsonrpc and types packages provide the core types needed to fully customize the server implementation to your heart's content.

What's a language server?

A language server is a program that provides language-specific features like auto-completion, go-to-definition, and hover information to an editor or IDE. Read more about the Language Server Protocol.

Feature roadmap

  • Feature-complete implementation of the Language Server Protocol Speficication - 3.17
  • A server framework that handles communication with the client and delegates the actual language server implementation using handlers.
  • Test utilities for testing language servers.
  • Examples included in all packages.

Documentation

Overview

package lsp simplifies the process of creating a language server. It provides a high-level framework for creating a language server. The included Server handles communication with the client and delegates the actual language server implementation to the Handler.

For users who need more flexibility, see the low-level jsonrpc and types packages. These provide the core types needed to fully customize the server implementation to your heart's content.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func UTF16Len

func UTF16Len(s []byte) int

UTF16Len returns the number of UTF-16 code units required to encode the given UTF-8 byte slice.

Types

type File

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

File is a representation of a text file that can be modified by LSP text document change events. It keeps track of line breaks to allow for efficient conversion between byte offsets and LSP positions.

func (*File) ApplyChange

func (f *File) ApplyChange(change types.TextDocumentContentChangeEvent) error

ApplyChange applies the given change to the file content.

func (File) Bytes

func (f File) Bytes() []byte

Bytes returns the raw bytes of the file.

func (*File) GetOffset

func (f *File) GetOffset(p types.Position) (int, error)

GetOffset returns the byte offset for the given LSP protocol position.

func (*File) GetPosition

func (f *File) GetPosition(offset int) (types.Position, error)

GetPosition returns the LSP protocol position for the given byte offset.

func (*File) Reset

func (f *File) Reset(s []byte)

Reset initializes the file with the given content.

type Handler

type Handler interface {
	Capabilities() types.ServerCapabilities
	HandleOpen(params types.DidOpenTextDocumentParams) error
	HandleClose(params types.DidCloseTextDocumentParams) error
	HandleChange(params types.DidChangeTextDocumentParams) error
	HandleDefinition(params types.DefinitionParams) ([]types.Location, error)
	HandleReferences(params types.ReferenceParams) ([]types.Location, error)
}

Handler is an interface for handling LSP requests.

type NopHandler

type NopHandler struct{}

NopHandler can be embedded in a struct to provide no-op implementations of ununsed Handler methods.

func (NopHandler) Capabilities

func (NopHandler) Capabilities() types.ServerCapabilities

Capabilities implements Handler.

func (NopHandler) HandleChange

HandleChange implements Handler.

func (NopHandler) HandleClose

HandleClose implements Handler.

func (NopHandler) HandleDefinition

func (NopHandler) HandleDefinition(types.DefinitionParams) ([]types.Location, error)

HandleDefinition implements Handler.

func (NopHandler) HandleOpen

HandleOpen implements Handler.

func (NopHandler) HandleReferences

func (NopHandler) HandleReferences(types.ReferenceParams) ([]types.Location, error)

HandleReferences implements Handler.

type Server

type Server struct {
	Reader     io.Reader
	Writer     io.Writer
	Handler    Handler
	ServerInfo types.ServerInfo
}

Server is an LSP server. It handles the I/O and delegates handling of requests to a Handler.

func (*Server) Run

func (s *Server) Run() error

Run is a blocking function that reads from the server's Reader, processes requests, and writes responses to the server's Writer. It returns an error if the server stops unexpectedly.

Directories

Path Synopsis
internal
mocks
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.
Package jsonrpc provides utilities for working with the JSON-RPC protocol.
Package jsonrpc provides utilities for working with the JSON-RPC protocol.
Package types contains the [Language Server Protocol specification] types.
Package types contains the [Language Server Protocol specification] types.

Jump to

Keyboard shortcuts

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