lsp

package
v0.31.0 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var InstallCommands = map[string]InstallInfo{

	"go": {
		ServerKey:   "go",
		PackageName: "gopls",
		Commands:    []string{"go install golang.org/x/tools/gopls@latest"},
	},
	"typescript": {
		ServerKey:   "typescript",
		PackageName: "vtsls",
		Commands:    []string{"npm i -g @vtsls/language-server typescript"},
	},
	"python": {
		ServerKey:   "python",
		PackageName: "pyright",
		Commands:    []string{"pip install pyright", "npm i -g pyright"},
	},
	"rust": {
		ServerKey:   "rust",
		PackageName: "rust-analyzer",
		Commands:    []string{"rustup component add rust-analyzer"},
	},

	"java": {
		ServerKey:   "java",
		PackageName: "jdtls",
		Commands:    []string{"brew install jdtls"},
	},
	"c": {
		ServerKey:   "c",
		PackageName: "clangd",
		Commands:    []string{"brew install llvm", "apt install clangd"},
	},
	"cpp": {
		ServerKey:   "cpp",
		PackageName: "clangd",
		Commands:    []string{"brew install llvm", "apt install clangd"},
	},
	"ruby": {
		ServerKey:   "ruby",
		PackageName: "solargraph",
		Commands:    []string{"gem install solargraph"},
	},
	"kotlin": {
		ServerKey:   "kotlin",
		PackageName: "kotlin-language-server",
		Commands:    []string{"brew install kotlin-language-server"},
	},
	"swift": {
		ServerKey:   "swift",
		PackageName: "sourcekit-lsp",
		Commands:    []string{},
	},
	"csharp": {
		ServerKey:   "csharp",
		PackageName: "csharp-ls",
		Commands:    []string{"dotnet tool install --global csharp-ls"},
	},
	"scala": {
		ServerKey:   "scala",
		PackageName: "metals",
		Commands:    []string{"brew install coursier/formulas/coursier && cs install metals"},
	},
	"php": {
		ServerKey:   "php",
		PackageName: "intelephense",
		Commands:    []string{"npm i -g intelephense"},
	},
	"elixir": {
		ServerKey:   "elixir",
		PackageName: "elixir-ls",
		Commands:    []string{"brew install elixir-ls"},
	},
	"lua": {
		ServerKey:   "lua",
		PackageName: "lua-language-server",
		Commands:    []string{"brew install lua-language-server", "apt install lua-language-server"},
	},

	"css": {
		ServerKey:   "css",
		PackageName: "vscode-css-language-server",
		Commands:    []string{"npm i -g vscode-langservers-extracted"},
	},
	"html": {
		ServerKey:   "html",
		PackageName: "vscode-html-language-server",
		Commands:    []string{"npm i -g vscode-langservers-extracted"},
	},
	"vue": {
		ServerKey:   "vue",
		PackageName: "@vue/language-server",
		Commands:    []string{"npm i -g @vue/language-server"},
	},
	"svelte": {
		ServerKey:   "svelte",
		PackageName: "svelte-language-server",
		Commands:    []string{"npm i -g svelte-language-server"},
	},

	"yaml": {
		ServerKey:   "yaml",
		PackageName: "yaml-language-server",
		Commands:    []string{"npm i -g yaml-language-server"},
	},
	"toml": {
		ServerKey:   "toml",
		PackageName: "taplo",
		Commands:    []string{"cargo install taplo-cli --locked"},
	},
	"sql": {
		ServerKey:   "sql",
		PackageName: "sqls",
		Commands:    []string{"go install github.com/lighttiger2505/sqls@latest"},
	},
	"bash": {
		ServerKey:   "bash",
		PackageName: "bash-language-server",
		Commands:    []string{"npm i -g bash-language-server"},
	},
	"markdown": {
		ServerKey:   "markdown",
		PackageName: "marksman",
		Commands:    []string{"brew install marksman"},
	},
}

InstallCommands はサーバーキー -> インストール情報のマッピング

Functions

func DetectLanguage

func DetectLanguage(path string) string

DetectLanguage returns the language ID for a file based on its extension

func FileToURI

func FileToURI(path string) string

FileToURI converts a file path to an LSP file:// URI

func GetAllInstallInfos

func GetAllInstallInfos() map[string]InstallInfo

GetAllInstallInfos は全てのインストール情報を返す

func GetLanguageExtensions

func GetLanguageExtensions(serverKey string) []string

GetLanguageExtensions は指定されたサーバーキーに対応する拡張子を返す

func IsServerInstalled

func IsServerInstalled(serverKey string, configs map[string]ServerConfig) bool

IsServerInstalled はLSPサーバーがインストールされているかチェック

func LanguageServerKey

func LanguageServerKey(language string) string

LanguageServerKey returns the key used in LSP configuration for a language This maps language IDs to their server configuration keys

func RunInstall

func RunInstall(serverKey string) error

RunInstall はLSPサーバーをインストール(最初のコマンドを実行) 成功した場合は nil、失敗した場合はエラーを返す

