semantic

package
v0.17.21 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LineColToOffset

func LineColToOffset(content string, line, col int) int

LineColToOffset converts a 1-based line and a 0-based column to a byte offset in the given content string. This is language-agnostic and shared across adapters.

Types

type Adapter

type Adapter interface {
	Run(input ToolInput) (ToolResult, error)
}

Adapter is implemented by language-specific semantic backends.

func NewCppAdapter

func NewCppAdapter() Adapter

NewCppAdapter constructs a C/C++ semantic adapter.

func NewGoAdapter

func NewGoAdapter() Adapter

NewGoAdapter constructs a Go semantic adapter.

func NewPythonAdapter

func NewPythonAdapter() Adapter

NewPythonAdapter constructs a Python semantic adapter.

func NewRustAdapter

func NewRustAdapter() Adapter

NewRustAdapter constructs a Rust semantic adapter.

func NewTypeScriptAdapter

func NewTypeScriptAdapter() Adapter

NewTypeScriptAdapter constructs a TS/JS semantic adapter.

type AdapterFactory

type AdapterFactory func() Adapter

AdapterFactory creates a new adapter instance.

type Capabilities

type Capabilities struct {
	Diagnostics   bool `json:"diagnostics"`
	Definition    bool `json:"definition"`
	Hover         bool `json:"hover"`
	Rename        bool `json:"rename"`
	References    bool `json:"references"`
	CodeActions   bool `json:"code_actions"`
	InlayHints    bool `json:"inlay_hints"`
	SignatureHelp bool `json:"signature_help"`
}

Capabilities describes which semantic features are available for a language.

type LSPQueryHelper

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

LSPQueryHelper provides common functionality for LSP-based semantic adapters. It handles the boilerplate of connecting to the LSP proxy, sending requests, and parsing responses.

func NewLSPQueryHelper

func NewLSPQueryHelper(languageID, binaryName string) *LSPQueryHelper

NewLSPQueryHelper creates a new helper for the given language and binary.

func (*LSPQueryHelper) BinaryAvailable

func (h *LSPQueryHelper) BinaryAvailable() bool

BinaryAvailable checks whether the configured binary is on PATH.

func (*LSPQueryHelper) Capabilities

func (h *LSPQueryHelper) Capabilities() Capabilities

Capabilities returns the full set of features this helper can support.

func (*LSPQueryHelper) RunDefinitionViaLSP

func (h *LSPQueryHelper) RunDefinitionViaLSP(_ context.Context, _ ToolInput) (*ToolDefinition, error)

RunDefinitionViaLSP sends a textDocument/definition request (placeholder).

func (*LSPQueryHelper) RunDiagnosticsViaLSP

func (h *LSPQueryHelper) RunDiagnosticsViaLSP(_ context.Context, _ ToolInput) ([]ToolDiagnostic, error)

RunDiagnosticsViaLSP is a placeholder for future LSP-based diagnostics.

func (*LSPQueryHelper) RunHoverViaLSP

func (h *LSPQueryHelper) RunHoverViaLSP(_ context.Context, _ ToolInput) (*ToolHover, error)

RunHoverViaLSP sends a textDocument/hover request (placeholder).

func (*LSPQueryHelper) RunReferencesViaLSP

func (h *LSPQueryHelper) RunReferencesViaLSP(_ context.Context, _ ToolInput) ([]ToolReferenceLocation, error)

RunReferencesViaLSP sends a textDocument/references request (placeholder).

type Position

type Position struct {
	Line   int `json:"line"`
	Column int `json:"column"`
}

Position is a 1-based location within a document.

type Registry

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

Registry maps language IDs to adapters. Two registration modes are supported:

  • Factory (RegisterAliases / Register): a new Adapter is created per request. Use for lightweight stateless adapters.
  • Singleton (RegisterSingleton): a shared Adapter instance handles all requests for the registered language IDs. Use for SessionPool or other stateful adapters that are expensive to create and should live across requests.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates an empty adapter registry.

func (*Registry) AdapterForLanguage

func (r *Registry) AdapterForLanguage(languageID string) (Adapter, bool)

AdapterForLanguage returns an adapter for the language ID and records the dispatch duration in the result's DurationMs field. Singletons take precedence over factory registrations for the same language ID.

func (*Registry) Register

func (r *Registry) Register(languageID string, factory AdapterFactory)

