lsp

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2025 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChunkHead

func ChunkHead(text string, textPos Position, pos Position) string

calculate the relative index of a position to a text

func FindPair

func FindPair(text string, lines []int, textPos Position, tokens []Token, lchar rune, rchar rune, start int, end int, notAllow rune) (int, int)

FindPair finds the right token index of lchar and left token index of rchar in a text start and end is the limit range of tokens notAllow is the character that not allow in the range

func FindSingle

func FindSingle(text string, lines []int, textPos Position, tokens []Token, sep string, start int, end int) int

FindSingle finds the single char's left token index in a text start and end is the limit range of tokens

func GetDistance

func GetDistance(text string, start Position, pos Position) int

func PositionOffset

func PositionOffset(file_uri string, text string, pos Position) int

func RegisterProvider added in v0.2.0

func RegisterProvider(lang uniast.Language, provider LanguageServiceProvider)

RegisterProvider makes a LanguageServiceProvider available for a given language. This function should be called from the init() function of a language-specific package.

func RelativePostionWithLines

func RelativePostionWithLines(lines []int, basePos Position, pos Position) int

calculate the relative index of a position to a text

func SetLocationMarshalJSONInline

func SetLocationMarshalJSONInline(inline bool)

Types

type ClientOptions

type ClientOptions struct {
	Server string
	uniast.Language
	Verbose               bool
	InitializationOptions interface{}
}

type DidOpenTextDocumentParams

type DidOpenTextDocumentParams struct {
	TextDocument TextDocumentItem `json:"textDocument"`
}

type DocumentRange

type DocumentRange struct {
	TextDocument lsp.TextDocumentIdentifier `json:"textDocument"`
	Range        Range                      `json:"range"`
}

type DocumentSymbol

type DocumentSymbol struct {
	Name     string            `json:"name"`
	Kind     SymbolKind        `json:"kind"`
	Tags     []json.RawMessage `json:"tags"`
	Location Location          `json:"location"`
	Children []*DocumentSymbol `json:"children"`
	Text     string            `json:"text"`
	Tokens   []Token           `json:"tokens"`
	Node     *sitter.Node      `json:"-"`
	Role     SymbolRole        `json:"-"`
}

func (*DocumentSymbol) MarshalJSON

func (s *DocumentSymbol) MarshalJSON() ([]byte, error)

func (*DocumentSymbol) MarshalText

func (s *DocumentSymbol) MarshalText() ([]byte, error)

func (*DocumentSymbol) String

func (s *DocumentSymbol) String() string

type DocumentURI

type DocumentURI lsp.DocumentURI

func NewURI

func NewURI(file string) DocumentURI

func (DocumentURI) File

func (l DocumentURI) File() string

type Hover added in v0.2.0

type Hover struct {
	Contents []MarkedString `json:"contents"`
	Range    *Range         `json:"range,omitempty"`
}

type LSPClient

type LSPClient struct {
	*jsonrpc2.Conn

	ClientOptions
	// contains filtered or unexported fields
}

func InitLSPForFirstTest added in v0.2.0

func InitLSPForFirstTest(lang uniast.Language, server string) (*LSPClient, string, error)

func NewLSPClient

func NewLSPClient(repo string, openfile string, wait time.Duration, opts ClientOptions) (*LSPClient, error)

func (*LSPClient) Call added in v0.2.0

func (cli *LSPClient) Call(ctx context.Context, method string, params, result interface{}, opts ...jsonrpc2.CallOption) error

Extra wrapper around json rpc to 1. implement a transparent, generic cache

func (*LSPClient) Close

func (c *LSPClient) Close() error

func (*LSPClient) Definition

func (cli *LSPClient) Definition(ctx context.Context, uri DocumentURI, pos Position) ([]Location, error)

func (*LSPClient) DidOpen

func (cli *LSPClient) DidOpen(ctx context.Context, file DocumentURI) (*TextDocumentItem, error)

