lsp

package
v0.0.0-...-69e9adf Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2025 License: Apache-2.0 Imports: 19 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 GetDistance

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

func PositionOffset

func PositionOffset(text string, pos Position) int

func RelativePostionWithLines

func RelativePostionWithLines(lines []int, textPos 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
}

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"`
}

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 LSPClient

type LSPClient struct {
	*jsonrpc2.Conn

	ClientOptions
	// contains filtered or unexported fields
}

func NewLSPClient

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

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) 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) 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) WaitFirstNotify

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

func (*LSPClient) WorkspaceSymbols

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

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) (string, string, error)

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

	// 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)
}

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 ModulePatcher

type ModulePatcher interface {
	// Patch is called after collect all symbol
	Patch(ast *uniast.Module)
}

ModulePatcher supplements some information for module

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 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 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 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

Jump to

Keyboard shortcuts

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