A lightweight XGo language server that runs in the browser using WebAssembly.
This project follows the Language Server Protocol (LSP) using JSON-RPC 2.0 for message exchange. However, unlike traditional LSP implementations that require a network transport layer, this project operates directly in the browser's memory space through its API interfaces.
Difference between xgols and xgolsw
xgolsruns locally, whilexgolswruns in the browser using WebAssembly.xgolssupports a workspace (multiple projects), whilexgolswsupports a single project.xgolssupports mixed programming of Go and XGo, whilexgolswonly supports a pure XGo project.
- [Optional] Generate required package data:
go generate ./internal/pkgdata ./internal/server- Build the project:
GOOS=js GOARCH=wasm go build -trimpath -ldflags "-s -w" -o xgolsw.wasmUse lib/wasm/wasm_exec.js from the Go toolchain used to build xgolsw.wasm. The JavaScript support file must match
the compiler's Go release, as described in the Go WebAssembly guide.
This project is a standard Go WebAssembly module. You can use it like any other Go WebAssembly modules in your web applications.
For detailed API references, please check the index.d.ts file.
Call server.dispose() when the instance is no longer needed, such as when closing a project. It cancels queued
requests, stops outgoing messages, and releases its project and callback references without waiting for running analysis
to finish. Repeated calls have no effect. Calls to handleMessage after disposal return an Error, including
previously obtained method references. Other instances in the same WASM runtime remain usable.
| Category | Method | Purpose & Explanation |
|---|---|---|
| Lifecycle Management | ||
initialize |
Performs initial handshake, establishes server capabilities and client configuration. | |
initialized |
Marks completion of initialization process, enabling request processing. | |
shutdown |
Protocol conformance only. | |
exit |
Protocol conformance only. | |
| Document Synchronization | ||
textDocument/didOpen |
Registers new document in server state and triggers initial diagnostics. | |
textDocument/didChange |
Synchronizes document content changes between client and server. | |
textDocument/didSave |
Processes document save events and triggers related operations. | |
textDocument/didClose |
Removes document from server state and cleans up resources. | |
| Code Intelligence | ||
textDocument/hover |
Shows types and documentation at cursor position. | |
textDocument/completion |
Generates context-aware code suggestions. | |
textDocument/signatureHelp |
Shows function/method signature information. | |
| Symbols & Navigation | ||
textDocument/declaration |
Finds symbol declarations. | |
textDocument/definition |
Locates symbol definitions across workspace. | |
textDocument/typeDefinition |
Navigates to type definitions of variables/fields. | |
textDocument/implementation |
Locates implementations. | |
textDocument/references |
Finds all references of a symbol. | |
textDocument/documentHighlight |
Highlights other occurrences of selected symbol. | |
textDocument/documentLink |
Provides clickable links within document content. | |
| Code Quality | ||
textDocument/publishDiagnostics |
Reports code errors and warnings in real-time. | |
textDocument/diagnostic |
Pulls diagnostics for documents on request (pull model). | |
workspace/diagnostic |
Pulls diagnostics for all workspace documents on request. | |
| Code Modification | ||
textDocument/codeAction |
Offers quickfix.xgo.mergeVarDeclarations to merge top-level var declarations in classfiles. |
|
textDocument/formatting |
Applies standardized formatting rules to document. | |
textDocument/prepareRename |
Validates renaming possibility and returns valid range for the operation. | |
textDocument/rename |
Performs consistent symbol renaming across workspace. | |
| Semantic Features | ||
textDocument/semanticTokens/full |
Provides semantic coloring for whole document. | |
textDocument/inlayHint |
Provides inline hints such as parameter names and type annotations. | |
| Other | ||
workspace/executeCommand |
Executes predefined commands for workspace-specific operations. |
Note: This table lists implemented message handlers. Clients should use the initialize response to determine which
capabilities are advertised for their client capabilities.
Put //xgols:notRecommended in a declaration's leading documentation comment to omit the declaration from proactive
symbol recommendations. The language server applies this policy to ordinary identifier and member completions,
property-name string completions such as showVar, and the predefinedNames returned by xgo.getInputSlots.
Clients using xgo.getProperties to offer suggestions can request the same server-side filtering by passing
excludeNotRecommended: true. Unmarked declarations stay subject to the usual candidate selection rules.
// reportTurn records execution for a course.
//xgols:notRecommended
func reportTurn(angle float64) {
println(angle)
}The directive supports functions, methods, variables (including classfile fields), constants, and types. Leading comments on fields and interface methods directly declared in a type definition or alias are also supported. It controls recommendations for that declaration only. Explicit uses retain normal compilation, diagnostics, hover, signature help, parameter hints, navigation, references, and rename behavior. A marked type does not mark its members, and a marked function does not mark declarations inside its body. Symbol browsing and file outlines are outside this policy.
The spelling is exact and case-sensitive. Put //xgols:notRecommended on its own line anywhere in the leading comment
group, before or after ordinary documentation. The group must be immediately above the declaration. Trailing whitespace
is allowed. Block comments, end-of-line comments, detached comment groups, and directives with arguments are not
recognized.
For grouped var, const, and type declarations, a directive on the group applies to every declaration in the group.
A directive on an individual spec applies only to that spec, including every name declared together by that spec.
Overload declarations follow the same declaration identity rule: marking an overload alias omits recommendations through
that alias; marking one implementation omits only candidates that use that implementation. Other unmarked aliases and
SDK overloads remain eligible. Enum entries do not accept this directive.
Imported declarations use directive metadata preserved in their .pkgdoc package data. pkgdatagen records these
annotations from raw comments in the Go files selected for the build. For a reconstructed API, annotate its Go
declaration. Use the XGoo_ constant for an overload group or the XGos_ declaration for a static member. See
XGo symbol encodings.
A directive present only in a dependency's XGo source is outside this collection. Existing package data without
annotations keeps its current behavior.
The xgo.renameResources command enables renaming of XGo resources referenced by string literals (e.g.,
play "explosion") across the workspace.
Request:
- method:
workspace/executeCommand - params:
XGoRenameResourcesExecuteCommandParamsdefined as follows:
type XGoRenameResourcesExecuteCommandParams = Omit<ExecuteCommandParams, 'command' | 'arguments'> & {
/**
* The identifier of the actual command handler.
*/
command: 'xgo.renameResources'
/**
* Arguments that the command should be invoked with.
*/
arguments: XGoRenameResourceParams[]
}/**
* Parameters to rename an XGo resource in the workspace.
*/
interface XGoRenameResourceParams {
/**
* The XGo resource to rename.
*/
resource: XGoResourceIdentifier
/**
* The new name of the XGo resource.
*/
newName: string
}/**
* The XGo resource's identifier.
*/
interface XGoResourceIdentifier {
/**
* The XGo resource's URI.
*/
uri: XGoResourceUri
}/**
* The XGo resource's URI.
*
* For example:
* - `spx://resources/sounds/MySound`
* - `spx://resources/sprites/MySprite`
* - `spx://resources/sprites/MySprite/costumes/MyCostume`
* - `spx://resources/sprites/MySprite/animations/MyAnimation`
* - `spx://resources/backdrops/MyBackdrop`
* - `spx://resources/widgets/MyWidget`
*/
type XGoResourceUri = stringResponse:
- result:
WorkspaceEdit|nulldescribing the modification to the workspace.nullshould be treated the same asWorkspaceEditwith no changes (no change was required). - error: code and message set when the rename operation cannot be performed for any reason.
The xgo.getInputSlots command retrieves all modifiable items (XGo input slots) in a document, which can be used to
provide UI controls for assisting users with code modifications.
Request:
- method:
workspace/executeCommand - params:
XGoGetInputSlotsExecuteCommandParamsdefined as follows:
type XGoGetInputSlotsExecuteCommandParams = Omit<ExecuteCommandParams, 'command' | 'arguments'> & {
/**
* The identifier of the actual command handler.
*/
command: 'xgo.getInputSlots'
/**
* Arguments that the command should be invoked with.
*/
arguments: [XGoGetInputSlotsParams]
}/**
* Parameters to retrieve XGo input slots in a document.
*/
interface XGoGetInputSlotsParams {
/**
* The text document.
*/
textDocument: TextDocumentIdentifier
}Response:
- result:
XGoInputSlot[]|nulldescribing the XGo input slots found in the document. Returned slots are ordered by source range and have pairwise non-overlapping ranges.nullindicates no XGo input slots were found. - error: code and message set when XGo input slots cannot be retrieved for any reason.
/**
* The XGo input slot for a modifiable item in code.
*/
interface XGoInputSlot {
/**
* The document range of the XGo input slot.
*/
range: Range
/**
* The kind of the XGo input slot.
*/
kind: XGoInputSlotKind
/**
* The accepted inputs for the XGo input slot.
*/
accept: XGoInputSlotAccept
/**
* The current input in the XGo input slot.
*/
input: XGoInput
/**
* The available user-predefined identifiers, omitting declarations marked `//xgols:notRecommended`.
*/
predefinedNames: string[]
}/**
* The kinds of XGo input slots.
*/
enum XGoInputSlotKind {
/**
* The slot accepts a value, which may be an in-place value or a predefined identifier.
*
* For example:
* - `123` in `println 123`
* - `name` in `println name`
*/
Value = 'value',
/**
* The slot accepts an address, which must be a predefined identifier.
*
* For example:
* - `x` in `x = 123`
* - `y` in `x = y`
*/
Address = 'address'
}/**
* The accepted input for an XGo input slot.
*/
type XGoInputSlotAccept =
| {
/**
* The input type accepted by the slot.
*/
type:
| XGoInputType.String
| XGoInputType.Integer
| XGoInputType.Decimal
| XGoInputType.Boolean
| XGoInputType.Unknown
| XGoInputType.SpxDirection
| XGoInputType.SpxLayerAction
| XGoInputType.SpxDirAction
| XGoInputType.SpxColor
| XGoInputType.SpxEffectKind
| XGoInputType.SpxKey
| XGoInputType.SpxSpecialObj
| XGoInputType.SpxRotationStyle
| XGoInputType.SpxPropertyName
}
| {
/**
* The input type accepted by the slot.
*/
type: XGoInputType.SpxResourceName | XGoInputType.SpxSpriteInstance
/**
* The resource context for the resource-backed input type.
*/
resourceContext: XGoResourceContextUri
}/**
* The type of input for a slot.
*/
enum XGoInputType {
/**
* String values.
*/
String = 'string',
/**
* Integer number values.
*/
Integer = 'integer',
/**
* Decimal number values.
*/
Decimal = 'decimal',
/**
* Boolean values.
*/
Boolean = 'boolean',
/**
* Unknown type.
*/
Unknown = 'unknown',
/**
* Resource name (`SpriteName`, `SoundName`, etc.) in spx.
*/
SpxResourceName = 'spx-resource-name',
/**
* Sprite instance resource references in spx.
*/
SpxSpriteInstance = 'spx-sprite-instance',
/**
* Direction values in spx.
*/
SpxDirection = 'spx-direction',
/**
* layerAction values in spx.
*/
SpxLayerAction = 'spx-layer-action',
/**
* dirAction values in spx.
*/
SpxDirAction = 'spx-dir-action',
/**
* Color values in spx.
*/
SpxColor = 'spx-color',
/**
* Effect kind values in spx.
*/
SpxEffectKind = 'spx-effect-kind',
/**
* Keyboard key values in spx.
*/
SpxKey = 'spx-key',
/**
* Special object values in spx.
*/
SpxSpecialObj = 'spx-special-obj',
/**
* Rotation style values in spx.
*/
SpxRotationStyle = 'spx-rotation-style',
/**
* Property name values in spx.
*/
SpxPropertyName = 'spx-property-name'
}/**
* The names of color constructors.
*/
type XGoInputTypeSpxColorConstructor = 'HSB' | 'HSBA'/**
* The input value with type information.
*/
type XGoInputTypedValue =
| { type: XGoInputType.String; value: string }
| { type: XGoInputType.Integer; value: number }
| { type: XGoInputType.Decimal; value: number }
| { type: XGoInputType.Boolean; value: boolean }
| { type: XGoInputType.Unknown; value: void }
| { type: XGoInputType.SpxResourceName; value: XGoResourceUri }
| { type: XGoInputType.SpxSpriteInstance; value: XGoResourceUri }
| { type: XGoInputType.SpxDirection; value: number }
| { type: XGoInputType.SpxLayerAction; value: string }
| { type: XGoInputType.SpxDirAction; value: string }
| {
type: XGoInputType.SpxColor
value: {
/**
* Constructor for color.
*/
constructor: XGoInputTypeSpxColorConstructor
/**
* Arguments passed to the constructor.
*/
args: number[]
}
}
| { type: XGoInputType.SpxEffectKind; value: string }
| { type: XGoInputType.SpxKey; value: string }
| { type: XGoInputType.SpxSpecialObj; value: string }
| { type: XGoInputType.SpxRotationStyle; value: string }
| { type: XGoInputType.SpxPropertyName; value: string }/**
* The URI of the resource context.
*
* For example:
* - `spx://resources/sprites`
* - `spx://resources/sounds`
* - `spx://resources/sprites/<sName>/costumes`
*/
type XGoResourceContextUri = string/**
* Represents the current input in a slot.
*/
type XGoInput<T extends XGoInputTypedValue = XGoInputTypedValue> =
| {
/**
* In-place value.
*
* For example:
* - `"hello world"`
* - `123`
* - `true`
* - spx `Left`
* - spx `HSB(0,0,0)`
*/
kind: XGoInputKind.InPlace
/**
* Type of the input.
*/
type: T['type']
/**
* In-place value.
*/
value: T['value']
}
| {
/**
* (Reference to) user predefined identifier.
*
* For example:
* - var `costume1`
* - const `name2`
* - field `num3`
*/
kind: XGoInputKind.Predefined
/**
* Type of the input.
*/
type: T['type']
/**
* Name for user predefined identifer.
*/
name: string
}/**
* The kind of input.
*/
enum XGoInputKind {
/**
* In-place value.
*
* For example:
* - `"hello world"`
* - `123`
* - `true`
* - spx `Left`
* - spx `HSB(0,0,0)`
*/
InPlace = 'in-place',
/**
* (Reference to) user predefined identifier.
*
* For example:
* - var `costume1`
* - const `name2`
* - field `num3`
*/
Predefined = 'predefined'
}The xgo.getProperties command retrieves readable properties for a named struct or classfile type in the package scope.
Type aliases to these types are also accepted. Properties include:
- Accessible fields, including promoted fields, with no restriction on their value types
- Methods that XGo can read as single-value auto-properties, including resolved overloads and inferred generic results
Embedded fields themselves are excluded. Member lookup and shadowing follow XGo's rules. Fields are listed before methods, and names within each group are sorted alphabetically.
Pass excludeNotRecommended: true to omit properties whose source declaration or selected overload implementation is
marked //xgols:notRecommended. The default query includes all readable properties, including for checking existing
property bindings.
Framework adapters can supply their runtime property rules. For spx game and sprite types, properties follow monitor
lookup and remain limited to supported scalar values, spx.Value, and spx.List. Ordinary types in the same project use
the XGo rules above.
Request:
- method:
workspace/executeCommand - params:
XGoGetPropertiesExecuteCommandParamsdefined as follows:
type XGoGetPropertiesExecuteCommandParams = Omit<ExecuteCommandParams, 'command' | 'arguments'> & {
/**
* The identifier of the actual command handler.
*/
command: 'xgo.getProperties'
/**
* Arguments that the command should be invoked with.
*/
arguments: [XGoGetPropertiesParams]
}/**
* Parameters to retrieve properties for a target.
*/
interface XGoGetPropertiesParams {
/**
* The name of a package-scope struct or classfile type, or an alias to one.
*/
target: string
/**
* Omit properties marked not recommended when listing suggestions. Defaults to false.
*/
excludeNotRecommended?: boolean
}Response:
- result:
XGoProperty[]describing the properties found. An empty array means no properties were found. - error: code and message set when properties cannot be retrieved for any reason.
/**
* A property of a target type.
*/
interface XGoProperty {
/**
* The property name.
*/
name: string
/**
* The property type as a string.
*/
type: string
/**
* The kind of property.
*/
kind: 'field' | 'method'
/**
* Optional documentation for the property.
*/
doc?: string
/**
* The definition identifier for this property.
*/
definition: XGoDefinitionIdentifier
}The textDocument/xgo.propertyRenamed notification is sent from the server to the client when a property (field or method) is renamed through the textDocument/rename request. This allows the client (IDE) to update any monitoring or tracking of that property.
Notification:
- method:
textDocument/xgo.propertyRenamed - params:
PropertyRenamedParamsdefined as follows:
/**
* Parameters for the property renamed notification.
*/
interface PropertyRenamedParams {
/**
* The package-scope type that exposes the renamed property.
*/
target: string
/**
* The original name of the property before renaming.
*/
oldName: string
/**
* The new name of the property after renaming.
*/
newName: string
/**
* The text document where the property is defined.
*/
textDocument: TextDocumentIdentifier
}Notifications are sent after the rename workspace edit is successfully constructed, using the same properties as
xgo.getProperties. Each affected target receives a notification, including type aliases and types
that expose the property through embedding. Shadowed properties and local types are excluded. Method property names use
their source aliases, such as label for Label. A notification is emitted only when the new property name still resolves
to the renamed declaration under the target's property rules. Properties removed or hidden by the rename do not produce
notifications.
/**
* The data of an XGo resource reference DocumentLink.
*/
interface XGoResourceRefDocumentLinkData {
/**
* The kind of the XGo resource reference.
*/
kind: XGoResourceRefKind
}/**
* The kind of the XGo resource reference.
*
* - stringLiteral: String literal as a resource-reference, e.g., `play "explosion"`
* - autoBindingReference: Reference for auto-binding variable as a resource-reference, e.g., `play explosion`
* - constantReference: Reference for constant as a resource-reference, e.g., `play EXPLOSION` (`EXPLOSION` is a constant)
*/
type XGoResourceRefKind = 'stringLiteral' | 'autoBindingReference' | 'constantReference'/**
* The data of a completion item.
*/
interface XGoCompletionItemData {
/**
* The corresponding definition of the completion item.
*/
definition?: XGoDefinitionIdentifier
}/**
* The identifier of a definition.
*/
interface XGoDefinitionIdentifier {
/**
* Full name of source package.
* If not provided, it's assumed to be kind-statement.
* If `main`, it's the current user package.
*
* For example:
* - `fmt`
* - `github.com/goplus/spx/v4`
* - `main`
*/
package?: string;
/**
* Exported name of the definition.
* If not provided, it's assumed to be kind-package.
*
* For example:
* - `Println`
* - `Sprite`
* - `Sprite.turn`
* - `for_statement_with_single_condition`
*/
name?: string;
/**
* Identifies an overload of the definition.
* It applies only when `name` is provided.
*/
overloadId?: string;
}