func (*LSPClient) DocumentSymbols

func (cli *LSPClient) DocumentSymbols(ctx context.Context, file DocumentURI) (map[Range]*DocumentSymbol, error)

func (*LSPClient) FileStructure

func (cli *LSPClient) FileStructure(ctx context.Context, file DocumentURI) ([]*DocumentSymbol, error)

func (*LSPClient) GetFile

func (cli *LSPClient) GetFile(uri DocumentURI) *TextDocumentItem

func (*LSPClient) GetParent

func (cli *LSPClient) GetParent(sym *DocumentSymbol) (ret *DocumentSymbol)

func (LSPClient) Handle

func (h LSPClient) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request)

func (*LSPClient) Hover added in v0.2.0

func (cli *LSPClient) Hover(ctx context.Context, uri DocumentURI, line, character int) (*Hover, error)

func (*LSPClient) Implementation added in v0.2.0

func (cli *LSPClient) Implementation(ctx context.Context, uri DocumentURI, pos Position) ([]Location, error)

func (*LSPClient) InitFiles added in v0.2.0

func (c *LSPClient) InitFiles()

func (*LSPClient) Line

func (cli *LSPClient) Line(uri DocumentURI, pos int) string

get line text of pos

func (*LSPClient) LineCounts

func (cli *LSPClient) LineCounts(uri DocumentURI) []int

func (*LSPClient) Locate

func (cli *LSPClient) Locate(id Location) (string, error)

read file and get the text of block of range

func (*LSPClient) PrepareTypeHierarchy added in v0.2.0

func (cli *LSPClient) PrepareTypeHierarchy(ctx context.Context, uri DocumentURI, pos Position) ([]TypeHierarchyItem, error)

func (*LSPClient) References

func (cli *LSPClient) References(ctx context.Context, id Location) ([]Location, error)

func (*LSPClient) SemanticTokens

func (cli *LSPClient) SemanticTokens(ctx context.Context, id Location) ([]Token, error)

func (*LSPClient) TypeDefinition

func (cli *LSPClient) TypeDefinition(ctx context.Context, uri DocumentURI, pos Position) ([]Location, error)

func (*LSPClient) TypeHierarchySubtypes added in v0.2.0

func (cli *LSPClient) TypeHierarchySubtypes(ctx context.Context, item TypeHierarchyItem) ([]TypeHierarchyItem, error)

func (*LSPClient) TypeHierarchySupertypes added in v0.2.0

func (cli *LSPClient) TypeHierarchySupertypes(ctx context.Context, item TypeHierarchyItem) ([]TypeHierarchyItem, error)

func (LSPClient) WaitFirstNotify

func (h LSPClient) WaitFirstNotify(method string) *jsonrpc2.Request

func (*LSPClient) WorkspaceSearchSymbols added in v0.2.0

func (cli *LSPClient) WorkspaceSearchSymbols(ctx context.Context, query string) ([]SymbolInformation, error)

func (*LSPClient) WorkspaceSymbols

func (cli *LSPClient) WorkspaceSymbols(ctx context.Context, query string) ([]DocumentSymbol, error)

type LanguageServiceProvider added in v0.2.0

type LanguageServiceProvider interface {
	// Hover provides hover information for a given position.
	// Implementations may have custom logic to parse results from different language servers.
	Hover(ctx context.Context, cli *LSPClient, uri DocumentURI, line, character int) (*Hover, error)

	// Implementation finds implementations of a symbol.
	Implementation(ctx context.Context, cli *LSPClient, uri DocumentURI, pos Position) ([]Location, error)

	// WorkspaceSymbols searches for symbols in the workspace.
	WorkspaceSearchSymbols(ctx context.Context, cli *LSPClient, query string) ([]SymbolInformation, error)

	// PrepareTypeHierarchy prepares a type hierarchy for a given position.
	PrepareTypeHierarchy(ctx context.Context, cli *LSPClient, uri DocumentURI, pos Position) ([]TypeHierarchyItem, error)

	// TypeHierarchySupertypes gets the supertypes of a type hierarchy item.
	TypeHierarchySupertypes(ctx context.Context, cli *LSPClient, item TypeHierarchyItem) ([]TypeHierarchyItem, error)

	// TypeHierarchySubtypes gets the subtypes of a type hierarchy item.
	TypeHierarchySubtypes(ctx context.Context, cli *LSPClient, item TypeHierarchyItem) ([]TypeHierarchyItem, error)
}