Register binds one language ID to an adapter factory.

func (*Registry) RegisterAliases

func (r *Registry) RegisterAliases(factory AdapterFactory, languageIDs ...string)

RegisterAliases binds one factory to many language IDs.

func (*Registry) RegisterSingleton

func (r *Registry) RegisterSingleton(adapter Adapter, languageIDs ...string)

RegisterSingleton binds a shared adapter instance to one or more language IDs. The same adapter instance is reused for every request on those language IDs. This is the right choice for a SessionPool or any adapter that maintains per-workspace state across calls.

type SessionAdapter

type SessionAdapter interface {
	Adapter
	// Healthy returns true if the session is still usable.
	// A false return causes the pool to close and replace the session.
	// Healthy must be safe for concurrent calls and must never block on a
	// mutex that a concurrent Run holds while the pool mutex is held — the
	// pool calls Healthy outside its own lock for exactly this reason.
	Healthy() bool
	// Close tears down the session and releases its resources.
	// Close must be idempotent and safe for concurrent calls: the pool may
	// close a session from multiple paths (eviction, TTL recycle, unhealthy
	// replacement) even while another goroutine is inside Healthy.
	Close() error
}

SessionAdapter extends Adapter with lifecycle management. Implement this for adapters that maintain persistent state (e.g. a long-lived language-server process) and should be reused across requests.

type SessionFactory

type SessionFactory func(workspaceRoot string) (SessionAdapter, error)

SessionFactory creates a new SessionAdapter for a given workspace root. It is called once when no healthy session exists for that root.

type SessionPool

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

SessionPool manages one SessionAdapter per workspace root. When an adapter becomes unhealthy, it is closed and a new one is created on the next request. Idle sessions are evicted after idleTTL (0 = never).

SessionPool implements Adapter so it can be registered directly via Registry.RegisterSingleton for any number of language IDs.

func NewGoSessionPool

func NewGoSessionPool(idleTTL time.Duration) *SessionPool

NewGoSessionPool creates a reusable per-workspace adapter pool for Go. Diagnostics remain local and stateless, while definitions are routed through a persistent gopls server for faster repeated lookups.

func NewSessionPool

func NewSessionPool(factory SessionFactory, idleTTL time.Duration) *SessionPool

NewSessionPool creates a pool backed by factory. idleTTL controls when idle sessions are evicted; pass 0 to disable eviction.

func NewTypeScriptSessionPool

func NewTypeScriptSessionPool(idleTTL time.Duration) *SessionPool

NewTypeScriptSessionPool creates a reusable per-workspace adapter pool for TypeScript-family languages backed by a persistent Node worker process.

func (*SessionPool) Close

func (p *SessionPool) Close()

Close shuts down all pooled sessions and empties the pool.

func (*SessionPool) EvictIdle

func (p *SessionPool) EvictIdle()

EvictIdle closes sessions that have been idle longer than idleTTL. Call this periodically (e.g. from a background goroutine) to reclaim resources.

func (*SessionPool) Run

func (p *SessionPool) Run(input ToolInput) (ToolResult, error)

Run implements Adapter. It routes the request to the pooled session for input.WorkspaceRoot, creating one if needed.

type ToolCodeAction

type ToolCodeAction struct {
	Title string               `json:"title"` // human-readable label like "Add import", "Organize imports"
	Kind  string               `json:"kind"`  // "quickfix", "refactor.extract", "source.organizeImports", etc
	Edits []ToolCodeActionEdit `json:"edits"`
}

ToolCodeAction represents a single code action available at a position.

type ToolCodeActionEdit

type ToolCodeActionEdit struct {
	FilePath string `json:"filePath"`
	From     int    `json:"from"` // 0-based byte offset
	To       int    `json:"to"`   // 0-based byte offset
	NewText  string `json:"newText"`
}

ToolCodeActionEdit is a single text edit within a code action.

type ToolDefinition

type ToolDefinition struct {
	Path   string `json:"path"`
	Line   int    `json:"line"`
	Column int    `json:"column"`
}

ToolDefinition is a normalized adapter definition target.

type ToolDiagnostic

type ToolDiagnostic struct {
	From     int    `json:"from"`
	To       int    `json:"to"`
	Severity string `json:"severity"`
	Message  string `json:"message"`
	Source   string `json:"source"`
}

ToolDiagnostic is an adapter diagnostic in editor offset coordinates.