func URIToFile

func URIToFile(uri string) string

URIToFile converts an LSP file:// URI to a file path

Types

type Client

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

Client manages multiple LSP servers (one per language)

func NewClient

func NewClient(rootPath string) *Client

NewClient creates a new LSP client

func (*Client) Close

func (c *Client) Close()

Close shuts down all LSP servers

func (*Client) FindReferences

func (c *Client) FindReferences(ctx context.Context, filePath string, line, character int, includeDeclaration bool) ([]Location, error)

FindReferences finds all references to a symbol

func (*Client) GetDiagnostics

func (c *Client) GetDiagnostics(ctx context.Context, filePath string) ([]Diagnostic, error)

GetDiagnostics gets diagnostics (errors, warnings) for a file

func (*Client) GetHover

func (c *Client) GetHover(ctx context.Context, filePath string, line, character int) (*HoverResult, error)

GetHover gets hover information for a symbol

func (*Client) GetServer

func (c *Client) GetServer(ctx context.Context, language string) (*Server, error)

GetServer returns the server for a language, starting it lazily if needed

func (*Client) GetServerForFile

func (c *Client) GetServerForFile(ctx context.Context, filePath string) (*Server, error)

GetServerForFile returns the appropriate server for a file path

func (*Client) GoToDefinition

func (c *Client) GoToDefinition(ctx context.Context, filePath string, line, character int) ([]Location, error)

GoToDefinition finds the definition of a symbol

func (*Client) Rename

func (c *Client) Rename(ctx context.Context, filePath string, line, character int, newName string) (*WorkspaceEdit, error)

Rename renames a symbol at the given position to a new name

func (*Client) SetConfigs

func (c *Client) SetConfigs(configs map[string]ServerConfig)

SetConfigs sets the server configurations

func (*Client) Status

func (c *Client) Status() map[string]string

Status returns the status of all configured servers

type ClientCapabilities

type ClientCapabilities struct {
	TextDocument TextDocumentClientCapabilities `json:"textDocument,omitempty"`
}

ClientCapabilities represents the client's capabilities

type DefinitionCapability

type DefinitionCapability struct {
	DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
}

DefinitionCapability represents definition capability

type Diagnostic

type Diagnostic struct {
	Range    Range              `json:"range"`
	Severity DiagnosticSeverity `json:"severity,omitempty"`
	Code     interface{}        `json:"code,omitempty"` // string or number
	Source   string             `json:"source,omitempty"`
	Message  string             `json:"message"`
}

Diagnostic represents a diagnostic (error, warning, info, hint)

type DiagnosticSeverity

type DiagnosticSeverity int

DiagnosticSeverity represents the severity of a diagnostic

const (
	DiagnosticSeverityError       DiagnosticSeverity = 1
	DiagnosticSeverityWarning     DiagnosticSeverity = 2
	DiagnosticSeverityInformation DiagnosticSeverity = 3
	DiagnosticSeverityHint        DiagnosticSeverity = 4
)

type DidCloseTextDocumentParams

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

DidCloseTextDocumentParams are the parameters for the didClose notification

type DidOpenTextDocumentParams

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

DidOpenTextDocumentParams are the parameters for the didOpen notification

type HoverCapability

type HoverCapability struct {
	DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
}

HoverCapability represents hover capability

type HoverParams

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

HoverParams are the parameters for the hover request

type HoverResult

type HoverResult struct {
	Contents MarkupContent `json:"contents"`
	Range    *Range        `json:"range,omitempty"`
}

HoverResult is the result of a hover request

type InitializeParams

type InitializeParams struct {
	ProcessID    int                `json:"processId"`
	RootURI      string             `json:"rootUri"`
	Capabilities ClientCapabilities `json:"capabilities"`
}

InitializeParams are the parameters for the initialize request

type InitializeResult

type InitializeResult struct {
	Capabilities ServerCapabilities `json:"capabilities"`
}

InitializeResult is the result of the initialize request

type InstallInfo

type InstallInfo struct {
	ServerKey   string   // サーバーキー(go, typescript等)
	PackageName string   // パッケージ名(gopls, vtsls等)
	Commands    []string // インストールコマンド(複数可: 1つ目を優先)
}

InstallInfo はLSPサーバーのインストール情報

func GetInstallInfo

func GetInstallInfo(serverKey string) (InstallInfo, bool)

GetInstallInfo はサーバーキーからインストール情報を取得

type LanguageInfo

type LanguageInfo struct {
	ServerKey  string   // LSP設定のキー(go, typescript等)
	Extensions []string // 検出された拡張子
	FileCount  int      // ファイル数
}

LanguageInfo は検出された言語の情報

func DetectProjectLanguages

func DetectProjectLanguages(rootDir string) ([]LanguageInfo, error)

DetectProjectLanguages はプロジェクト内の言語を検出

type Location

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

Location represents a location in a text document

type MarkupContent

type MarkupContent struct {
	Kind  string `json:"kind"` // "plaintext" or "markdown"
	Value string `json:"value"`
}

