Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remote module example #172

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions examples/remote/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
This this example Neovim plugin shows how to invoke a [Go](https://go.dev/)
function from a plugin.

The plugin starts a Go program containing the function as a child process. The
plugin invokes functions in the child process using
[RPC](https://neovim.io/doc/user/api.html#RPC).

Use the following steps to run the plugin:

1. Build the program with the [go tool](https://golang.org/cmd/go/) to an
executable named `helloremote`. Ensure that the executable is in a directory in
the `PATH` environment variable.
```
$ cd helloremote
$ go build
```
1. Install the plugin in this directory using a plugin manager or by adding
this directory to the
[runtimepath](https://neovim.io/doc/user/options.html#'runtimepath').
1. Start Nvim and run the following command:
```vim
:Hello world!
```
5 changes: 5 additions & 0 deletions examples/remote/helloremote/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module example.com/remote/helloremote

go 1.22.2

require github.com/neovim/go-client v1.2.1
2 changes: 2 additions & 0 deletions examples/remote/helloremote/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/neovim/go-client v1.2.1 h1:kl3PgYgbnBfvaIoGYi3ojyXH0ouY6dJY/rYUCssZKqI=
github.com/neovim/go-client v1.2.1/go.mod h1:EeqCP3z1vJd70JTaH/KXz9RMZ/nIgEFveX83hYnh/7c=
40 changes: 40 additions & 0 deletions examples/remote/helloremote/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"fmt"
"log"
"os"
"strings"

"github.com/neovim/go-client/nvim"
)

func hello(v *nvim.Nvim, args []string) error {
return v.WriteOut(fmt.Sprintf("Hello %s\n", strings.Join(args, " ")))
}

func main() {
// Turn off timestamps in output.
log.SetFlags(0)

// Direct writes by the application to stdout garble the RPC stream.
// Redirect the application's direct use of stdout to stderr.
stdout := os.Stdout
os.Stdout = os.Stderr

// Create a client connected to stdio. Configure the client to use the
// standard log package for logging.
v, err := nvim.New(os.Stdin, stdout, stdout, log.Printf)
if err != nil {
log.Fatal(err)
}

// Register function with the client.
v.RegisterHandler("hello", hello)

// Run the RPC message loop. The Serve function returns when
// nvim closes.
if err := v.Serve(); err != nil {
log.Fatal(err)
}
}
14 changes: 14 additions & 0 deletions examples/remote/plugin/hello.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
local chan

local function ensure_job()
if chan then
return chan
end
chan = vim.fn.jobstart({ 'helloremote' }, { rpc = true })
return chan
end

vim.api.nvim_create_user_command('Hello', function(args)
vim.fn.rpcrequest(ensure_job(), 'hello', args.fargs)
end, { nargs = '*' })

Loading