Skip to content

Commit

Permalink
Merge pull request #270 from vim-denops/add-client-info
Browse files Browse the repository at this point in the history
👍 Add client info on Neovim
  • Loading branch information
lambdalisue authored Aug 6, 2023
2 parents 22e7ae3 + a2af54e commit 9f1b8cf
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 7 deletions.
25 changes: 25 additions & 0 deletions denops/@denops-private/host/nvim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from "https://deno.land/x/messagepack_rpc@v2.0.3/mod.ts#^";
import { Invoker, isInvokerMethod } from "./invoker.ts";
import { errorDeserializer, errorSerializer } from "../error.ts";
import { getVersionOr } from "../version.ts";
import { Host } from "./base.ts";

export class Neovim implements Host {
Expand All @@ -28,6 +29,26 @@ export class Neovim implements Host {
this.#client = new Client(this.#session, {
errorDeserializer,
});
getVersionOr({}).then((version) => {
this.#client.notify(
"nvim_set_client_info",
"denops",
version,
"remote",
{
invoke: {
async: false,
nargs: 2,
},
},
{
"website": "https://github.com/vim-denops/denops.vim",
"license": "MIT",
"logo":
"https://github.com/vim-denops/denops-logos/blob/main/20210403-main/denops.png?raw=true",
},
);
});
}

redraw(_force?: boolean): Promise<void> {
Expand All @@ -54,6 +75,10 @@ export class Neovim implements Host {

register(invoker: Invoker): void {
this.#session.dispatcher = {
nvim_error_event(type, message) {
console.error(`nvim_error_event(${type})`, message);
},

void() {
return Promise.resolve();
},
Expand Down
7 changes: 7 additions & 0 deletions denops/@denops-private/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { Disposable } from "https://deno.land/x/disposable@v1.1.1/mod.ts#^";
import { Host } from "./host/base.ts";
import { Invoker, RegisterOptions, ReloadOptions } from "./host/invoker.ts";
import { traceReadableStream, traceWritableStream } from "./trace_stream.ts";
import { errorDeserializer, errorSerializer } from "./error.ts";
import type { Meta } from "../@denops/mod.ts";

Expand Down Expand Up @@ -74,6 +75,7 @@ export class Service implements Disposable {
readableStreamFromWorker(worker),
writableStreamFromWorker(worker),
this,
trace,
);
this.#plugins.set(name, {
script,
Expand Down Expand Up @@ -137,7 +139,12 @@ function buildServiceSession(
reader: ReadableStream<Uint8Array>,
writer: WritableStream<Uint8Array>,
service: Service,
trace: boolean,
) {
if (trace) {
reader = traceReadableStream(reader, { prefix: "worker -> denops: " });
writer = traceWritableStream(writer, { prefix: "denops -> worker: " });
}
const session = new Session(reader, writer, {
errorSerializer,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import {

export function traceReadableStream(
stream: ReadableStream<Uint8Array>,
{ prefix, suffix }: { prefix?: string; suffix?: string } = {},
) {
return stream.pipeThrough(
new TransformStream({
transform(chunk, controller) {
const data = decode(chunk);
if (isMessage(data)) {
const m = stringifyMessage(data);
// Vim -> Denops
console.log("v -> d:", m);
console.log(`${prefix ?? ""}${m}${suffix ?? ""}`);
}
controller.enqueue(chunk);
},
Expand All @@ -25,6 +25,7 @@ export function traceReadableStream(

export function traceWritableStream(
stream: WritableStream<Uint8Array>,
{ prefix, suffix }: { prefix?: string; suffix?: string } = {},
) {
return pipeThroughFrom(
stream,
Expand All @@ -33,8 +34,7 @@ export function traceWritableStream(
const data = decode(chunk);
if (isMessage(data)) {
const m = stringifyMessage(data);
// Denops -> Vim
console.log("d -> v:", m);
console.log(`${prefix ?? ""}${m}${suffix ?? ""}`);
}
controller.enqueue(chunk);
},
Expand Down
31 changes: 31 additions & 0 deletions denops/@denops-private/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
dirname,
fromFileUrl,
} from "https://deno.land/std@0.197.0/path/mod.ts";
import { parse, SemVer } from "https://deno.land/std@0.197.0/semver/mod.ts";

const decoder = new TextDecoder();

export async function getVersion(): Promise<SemVer> {
const cwd = dirname(fromFileUrl(import.meta.url));
const command = new Deno.Command("git", {
args: ["describe", "--tags", "--always"],
cwd,
stdin: "null",
stdout: "piped",
stderr: "piped",
});
const { success, stdout, stderr } = await command.output();
if (!success) {
throw new Error(`failed to estimate version: ${decoder.decode(stderr)}`);
}
return parse(decoder.decode(stdout).trim());
}

export async function getVersionOr<T>(fallback: T): Promise<SemVer | T> {
try {
return await getVersion();
} catch {
return fallback;
}
}
6 changes: 3 additions & 3 deletions denops/@denops-private/worker/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import type { Denops, Meta } from "../../@denops/mod.ts";
import { DenopsImpl } from "../impl.ts";
import { patchConsole } from "./patch_console.ts";
import { traceReadableStream, traceWritableStream } from "./trace_stream.ts";
import { traceReadableStream, traceWritableStream } from "../trace_stream.ts";
import { errorDeserializer, errorSerializer } from "../error.ts";

const worker = self as unknown as Worker & { name: string };
Expand All @@ -23,8 +23,8 @@ async function main(
let reader = readableStreamFromWorker(worker);
let writer = writableStreamFromWorker(worker);
if (trace) {
reader = traceReadableStream(reader);
writer = traceWritableStream(writer);
reader = traceReadableStream(reader, { prefix: "worker -> plugin: " });
writer = traceWritableStream(writer, { prefix: "plugin -> worker: " });
}
const session = new Session(reader, writer, { errorSerializer });
session.onMessageError = (error, message) => {
Expand Down

0 comments on commit 9f1b8cf

Please sign in to comment.