protocol

package
v0.2.8 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package protocol provides definitions for the Language Server Protocol (LSP). It includes structures and constants for common LSP operations and types.

Index

Constants

View Source
const (
	Initialize                     = "initialize"
	Initialized                    = "initialized"
	Shutdown                       = "shutdown"
	Exit                           = "exit"
	TextDocumentDidOpen            = "textDocument/didOpen"
	TextDocumentDidChange          = "textDocument/didChange"
	TextDocumentDidSave            = "textDocument/didSave"
	TextDocumentDidClose           = "textDocument/didClose"
	TextDocumentCompletion         = "textDocument/completion"
	TextDocumentHover              = "textDocument/hover"
	TextDocumentDefinition         = "textDocument/definition"
	TextDocumentReferences         = "textDocument/references"
	TextDocumentDocumentSymbol     = "textDocument/documentSymbol"
	TextDocumentFormatting         = "textDocument/formatting"
	TextDocumentRename             = "textDocument/rename"
	TextDocumentCodeAction         = "textDocument/codeAction"
	TextDocumentSemanticTokensFull = "textDocument/semanticTokens/full"
	TextDocumentPublishDiagnostics = "textDocument/publishDiagnostics"
)

The following constants define method names for LSP requests and notifications.

Variables

This section is empty.

Functions

func BuildTokens

func BuildTokens(tokens []Token) []uint32

BuildTokens builds a list of sorted tokens from a list of tokens.

func CompareToken

func CompareToken(a, b Token) int

CompareToken compares two tokens.

func PositionFor

func PositionFor(pos int, doc *document.Document) (line, col int)

PositionFor returns the line and column for a position in a document.

func Register

func Register(extension string, handler Handler)

Register registers a handler for a file extension.

func ToJSON

func ToJSON(v any) json.RawMessage

func TokenModifiers

func TokenModifiers() []string

func TokenTypes

func TokenTypes() []string

TokenTypes returns a list of token types.

Types

type AddTokenHook

type AddTokenHook func(tokens *[]Token, doc *document.Document, lit string, pos int, tokenType TokenType) bool

AddTokenHook is a hook that is called for each token added. If the hook returns true, the token is not added.

type ClientCapabilities

type ClientCapabilities struct {
	Workspace    WorkspaceClientCapabilities    `json:"workspace,omitempty"`
	TextDocument TextDocumentClientCapabilities `json:"textDocument,omitempty"`
	Window       WindowClientCapabilities       `json:"window,omitempty"`
}

ClientCapabilities represents the capabilities provided by the client.

type CodeActionContext

type CodeActionContext struct {
	Diagnostics []Diagnostic `json:"diagnostics"`
	Only        []string     `json:"only,omitempty"`
}

CodeActionContext represents the context of a code action request.

type CodeActionParams

type CodeActionParams struct {
	TextDocument TextDocumentIdentifier `json:"textDocument"`
	Range        Range                  `json:"range"`
	Context      CodeActionContext      `json:"context"`
}

CodeActionParams represents the parameters of a textDocument/codeAction request.

type Command

type Command struct {
	Title     string `json:"title"`
	Command   string `json:"command"`
	Arguments []any  `json:"arguments,omitempty"`
}

Command represents a reference to a command.

type CompletionClientCapabilities

type CompletionClientCapabilities struct {
	DynamicRegistration bool                             `json:"dynamicRegistration,omitempty"`
	CompletionItem      CompletionItemClientCapabilities `json:"completionItem,omitempty"`
}

CompletionClientCapabilities represents the client capabilities for completion.

type CompletionContext

type CompletionContext struct {
	TriggerKind      CompletionTriggerKind `json:"triggerKind"`
	TriggerCharacter string                `json:"triggerCharacter,omitempty"`
}

CompletionContext represents additional information about the context in which a completion request is triggered.

type CompletionItem

