sun::lsp

namespace · Source (opens in a new tab)

Provides compiler-backed editor features through the language server protocol.

Classes

Structs

Functions

annotationIn

public · function · Source (opens in a new tab)

const sun::ast::TypeAnnotation * sun::lsp::annotationIn(const ExprAST &node, int offset)

The annotation under the cursor among those written on a node, or null.

Finds the type annotation covering the requested byte offset.

Related: sun::ast::TypeAnnotation, ExprAST

applyPathVariables

public · function · static · Source (opens in a new tab)

static bool sun::lsp::applyPathVariables(const llvm::json::Object &config)

Install manifest path variables ($NAME) from a configuration object's "pathVariables" member ({NAME: dir, ...}).

Replaces the previous set so removed variables disappear. Returns true if the member was present.

byteOffsetFromLspPosition

public · function · Source (opens in a new tab)

int sun::lsp::byteOffsetFromLspPosition(const std::string &text, int line, int character)

Byte offset for a protocol position.

A column past the end of the line stops at the line break; a line past the end of the text stops at the end. A column inside a surrogate pair snaps to the start of that character.

checkNewName

public · function · Source (opens in a new tab)

std::string sun::lsp::checkNewName(const std::string &newName)

Why newName cannot be a symbol name, or empty when it can: it must lex as a single identifier, so keywords and punctuation are refused.

Validates a proposed identifier before applying a rename.

collectTestSpans

public · function · Source (opens in a new tab)

std::vector< TestSpan > sun::lsp::collectTestSpans(const sun::ast::BlockExprAST &ast)

Every test_function in the program, whichever file declares it.

Collects the source ranges occupied by test declarations.

Related: TestSpan, sun::ast::BlockExprAST

collectTests

public · function · Source (opens in a new tab)

std::vector< TestItem > sun::lsp::collectTests(const sun::ast::BlockExprAST &ast, const std::string &documentPath, const std::string &source)

Every test_function declared in the document at documentPath, walking module bodies the way the test runner does so ids match the runner's dotted names.

Collects runnable test declarations and their editor locations.

source is the document's text, used to narrow each declaration span to the name token.

Related: TestItem, sun::ast::BlockExprAST

computeDefinition

public · function · Source (opens in a new tab)

std::optional< SymbolLocation > sun::lsp::computeDefinition(const sun::ast::BlockExprAST &program, const std::string &filePath, const std::string &source, int byteOffset)

Where the symbol at byteOffset was declared: a local, a parameter, a loop variable, a match or catch binding, a module-level function, class, interface, enum, variable or alias, a member behind obj.member, the class behind this, or the type a written annotation names.

Returns the definition location for the symbol at a document offset.

On a declaration's own header the declaration itself is returned. The range covers the declared name. Declarations from a .moon bundle resolve to the library's source file when the bundle recorded it and the file can be read. Returns nothing for literals, operators, statements and whitespace.

Related: SymbolLocation, sun::ast::BlockExprAST

computeHover

public · function · Source (opens in a new tab)

std::optional< Hover > sun::lsp::computeHover(const sun::ast::BlockExprAST &program, const std::string &filePath, const std::string &source, int byteOffset)

Hover text for the construct at byteOffset in an analyzed program: name: T for variables and members, full signatures for functions, class Name for definitions, module Name for module name tokens, and the type alone for literals and operators.

Returns documentation and type details for the symbol under the cursor.

Inside a generic class or function body, types come from the first specialization and are printed in terms of the type parameters. source is the document text; declarations whose types were never resolved (analysis stopped at an earlier error, or a generic that is never used) fall back to the type annotation written there. The comment directly above the symbol's declaration is returned as documentation. Returns nothing for statements and untyped nodes.

Related: Hover, sun::ast::BlockExprAST

computeReferences

public · function · Source (opens in a new tab)

std::vector< SymbolLocation > sun::lsp::computeReferences(const BlockExprAST &program, const std::string &filePath, const std::string &source, int byteOffset, bool includeDeclaration)

Every place the symbol at byteOffset is named, across all files of the analyzed program, sorted by file then offset: uses of a local, parameter, loop variable, match or catch binding, function, class, interface, enum, variant, field or method, including type names written in annotations and implements lists.

Returns references to the symbol selected at a document offset.

