Skip to content

Commit

Permalink
Merge pull request #4 from x1unix/feat/support-wasm
Browse files Browse the repository at this point in the history
feat: introduce connector for js/wasm
  • Loading branch information
x1unix committed Jul 19, 2024
2 parents c717bac + ef33fd5 commit e0df7e3
Show file tree
Hide file tree
Showing 18 changed files with 817 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func GnoplsCmd() *cobra.Command {
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
slog.Info("Initializing Server...")
env := &env.Env{
procEnv := &env.Env{
GNOROOT: os.Getenv("GNOROOT"),
GNOHOME: env.GnoHome(),
}
err := lsp.RunServer(cmd.Context(), env)
err := lsp.RunServer(cmd.Context(), procEnv)
if err != nil {
return err
}
Expand Down
12 changes: 12 additions & 0 deletions internal/env/conn_js.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package env

import (
"context"
"net"

"github.com/gnolang/gnopls/internal/js"
)

func GetConnection(ctx context.Context) (net.Conn, error) {
return js.DialHost(ctx)
}
15 changes: 15 additions & 0 deletions internal/env/conn_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build !js

package env

import (
"context"
"net"
"os"

"go.lsp.dev/pkg/fakenet"
)

func GetConnection(_ context.Context) (net.Conn, error) {
return fakenet.NewConn("stdio", os.Stdin, os.Stdout), nil
}
22 changes: 22 additions & 0 deletions internal/js/conn_js.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Package js provides primitives to integrate language server with javascript environments such as browsers and Node.
package js

import (
"context"
"net"

"go.lsp.dev/pkg/fakenet"
)

// DialHost registers LSP message listener in JavaScript host and returns connection to use by LSP server.
//
// This function should be called only once before starting LSP server.
func DialHost(ctx context.Context) (net.Conn, error) {
reader, err := registerRequestListener(ctx)
if err != nil {
return nil, err
}

conn := fakenet.NewConn("js", reader, messageWriter)
return conn, nil
}
4 changes: 4 additions & 0 deletions internal/js/example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
.DS_Store
.vscode
.idea
14 changes: 14 additions & 0 deletions internal/js/example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Gnopls Embedding Example

This directory contains a bare-minimum code to integrate Gnopls as a WebAssembly module
into browser or Node.js environment.

This example omits such nuances as editor integration or file system support and focuses just on basics.

## Prerequisites

* Copy `wasm_exec.js` file using following command:
* `cp $(go env GOROOT)/misc/wasm/wasm_exec.js .`
* Build gnopls as a WebAssembly file for JavaScript environment:
* `GOOS=js GOARCH=wasm make build`
* Modify paths to `wasm_exec.js` and WASM file in [worker.ts](./worker.ts) file.
251 changes: 251 additions & 0 deletions internal/js/example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions internal/js/example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "example",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@types/golang-wasm-exec": "^1.15.2",
"comlink": "^4.4.1",
"monaco-languageclient": "^8.7.0",
"vscode-languageclient": "^9.0.1",
"vscode-languageserver-protocol": "^3.17.5"
}
}
Loading

0 comments on commit e0df7e3

Please sign in to comment.