Skip to content

Commit

Permalink
Update README, example, add a kill function, make exttypes be encoded
Browse files Browse the repository at this point in the history
  • Loading branch information
smolck committed Nov 25, 2020
1 parent 1599f39 commit 12f1294
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 45 deletions.
38 changes: 13 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,27 @@ Still a WIP, so any feedback, contributions, etc. are greatly appreciated.
```dart
import 'package:dart_nvim_api/dart_nvim_api.dart';
main(List<String> args) async {
// Start up Neovim instance and communicate over stdin/stdout:
var nvim = Neovim(nvimBinaryPath: 'nvim');
// Or connect to already running instance over TCP:
// var nvim = Neovim.connectToRunningInstance(host: '127.0.0.1', port: 8888);
void main(List<String> args) async {
// Start up Neovim instance, with optional `onNotify` and `onRequest`
// callbacks.
// See also Nvim.child()
var nvim = await Nvim.spawn();
// Run Neovim ex command.
await nvim.command("echo 'hello'");
// Get ex command output.
assert(await nvim.commandOutput("echo 'hello'") == null);
assert(await nvim.exec('echo 1 + 1', true) == '2');
// Buffer example:
var buf = await nvim.createBuf(true, false);
var bufNum = await buf.getNumber(nvim);
assert(bufNum == 2);
assert(await nvim.getCurrentBuf() is Buffer);
var bufNameWithoutPath = 'some name';
await nvim.bufSetName(buf, bufNameWithoutPath);
var bufName = await nvim.bufGetName(buf);
assert(bufName.contains(bufNameWithoutPath));
// Beyond that, you can run any Neovim api command. See `:help api-rpc` doc in Neovim.
// See also `example` directory.
// Kill Neovim when done.
nvim.kill();
}
```

# Contributing
Changes to the `Neovim`, `Window`, `Buffer`, and `Tabpage` classes should be done
in the template files in the `gen_bindings/src` folder. To generate `lib/src/*.dart` do
the following from the project root (requires `pip` in addition to `python` v3.7.4. Note that older versions
of Python 3 may work, I just haven't tested them):
```console
$ pip install -g datetime jinja2
# Replacing <nvim binary path> as necessary:
$ python gen_bindings/gen_bindings.py <nvim binary path> 'lib/src' 'gen_bindings/src/'
```

Changes to any other files can be done as usual.
24 changes: 13 additions & 11 deletions example/example.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import 'package:dart_nvim_api/dart_nvim_api.dart';

main(List<String> args) async {
// Start up Neovim instance and communicate over stdin/stdout:
var nvim = Neovim(nvimBinaryPath: 'nvim');

// Or connect to already running instance over TCP:
// var nvim = Neovim.connectToRunningInstance(host: '127.0.0.1', port: 8888);
void main(List<String> args) async {
// Start up Neovim instance, with optional `onNotify` and `onRequest`
// callbacks.
// See also Nvim.child()
var nvim = await Nvim.spawn();

// Run Neovim ex command.
await nvim.command("echo 'hello'");

// Get ex command output.
assert(await nvim.commandOutput("echo 'hello'") == null);

assert(await nvim.exec('echo 1 + 1', true) == '2');

// Buffer example:
var buf = await nvim.createBuf(true, false);
var bufNum = await buf.getNumber(nvim);
assert(bufNum == 2);
assert(await nvim.getCurrentBuf() is Buffer);
var bufNameWithoutPath = 'some name';
await nvim.bufSetName(buf, bufNameWithoutPath);
var bufName = await nvim.bufGetName(buf);
assert(bufName.contains(bufNameWithoutPath));

// Beyond that, you can run any Neovim api command. See `:help api-rpc` doc in Neovim.
// Kill Neovim when done.
nvim.kill();
}
7 changes: 0 additions & 7 deletions lib/dart_nvim_api.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
/* export 'src/tabpage.dart';
export 'src/window.dart';
export 'src/buffer.dart';
export 'src/ui_events.dart';
export 'src/session.dart';
export 'src/ui_attach_options.dart';
export 'src/neovim.dart'; */
export 'src/neovim.dart';
export 'src/gen/nvim.g.dart';
export 'src/gen/buffer.g.dart';
Expand Down
24 changes: 24 additions & 0 deletions lib/src/ext_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,27 @@ class ExtTypeDecoder extends ExtDecoder {
}
}
}

class ExtTypeEncoder extends ExtEncoder {
@override
int extTypeForObject(dynamic object) {
if (object is Buffer) {
return 0;
} else if (object is Window) {
return 1;
} else if (object is Tabpage) {
return 2;
}

throw "Can't decode object, not a Neovim type: $object";
}

@override
Uint8List encodeObject(dynamic object) {
if (object is Buffer || object is Window || object is Tabpage) {
return serialize(object.data);
}

throw "Object wasn't a Neovim ext type!";
}
}
8 changes: 6 additions & 2 deletions lib/src/neovim.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,16 @@ class Nvim {
];

if (_nvim != null) {
_nvim.stdin.add(mpack.serialize(cmd));
_nvim.stdin.add(mpack.serialize(cmd, extEncoder: ExtTypeEncoder()));
} else {
stdout.add(mpack.serialize(cmd));
stdout.add(mpack.serialize(cmd, extEncoder: ExtTypeEncoder()));
}

_waiting[reqId] = Completer();
return _waiting[reqId].future;
}

void kill() {
_nvim.kill();
}
}

0 comments on commit 12f1294

Please sign in to comment.