With includeDeclaration the declared name is listed too; a declaration from a .moon bundle resolves to the library's source file when the bundle recorded it. Uses inside library code are not listed, since bundles carry no function bodies. An interface member and the class members implementing it are listed together, as one group. Empty for literals, operators, statements and whitespace.

Related: SymbolLocation, BlockExprAST

computeRename

public · function · Source (opens in a new tab)

std::optional< Rename > sun::lsp::computeRename(const sun::ast::BlockExprAST &program, const std::string &filePath, const std::string &source, int byteOffset)

The symbol at byteOffset with every place its name is written, found as computeReferences finds them.

Collects the declaration and use sites needed to rename a symbol.

An interface member and the class members implementing it are renamed together, since renaming one side alone would break the program. A symbol declared in a library (loaded from a .moon bundle) is refused, since its uses inside the library cannot be edited. Nothing when the cursor is not on a symbol.

Related: Rename, sun::ast::BlockExprAST

declarationKey

public · function · Source (opens in a new tab)

DeclarationKey sun::lsp::declarationKey(const Declaration &declaration, const std::string &file)

A location without a file belongs to file.

Builds the key used to group references to the same declaration.

Related: DeclarationKey, Declaration

declarationName

public · function · Source (opens in a new tab)

std::string sun::lsp::declarationName(const ExprAST &node)

Name a module-level declaration is known by, or empty for other nodes.

Returns the unqualified name introduced by a declaration node.

Related: ExprAST

declarationOf

public · function · Source (opens in a new tab)

Declaration sun::lsp::declarationOf(const ExprAST &node)

Extracts declaration information from a syntax node.

Related: Declaration, ExprAST

declarationQualifiedName

public · function · Source (opens in a new tab)

QualifiedName sun::lsp::declarationQualifiedName(const ExprAST &node)

Qualified name the analyzer gave a declaration, or empty.

Returns the declaration name together with its enclosing scopes.

Related: QualifiedName, ExprAST

declarationUnder

public · function · Source (opens in a new tab)

std::optional< Declaration > sun::lsp::declarationUnder(const BlockExprAST &program, const Target &target, int offset, const std::string &source)

The declaration behind the node under the cursor.

Resolves the declaration under a selected source position.

Related: Declaration, BlockExprAST, Target

declaresParameter

public · function · Source (opens in a new tab)

bool sun::lsp::declaresParameter(const sun::ast::PrototypeAST &proto, const std::string &name)

Reports whether a function signature declares the requested parameter.

Related: sun::ast::PrototypeAST

findAnnotatedType

public · function · Source (opens in a new tab)

const ExprAST * sun::lsp::findAnnotatedType(const BlockExprAST &program, const sun::ast::TypeAnnotation &annotation)

The user-defined type an annotation names, looking through ref, pointer and array wrappers when their element has no span of its own.

Resolves the type named by a source annotation.

Related: ExprAST, BlockExprAST, sun::ast::TypeAnnotation

findDeclaration

public · function · Source (opens in a new tab)

const ExprAST * sun::lsp::findDeclaration(const BlockExprAST &program, const std::string &name, const QualifiedName &qualified)

Module-level declaration with this name; a qualified-name match wins over a plain name match when the reference was resolved by the analyzer.

Finds a declaration by its local and qualified names.

Related: ExprAST, BlockExprAST, QualifiedName

findDeclarationAt

public · function · Source (opens in a new tab)

std::optional< Declaration > sun::lsp::findDeclarationAt(const BlockExprAST &program, const std::string &documentPath, const std::string &source, int byteOffset)

Where the symbol at byteOffset was declared: the type a written annotation names, else the declaration behind the node under the cursor.

Finds the declaration associated with a document byte offset.

Related: Declaration, BlockExprAST

findDeclarationOf

public · function · Source (opens in a new tab)

std::optional< Declaration > sun::lsp::findDeclarationOf(const BlockExprAST &program, const std::vector< const ExprAST * > &chain, const ExprAST &node, const DeclarationIndex *declarations=nullptr)

Where the symbol under node was declared, or nothing.

Resolves the declaration referenced by a node in its enclosing scopes.

An optional index must cover the same program and is used for repeated reference lookups.

Related: Declaration, BlockExprAST, ExprAST, DeclarationIndex