type ToolHover

type ToolHover struct {
	Contents    string `json:"contents"` // Markdown content
	StartLine   int    `json:"start_line,omitempty"`
	StartColumn int    `json:"start_column,omitempty"`
	EndLine     int    `json:"end_line,omitempty"`
	EndColumn   int    `json:"end_column,omitempty"`
}

ToolHover is a hover tooltip result with markdown content.

type ToolInlayHint

type ToolInlayHint struct {
	From  int    `json:"from"`  // 0-based byte offset where hint is displayed
	To    int    `json:"to"`    // 0-based byte offset (end of hint range, typically From)
	Label string `json:"label"` // text to display
	Kind  string `json:"kind"`  // "type", "parameter", or "none"
}

ToolInlayHint is an adapter inlay hint in editor offset coordinates.

type ToolInput

type ToolInput struct {
	WorkspaceRoot string    `json:"workspaceRoot"`
	FilePath      string    `json:"filePath"`
	Content       string    `json:"content"`
	Method        string    `json:"method"`
	Position      *Position `json:"position,omitempty"`
	// Trigger distinguishes how the request was initiated.
	// "edit" means an in-progress keystroke; "save" means an explicit save.
	// Adapters may use this to skip expensive checks on "edit" (e.g. go vet).
	Trigger string `json:"trigger,omitempty"`
}

ToolInput is the normalized request shape sent to language adapters.

type ToolReferenceLocation

type ToolReferenceLocation struct {
	FilePath string `json:"filePath"`
	Line     int    `json:"line"`     // 1-based line number
	StartCol int    `json:"startCol"` // 1-based start column
	EndCol   int    `json:"endCol"`   // 1-based end column
	LineText string `json:"lineText"`
}

ToolReferenceLocation is a single reference location in find-all-references.

type ToolReferences

type ToolReferences struct {
	Locations []ToolReferenceLocation `json:"locations"`
	// SymbolName is the resolved name of the referenced symbol.
	SymbolName string `json:"symbolName"`
}

ToolReferences is the find-all-references result.

type ToolRename

type ToolRename struct {
	Locations []ToolRenameLocation `json:"locations"`
}

ToolRename is the rename preview result.

type ToolRenameLocation

type ToolRenameLocation struct {
	FilePath string `json:"filePath"`
	From     int    `json:"from"` // 0-based byte offset
	To       int    `json:"to"`   // 0-based byte offset
}

ToolRenameLocation is a single rename edit location in a file.

type ToolResult

type ToolResult struct {
	Capabilities  Capabilities       `json:"capabilities"`
	Diagnostics   []ToolDiagnostic   `json:"diagnostics,omitempty"`
	Definition    *ToolDefinition    `json:"definition,omitempty"`
	Hover         *ToolHover         `json:"hover,omitempty"`
	Rename        *ToolRename        `json:"rename,omitempty"`
	References    *ToolReferences    `json:"references,omitempty"`
	CodeActions   []ToolCodeAction   `json:"code_actions,omitempty"`
	InlayHints    []ToolInlayHint    `json:"inlay_hints,omitempty"`
	SignatureHelp *ToolSignatureHelp `json:"signature_help,omitempty"`
	Error         string             `json:"error,omitempty"`
	// DurationMs is the wall-clock time the adapter took to run, in milliseconds.
	// Populated by the registry dispatch layer, not by individual adapters.
	DurationMs int64 `json:"duration_ms,omitempty"`
}

ToolResult is the normalized adapter response.

type ToolSignatureHelp

type ToolSignatureHelp struct {
	Signatures      []ToolSignatureHelpSignature `json:"signatures"`
	ActiveSignature int                          `json:"activeSignature"`
	ActiveParameter int                          `json:"activeParameter"`
}

ToolSignatureHelp is the signature help result.

type ToolSignatureHelpParameter

type ToolSignatureHelpParameter struct {
	Label         string `json:"label"`
	Documentation string `json:"documentation,omitempty"`
}

ToolSignatureHelpParameter is a single parameter in a function signature.

type ToolSignatureHelpSignature

type ToolSignatureHelpSignature struct {
	Label         string                       `json:"label"`
	Documentation string                       `json:"documentation,omitempty"`
	Parameters    []ToolSignatureHelpParameter `json:"parameters"`
}

ToolSignatureHelpSignature is a single function signature.

Jump to

Keyboard shortcuts

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