Skip to content

Commit

Permalink
Add getBaseText() for column
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed May 17, 2024
1 parent 869d346 commit 2bf1bcd
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
14 changes: 14 additions & 0 deletions denops/ddu/base/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ export type OnInitArguments<Params extends BaseColumnParams> = {
columnParams: Params;
};

export type GetBaseTextArguments<Params extends BaseColumnParams> = {
denops: Denops;
context: Context;
options: DduOptions;
columnOptions: ColumnOptions;
columnParams: Params;
item: DduItem;
};

export type GetLengthArguments<Params extends BaseColumnParams> = {
denops: Denops;
context: Context;
Expand All @@ -33,6 +42,7 @@ export type GetTextArguments<Params extends BaseColumnParams> = {
startCol: number;
endCol: number;
item: DduItem;
baseText?: string;
};

export type GetTextResult = {
Expand All @@ -49,6 +59,10 @@ export abstract class BaseColumn<Params extends BaseColumnParams> {

onInit(_args: OnInitArguments<Params>): void | Promise<void> {}

abstract getBaseText(
{}: GetBaseTextArguments<Params>,
): string | Promise<string>;

abstract getLength({}: GetLengthArguments<Params>): number | Promise<number>;

abstract getText(
Expand Down
1 change: 1 addition & 0 deletions denops/ddu/ddu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ export class Ddu {
item.treePath &&
this.#isExpanded(convertTreePath(item.treePath)),
),
__columnTexts: {},
__groupedPath: "",
};
}
Expand Down
22 changes: 17 additions & 5 deletions denops/ddu/ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,11 +567,6 @@ export async function callColumns(
return items;
}

for (const item of items) {
item.display = "";
item.highlights = [];
}

type CachedColumn = {
column: BaseColumn<BaseColumnParams>;
columnOptions: ColumnOptions;
Expand Down Expand Up @@ -608,6 +603,11 @@ export async function callColumns(
};
}

for (const item of items) {
item.display = "";
item.highlights = [];
}

for (const item of items) {
let startCol = 1;
for (const index of userColumns.keys()) {
Expand All @@ -616,6 +616,17 @@ export async function callColumns(
continue;
}

if (!item.__columnTexts[index] && cachedColumn.column.getBaseText) {
item.__columnTexts[index] = await cachedColumn.column.getBaseText({
denops,
context,
options,
columnOptions: cachedColumn.columnOptions,
columnParams: cachedColumn.columnParams,
item,
});
}

const text = await cachedColumn.column.getText({
denops,
context,
Expand All @@ -625,6 +636,7 @@ export async function callColumns(
startCol,
endCol: startCol + cachedColumn.length,
item,
baseText: item.__columnTexts[index],
});

if (text.highlights && item.highlights) {
Expand Down
1 change: 1 addition & 0 deletions denops/ddu/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export type DduItem =
__sourceName: string;
__level: number;
__expanded: boolean;
__columnTexts: Record<number, string>;
__groupedPath: string;
};

Expand Down
5 changes: 5 additions & 0 deletions doc/ddu.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,11 @@ NOTE: If you call Vim functions, it is not asynchronous.
------------------------------------------------------------------------------
COLUMN ATTRIBUTES *ddu-column-attributes*

*ddu-column-attribute-getBaseText*
getBaseText (function) (Optional)
It is called to get column base text.
It is called once for the item.

*ddu-column-attribute-getLength*
getLength (function) (Required)
It is called to get column length.
Expand Down

0 comments on commit 2bf1bcd

Please sign in to comment.