findInnermostNodeAt

public · function · Source (opens in a new tab)

const ExprAST * sun::lsp::findInnermostNodeAt(const sun::ast::BlockExprAST &program, const std::string &filePath, int byteOffset)

Innermost node from filePath whose span contains byteOffset, or null.

Finds the most deeply nested syntax node covering a document offset.

Nodes without a span (module wrappers from merged compilation) are looked through; moon import stubs and nodes from other files are skipped.

Related: sun::ast::ExprAST, sun::ast::BlockExprAST

findLocalDeclaration

public · function · Source (opens in a new tab)

std::optional< Declaration > sun::lsp::findLocalDeclaration(const std::vector< const ExprAST * > &chain, const ExprAST &node, const std::string &name)

Declaration of a local name visible at node: the closest earlier var/const/ref in an enclosing block, or an enclosing loop variable.

Searches enclosing lexical scopes for a local declaration.

Parameters have no declaration of their own and yield nothing.

Related: Declaration, ExprAST

findMember

public · function · Source (opens in a new tab)

std::optional< Declaration > sun::lsp::findMember(const ExprAST &definition, const std::string &member)

A member (method, field or variant) inside a definition.

Finds a named member in a type definition.

Related: Declaration, ExprAST

findMemberDeclaration

public · function · Source (opens in a new tab)

std::optional< Declaration > sun::lsp::findMemberDeclaration(const BlockExprAST &program, const ExprAST &object, const std::string &member, const QualifiedName &qualifiedName)

The member behind object.member: a module's item (the analyzer records which module's in qualifiedName), or a field, method or variant of the object's type.

Resolves the named member on the receiver's type.

A match pattern's object carries no type, so a bare name there is looked up as a type.

Related: Declaration, BlockExprAST, ExprAST, QualifiedName

findOccurrences

public · function · Source (opens in a new tab)

Occurrences sun::lsp::findOccurrences(const BlockExprAST &program, const std::string &documentPath, const std::string &source, const std::vector< Declaration > &targets, bool includeDeclaration)

Where each of targets is named, as computeReferences lists them, merged into one sorted list.

Collects source occurrences of the selected declarations.

Declarations outside the tree are not added.

Related: Occurrences, BlockExprAST, Declaration

findParameter

public · function · Source (opens in a new tab)

std::optional< Declaration > sun::lsp::findParameter(const std::vector< const ExprAST * > &chain, const std::string &name)

The nearest enclosing function or lambda declaring name as a parameter.

Finds a named parameter in the enclosing function signatures.

Related: Declaration, ExprAST

findTypeDefinition

public · function · Source (opens in a new tab)

const ExprAST * sun::lsp::findTypeDefinition(const BlockExprAST &program, const sun::types::Type &type)

Definition node (class, interface or enum) behind a type.

Finds the syntax declaration corresponding to a semantic type.

Related: ExprAST, BlockExprAST, sun::types::Type

findWord

public · function · Source (opens in a new tab)

int sun::lsp::findWord(const std::string &text, const std::string &name, size_t from, size_t to)

Offset of the first whole word name in text[from, to), or -1.

Finds a complete identifier within a bounded source range.

firstSpecialization

public · function · Source (opens in a new tab)

const ExprAST * sun::lsp::firstSpecialization(const ExprAST &node, Bindings &bindings)

First specialization of a generic class or function, with its bindings; null when the node is not a generic template or was never used.

Finds a concrete generic instance and its type bindings for editor lookup.

Related: ExprAST, Bindings

forEachAnnotation

public · function · Source (opens in a new tab)

void sun::lsp::forEachAnnotation(const ExprAST &node, const AnnotationFn &fn)

Visits type annotations directly associated with a syntax node.

Related: ExprAST, AnnotationFn

forEachCatchBinding

public · function · Source (opens in a new tab)

void sun::lsp::forEachCatchBinding(const sun::ast::TryCatchExprAST &tryCatch, const CatchBindingFn &fn)

Visits the error bindings introduced by catch clauses.

Related: sun::ast::TryCatchExprAST, CatchBindingFn

isDefinition

public · function · Source (opens in a new tab)

bool sun::lsp::isDefinition(sun::ast::ASTNodeType kind)

Reports whether the syntax-node kind introduces a declaration.