MarkupContent represents markup content (plaintext or markdown)

type Notification

type Notification struct {
	JSONRPC string      `json:"jsonrpc"`
	Method  string      `json:"method"`
	Params  interface{} `json:"params,omitempty"`
}

Notification represents a JSON-RPC 2.0 notification (no ID, no response expected)

type Position

type Position struct {
	Line      int `json:"line"`      // 0-indexed line number
	Character int `json:"character"` // 0-indexed character offset
}

Position represents a position in a text document (0-indexed)

type PublishDiagnosticsParams

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

PublishDiagnosticsParams are the parameters for the publishDiagnostics notification

type Range

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

Range represents a text range in a document

type ReferenceContext

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

ReferenceContext represents the context for a reference request

type ReferenceParams

type ReferenceParams struct {
	TextDocument TextDocumentIdentifier `json:"textDocument"`
	Position     Position               `json:"position"`
	Context      ReferenceContext       `json:"context"`
}

ReferenceParams are the parameters for the references request

type ReferencesCapability

type ReferencesCapability struct {
	DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
}

ReferencesCapability represents references capability

type RenameParams

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

RenameParams are the parameters for the rename request

type Request

type Request struct {
	JSONRPC string      `json:"jsonrpc"`
	ID      int         `json:"id"`
	Method  string      `json:"method"`
	Params  interface{} `json:"params,omitempty"`
}

Request represents a JSON-RPC 2.0 request

type Response

type Response struct {
	JSONRPC string          `json:"jsonrpc"`
	ID      int             `json:"id"`
	Result  json.RawMessage `json:"result,omitempty"`
	Error   *ResponseError  `json:"error,omitempty"`
}

Response represents a JSON-RPC 2.0 response

type ResponseError

type ResponseError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Data    any    `json:"data,omitempty"`
}

ResponseError represents a JSON-RPC 2.0 error

type Server

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

Server manages a single LSP server process

func NewServer

func NewServer(name string) *Server

NewServer creates a new LSP server instance (does not start yet)

func (*Server) Call

func (s *Server) Call(ctx context.Context, method string, params interface{}) (*Response, error)

Call sends a JSON-RPC request and waits for the response

func (*Server) ClearDiagnostics

func (s *Server) ClearDiagnostics(filePath string)

ClearDiagnostics clears diagnostics for a file

func (*Server) Close

func (s *Server) Close() error

Close shuts down the LSP server gracefully

func (*Server) CloseDocument

func (s *Server) CloseDocument(path string) error

CloseDocument sends a textDocument/didClose notification

func (*Server) GetLastDiagnostics

func (s *Server) GetLastDiagnostics(filePath string) []Diagnostic

GetLastDiagnostics returns the last received diagnostics for a file

func (*Server) IsRunning

func (s *Server) IsRunning() bool

IsRunning returns true if the server is initialized and running

func (*Server) Name

func (s *Server) Name() string

Name returns the server name

func (*Server) OpenDocument

func (s *Server) OpenDocument(path, languageID, content string) error

OpenDocument sends a textDocument/didOpen notification

func (*Server) Start

func (s *Server) Start(ctx context.Context, command string, args []string, rootURI string) error

Start launches the LSP server process and initializes it

type ServerCapabilities

type ServerCapabilities struct {
	ReferencesProvider bool `json:"referencesProvider,omitempty"`
	DefinitionProvider bool `json:"definitionProvider,omitempty"`
	HoverProvider      bool `json:"hoverProvider,omitempty"`
	TextDocumentSync   int  `json:"textDocumentSync,omitempty"` // 0=None, 1=Full, 2=Incremental
}

ServerCapabilities represents the server's capabilities

type ServerConfig

type ServerConfig struct {
	Command  string
	Args     []string
	Disabled bool
}

ServerConfig holds configuration for an LSP server

type TextDocumentClientCapabilities

type TextDocumentClientCapabilities struct {
	References *ReferencesCapability `json:"references,omitempty"`
	Definition *DefinitionCapability `json:"definition,omitempty"`
	Hover      *HoverCapability      `json:"hover,omitempty"`
}

TextDocumentClientCapabilities represents text document capabilities

type TextDocumentIdentifier

type TextDocumentIdentifier struct {
	URI string `json:"uri"`
}

TextDocumentIdentifier identifies a text document

type TextDocumentItem

type TextDocumentItem struct {
	URI        string `json:"uri"`
	LanguageID string `json:"languageId"`
	Version    int    `json:"version"`
	Text       string `json:"text"`
}

TextDocumentItem represents a text document (for didOpen)

type TextDocumentPositionParams

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

TextDocumentPositionParams represents a position in a text document

type TextEdit

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

TextEdit represents a textual edit applicable to a text document

type WorkspaceEdit

type WorkspaceEdit struct {
	Changes map[string][]TextEdit `json:"changes,omitempty"`
}

WorkspaceEdit represents changes to many resources managed in the workspace

Jump to

Keyboard shortcuts

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