type CompletionItem struct {
	Label               string             `json:"label"`
	Kind                CompletionItemKind `json:"kind,omitempty"`
	Detail              string             `json:"detail,omitempty"`
	Documentation       json.RawMessage    `json:"documentation,omitempty"`
	Deprecated          bool               `json:"deprecated,omitempty"`
	Preselect           bool               `json:"preselect,omitempty"`
	SortText            string             `json:"sortText,omitempty"`
	FilterText          string             `json:"filterText,omitempty"`
	InsertText          string             `json:"insertText,omitempty"`
	InsertTextFormat    InsertTextFormat   `json:"insertTextFormat,omitempty"`
	TextEdit            *TextEdit          `json:"textEdit,omitempty"`
	AdditionalTextEdits []TextEdit         `json:"additionalTextEdits,omitempty"`
	CommitCharacters    []string           `json:"commitCharacters,omitempty"`
	Command             *Command           `json:"command,omitempty"`
	Data                any                `json:"data,omitempty"`
}

CompletionItem represents a completion item.

type CompletionItemClientCapabilities

type CompletionItemClientCapabilities struct {
	SnippetSupport          bool     `json:"snippetSupport,omitempty"`
	CommitCharactersSupport bool     `json:"commitCharactersSupport,omitempty"`
	DocumentationFormat     []string `json:"documentationFormat,omitempty"`
	DeprecatedSupport       bool     `json:"deprecatedSupport,omitempty"`
	PreselectSupport        bool     `json:"preselectSupport,omitempty"`
}

CompletionItemClientCapabilities represents the client capabilities for completion items.

type CompletionItemKind

type CompletionItemKind int

CompletionItemKind represents the kind of a completion item.

type CompletionList

type CompletionList struct {
	IsIncomplete bool             `json:"isIncomplete"`
	Items        []CompletionItem `json:"items"`
}

CompletionList represents the result of a completion request.

type CompletionOptions

type CompletionOptions struct {
	ResolveProvider   bool     `json:"resolveProvider,omitempty"`
	TriggerCharacters []string `json:"triggerCharacters,omitempty"`
}

CompletionOptions represents the options for completion support.

type CompletionParams

type CompletionParams struct {
	TextDocumentPositionParams
	Context *CompletionContext `json:"context,omitempty"`
}

CompletionParams represents the parameters of a textDocument/completion request.

type CompletionTriggerKind

type CompletionTriggerKind int

CompletionTriggerKind represents how a completion was triggered.

const (
	CompletionTriggerInvoked                         CompletionTriggerKind = 1
	CompletionTriggerTriggerCharacter                CompletionTriggerKind = 2
	CompletionTriggerTriggerForIncompleteCompletions CompletionTriggerKind = 3
)

type DefinitionParams

type DefinitionParams struct {
	TextDocumentPositionParams
}

DefinitionParams represents the parameters of a textDocument/definition request.

type Diagnostic

type Diagnostic struct {
	Range              Range                          `json:"range"`
	Severity           int                            `json:"severity,omitempty"`
	Code               any                            `json:"code,omitempty"`
	Source             string                         `json:"source,omitempty"`
	Message            string                         `json:"message"`
	Tags               []int                          `json:"tags,omitempty"`
	RelatedInformation []DiagnosticRelatedInformation `json:"relatedInformation,omitempty"`
}

Diagnostic represents a diagnostic, such as a compiler error or warning.

type DiagnosticRelatedInformation

type DiagnosticRelatedInformation struct {
	Location Location `json:"location"`
	Message  string   `json:"message"`
}

DiagnosticRelatedInformation represents a related piece of information for a diagnostic.

type DocumentFormattingParams

type DocumentFormattingParams struct {
	TextDocument TextDocumentIdentifier `json:"textDocument"`
	Options      FormattingOptions      `json:"options"`
}

DocumentFormattingParams represents the parameters of a textDocument/formatting request.

type DocumentManager

