Skip to content

Commit

Permalink
💥 Read meta only once in initialization
Browse files Browse the repository at this point in the history
This is a minor but a breaking change. This change affects the
`g:denops#debug` variable. After this commit, the variable must
be configured prior to the denops initialization.
  • Loading branch information
lambdalisue committed Dec 30, 2023
1 parent 860918e commit e91e0a1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 30 deletions.
13 changes: 5 additions & 8 deletions autoload/denops/plugin.vim
Original file line number Diff line number Diff line change
Expand Up @@ -64,35 +64,32 @@ function! denops#plugin#register(plugin, ...) abort
let l:script = a:1
let l:options = a:0 > 1 ? a:2 : {}
endif
let l:meta = denops#_internal#meta#get()
let l:options = s:options(l:options, {
\ 'mode': 'error',
\})
return s:register(a:plugin, l:script, l:meta, l:options)
return s:register(a:plugin, l:script, l:options)
endfunction

function! denops#plugin#reload(plugin, ...) abort
let l:options = a:0 > 0 ? a:1 : {}
let l:meta = denops#_internal#meta#get()
let l:options = s:options(l:options, {
\ 'mode': 'error',
\})
let l:trace = s:trace(a:plugin)
let l:args = [a:plugin, l:meta, l:options, l:trace]
let l:args = [a:plugin, l:options, l:trace]
call denops#_internal#echo#debug(printf('reload plugin: %s', l:args))
call denops#_internal#server#chan#notify('invoke', ['reload', l:args])
endfunction

function! denops#plugin#discover(...) abort
let l:meta = denops#_internal#meta#get()
let l:options = s:options(a:0 > 0 ? a:1 : {}, {
\ 'mode': 'skip',
\})
let l:plugins = {}
call s:gather_plugins(l:plugins)
call denops#_internal#echo#debug(printf('%d plugins are discovered', len(l:plugins)))
for [l:plugin, l:script] in items(l:plugins)
call s:register(l:plugin, l:script, l:meta, l:options)
call s:register(l:plugin, l:script, l:options)
endfor
endfunction

Expand Down Expand Up @@ -134,11 +131,11 @@ function! s:options(base, default) abort
return l:options
endfunction

function! s:register(plugin, script, meta, options) abort
function! s:register(plugin, script, options) abort
execute printf('doautocmd <nomodeline> User DenopsSystemPluginRegister:%s', a:plugin)
let l:script = denops#_internal#path#norm(a:script)
let l:trace = s:trace(a:plugin)
let l:args = [a:plugin, l:script, a:meta, a:options, l:trace]
let l:args = [a:plugin, l:script, a:options, l:trace]
call denops#_internal#echo#debug(printf('register plugin: %s', l:args))
call denops#_internal#server#chan#notify('invoke', ['register', l:args])
endfunction
Expand Down
5 changes: 4 additions & 1 deletion denops/@denops-private/cli.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ensure } from "https://deno.land/x/[email protected]/mod.ts";
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
import { pop } from "https://deno.land/x/[email protected]/mod.ts";
import { usingResource } from "https://deno.land/x/[email protected]/mod.ts#^";
import { Service } from "./service.ts";
import { Vim } from "./host/vim.ts";
import { Neovim } from "./host/nvim.ts";
import { isMeta } from "./util.ts";

type Host = typeof Vim | typeof Neovim;

