Skip to content

Commit

Permalink
Merge pull request #5 from vim-denops/singleton
Browse files Browse the repository at this point in the history
Follow denops-deno 0.6
  • Loading branch information
lambdalisue committed Feb 20, 2021
2 parents 2e17e6c + 033be94 commit 057752a
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 193 deletions.
14 changes: 6 additions & 8 deletions autoload/denops/api.vim
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
function! denops#api#echo(text) abort
for line in split(a:msg, '\n')
echo line
endfor
function! denops#api#cmd(cmd, context) abort
call extend(l:, a:context)
call execute(a:cmd, '')
endfunction

function! denops#api#echomsg(text) abort
for line in split(a:msg, '\n')
echomsg line
endfor
function! denops#api#eval(expr, context) abort
call extend(l:, a:context)
return eval(a:expr)
endfunction
8 changes: 2 additions & 6 deletions denops/denops/cli/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ import { flags } from "../deps.ts";
import { Service } from "../service.ts";
import { createVim } from "../host/vim.ts";
import { createNeovim } from "../host/nvim.ts";
import { updateContext } from "../context.ts";

const options = flags.parse(Deno.args);
const context = updateContext({
mode: options.mode,
debug: options.debug,
});
const mode = options.mode ?? "neovim";

// Connect to the address
const address = JSON.parse(options.address);
const conn = await Deno.connect(address);

// Create host and start communication
const createHost = context.mode === "vim" ? createVim : createNeovim;
const createHost = mode === "vim" ? createVim : createNeovim;
const host = createHost(conn, conn);
const service = new Service(host);
host.registerService(service);
Expand Down
25 changes: 0 additions & 25 deletions denops/denops/context.ts

This file was deleted.

40 changes: 0 additions & 40 deletions denops/denops/context_test.ts

This file was deleted.

20 changes: 17 additions & 3 deletions denops/denops/deps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
export * as flags from "https://deno.land/std@0.86.0/flags/mod.ts";
export * as msgpackRpc from "https://deno.land/x/msgpack_rpc@v2.4/mod.ts";
export * as vimChannelCommand from "https://deno.land/x/vim_channel_command@v0.1/mod.ts";
export * as denops from "https://deno.land/x/denops@v0.5/mod.ts";

export type {
Dispatcher,
DispatcherFrom,
} from "https://deno.land/x/msgpack_rpc@v2.6/mod.ts";
export { Session } from "https://deno.land/x/msgpack_rpc@v2.6/mod.ts";

export type { Message as VimMessage } from "https://deno.land/x/vim_channel_command@v0.1/mod.ts";
export { Session as VimSession } from "https://deno.land/x/vim_channel_command@v0.1/mod.ts";

export type { Api, Context } from "https://deno.land/x/denops@v0.6/mod.ts";
export {
Denops,
isContext,
WorkerReader,
WorkerWriter,
} from "https://deno.land/x/denops@v0.6/mod.ts";
42 changes: 8 additions & 34 deletions denops/denops/host/base.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
import { Service } from "../service.ts";
import { Api, Context } from "../deps.ts";