type DocumentManager interface {
	UpdateDocument(doc *document.Document)
	GetDocument(uri string) *document.Document
	RemoveDocument(uri string)
}

DocumentManager is the interface that must be implemented by a document manager.

type DocumentSymbolParams

type DocumentSymbolParams struct {
	TextDocument TextDocumentIdentifier `json:"textDocument"`
}

DocumentSymbolParams represents the parameters of a textDocument/documentSymbol request.

type FormattingOptions

type FormattingOptions struct {
	TabSize      int  `json:"tabSize"`
	InsertSpaces bool `json:"insertSpaces"`
}

FormattingOptions represents options for formatting.

type Handler

type Handler interface {
	// HandleSemanticTokens handles a "textDocument/semanticTokens/full" request.
	HandleSemanticTokens(context.Context, DocumentManager, *document.Document) (any, error)
}

Handler is the interface that must be implemented by a language server handler.

func Lookup

func Lookup(extension string) Handler

Lookup returns the handler for a file extension.

type Hover

type Hover struct {
	Contents json.RawMessage `json:"contents"`
	Range    *Range          `json:"range,omitempty"`
}

Hover represents the result of a hover request.

type HoverClientCapabilities

type HoverClientCapabilities struct {
	DynamicRegistration bool     `json:"dynamicRegistration,omitempty"`
	ContentFormat       []string `json:"contentFormat,omitempty"`
}

HoverClientCapabilities represents the client capabilities for hover.

type HoverParams

type HoverParams struct {
	TextDocumentPositionParams
}

HoverParams represents the parameters of a textDocument/hover request.

type InitializeParams

type InitializeParams struct {
	ProcessID             int                `json:"processId"`             // The process ID of the parent process that started the server.
	RootURI               string             `json:"rootUri"`               // The rootUri of the workspace.
	InitializationOptions any                `json:"initializationOptions"` // User provided initialization options.
	Capabilities          ClientCapabilities `json:"capabilities"`          // The capabilities provided by the client (editor or tool).
}

InitializeParams represents the parameters sent in an initialize request.

type InitializeResult

type InitializeResult struct {
	Capabilities ServerCapabilities `json:"capabilities"` // The capabilities the language server provides.
}

InitializeResult represents the result of an initialize request.

type InsertTextFormat

type InsertTextFormat int

InsertTextFormat represents the format of the insert text.

const (
	InsertTextFormatPlainText InsertTextFormat = 1
	InsertTextFormatSnippet   InsertTextFormat = 2
)

type Location

type Location struct {
	URI   string `json:"uri"`   // The text document's URI.
	Range Range  `json:"range"` // The location's range.
}

Location represents a location inside a resource, such as a line inside a text file.

type MarkupContent

type MarkupContent struct {
	// Kind is the type of the MarkupContent.
	// It can be "plaintext" or "markdown".
	Kind string `json:"kind"`

	// Value is the actual content value.
	// This will be the text shown to the user, with the format depending on the "kind".
	Value string `json:"value"`
}

MarkupContent represents a string value with a specific content type.

type Position

type Position struct {
	Line      int `json:"line"`      // Line position in a document (zero-based).
	Character int `json:"character"` // Character offset on a line in a document (zero-based).
}

Position represents a position in a text document.

type PublishDiagnosticsParams

type PublishDiagnosticsParams struct {
	URI         string       `json:"uri"`
	Diagnostics []Diagnostic `json:"diagnostics"`
}

PublishDiagnosticsParams represents the parameters of a textDocument/publishDiagnostics notification.

type Range

type Range struct {
	Start Position `json:"start"` // The range's start position.
	End   Position `json:"end"`   // The range's end position.
}

Range represents a range in a text document.

type ReferenceContext

type ReferenceContext struct {
	IncludeDeclaration bool `json:"includeDeclaration"`
}

ReferenceContext represents the context of a reference request.

type ReferenceParams

type ReferenceParams struct {
	TextDocumentPositionParams
	Context ReferenceContext `json:"context"`
}