Related: sun::ast::ASTNodeType

lineStart

public · function · Source (opens in a new tab)

size_t sun::lsp::lineStart(const std::string &text, int line)

Byte offset of the first character on a zero-based line (text.size() if the line does not exist).

locate

public · function · Source (opens in a new tab)

std::optional< Target > sun::lsp::locate(const BlockExprAST &program, const std::string &documentPath, int offset)

Node chain at the offset.

Finds the syntax node and ancestor chain at a document offset.

Generic templates are analyzed only through their specializations, so the chain is redirected into the first specialization of any generic class or function it passes through; the clones keep the template's source spans.

Related: Target, BlockExprAST

lspPositionFromByteOffset

public · function · Source (opens in a new tab)

LspPosition sun::lsp::lspPositionFromByteOffset(const std::string &text, int offset)

Protocol position for a byte offset (clamped to the text).

Related: LspPosition

makeSymbolLocation

public · function · Source (opens in a new tab)

SymbolLocation sun::lsp::makeSymbolLocation(const std::string &filePath, const sun::support::Position &range, const std::string &text)

Protocol form of a byte range inside text.

Converts a source span to an editor navigation location.

Related: SymbolLocation, sun::support::Position

memberGroupOf

public · function · Source (opens in a new tab)

MemberGroup sun::lsp::memberGroupOf(const BlockExprAST &program, const Declaration &declaration, const std::string &documentPath)

Finds related member declarations that share a rename operation.

Related: MemberGroup, BlockExprAST, Declaration

nameRange

public · function · Source (opens in a new tab)

Position sun::lsp::nameRange(const sun::support::Position &span, const std::string &name, const std::string &text)

The name token of a declaration: the span itself when it already is the name, otherwise the first whole word name inside it, or the name just before it (a signature span starts at its parenthesis).

Finds the identifier's exact range within its declaration span.

Falls back to an empty range at the start of the span.

Related: sun::support::Position

nameRangeOf

public · function · Source (opens in a new tab)

Position sun::lsp::nameRangeOf(const Declaration &declaration, const std::string &text)

The declared name's range, in the declaration's own file.

Finds the exact name range of a resolved declaration.

Related: sun::support::Position, Declaration

normalizePath

public · function · Source (opens in a new tab)

std::string sun::lsp::normalizePath(const std::string &path)

Canonical form of a path that exists on disk; other paths are unchanged.

Converts a path to the normalized form used for document comparisons.

ownDeclaration

public · function · Source (opens in a new tab)

std::optional< Declaration > sun::lsp::ownDeclaration(const ExprAST &node, int offset, const std::string &source)

The declaration for a cursor on a definition's own header: the definition, or the field, variant, parameter or binding written there.

Finds a declaration introduced at the selected position within a node.

Related: Declaration, ExprAST

parameterRange

public · function · Source (opens in a new tab)

std::optional< Position > sun::lsp::parameterRange(const sun::ast::ExprAST &owner, const std::string &name, const std::string &text)

Where name is declared in a signature: name: or name... after the opening parenthesis.

Finds the source range of a named function parameter.

Related: sun::support::Position, sun::ast::ExprAST

prototypeOf

public · function · Source (opens in a new tab)

const PrototypeAST * sun::lsp::prototypeOf(const sun::ast::ExprAST &node)

Signature of a function or lambda; null for other nodes.

Returns the signature belonging to a function-like syntax node.

Related: sun::ast::PrototypeAST, sun::ast::ExprAST

rangeAt

public · function · Source (opens in a new tab)

Position sun::lsp::rangeAt(const sun::support::Position &base, int offset, int length)

length bytes at offset in the file of base

Creates a source span from an offset and length within a base position.

Related: sun::support::Position

readEntrypoints

public · function · static · Source (opens in a new tab)

static std::optional< std::vector< std::string > > sun::lsp::readEntrypoints(const llvm::json::Object &config)

Read a configuration object's "entrypoints" member.

Entries may be strings or {path: string} objects. Returns nullopt if the member is absent.

readSunConfigs

public · function · static · Source (opens in a new tab)

static std::optional< std::vector< std::string > > sun::lsp::readSunConfigs(const llvm::json::Object &config)

The sun_configs setting: sun-config.json files whose entrypoints the server should know about.

