Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

goplus/xgolsw

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

xgolsw

Test codecov Go Report Card Go Reference

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

  • xgols runs locally, while xgolsw runs in the browser using WebAssembly.
  • xgols supports a workspace (multiple projects), while xgolsw supports a single project.
  • xgols supports mixed programming of Go and XGo, while xgolsw only supports a pure XGo project.

Building from source

  1. [Optional] Generate required package data:
go generate ./internal/pkgdata
  1. Build the project:
GOOS=js GOARCH=wasm go build -trimpath -o xgolsw.wasm

Usage

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.

Supported LSP methods

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

Predefined commands

XGo resource renaming

The xgo.renameResources command enables renaming of XGo resources referenced by string literals (e.g., play "explosion") across the workspace.

Request:

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

Response:

  • result: WorkspaceEdit | null describing the modification to the workspace. null should be treated the same as WorkspaceEdit with no changes (no change was required).
  • error: code and message set when the rename operation cannot be performed for any reason.

XGo input slots lookup

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:

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[] | null describing the XGo input slots found in the document. null indicates 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.
   */
  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
    }
  | {
      /**
       * The input type accepted by the slot.
       */
      type: XGoInputType.SpxResourceName

      /**
       * The resource context for the resource name 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',

  /**
   * 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'
}
/**
 * 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.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 }
/**
 * 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'
}

Other JSON structures

Document link data types

/**
 * 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'

Completion item data types

/**
 * 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/v2`
   * - `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;

  /**
   * Overload Identifier.
   */
  overloadId?: string;
}

About

XGo Language Server that runs in the browser using WebAssembly

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 9

Languages