ReferenceParams represents the parameters of a textDocument/references request.

type RenameParams

type RenameParams struct {
	TextDocument TextDocumentIdentifier `json:"textDocument"`
	Position     Position               `json:"position"`
	NewName      string                 `json:"newName"`
}

RenameParams represents the parameters of a textDocument/rename request.

type SemanticTokensLegend

type SemanticTokensLegend struct {
	TokenTypes     []string `json:"tokenTypes"`
	TokenModifiers []string `json:"tokenModifiers"`
}

SemanticTokensLegend represents the legend for semantic tokens.

type SemanticTokensOptions

type SemanticTokensOptions struct {
	Legend SemanticTokensLegend `json:"legend"`
	Range  bool                 `json:"range,omitempty"`
	Full   bool                 `json:"full,omitempty"`
}

SemanticTokensOptions represents the options for semantic tokens support.

type SemanticTokensParams

type SemanticTokensParams struct {
	TextDocument TextDocumentIdentifier `json:"textDocument"`
}

SemanticTokensParams represents the parameters of a textDocument/semanticTokens/full request.

type SemanticTokensResult

type SemanticTokensResult struct {
	// The result id. If provided and clients support delta updating, this id
	// can be sent in a semantic token request to request delta updates.
	ResultID string `json:"resultId,omitempty"`

	// The actual tokens. For a detailed description of the format please refer
	// to the LSP spec at:
	// https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#textDocument_semanticTokens
	Data []uint32 `json:"data"`
}

SemanticTokens represents the result of a textDocument/semantic SemanticTokens represents the result of a textDocument/semanticTokens/full request.

type ServerCapabilities

type ServerCapabilities struct {
	TextDocumentSync           int                    `json:"textDocumentSync,omitempty"`
	CompletionProvider         *CompletionOptions     `json:"completionProvider,omitempty"`
	HoverProvider              bool                   `json:"hoverProvider,omitempty"`
	DefinitionProvider         bool                   `json:"definitionProvider,omitempty"`
	ReferencesProvider         bool                   `json:"referencesProvider,omitempty"`
	DocumentSymbolProvider     bool                   `json:"documentSymbolProvider,omitempty"`
	WorkspaceSymbolProvider    bool                   `json:"workspaceSymbolProvider,omitempty"`
	CodeActionProvider         bool                   `json:"codeActionProvider,omitempty"`
	DocumentFormattingProvider bool                   `json:"documentFormattingProvider,omitempty"`
	RenameProvider             bool                   `json:"renameProvider,omitempty"`
	SemanticTokensProvider     *SemanticTokensOptions `json:"semanticTokensProvider,omitempty"`
}

ServerCapabilities represents the capabilities provided by the language server.

type TextDocumentClientCapabilities

type TextDocumentClientCapabilities struct {
	Synchronization TextDocumentSyncClientCapabilities `json:"synchronization,omitempty"`
	Completion      CompletionClientCapabilities       `json:"completion,omitempty"`
	Hover           HoverClientCapabilities            `json:"hover,omitempty"`
}

TextDocumentClientCapabilities represents the text document-specific client capabilities.

type TextDocumentContentChangeEvent

type TextDocumentContentChangeEvent struct {
	Range       *Range `json:"range,omitempty"`       // The range of the document that changed.
	RangeLength int    `json:"rangeLength,omitempty"` // The length of the range that got replaced.
	Text        string `json:"text"`                  // The new text of the range/document.
}

TextDocumentContentChangeEvent represents a change to a text document.

type TextDocumentIdentifier

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

TextDocumentIdentifier identifies a text document using its URI.

type TextDocumentItem

type TextDocumentItem struct {
	URI        string `json:"uri"`        // The text document's URI.
	LanguageID string `json:"languageId"` // The text document's language identifier.
	Version    int    `json:"version"`    // The version number of this document (it will increase after each change, including undo/redo).
	Text       string `json:"text"`       // The content of the opened text document.
}