/**
* A Host (Vim/Neovim) interface.
* Host (Vim/Neovim) interface.
*/
export interface Host {
/**
* Execute a command (expr) on the host.
*/
command(expr: string): Promise<void>;

/**
* Evaluate an expression (expr) on the host and return the result.
*/
eval(expr: string): Promise<unknown>;

/**
* Call a function on the host and return the result.
*/
call(fn: string, args: unknown[]): Promise<unknown>;

/**
* Echo text on the host.
*/
echo(text: string): Promise<void>;

/**
* Echo text on the host.
*/
echomsg(text: string): Promise<void>;

export interface Host extends Api {
/**
* Register service which is visible from the host through RPC.
*/
Expand All @@ -41,17 +17,15 @@ export interface Host {
}

export abstract class AbstractHost implements Host {
abstract command(expr: string): Promise<void>;
abstract eval(expr: string): Promise<unknown>;
abstract call(fn: string, args: unknown[]): Promise<unknown>;
abstract call(func: string, ...args: unknown[]): Promise<unknown>;
abstract registerService(sservice: Service): void;
abstract waitClosed(): Promise<void>;

async echo(text: string): Promise<void> {
await this.call("denops#api#echo", [text]);
async cmd(cmd: string, context: Context): Promise<void> {
await this.call("denops#api#cmd", cmd, context);
}

async echomsg(text: string): Promise<void> {
await this.call("denops#api#echomsg", [text]);
async eval(expr: string, context: Context): Promise<unknown> {
return await this.call("denops#api#eval", expr, context);
}
}
25 changes: 6 additions & 19 deletions denops/denops/host/nvim.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
import { msgpackRpc } from "../deps.ts";
import { DispatcherFrom, Session } from "../deps.ts";
import { AbstractHost } from "./base.ts";
import { Service } from "../service.ts";

class Neovim extends AbstractHost {
#session: msgpackRpc.Session;
#session: Session;
#listener: Promise<void>;

constructor(session: msgpackRpc.Session) {
constructor(session: Session) {
super();
this.#session = session;
this.#listener = this.#session.listen();
}

async command(expr: string): Promise<void> {
await this.#session.call("nvim_command", expr);
}

async eval(expr: string): Promise<unknown> {
return await this.#session.call("nvim_eval", expr);
}

async call(fn: string, args: unknown[]): Promise<unknown> {
async call(fn: string, ...args: unknown[]): Promise<unknown> {
return await this.#session.call("nvim_call_function", fn, args);
}

registerService(service: Service): void {
type Dispatcher = {
[K in keyof Service]: Service[K] extends (...args: infer Args) => unknown
? (...args: { [K in keyof Args]: unknown }) => Promise<unknown>
: never;
};
const dispatcher: Dispatcher = {
const dispatcher: DispatcherFrom<Service> = {
async register(name: unknown, script: unknown): Promise<unknown> {
if (typeof name !== "string") {
throw new Error(`'name' in 'register()' of host must be a string`);
Expand Down Expand Up @@ -70,6 +57,6 @@ export function createNeovim(
reader: Deno.Reader & Deno.Closer,
writer: Deno.Writer,
): Neovim {
const session = new msgpackRpc.Session(reader, writer);
const session = new Session(reader, writer);
return new Neovim(session);
}
24 changes: 7 additions & 17 deletions denops/denops/host/vim.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
import { vimChannelCommand } from "../deps.ts";
import { VimMessage, VimSession } from "../deps.ts";
import { AbstractHost } from "./base.ts";
import { Service } from "../service.ts";

class Vim extends AbstractHost {
#session: vimChannelCommand.Session;
#session: VimSession;
#listener: Promise<void>;

constructor(session: vimChannelCommand.Session) {
constructor(session: VimSession) {
super();
this.#session = session;
this.#listener = this.#session.listen();
}

async command(expr: string): Promise<void> {
await this.#session.ex(expr);
}

async eval(expr: string): Promise<unknown> {
return await this.#session.expr(expr);
}

async call(fn: string, args: unknown[]): Promise<unknown> {
return await this.#session.call(fn, ...args);
async call(func: string, ...args: unknown[]): Promise<unknown> {
return await this.#session.call(func, ...args);
}

registerService(service: Service): void {
this.#session.replaceCallback(async function (
message: vimChannelCommand.Message,
) {
this.#session.replaceCallback(async function (message: VimMessage) {
const [msgid, expr] = message;
let ok = null;
let err = null;
Expand All @@ -53,7 +43,7 @@ export function createVim(
reader: Deno.Reader & Deno.Closer,
writer: Deno.Writer,
): Vim {
const session = new vimChannelCommand.Session(reader, writer);
const session = new VimSession(reader, writer);
return new Vim(session);
}

Expand Down
Loading

0 comments on commit 057752a

Please sign in to comment.