Skip to content
This repository was archived by the owner on Jan 31, 2025. It is now read-only.

Commit e0df7e3

Browse files
authored
Merge pull request #4 from x1unix/feat/support-wasm
feat: introduce connector for js/wasm
2 parents c717bac + ef33fd5 commit e0df7e3

18 files changed

+817
-8
lines changed

cmd/cmd.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ func GnoplsCmd() *cobra.Command {
1818
SilenceUsage: true,
1919
RunE: func(cmd *cobra.Command, args []string) error {
2020
slog.Info("Initializing Server...")
21-
env := &env.Env{
21+
procEnv := &env.Env{
2222
GNOROOT: os.Getenv("GNOROOT"),
2323
GNOHOME: env.GnoHome(),
2424
}
25-
err := lsp.RunServer(cmd.Context(), env)
25+
err := lsp.RunServer(cmd.Context(), procEnv)
2626
if err != nil {
2727
return err
2828
}

internal/env/conn_js.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package env
2+
3+
import (
4+
"context"
5+
"net"
6+
7+
"github.com/gnolang/gnopls/internal/js"
8+
)
9+
10+
func GetConnection(ctx context.Context) (net.Conn, error) {
11+
return js.DialHost(ctx)
12+
}

internal/env/conn_other.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//go:build !js
2+
3+
package env
4+
5+
import (
6+
"context"
7+
"net"
8+
"os"
9+
10+
"go.lsp.dev/pkg/fakenet"
11+
)
12+
13+
func GetConnection(_ context.Context) (net.Conn, error) {
14+
return fakenet.NewConn("stdio", os.Stdin, os.Stdout), nil
15+
}

internal/js/conn_js.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Package js provides primitives to integrate language server with javascript environments such as browsers and Node.
2+
package js
3+
4+
import (
5+
"context"
6+
"net"
7+
8+
"go.lsp.dev/pkg/fakenet"
9+
)
10+
11+
// DialHost registers LSP message listener in JavaScript host and returns connection to use by LSP server.
12+
//
13+
// This function should be called only once before starting LSP server.
14+
func DialHost(ctx context.Context) (net.Conn, error) {
15+
reader, err := registerRequestListener(ctx)
16+
if err != nil {
17+
return nil, err
18+
}
19+
20+
conn := fakenet.NewConn("js", reader, messageWriter)
21+
return conn, nil
22+
}

internal/js/example/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
.DS_Store
3+
.vscode
4+
.idea

internal/js/example/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Gnopls Embedding Example
2+
3+
This directory contains a bare-minimum code to integrate Gnopls as a WebAssembly module
4+
into browser or Node.js environment.
5+
6+
This example omits such nuances as editor integration or file system support and focuses just on basics.
7+
8+
## Prerequisites
9+
10+
* Copy `wasm_exec.js` file using following command:
11+
* `cp $(go env GOROOT)/misc/wasm/wasm_exec.js .`
12+
* Build gnopls as a WebAssembly file for JavaScript environment:
13+
* `GOOS=js GOARCH=wasm make build`
14+
* Modify paths to `wasm_exec.js` and WASM file in [worker.ts](./worker.ts) file.

internal/js/example/package-lock.json

+251
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/js/example/package.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "example",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"@types/golang-wasm-exec": "^1.15.2",
13+
"comlink": "^4.4.1",
14+
"monaco-languageclient": "^8.7.0",
15+
"vscode-languageclient": "^9.0.1",
16+
"vscode-languageserver-protocol": "^3.17.5"
17+
}
18+
}

0 commit comments

Comments
 (0)