Skip to content

package lsp

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

Functions

func CompleteForText

func CompleteForText(
    path string,
    text string,
    pos protocol.Position,
    projects *ProjectCache,
) (protocol.CompletionList, *protocol.ResponseError)

CompleteForText resolves a completion request.

func DefinitionForText

func DefinitionForText(
    path string,
    text string,
    pos protocol.Position,
    projects *ProjectCache,
) ([]protocol.Location, *protocol.ResponseError)

DefinitionForText resolves a go-to-definition request.

func DiagnosticsForError

func DiagnosticsForError(text string, err error) []protocol.Diagnostic

DiagnosticsForError converts UB parser and syntax errors to LSP diagnostics.

func DiagnosticsForText

func DiagnosticsForText(path string, text string) []protocol.Diagnostic

DiagnosticsForText parses and validates UB source text into LSP diagnostics.

func DiagnosticsForTextWithProjects

func DiagnosticsForTextWithProjects(
    path string,
    text string,
    projects *ProjectCache,
) []protocol.Diagnostic

DiagnosticsForTextWithProjects adds no-fetch semantic checks when project data is available.

func DocumentSymbolsForText

func DocumentSymbolsForText(
    path string,
    text string,
) ([]protocol.DocumentSymbol, *protocol.ResponseError)

DocumentSymbolsForText parses UB text and returns document symbols.

func FileURIToPath

func FileURIToPath(rawURI string) (string, error)

FileURIToPath converts a file URI to a local path.

func FormatText

func FormatText(path string, text string) ([]protocol.TextEdit, *protocol.ResponseError)

FormatText formats one UB document and returns LSP text edits.

func HoverForText

func HoverForText(
    path string,
    text string,
    pos protocol.Position,
    projects *ProjectCache,
) (*protocol.Hover, *protocol.ResponseError)

HoverForText resolves a hover request.

func LSPToOffset

func LSPToOffset(text string, pos protocol.Position) (int, bool)

LSPToOffset converts a zero-based UTF-16 LSP position to a byte offset.

func OffsetToLSP

func OffsetToLSP(text string, offset int) protocol.Position

OffsetToLSP converts a byte offset to a zero-based UTF-16 LSP position.

func PathToFileURI

func PathToFileURI(path string) string

PathToFileURI converts a local path to a file URI.

func Serve

func Serve(ctx context.Context, in io.Reader, out io.Writer, version string) error

Serve runs an LSP session over stdio-compatible streams.

func ServeWithOptions

func ServeWithOptions(ctx context.Context, in io.Reader, out io.Writer, options Options) error

ServeWithOptions runs an LSP session over stdio-compatible streams.

Types

type Document

type Document struct {
    URI     string
    Path    string
    Version int32
    Text    string
    Lines   []LineInfo
}

Document is the open-text state for one LSP text document.

type DocumentStore

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

DocumentStore tracks open documents by URI.

func NewDocumentStore

func NewDocumentStore() *DocumentStore

NewDocumentStore returns an empty document store.

func (*DocumentStore) Change

func (s *DocumentStore) Change(uri string, version int32, text string) (*Document, error)

Change replaces an open document with a full text snapshot.

func (*DocumentStore) Close

func (s *DocumentStore) Close(uri string)

Close removes an open document snapshot.

func (*DocumentStore) Get

func (s *DocumentStore) Get(uri string) (*Document, bool)

Get returns an open document snapshot by URI.

func (*DocumentStore) Open

func (s *DocumentStore) Open(uri string, version int32, text string) (*Document, error)

Open stores a full text document snapshot.

type ImportResolver

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

ImportResolver resolves LSP imports without network access.

func NewImportResolver

func NewImportResolver(
    root string,
    project *deps.Project,
    lock *deps.ProjectLock,
    remote cachedRemoteSource,
) *ImportResolver

NewImportResolver returns an import resolver for one project root.

func (*ImportResolver) Resolve

func (r *ImportResolver) Resolve(ref resolve.ImportRef) (*resolve.Source, error)

Resolve resolves ref or returns an error when no cached source exists.

func (*ImportResolver) ResolveNoFetch

func (r *ImportResolver) ResolveNoFetch(
    ref resolve.ImportRef,
) (*resolve.Source, bool, error)

ResolveNoFetch resolves ref from local files or cached remote data only.

type LineInfo

type LineInfo struct {
    Start int
    End   int
}

LineInfo records byte offsets for one logical line.

type Options

type Options struct {
    Version string
    Trace   io.Writer
    Log     io.Writer
}

Options configures an LSP session.

type Project

type Project struct {
    Root          string
    Marker        projectmarker.Marker
    DepsProject   *deps.Project
    ProjectLock   *deps.ProjectLock
    Resolver      *ImportResolver
    GoSchemas     *compile.SchemaCache
    GoIndex       *goschema.SourceIndexCache
    GoModuleRoots []goschema.ModuleRoot
}

Project holds cached LSP data for one project marker root.

func (*Project) EnsureGoModuleRoot

func (p *Project) EnsureGoModuleRoot(source *resolve.Source)

EnsureGoModuleRoot adds the Go module root for source when it can be found.

type ProjectCache

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

ProjectCache stores project data by marker root.

func NewProjectCache

func NewProjectCache(workspaceRoot string) *ProjectCache

NewProjectCache returns an empty project cache.

func (*ProjectCache) InvalidatePath

func (c *ProjectCache) InvalidatePath(path string)

InvalidatePath removes cached project data affected by a saved path.

func (*ProjectCache) ProjectForPath

func (c *ProjectCache) ProjectForPath(path string) (*Project, error)

ProjectForPath returns cached project data for path's nearest marker root.

func (*ProjectCache) SetWorkspaceRoots

func (c *ProjectCache) SetWorkspaceRoots(roots []string)

SetWorkspaceRoots sets weak roots used for loose files without project markers.

type Session

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

Session owns the state for one LSP client connection.

func NewSession

func NewSession(version string) *Session

NewSession returns a new LSP session.

func (*Session) Exit

func (s *Session) Exit() bool

Exit reports whether the client has requested exit.

func (*Session) HandleRequest

func (s *Session) HandleRequest(
    ctx context.Context,
    req *protocol.RequestMessage,
) (any, *protocol.ResponseError)

HandleRequest dispatches one JSON-RPC request to the session.

func (*Session) SetSender

func (s *Session) SetSender(sender protocol.Sender)

SetSender sets the server-to-client notification sender.

func (*Session) Shutdown

func (s *Session) Shutdown() bool

Shutdown reports whether the client has requested shutdown.

func (*Session) StopRequested

func (s *Session) StopRequested() bool

StopRequested reports whether the protocol server should stop serving.