Skip to content

Commit

Permalink
Initial VimL Interop (onivim#30)
Browse files Browse the repository at this point in the history
* Add init.vim

* Add dune file to copy init.vim

* Set up init vim loading

* Add simple logger

* Use logging API

* Augment logging

* Parse oni_plugin_notify messages

* Formatting
  • Loading branch information
bryphe authored Feb 6, 2019
1 parent d816250 commit 61fea4d
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 22 deletions.
4 changes: 4 additions & 0 deletions assets/viml/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(install
(section bin)
(package Oni2)
(files init.vim))
50 changes: 50 additions & 0 deletions assets/viml/init.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
" init.vim
" Entry point for Oni interop
" Sets defaults for Oni2, and wires up some RPC for autocmds

if exists("g:loaded_oni_interop_plugin")
finish
endif

set hidden
set nocursorline
set nobackup
set nowritebackup
set noswapfile

syntax off

let g:loaded_oni_interop_plugin = 1

function OniNotify(args)
call rpcnotify(1, "oni_plugin_notify", a:args)
endfunction

function OniNotifyAutocmd(eventName)
let context = OniGetContext()
call OniNotify(["autocmd", a:eventName, context])
endfunction

augroup OniEventListeners
autocmd!
autocmd! BufWritePre * :call OniNotifyAutocmd("BufWritePre")
autocmd! BufWritePost * :call OniNotifyAutocmd("BufWritePost")
autocmd! BufEnter * :call OniNotifyAutocmd("BufEnter")
autocmd! BufRead * :call OniNotifyAutocmd("BufRead")
autocmd! BufWinEnter * :call OniNotifyAutocmd("BufWinEnter")
autocmd! BufDelete * :call OniNotifyAutocmd("BufDelete")
autocmd! BufUnload * :call OniNotifyAutocmd("BufUnload")
autocmd! BufWipeout * :call OniNotifyAutocmd("BufWipeout")
autocmd! CursorMoved * :call OniNotifyAutocmd("CursorMoved")
autocmd! CursorMovedI * :call OniNotifyAutocmd("CursorMovedI")
augroup END

function OniGetContext()
let bufferNumber = bufnr("%")
let line = line(".")
let column = col(".")

let context = [bufferNumber, line, column]

return context
endfunction
37 changes: 17 additions & 20 deletions src/editor/Core/Buffer.re
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,23 @@ let slice = (~lines: array(string), ~start, ~length, ()) => {
};
};

let applyUpdate = (lines: array(string), update: BufferUpdate.t) => {

if (update.endLine <= update.startLine) {
Array.of_list(update.lines)
} else {

let prev = slice(~lines, ~start=0, ~length=update.startLine, ());
let post =
slice(
~lines,
~start=update.endLine,
~length=Array.length(lines) - update.endLine,
(),
);

let lines = Array.of_list(update.lines);

Array.concat([prev, lines, post]);
}
};
let applyUpdate = (lines: array(string), update: BufferUpdate.t) =>
if (update.endLine <= update.startLine) {
Array.of_list(update.lines);
} else {
let prev = slice(~lines, ~start=0, ~length=update.startLine, ());
let post =
slice(
~lines,
~start=update.endLine,
~length=Array.length(lines) - update.endLine,
(),
);

let lines = Array.of_list(update.lines);

Array.concat([prev, lines, post]);
};