LanguageServiceProvider defines methods for language-specific LSP features. It allows for extending the base LSPClient with language-specific capabilities without creating circular dependencies.

func GetProvider added in v0.2.0

func GetProvider(lang uniast.Language) LanguageServiceProvider

GetProvider returns the registered LanguageServiceProvider for a given language. It returns nil if no provider is registered for the language.

type LanguageSpec

type LanguageSpec interface {
	// initialize a root workspace, and return all modules [modulename=>abs-path] inside
	WorkSpace(root string) (map[string]string, error)

	// give an absolute file path and returns its module name and package path
	// external path should alse be supported
	// FIXEM: some language (like rust) may have sub-mods inside a file, but we still consider it as a unity mod here
	NameSpace(path string, file *uniast.File) (string, string, error)

	// tells if a file belang to language AST
	ShouldSkip(path string) bool

	// FileImports parse file codes to get its imports
	FileImports(content []byte) ([]uniast.Import, error)

	// return the first declaration token of a symbol, as Type-Name
	DeclareTokenOfSymbol(sym DocumentSymbol) int

	// tells if a token is an AST entity
	IsEntityToken(tok Token) bool

	// tells if a token is a std token
	IsStdToken(tok Token) bool

	// return the SymbolKind of a token
	TokenKind(tok Token) SymbolKind

	// tells if a symbol is a main function
	IsMainFunction(sym DocumentSymbol) bool

	// tells if a symbol is a language symbol (func, type, variable, etc) in workspace
	IsEntitySymbol(sym DocumentSymbol) bool

	// tells if a symbol is public in workspace
	IsPublicSymbol(sym DocumentSymbol) bool

	// declare if the language has impl symbol
	// if it return true, the ImplSymbol() will be called
	HasImplSymbol() bool
	// if a symbol is an impl symbol, return the token index of interface type, receiver type and first-method start (-1 means not found)
	// ortherwise the collector will use FunctionSymbol() as receiver type token index (-1 means not found)
	ImplSymbol(sym DocumentSymbol) (int, int, int)

	// if a symbol is a Function or Method symbol,  return the token index of Receiver (-1 means not found),TypeParameters, InputParameters and Outputs
	FunctionSymbol(sym DocumentSymbol) (int, []int, []int, []int)

	// Handle a unloaded internal symbol, like `lazy_static!` in rust
	GetUnloadedSymbol(from Token, define Location) (string, error)
	// some language may allow local symbols inside another symbol
	ProtectedSymbolKinds() []SymbolKind
}

Detailed implementation used for collect LSP symbols and transform them to UniAST

type Location

type Location struct {
	URI   DocumentURI `json:"uri"`
	Range Range       `json:"range"`
}

func (Location) Include

func (a Location) Include(b Location) bool

func (Location) MarshalJSON

func (l Location) MarshalJSON() ([]byte, error)

func (Location) MarshalText

func (l Location) MarshalText() ([]byte, error)

func (Location) String

func (l Location) String() string

type MarkedString added in v0.2.0

type MarkedString markedString

type Position

type Position lsp.Position

func (Position) Less

func (r Position) Less(s Position) bool

func (Position) String

func (r Position) String() string

type Range

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

func (Range) Include

func (a Range) Include(b Range) bool

func (Range) MarshalJSON

func (r Range) MarshalJSON() ([]byte, error)

func (Range) MarshalText

func (r Range) MarshalText() ([]byte, error)

func (Range) String

func (r Range) String() string

