Skip to content

Commit

Permalink
feat(tools/spxls): add implementation
Browse files Browse the repository at this point in the history
Fixes #1059

Signed-off-by: Aofei Sheng <[email protected]>
  • Loading branch information
aofei committed Nov 15, 2024
1 parent c8b100f commit f99ff46
Show file tree
Hide file tree
Showing 10 changed files with 1,105 additions and 10 deletions.
37 changes: 27 additions & 10 deletions tools/spxls/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@

A lightweight Go+ language server for [spx](https://github.com/goplus/spx) that runs in the browser using WebAssembly.

## LSP methods
This project follows the [Language Server Protocol (LSP)](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/)
using [JSON-RPC 2.0](https://www.jsonrpc.org/specification) 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.

## Usage

This project is a standard Go WebAssembly module. You can use it like any other Go WASM modules in your web applications.

For detailed API references, please check the [index.d.ts](index.d.ts) file.

## Supported LSP methods

| Category | Method | Purpose & Explanation |
|----------|--------|-----------------------|
Expand All @@ -29,6 +40,8 @@ A lightweight Go+ language server for [spx](https://github.com/goplus/spx) that
|| [`textDocument/typeDefinition`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#textDocument_typeDefinition) | Navigates to type definitions of variables/fields. |
|| [`textDocument/implementation`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#textDocument_implementation) | Locates implementations. |
|| [`textDocument/references`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#textDocument_references) | Finds all references of a symbol. |
|| [`textDocument/documentLink`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#textDocument_documentLink) | Provides clickable links within document content. |
|| [`documentLink/resolve`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#documentLink_resolve) | Provides detailed target information for selected document links. |
|| [`textDocument/documentSymbol`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#textDocument_documentSymbol) | Provides document symbols for outline/navigation. |
|| [`textDocument/documentHighlight`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#textDocument_documentHighlight) | Highlights other occurrences of selected symbol. |
|| [`workspace/symbol`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#workspace_symbol) | Provides workspace-wide symbol search with name matching patterns. |
Expand Down Expand Up @@ -69,36 +82,40 @@ interface ExecuteCommandParams {
/**
* Arguments that the command should be invoked with.
*/
arguments: RenameResourceParams[]
arguments: SpxRenameResourceParams[]
}
```

```typescript
/**
* Parameters to rename a resource in the workspace.
* Parameters to rename a spx resource in the workspace.
*/
interface RenameResourceParams {
interface SpxRenameResourceParams {
/**
* The resource.
* The spx resource.
*/
resource: ResourceIdentifier
resource: SpxResourceIdentifier

/**
* The new name of the resource.
* The new name of the spx resource.
*/
newName: string
}
```

```typescript
interface ResourceIdentifier {
interface SpxResourceIdentifier {
/**
* The resource's URI.
* The spx resource's URI.
*/
uri: URI
uri: SpxResourceUri
}
```

```typescript
type SpxResourceUri = string
```
*Response:*
- result: [`WorkspaceEdit`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#workspaceEdit)
Expand Down
3 changes: 3 additions & 0 deletions tools/spxls/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/goplus/builder/tools/spxls

go 1.21.0
122 changes: 122 additions & 0 deletions tools/spxls/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/**
* A lightweight Go+ language server for spx that runs in the browser using WebAssembly.
*/
export declare class Spxls {
/**
* Creates a new instance of the spx language server.
*
* @param filesProvider - Function that provides access to the workspace files. This will be called whenever the
* language server needs to access the file system. The returned map should have relative file
* paths as keys and file contents values.
*
* @param messageReplier - Function called when the language server needs to reply to the client. The client should
* handle these messages according to the LSP specification.
*/
constructor(filesProvider: () => Files, messageReplier: (message: Message) => void)

/**
* Handles incoming LSP messages from the client.
*
* @param message - The message to process. Any required response will be sent via the messageReplier callback.
*/
handleMessage(message: Message): void
}

/**
* A general message as defined by JSON-RPC. The language server protocol always uses “2.0” as the `jsonrpc` version.
*
* See https://microsoft.github.io/language-server-protocol/specifications/base/0.9/specification/#abstractMessage.
*/
export interface Message {
jsonrpc: string
}

/**
* A request message to describe a request between the client and the server. Every processed request must send a
* response back to the sender of the request.
*
* See https://microsoft.github.io/language-server-protocol/specifications/base/0.9/specification/#requestMessage.
*/
export interface RequestMessage extends Message {
/**
* The request id.
*/
id: number | string

/**
* The method to be invoked.
*/
method: string

/**
* The method's params.
*/
params?: any[] | object
}

/**
* A Response Message sent as a result of a request. If a request doesn’t provide a result value the receiver of a
* request still needs to return a response message to conform to the JSON-RPC specification. The result property of the
* ResponseMessage should be set to `null` in this case to signal a successful request.
*
* See https://microsoft.github.io/language-server-protocol/specifications/base/0.9/specification/#responseMessage.
*/
export interface ResponseMessage extends Message {
/**
* The request id.
*/
id: number | string | null

/**
* The result of a request. This member is REQUIRED on success.
* This member MUST NOT exist if there was an error invoking the method.
*/
result?: string | number | boolean | any[] | object | null

/**
* The error object in case a request fails.
*/
error?: ResponseError
}

export interface ResponseError {
/**
* A number indicating the error type that occurred.
*/
code: number

/**
* A string providing a short description of the error.
*/
message: string

/**
* A primitive or structured value that contains additional
* information about the error. Can be omitted.
*/
data?: string | number | boolean | any[] | object | null
}

/**
* A notification message. A processed notification message must not send a response back. They work like events.
*
* See https://microsoft.github.io/language-server-protocol/specifications/base/0.9/specification/#notificationMessage.
*/
export interface NotificationMessage extends Message {
/**
* The method to be invoked.
*/
method: string

/**
* The notification's params.
*/
params?: any[] | object
}

/**
* Map from relative path to file content.
*/
export type Files = {
[path: string]: Uint8Array
}
8 changes: 8 additions & 0 deletions tools/spxls/internal/jsonrpc2/jsonrpc2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package jsonrpc2 is a minimal implementation of the JSON RPC 2 spec.
// https://www.jsonrpc.org/specification
// It is intended to be compatible with other implementations at the wire level.
package jsonrpc2
Loading

0 comments on commit f99ff46

Please sign in to comment.