let update = (buf: t, update: BufferUpdate.t) => {
let ret: t = {...buf, lines: applyUpdate(buf.lines, update)};
Expand Down
18 changes: 18 additions & 0 deletions src/editor/Core/Log.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Log.re
*
* Simple console logger
*/

let isDebugLoggingEnabled =
switch (Sys.getenv_opt("ONI2_DEBUG")) {
| Some(_) => true
| _ => false
};

let info = msg => print_endline("[INFO] " ++ msg);

let debug = msg =>
isDebugLoggingEnabled ? print_endline("[DEBUG] " ++ msg) : ();

let error = msg => prerr_endline("[ERROR] " ++ msg);
1 change: 1 addition & 0 deletions src/editor/Core/Oni_Core.re
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

module Actions = Actions;
module Buffer = Buffer;
module Log = Log;
module Reducer = Reducer;
module State = State;
module TokenizedBuffer = TokenizedBuffer;
Expand Down
34 changes: 34 additions & 0 deletions src/editor/Neovim/Notification.re
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
/* open Oni_Core; */
/* open Rench; */

open Types;

module BufferLinesNotification = {
type t = {
changedTick: int,
Expand Down Expand Up @@ -34,6 +36,7 @@ type t =
| Redraw
| ModeChanged(string)
| BufferLines(BufferLinesNotification.t)
| CursorMoved(AutoCommandContext.t)
| Ignored;

module M = Msgpck;
Expand All @@ -53,6 +56,28 @@ let parseRedraw = (msgs: list(Msgpck.t)) => {
msgs |> List.map(p);
};

exception InvalidAutoCommandContext;

let parseAutoCommand = (autocmd: string, args: list(Msgpck.t)) => {
let context =
switch (args) {
| [M.Int(activeBufferId), M.Int(cursorLine), M.Int(cursorColumn)] =>
Types.AutoCommandContext.create(
~activeBufferId,
~cursorLine,
~cursorColumn,
(),
)
| _ => raise(InvalidAutoCommandContext)
};

switch (autocmd) {
| "CursorMoved" => CursorMoved(context)
| "CursorMovedI" => CursorMoved(context)
| _ => Ignored
};
};

let parse = (t: string, msg: Msgpck.t) => {
let msgs =
switch (t, msg) {
Expand All @@ -76,6 +101,14 @@ let parse = (t: string, msg: Msgpck.t) => {
),
),
]
| (
"oni_plugin_notify",
M.List([
M.List([M.String("autocmd"), M.String(autocmd), M.List(args)]),
]),
) =>
let result = parseAutoCommand(autocmd, args);
[result];
| ("redraw", M.List(msgs)) => parseRedraw(msgs)
| _ => [Ignored]
};
Expand All @@ -87,6 +120,7 @@ let show = (n: t) => {
switch (n) {
| Redraw => "redraw"
| ModeChanged(s) => "mode changed: " ++ s
| CursorMoved(c) => "cursor moved: " ++ AutoCommandContext.show(c)
| BufferLines(_) => "buffer lines"
| _ => "unknown"
};
Expand Down
23 changes: 23 additions & 0 deletions src/editor/Neovim/Types.re
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,26 @@ module EditorState = {
cursorPosition: CursorPosition.t,
};
};

module AutoCommandContext = {
type t = {
activeBufferId: int,
cursorLine: int,
cursorColumn: int,
};

let create = (~activeBufferId, ~cursorLine, ~cursorColumn, ()) => {
activeBufferId,
cursorLine,
cursorColumn,
};

let show = (v: t) => {
"activeBufferId: "
++ string_of_int(v.activeBufferId)
++ " line: "
++ string_of_int(v.cursorLine)
++ " column: "
++ string_of_int(v.cursorColumn);
};
};
17 changes: 15 additions & 2 deletions src/editor/bin/Oni2.re
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ let init = app => {

UI.start(w, render);

let nvim = NeovimProcess.start(~neovimPath, ~args=[|"--embed"|]);
let initVimPath =
Revery_Core.Environment.getExecutingDirectory() ++ "init.vim";
Core.Log.debug("initVimPath: " ++ initVimPath);

let nvim =
NeovimProcess.start(~neovimPath, ~args=[|"-u", initVimPath, "--embed"|]);
let msgpackTransport =
MsgpackTransport.make(
~onData=nvim.stdout.onData,
Expand Down Expand Up @@ -154,7 +159,15 @@ let init = app => {
| ModeChanged("normal") => Core.Actions.ChangeMode(Normal)
| ModeChanged("insert") => Core.Actions.ChangeMode(Insert)
| ModeChanged(_) => Core.Actions.ChangeMode(Other)
| BufferLines(bc) => Core.Actions.BufferUpdate(Core.Types.BufferUpdate.create(~startLine=bc.firstLine, ~endLine=bc.lastLine, ~lines=bc.lines, ()))
| BufferLines(bc) =>
Core.Actions.BufferUpdate(
Core.Types.BufferUpdate.create(
~startLine=bc.firstLine,
~endLine=bc.lastLine,
~lines=bc.lines,
(),
),
)
| _ => Noop
};

Expand Down

0 comments on commit 61fea4d

Please sign in to comment.