type SemanticTokens

type SemanticTokens struct {
	ResultID string   `json:"resultId"`
	Data     []uint32 `json:"data"`
}

type SemanticTokensFullParams added in v0.2.0

type SemanticTokensFullParams struct {
	TextDocument lsp.TextDocumentIdentifier `json:"textDocument"`
}

type SymbolInformation added in v0.2.0

type SymbolInformation struct {
	Name          string     `json:"name"`
	Kind          SymbolKind `json:"kind"`
	Location      Location   `json:"location"`
	ContainerName string     `json:"containerName,omitempty"`
}

type SymbolKind

type SymbolKind = lsp.SymbolKind
const (
	SKUnknown       SymbolKind = 1
	SKFile          SymbolKind = 1
	SKModule        SymbolKind = 2
	SKNamespace     SymbolKind = 3
	SKPackage       SymbolKind = 4
	SKClass         SymbolKind = 5
	SKMethod        SymbolKind = 6
	SKProperty      SymbolKind = 7
	SKField         SymbolKind = 8
	SKConstructor   SymbolKind = 9
	SKEnum          SymbolKind = 10
	SKInterface     SymbolKind = 11
	SKFunction      SymbolKind = 12
	SKVariable      SymbolKind = 13
	SKConstant      SymbolKind = 14
	SKString        SymbolKind = 15
	SKNumber        SymbolKind = 16
	SKBoolean       SymbolKind = 17
	SKArray         SymbolKind = 18
	SKObject        SymbolKind = 19
	SKKey           SymbolKind = 20
	SKNull          SymbolKind = 21
	SKEnumMember    SymbolKind = 22
	SKStruct        SymbolKind = 23
	SKEvent         SymbolKind = 24
	SKOperator      SymbolKind = 25
	SKTypeParameter SymbolKind = 26
)

The SymbolKind values are defined at https://microsoft.github.io/language-server-protocol/specification.

type SymbolRole added in v0.2.0

type SymbolRole int
const (
	DEFINITION SymbolRole = 1
	REFERENCE  SymbolRole = 2
)

type TextDocumentIdentifier added in v0.2.0

type TextDocumentIdentifier struct {
	/**
	 * The text document's URI.
	 */
	URI DocumentURI `json:"uri"`
}

type TextDocumentItem

type TextDocumentItem struct {
	URI         DocumentURI               `json:"uri"`
	LanguageID  string                    `json:"languageId"`
	Version     int                       `json:"version"`
	Text        string                    `json:"text"`
	LineCounts  []int                     `json:"-"`
	Symbols     map[Range]*DocumentSymbol `json:"-"`
	Definitions map[Position][]Location   `json:"-"`
}

type TextDocumentPositionParams added in v0.2.0

type TextDocumentPositionParams struct {
	/**
	 * The text document.
	 */
	TextDocument TextDocumentIdentifier `json:"textDocument"`

	/**
	 * The position inside the text document.
	 */
	Position Position `json:"position"`
}

type Token

type Token struct {
	Location  Location `json:"location"`
	Type      string   `json:"type"`
	Modifiers []string `json:"modifiers"`
	Text      string   `json:"text"`
}

func (*Token) String

func (t *Token) String() string

type TypeHierarchyItem added in v0.2.0

type TypeHierarchyItem struct {
	Name           string      `json:"name"`
	Kind           SymbolKind  `json:"kind"`
	Detail         string      `json:"detail,omitempty"`
	URI            DocumentURI `json:"uri"`
	Range          Range       `json:"range"`
	SelectionRange Range       `json:"selectionRange"`
	Data           interface{} `json:"data,omitempty"`
}

TypeHierarchyItem represents a node in the type hierarchy tree.

@since 3.17.0

type WorkspaceSymbolParams added in v0.2.0

type WorkspaceSymbolParams struct {
	Query string `json:"query"`
	Limit int    `json:"limit"`
}

Jump to

Keyboard shortcuts

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