Expand Down Expand Up @@ -35,7 +37,8 @@ async function handleConn(conn: Deno.Conn): Promise<void> {

// Create host and service
await usingResource(new hostClass(r2, writer), async (host) => {
await usingResource(new Service(host), async (_service) => {
const meta = ensure(await host.call("denops#_internal#meta#get"), isMeta);
await usingResource(new Service(host, meta), async (_service) => {
await host.call("execute", "doautocmd <nomodeline> User DenopsReady");
await host.waitClosed();
if (!quiet) {
Expand Down
7 changes: 2 additions & 5 deletions denops/@denops-private/host/invoker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { is } from "https://deno.land/x/[email protected]/mod.ts";
import { Service } from "../service.ts";
import type { Meta } from "../../@denops/mod.ts";

export type RegisterOptions = {
/**
Expand Down Expand Up @@ -33,20 +32,18 @@ export class Invoker {
register(
name: string,
script: string,
meta: Meta,
options: RegisterOptions,
trace: boolean,
): void {
this.#service.register(name, script, meta, options, trace);
this.#service.register(name, script, options, trace);
}

reload(
name: string,
meta: Meta,
options: ReloadOptions,
trace: boolean,
): void {
this.#service.reload(name, meta, options, trace);
this.#service.reload(name, options, trace);
}

dispatch(name: string, fn: string, args: unknown[]): Promise<unknown> {
Expand Down
30 changes: 14 additions & 16 deletions denops/@denops-private/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,33 @@ export class Service implements Disposable {
session: Session;
client: Client;
}>;
host: Host;
readonly host: Host;
readonly meta: Meta;

constructor(host: Host) {
constructor(host: Host, meta: Meta) {
this.#plugins = new Map();
this.host = host;
this.host.register(new Invoker(this));
this.meta = meta;
}

register(
name: string,
script: string,
meta: Meta,
options: RegisterOptions,
trace: boolean,
): void {
const plugin = this.#plugins.get(name);
if (plugin) {
if (options.mode === "reload") {
if (meta.mode === "debug") {
if (this.meta.mode === "debug") {
console.log(
`A denops plugin '${name}' is already registered. Reload`,
);
}
plugin.worker.terminate();
} else if (options.mode === "skip") {
if (meta.mode === "debug") {
if (this.meta.mode === "debug") {
console.log(`A denops plugin '${name}' is already registered. Skip`);
}
return;
Expand All @@ -71,10 +72,13 @@ export class Service implements Disposable {
// https://github.com/vim-denops/denops.vim/issues/227
const suffix = `#${performance.now()}`;
const scriptUrl = resolveScriptUrl(script);
worker.postMessage({ scriptUrl: `${scriptUrl}${suffix}`, meta, trace });
worker.postMessage({
scriptUrl: `${scriptUrl}${suffix}`,
meta: this.meta,
trace,
});
const session = buildServiceSession(
name,
meta,
readableStreamFromWorker(worker),
writableStreamFromWorker(worker),
this,
Expand All @@ -90,24 +94,21 @@ export class Service implements Disposable {

reload(
name: string,
meta: Meta,
options: ReloadOptions,
trace: boolean,
): void {
const plugin = this.#plugins.get(name);
if (!plugin) {
if (options.mode === "skip") {
if (meta.mode === "debug") {
if (this.meta.mode === "debug") {
console.log(`A denops plugin '${name}' is not registered yet. Skip`);
}
return;
} else {
throw new Error(`A denops plugin '${name}' is not registered yet`);
}
}
this.register(name, plugin.script, { ...meta, mode: "release" }, {
mode: "reload",
}, trace);
this.register(name, plugin.script, { mode: "reload" }, trace);
}

async dispatch(name: string, fn: string, args: unknown[]): Promise<unknown> {
Expand Down Expand Up @@ -138,7 +139,6 @@ export class Service implements Disposable {

function buildServiceSession(
name: string,
meta: Meta,
reader: ReadableStream<Uint8Array>,
writer: WritableStream<Uint8Array>,
service: Service,
Expand All @@ -160,9 +160,7 @@ function buildServiceSession(
session.dispatcher = {
reload: (trace) => {
assert(trace, is.Boolean);
service.reload(name, meta, {
mode: "skip",
}, trace);
service.reload(name, { mode: "skip" }, trace);
return Promise.resolve();
},

Expand Down
1 change: 1 addition & 0 deletions doc/denops.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ VARIABLE *denops-variable*
*g:denops#debug*
Set 1 to enable debug mode. In debug mode, the additional debug
messages of denops itself will be shown.
This variable must be configured prior to the denops initialization.

Default: 0

Expand Down

0 comments on commit e91e0a1

Please sign in to comment.