Paths arrive absolute from the client.

refreshEntrypoints

public · function · static · Source (opens in a new tab)

static void sun::lsp::refreshEntrypoints()

Re-read the configured sun-config files and hand the manager the union of their entrypoints, explicitly configured ones, and open manifest files (configs first, so a file covered by both maps to the config's entrypoint).

A config that fails to parse is skipped: a bad editor setting must not kill the server.

resolveSymbol

public · function · Source (opens in a new tab)

std::optional< Declaration > sun::lsp::resolveSymbol(const BlockExprAST &program, const std::vector< const ExprAST * > &chain, const ExprAST &node, const DeclarationIndex *declarations=nullptr)

The declaration a name-bearing node refers to: a local, a parameter, or what findDeclarationOf finds.

Resolves a syntax node to the declaration it denotes.

Used for the cursor and for every candidate reference alike, so both land on the same declaration. The optional index must cover the same program.

Related: Declaration, BlockExprAST, ExprAST, DeclarationIndex

siteAt

public · function · Source (opens in a new tab)

std::optional< SymbolLocation > sun::lsp::siteAt(const Rename &rename, const std::string &filePath, int byteOffset)

The site in filePath holding byteOffset, cursor at the end included; nothing when the cursor is outside every site.

Finds the rename occurrence covering the selected document offset.

Related: SymbolLocation, Rename

sliceSpan

public · function · Source (opens in a new tab)

std::string sun::lsp::sliceSpan(const std::string &source, const sun::support::Position &loc)

Text covered by a span, or empty when the span is missing or out of range.

Returns the source text covered by a recorded span.

Related: sun::support::Position

sourceFor

public · function · Source (opens in a new tab)

std::string sun::lsp::sourceFor(const sun::support::Position &declaration, const std::string &documentPath, const std::string &documentSource)

Text of the file a declaration lives in: the document itself, or a file registered during compilation (another file of the same manifest); empty when neither.

Loads declaration source, reusing the open document text when possible.

Related: sun::support::Position

spanContains

public · function · Source (opens in a new tab)

bool sun::lsp::spanContains(const sun::support::Position &loc, int offset)

Reports whether a source span contains the requested byte offset.

Related: sun::support::Position

stripReference

public · function · Source (opens in a new tab)

const sun::types::Type * sun::lsp::stripReference(const sun::types::Type *type)

Unwraps reference types to inspect the underlying value type.

Related: sun::types::Type

textHas

public · function · Source (opens in a new tab)

bool sun::lsp::textHas(const std::string &text, int offset, const std::string &word)

True when word is written at offset.

Reports whether the expected spelling occurs at a source offset.

textOf

public · function · Source (opens in a new tab)

std::optional< std::string > sun::lsp::textOf(const sun::support::Position &location, const std::string &documentPath, const std::string &source)

Text of the file holding a location: the document, a file of the same manifest, or a library source read from disk.

Retrieves source text for a location, reusing the open document when possible.

Related: sun::support::Position

tokenKindToLSPType

public · function · Source (opens in a new tab)

int sun::lsp::tokenKindToLSPType(TokenKind kind)

Map TokenKind to LSP semantic token type index (-1 = skip).

Related: TokenKind

utf16Units

public · function · Source (opens in a new tab)

int sun::lsp::utf16Units(size_t utf8Length)

UTF-16 code units for a sequence of this byte length.

utf8Length

public · function · Source (opens in a new tab)

size_t sun::lsp::utf8Length(unsigned char lead)

Number of bytes in the UTF-8 sequence that starts with this byte.

Variables

Class

public · variable · Source (opens in a new tab)

int sun::lsp::Class = 2

No documentation comment.

Comment

public · variable · Source (opens in a new tab)

int sun::lsp::Comment = 17

No documentation comment.

Enum

public · variable · Source (opens in a new tab)

int sun::lsp::Enum = 3

No documentation comment.

EnumMember

public · variable · Source (opens in a new tab)

int sun::lsp::EnumMember = 10

No documentation comment.

Event

public · variable · Source (opens in a new tab)

int sun::lsp::Event = 11

No documentation comment.

Function

public · variable · Source (opens in a new tab)

int sun::lsp::Function = 12

No documentation comment.

Interface

public · variable · Source (opens in a new tab)

int sun::lsp::Interface = 4

No documentation comment.

Keyword

public · variable · Source (opens in a new tab)

int sun::lsp::Keyword = 15

No documentation comment.

Lifecycle

public · variable · Source (opens in a new tab)

int sun::lsp::Lifecycle = 22

No documentation comment.

Macro

public · variable · Source (opens in a new tab)

int sun::lsp::Macro = 14

No documentation comment.

Method

public · variable · Source (opens in a new tab)

int sun::lsp::Method = 13

No documentation comment.

Modifier

public · variable · Source (opens in a new tab)

int sun::lsp::Modifier = 16

No documentation comment.

Module

public · variable · Source (opens in a new tab)

int sun::lsp::Module = 0

No documentation comment.

Number

public · variable · Source (opens in a new tab)

int sun::lsp::Number = 19

No documentation comment.

Operator

public · variable · Source (opens in a new tab)

int sun::lsp::Operator = 21

No documentation comment.

Parameter

public · variable · Source (opens in a new tab)

int sun::lsp::Parameter = 7

No documentation comment.

Property

public · variable · Source (opens in a new tab)

int sun::lsp::Property = 9

No documentation comment.

Regexp

public · variable · Source (opens in a new tab)

int sun::lsp::Regexp = 20

No documentation comment.

String

public · variable · Source (opens in a new tab)

int sun::lsp::String = 18

No documentation comment.

Struct

public · variable · Source (opens in a new tab)

int sun::lsp::Struct = 5

No documentation comment.

Type

public · variable · Source (opens in a new tab)

int sun::lsp::Type = 1

No documentation comment.

TypeParameter

public · variable · Source (opens in a new tab)

int sun::lsp::TypeParameter = 6

No documentation comment.

Variable

public · variable · Source (opens in a new tab)

int sun::lsp::Variable = 8

No documentation comment.

configEntrypointInfo

public · variable · static · Source (opens in a new tab)

std::map<std::string, sun::driver::ConfigEntrypoint> sun::lsp::configEntrypointInfo

No documentation comment.

Related: sun::driver::ConfigEntrypoint

configuredSunConfigs

public · variable · static · Source (opens in a new tab)

std::vector<std::string> sun::lsp::configuredSunConfigs

No documentation comment.

diagnosticsCache

public · variable · static · Source (opens in a new tab)

DiagnosticsCache sun::lsp::diagnosticsCache

No documentation comment.

Related: DiagnosticsCache

entrypointManager

public · variable · static · Source (opens in a new tab)

EntrypointManager sun::lsp::entrypointManager

No documentation comment.

Related: EntrypointManager

explicitEntrypoints

public · variable · static · Source (opens in a new tab)

std::vector<std::string> sun::lsp::explicitEntrypoints

No documentation comment.

openSourceOverrides

public · variable · static · Source (opens in a new tab)

std::map<std::string, std::string> sun::lsp::openSourceOverrides

Current editor buffers, used to resolve manifests and analyze unsaved edits.

Type Aliases

AnnotationFn

public · typedef · Source (opens in a new tab)

using sun::lsp::AnnotationFn =  std::function<void(const sun::ast::TypeAnnotation&)>

Every annotation written on a node: parameter and return types, a variable's or loop variable's type, field and payload types, type arguments, catch binding types.

Related: sun::ast::TypeAnnotation

Bindings

public · typedef · Source (opens in a new tab)

using sun::lsp::Bindings =  std::vector<std::pair<std::string, sun::types::TypePtr>>

Type parameter name -> the type it stands for in one specialization.

Related: sun::types::TypePtr

CatchBindingFn

public · typedef · Source (opens in a new tab)

using sun::lsp::CatchBindingFn =  std::function<void(
    const sun::ast::CatchClause&, const sun::support::Position& header)>

Catch clauses with the span that declares each one's binding: a binding has no position of its own, so it is the text between the previous block and the clause's body.

Related: sun::ast::CatchClause, sun::support::Position

DeclarationIndex

public · typedef · Source (opens in a new tab)

using sun::lsp::DeclarationIndex = 
    std::unordered_map<sun::semantic_analysis::DeclarationId, const ExprAST*>

Maps declaration identities to their first syntax node in a live program.

Related: sun::semantic_analysis::DeclarationId, ExprAST