Skip to content

Commit

Permalink
Add dynamicUi option
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Nov 4, 2024
1 parent 1bde4b1 commit feb4798
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 39 deletions.
54 changes: 23 additions & 31 deletions denops/ddc/context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type {
BaseParams,
Callback,
Context,
ContextBuilder,
ContextCallback,
ContextCallbacks,
DdcEvent,
DdcOptions,
Expand All @@ -12,7 +12,7 @@ import type {
UserOptions,
} from "./types.ts";
import { defaultSourceOptions } from "./base/source.ts";
import { printError } from "./utils.ts";
import { callCallback, printError } from "./utils.ts";

import type { Denops } from "jsr:@denops/std@~7.3.0";
import * as op from "jsr:@denops/std@~7.3.0/option";
Expand Down Expand Up @@ -65,6 +65,7 @@ export function defaultDdcOptions(): DdcOptions {
],
backspaceCompletion: false,
cmdlineSources: [],
dynamicUi: "",
filterOptions: {},
filterParams: {},
hideOnEvents: false,
Expand Down Expand Up @@ -204,32 +205,23 @@ class Custom {
bufnr: number,
options: UserOptions,
): Promise<DdcOptions> {
const callContextCallback = async (callback: ContextCallback) => {
if (!denops || !callback) {
return {};
}

if (is.String(callback)) {
if (callback === "") {
return {};
}

return await denops.call(
"denops#callback#call",
callback,
) as Partial<DdcOptions>;
} else {
return await callback(denops);
}
};

const contextGlobal = await callContextCallback(this.context.global);
const contextGlobal = await callCallback(
denops,
this.context.global,
{},
) as Partial<DdcOptions | null>;
const filetype = this.filetype[ft] || {};
const contextFiletype = await callContextCallback(
const contextFiletype = await callCallback(
denops,
this.context.filetype[ft],
);
{},
) as Partial<DdcOptions | null>;
const buffer = this.buffer[bufnr] || {};
const contextBuffer = await callContextCallback(this.context.buffer[bufnr]);
const contextBuffer = await callCallback(
denops,
this.context.buffer[bufnr],
{},
) as Partial<DdcOptions | null>;

return foldMerge(mergeDdcOptions, defaultDdcOptions, [
this.global,
Expand All @@ -254,15 +246,15 @@ class Custom {
this.buffer[bufnr] = options;
return this;
}
setContextGlobal(callback: ContextCallback): Custom {
setContextGlobal(callback: Callback): Custom {
this.context.global = callback;
return this;
}
setContextFiletype(callback: ContextCallback, ft: string): Custom {
setContextFiletype(callback: Callback, ft: string): Custom {
this.context.filetype[ft] = callback;
return this;
}
setContextBuffer(callback: ContextCallback, bufnr: number): Custom {
setContextBuffer(callback: Callback, bufnr: number): Custom {
this.context.buffer[bufnr] = callback;
return this;
}
Expand Down Expand Up @@ -528,13 +520,13 @@ export class ContextBuilderImpl implements ContextBuilder {
setBuffer(bufnr: number, options: Partial<DdcOptions>) {
this.#custom.setBuffer(bufnr, options);
}
setContextGlobal(callback: ContextCallback) {
setContextGlobal(callback: Callback) {
this.#custom.setContextGlobal(callback);
}
setContextFiletype(callback: ContextCallback, ft: string) {
setContextFiletype(callback: Callback, ft: string) {
this.#custom.setContextFiletype(callback, ft);
}
setContextBuffer(callback: ContextCallback, bufnr: number) {
setContextBuffer(callback: Callback, bufnr: number) {
this.#custom.setContextBuffer(callback, bufnr);
}

Expand Down
9 changes: 9 additions & 0 deletions denops/ddc/ddc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
getSource,
getUi,
} from "./ext.ts";
import { callCallback } from "./utils.ts";

import type { Denops } from "jsr:@denops/std@~7.3.0";
import * as autocmd from "jsr:@denops/std@~7.3.0/autocmd";
Expand Down Expand Up @@ -476,6 +477,14 @@ export class Ddc {
return;
}

const dynamicUi = await callCallback(denops, options.dynamicUi, {
completePos,
items,
}) as string | null;
if (dynamicUi) {
options.ui = dynamicUi;
}

await (async function write(ddc: Ddc) {
await batch(denops, async (denops: Denops) => {
await vars.g.set(denops, "ddc#_changedtick", context.changedTick);
Expand Down
17 changes: 9 additions & 8 deletions denops/ddc/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ export type Context = {
nextInput: string;
};

export type ContextCallback =
export type Callback =
| string
| ((denops: Denops) => Promise<Partial<DdcOptions>>);
| ((denops: Denops, args: Record<string, unknown>) => Promise<unknown>);

export type ContextCallbacks = {
global: ContextCallback;
filetype: Record<string, ContextCallback>;
buffer: Record<number, ContextCallback>;
global: Callback;
filetype: Record<string, Callback>;
buffer: Record<number, Callback>;
};

export interface ContextBuilder {
Expand All @@ -45,9 +45,9 @@ export interface ContextBuilder {
setGlobal(options: Partial<DdcOptions>): void;
setFiletype(ft: string, options: Partial<DdcOptions>): void;
setBuffer(bufnr: number, options: Partial<DdcOptions>): void;
setContextGlobal(callback: ContextCallback): void;
setContextFiletype(callback: ContextCallback, ft: string): void;
setContextBuffer(callback: ContextCallback, bufnr: number): void;
setContextGlobal(callback: Callback): void;
setContextFiletype(callback: Callback, ft: string): void;
setContextBuffer(callback: Callback, bufnr: number): void;
patchGlobal(options: Partial<DdcOptions>): void;
patchFiletype(ft: string, options: Partial<DdcOptions>): void;
patchBuffer(bufnr: number, options: Partial<DdcOptions>): void;
Expand All @@ -70,6 +70,7 @@ export type DdcOptions = {
autoCompleteEvents: DdcEvent[];
backspaceCompletion: boolean;
cmdlineSources: UserSource[] | Record<string, UserSource[]>;
dynamicUi: Callback;
filterOptions: Record<FilterName, Partial<FilterOptions>>;
filterParams: Record<FilterName, Partial<BaseParams>>;
hideOnEvents: boolean;
Expand Down
27 changes: 27 additions & 0 deletions denops/ddc/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { Callback } from "./types.ts";

import type { Denops } from "jsr:@denops/std@~7.3.0";
import * as op from "jsr:@denops/std@~7.3.0/option";

import { is } from "jsr:@core/unknownutil@~4.3.0/is";
import { assertEquals } from "jsr:@std/assert@~1.0.2/equals";

export async function convertKeywordPattern(
Expand Down Expand Up @@ -107,6 +110,30 @@ export async function safeStat(path: string): Promise<Deno.FileInfo | null> {
return null;
}

export async function callCallback(
denops: Denops | null,
callback: Callback,
args: Record<string, unknown>,
): Promise<unknown | null> {
if (!denops || !callback) {
return null;
}

if (is.String(callback)) {
if (callback === "") {
return null;
}

return await denops.call(
"denops#callback#call",
callback,
args,
);
} else {
return await callback(denops, args);
}
}

Deno.test("vimoption2ts", () => {
assertEquals(vimoption2ts("@,48-57,_,\\"), "a-zA-Z0-9_\\\\");
assertEquals(vimoption2ts("@,-,48-57,_"), "a-zA-Z0-9_-");
Expand Down
13 changes: 13 additions & 0 deletions doc/ddc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ cmdlineSources
\ '=': ['input'],
\ })
<
*ddc-option-dynamicUi*
dynamicUi
It is the function which overwrites |ddc-option-ui| after
gather items.
The function returns new ui name.
NOTE: {func} is evaluated after user input and it affects
completion performance.

Default: ""

*ddc-option-filterOptions*
filterOptions
It is a dictionary that maps filter names to its options.
Expand Down Expand Up @@ -1710,6 +1720,9 @@ https://github.com/Milly/ddc-unprintable
==============================================================================
COMPATIBILITY *ddc-compatibility*

2024.11.04
* Change callback function type.

2024.08.30
* Refactor types.ts.

Expand Down

0 comments on commit feb4798

Please sign in to comment.