Skip to content

Commit

Permalink
Merge pull request #155 from 4513ECHO/resolve_circular_reference
Browse files Browse the repository at this point in the history
[WIP] Resolve circular reference
  • Loading branch information
Shougo authored Aug 30, 2024
2 parents 45a479e + 8f9183c commit 3b9c29a
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 188 deletions.
5 changes: 3 additions & 2 deletions denops/ddc/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Ddc } from "./ddc.ts";
import { ContextBuilder, type ContextCallbacks } from "./context.ts";
import { ContextBuilderImpl } from "./context.ts";
import type {
ContextCallbacks,
DdcEvent,
DdcExtType,
DdcItem,
Expand All @@ -26,7 +27,7 @@ import { toFileUrl } from "jsr:@std/path@~1.0.2/to-file-url";
export const main: Entrypoint = (denops: Denops) => {
const loader = new Loader();
const ddc = new Ddc(loader);
const contextBuilder = new ContextBuilder();
const contextBuilder = new ContextBuilderImpl();
const cbContext = createCallbackContext();
const lock = new Lock(0);
let queuedEvent: DdcEvent | null = null;
Expand Down
3 changes: 1 addition & 2 deletions denops/ddc/base/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { ContextBuilder } from "../context.ts";
import type { DdcExtType } from "../types.ts";
import type { ContextBuilder, DdcExtType } from "../types.ts";

import type { Denops } from "jsr:@denops/std@~7.1.0";

Expand Down
11 changes: 5 additions & 6 deletions denops/ddc/base/filter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {
BaseParams,
Context,
DdcEvent,
DdcOptions,
Expand All @@ -10,15 +11,13 @@ import type {

import type { Denops } from "jsr:@denops/std@~7.1.0";

export type BaseFilterParams = Record<string, unknown>;

export type OnInitArguments<Params extends BaseFilterParams> = {
export type OnInitArguments<Params extends BaseParams> = {
denops: Denops;
filterOptions: FilterOptions;
filterParams: Params;
};

export type OnEventArguments<Params extends BaseFilterParams> = {
export type OnEventArguments<Params extends BaseParams> = {
denops: Denops;
context: Context;
onCallback: OnCallback;
Expand All @@ -27,7 +26,7 @@ export type OnEventArguments<Params extends BaseFilterParams> = {
filterParams: Params;
};

export type FilterArguments<Params extends BaseFilterParams> = {
export type FilterArguments<Params extends BaseParams> = {
denops: Denops;
context: Context;
onCallback: OnCallback;
Expand All @@ -39,7 +38,7 @@ export type FilterArguments<Params extends BaseFilterParams> = {
items: Item[];
};

export abstract class BaseFilter<Params extends BaseFilterParams> {
export abstract class BaseFilter<Params extends BaseParams> {
name = "";
isInitialized = false;
apiVersion = 4;
Expand Down
24 changes: 8 additions & 16 deletions denops/ddc/base/source.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {
BaseParams,
Context,
DdcEvent,
DdcGatherItems,
Expand All @@ -10,31 +11,26 @@ import type {
SourceOptions,
} from "../types.ts";
import { convertKeywordPattern } from "../utils.ts";
import type { Loader } from "../loader.ts";

import type { Denops } from "jsr:@denops/std@~7.1.0";

export type BaseSourceParams = Record<string, unknown>;

export type OnInitArguments<Params extends BaseSourceParams> = {
export type OnInitArguments<Params extends BaseParams> = {
denops: Denops;
sourceOptions: SourceOptions;
sourceParams: Params;
loader: Loader;
};

export type OnEventArguments<Params extends BaseSourceParams> = {
export type OnEventArguments<Params extends BaseParams> = {
denops: Denops;
context: Context;
onCallback: OnCallback;
options: DdcOptions;
sourceOptions: SourceOptions;
sourceParams: Params;
loader: Loader;
};

export type OnCompleteDoneArguments<
Params extends BaseSourceParams,
Params extends BaseParams,
UserData extends unknown = unknown,
> = {
denops: Denops;
Expand All @@ -43,13 +39,12 @@ export type OnCompleteDoneArguments<
options: DdcOptions;
sourceOptions: SourceOptions;
sourceParams: Params;
loader: Loader;
// To prevent users from accessing internal variables.
userData: UserData;
};

export type GetPreviewerArguments<
Params extends BaseSourceParams,
Params extends BaseParams,
UserData extends unknown = unknown,
> = {
denops: Denops;
Expand All @@ -59,34 +54,31 @@ export type GetPreviewerArguments<
sourceParams: Params;
item: Item<UserData>;
previewContext: PreviewContext;
loader: Loader;
};

export type GetCompletePositionArguments<Params extends BaseSourceParams> = {
export type GetCompletePositionArguments<Params extends BaseParams> = {
denops: Denops;
context: Context;
onCallback: OnCallback;
options: DdcOptions;
sourceOptions: SourceOptions;
sourceParams: Params;
loader: Loader;
};

export type GatherArguments<Params extends BaseSourceParams> = {
export type GatherArguments<Params extends BaseParams> = {
denops: Denops;
context: Context;
onCallback: OnCallback;
options: DdcOptions;
sourceOptions: SourceOptions;
sourceParams: Params;
loader: Loader;
completePos: number;
completeStr: string;
isIncomplete?: boolean;
};

export abstract class BaseSource<
Params extends BaseSourceParams,
Params extends BaseParams,
UserData extends unknown = unknown,
> {
name = "";
Expand Down
22 changes: 13 additions & 9 deletions denops/ddc/base/ui.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import type { Context, DdcItem, DdcOptions, UiOptions } from "../types.ts";
import type {
BaseParams,
Context,
DdcItem,
DdcOptions,
UiOptions,
} from "../types.ts";

import type { Denops } from "jsr:@denops/std@~7.1.0";

export type BaseUiParams = Record<string, unknown>;

export type OnInitArguments<Params extends BaseUiParams> = {
export type OnInitArguments<Params extends BaseParams> = {
denops: Denops;
uiOptions: UiOptions;
uiParams: Params;
};

export type SkipCompletionArguments<Params extends BaseUiParams> = {
export type SkipCompletionArguments<Params extends BaseParams> = {
denops: Denops;
context: Context;
options: DdcOptions;
uiOptions: UiOptions;
uiParams: Params;
};

export type ShowArguments<Params extends BaseUiParams> = {
export type ShowArguments<Params extends BaseParams> = {
denops: Denops;
context: Context;
options: DdcOptions;
Expand All @@ -28,23 +32,23 @@ export type ShowArguments<Params extends BaseUiParams> = {
uiParams: Params;
};

export type HideArguments<Params extends BaseUiParams> = {
export type HideArguments<Params extends BaseParams> = {
denops: Denops;
context: Context;
options: DdcOptions;
uiOptions: UiOptions;
uiParams: Params;
};

export type VisibleArguments<Params extends BaseUiParams> = {
export type VisibleArguments<Params extends BaseParams> = {
denops: Denops;
context: Context;
options: DdcOptions;
uiOptions: UiOptions;
uiParams: Params;
};

export abstract class BaseUi<Params extends BaseUiParams> {
export abstract class BaseUi<Params extends BaseParams> {
name = "";
isInitialized = false;
apiVersion = 2;
Expand Down
40 changes: 13 additions & 27 deletions denops/ddc/context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type {
BaseFilterParams,
BaseSourceParams,
BaseUiParams,
BaseParams,
Context,
ContextBuilder,
ContextCallback,
ContextCallbacks,
DdcEvent,
DdcOptions,
FilterOptions,
Expand Down Expand Up @@ -42,19 +43,9 @@ function overwrite<T>(a: T, b: Partial<T>): T {
export const mergeUiOptions: Merge<UiOptions> = overwrite;
export const mergeSourceOptions: Merge<SourceOptions> = overwrite;
export const mergeFilterOptions: Merge<FilterOptions> = overwrite;
export const mergeUiParams: Merge<BaseUiParams> = overwrite;
export const mergeSourceParams: Merge<BaseSourceParams> = overwrite;
export const mergeFilterParams: Merge<BaseFilterParams> = overwrite;

export type ContextCallback =
| string
| ((denops: Denops) => Promise<Partial<DdcOptions>>);

export type ContextCallbacks = {
global: ContextCallback;
filetype: Record<string, ContextCallback>;
buffer: Record<number, ContextCallback>;
};
export const mergeUiParams: Merge<BaseParams> = overwrite;
export const mergeSourceParams: Merge<BaseParams> = overwrite;
export const mergeFilterParams: Merge<BaseParams> = overwrite;

export function foldMerge<T>(
merge: Merge<T>,
Expand Down Expand Up @@ -416,21 +407,16 @@ function isNegligible(older: World, newer: World): boolean {
older.event === newer.event;
}

export class ContextBuilder {
export class ContextBuilderImpl implements ContextBuilder {
#lastWorld: World = initialWorld();
#custom: Custom = new Custom();

// Re-export for denops.dispatcher
async _cacheWorld(denops: Denops, event: DdcEvent): Promise<World> {
return await cacheWorld(denops, event);
}

async createContext(
denops: Denops,
event: DdcEvent,
options: UserOptions = {},
): Promise<[boolean, Context, DdcOptions]> {
const world = await this._cacheWorld(denops, event);
const world = await cacheWorld(denops, event);
const old = this.#lastWorld;
this.#lastWorld = world;
let skip = false;
Expand Down Expand Up @@ -458,9 +444,9 @@ export class ContextBuilder {

const userOptions = await this.#getUserOptions(denops, world, options);

await this.validate(denops, "options", userOptions, defaultDdcOptions());
await this.#validate(denops, "options", userOptions, defaultDdcOptions());
for (const key in userOptions.sourceOptions) {
await this.validate(
await this.#validate(
denops,
"sourceOptions",
userOptions.sourceOptions[key],
Expand Down Expand Up @@ -513,11 +499,11 @@ export class ContextBuilder {
return this.#custom.buffer;
}
async getCurrent(denops: Denops): Promise<DdcOptions> {
const world = await this._cacheWorld(denops, "Manual");
const world = await cacheWorld(denops, "Manual");
return this.#getUserOptions(denops, world);
}

async validate(
async #validate(
denops: Denops,
name: string,
options: Record<string, unknown>,
Expand Down
Loading

0 comments on commit 3b9c29a

Please sign in to comment.