Skip to content

Commit 77f98f1

Browse files
committed
Add remote module example
The example shows how to create a "remote module" as described in neovim/neovim#27949.
1 parent e94cce5 commit 77f98f1

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

examples/remote/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
This this example Neovim plugin shows how to invoke a [Go](https://go.dev/)
2+
function from a plugin.
3+
4+
The plugin starts a Go program containing the function as a child process. The
5+
plugin invokes functions in the child process using
6+
[RPC](https://neovim.io/doc/user/api.html#RPC).
7+
8+
Use the following steps to run the plugin:
9+
10+
1. Build the program with the [go tool](https://golang.org/cmd/go/) to an
11+
executable named `helloremote`. Ensure that the executable is in a directory in
12+
the `PATH` environment variable.
13+
```
14+
$ cd helloremote
15+
$ go build
16+
```
17+
1. Install the plugin in this directory using a plugin manager or by adding
18+
this directory to the
19+
[runtimepath](https://neovim.io/doc/user/options.html#'runtimepath').
20+
1. Start Nvim and run the following command:
21+
```vim
22+
:Hello world!
23+
```

examples/remote/helloremote/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module example.com/remote/helloremote
2+
3+
go 1.22.2
4+
5+
require github.com/neovim/go-client v1.2.1

examples/remote/helloremote/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/neovim/go-client v1.2.1 h1:kl3PgYgbnBfvaIoGYi3ojyXH0ouY6dJY/rYUCssZKqI=
2+
github.com/neovim/go-client v1.2.1/go.mod h1:EeqCP3z1vJd70JTaH/KXz9RMZ/nIgEFveX83hYnh/7c=

examples/remote/helloremote/main.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"strings"
8+
9+
"github.com/neovim/go-client/nvim"
10+
)
11+
12+
func hello(v *nvim.Nvim, args []string) error {
13+
return v.WriteOut(fmt.Sprintf("Hello %s\n", strings.Join(args, " ")))
14+
}
15+
16+
func main() {
17+
// Turn off timestamps in output.
18+
log.SetFlags(0)
19+
20+
// Direct writes by the application to stdout garble the RPC stream.
21+
// Redirect the application's direct use of stdout to stderr.
22+
stdout := os.Stdout
23+
os.Stdout = os.Stderr
24+
25+
// Create a client connected to stdio. Configure the client to use the
26+
// standard log package for logging.
27+
v, err := nvim.New(os.Stdin, stdout, stdout, log.Printf)
28+
if err != nil {
29+
log.Fatal(err)
30+
}
31+
32+
// Register function with the client.
33+
v.RegisterHandler("hello", hello)
34+
35+
// Run the RPC message loop. The Serve function returns when
36+
// nvim closes.
37+
if err := v.Serve(); err != nil {
38+
log.Fatal(err)
39+
}
40+
}

examples/remote/plugin/hello.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
local chan
2+
3+
local function ensure_job()
4+
if chan then
5+
return chan
6+
end
7+
chan = vim.fn.jobstart({ 'helloremote' }, { rpc = true })
8+
return chan
9+
end
10+
11+
vim.api.nvim_create_user_command('Hello', function(args)
12+
vim.fn.rpcrequest(ensure_job(), 'hello', args.fargs)
13+
end, { nargs = '*' })
14+

0 commit comments

Comments
 (0)