TextDocumentItem represents an open text document.

type TextDocumentPositionParams

type TextDocumentPositionParams struct {
	TextDocument TextDocumentIdentifier `json:"textDocument"`
	Position     Position               `json:"position"`
}

TextDocumentPositionParams represents a text document and a position inside that document.

type TextDocumentSyncClientCapabilities

type TextDocumentSyncClientCapabilities struct {
	DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
	WillSave            bool `json:"willSave,omitempty"`
	WillSaveWaitUntil   bool `json:"willSaveWaitUntil,omitempty"`
	DidSave             bool `json:"didSave,omitempty"`
}

TextDocumentSyncClientCapabilities represents the client capabilities for document synchronization.

type TextEdit

type TextEdit struct {
	Range   Range  `json:"range"`
	NewText string `json:"newText"`
}

TextEdit represents a text edit applicable to a text document.

type Token

type Token struct {
	Pos       uint32    // 0-based character offset
	Line      uint32    // 0-based line number
	Character uint32    // 0-based character offset
	Length    uint32    // length of the token
	TokenType TokenType // token type
	TokenMod  uint32    // token modifier
}

Token represents a token in a document.

func AddToken

func AddToken[Pos constraints.Integer](tokens *[]Token, doc *document.Document, lit string, position Pos, tokenType TokenType, hooks ...AddTokenHook) []Token

AddToken adds a token to the list of tokens. It splits the token by newlines and adds each line as a separate token. The hook is called for each token added. If the hook returns true, the token is not added.

func (Token) String

func (t Token) String() string

String returns a string representation of the token.

type TokenModifier

type TokenModifier int
const (
	TokenModifierDeclaration TokenModifier = iota // declaration

)

func (TokenModifier) String

func (i TokenModifier) String() string

type TokenType

type TokenType uint32

TokenType constants aligned with VSCode's standard semantic token types

const (
	TokenTypeText          TokenType = iota // text
	TokenTypeNamespace                      // namespace
	TokenTypeType                           // type
	TokenTypeClass                          // class
	TokenTypeEnum                           // enum
	TokenTypeInterface                      // interface
	TokenTypeStruct                         // struct
	TokenTypeTypeParameter                  // typeParameter
	TokenTypeParameter                      // parameter
	TokenTypeVariable                       // variable
	TokenTypeProperty                       // property
	TokenTypeEnumMember                     // enumMember
	TokenTypeEvent                          // event
	TokenTypeFunction                       // function
	TokenTypeMethod                         // method
	TokenTypeMacro                          // macro
	TokenTypeKeyword                        // keyword
	TokenTypeModifier                       // modifier
	TokenTypeComment                        // comment
	TokenTypeString                         // string
	TokenTypeNumber                         // number
	TokenTypeRegexp                         // regexp
	TokenTypeOperator                       // operator

	// token aliases
	TokenTypeConst               = TokenTypeEnumMember // const
	TokenTypeAnnotation          = TokenTypeRegexp     // annotation
	TokenTypeAnnotationParameter = TokenTypeMacro      // annotationParameter
)

func (TokenType) String

func (i TokenType) String() string

type VersionedTextDocumentIdentifier

type VersionedTextDocumentIdentifier struct {
	TextDocumentIdentifier
	Version int `json:"version"` // The version number of this document.
}

VersionedTextDocumentIdentifier identifies a specific version of a text document.

type WindowClientCapabilities

type WindowClientCapabilities struct {
	WorkDoneProgress bool `json:"workDoneProgress,omitempty"`
}

WindowClientCapabilities represents the window-specific client capabilities.

type WorkspaceClientCapabilities

type WorkspaceClientCapabilities struct {
	ApplyEdit bool `json:"applyEdit,omitempty"`
}

WorkspaceClientCapabilities represents the workspace-specific client capabilities.

Jump to

Keyboard shortcuts

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