Skip to content

Commit

Permalink
Improve UI APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Jul 16, 2024
1 parent c907426 commit 33faa70
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
12 changes: 8 additions & 4 deletions autoload/ddu/ui.vim
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,17 @@ function ddu#ui#multi_actions(
endfunction

function ddu#ui#get_item(name=b:->get('ddu_ui_name', '')) abort
call ddu#ui_sync_action(a:name, 'getItem', {})
return b:->get('ddu_ui_item', {})
endfunction

function ddu#ui#get_items(name=b:->get('ddu_ui_name', '')) abort
call ddu#ui_sync_action(a:name, 'getItems', {})
return b:->get('ddu_ui_items', [])
endfunction

function ddu#ui#get_selected_items(name=b:->get('ddu_ui_name', '')) abort
call ddu#ui_sync_action(a:name, 'getSelectedItems', {})
return b:->get('ddu_ui_selected_items', [])
return b:->get('ddu_ui_selected_items', [])->empty()
\ ? [ddu#ui#get_items(a:name)]
\ : b:->get('ddu_ui_selected_items', [])
endfunction

function ddu#ui#visible(
Expand All @@ -46,3 +45,8 @@ function ddu#ui#winids(name=b:->get('ddu_ui_name', '')) abort
return ddu#denops#_running() ?
\ ddu#denops#_request('uiWinids', [a:name]) : []
endfunction

function ddu#ui#update_cursor(name=b:->get('ddu_ui_name', '')) abort
return ddu#denops#_running() ?
\ ddu#denops#_request('uiUpdateCursor', [a:name]) : []
endfunction
8 changes: 8 additions & 0 deletions denops/ddu/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,5 +505,13 @@ export const main: Entrypoint = (denops: Denops) => {
const ddu = getDdu(name);
return await ddu.uiWinids(denops);
},
async uiUpdateCursor(
arg1: unknown,
): Promise<void> {
const name = ensure(arg1, is.String) as string;

const ddu = getDdu(name);
await ddu.uiUpdateCursor(denops);
},
};
};
10 changes: 10 additions & 0 deletions denops/ddu/base/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ export type WinidArguments<Params extends BaseUiParams> = {
uiParams: Params;
};

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

export type UiActionArguments<Params extends BaseUiParams> = {
denops: Denops;
ddu: Ddu;
Expand Down Expand Up @@ -167,6 +175,8 @@ export abstract class BaseUi<
return [];
}

updateCursor(_args: UpdateCursorArguments<Params>): void | Promise<void> {}

actions: UiActions<Params> = {};

abstract params(): Params;
Expand Down
21 changes: 21 additions & 0 deletions denops/ddu/ddu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,27 @@ export class Ddu {
});
}

async uiUpdateCursor(
denops: Denops,
) {
const [ui, uiOptions, uiParams] = await getUi(
denops,
this.#loader,
this.#options,
);
if (!ui?.updateCursor || this.#quitted) {
return;
}

await ui.updateCursor({
denops,
context: this.#context,
options: this.#options,
uiOptions,
uiParams,
});
}

async setInput(denops: Denops, input: string) {
if (this.#options.expandInput) {
input = await fn.expand(denops, input) as string;
Expand Down
20 changes: 14 additions & 6 deletions doc/ddu.txt
Original file line number Diff line number Diff line change
Expand Up @@ -585,22 +585,21 @@ ddu#ui#do_action({action-name}[, {params}[, {ui-name}]])
ddu#ui#get_item()
Return the current cursor item as |Dictionary| from current
buffer UI.
NOTE: current UI must support "getItem" action.
NOTE: You must not call it in |autocmd|.
NOTE: current UI must update "b:ddu_ui_item" when current
cursor is changed.

*ddu#ui#get_items()*
ddu#ui#get_items()
Return the current items as |List| of |Dictionary| from
current buffer UI.
NOTE: current UI must support "getItems" action.
NOTE: You must not call it in |autocmd|.
NOTE: current UI must update "b:ddu_ui_items" when redraw.

*ddu#ui#get_selected_items()*
ddu#ui#get_selected_items()
Return the selected items as |List| of |Dictionary| from
current buffer UI.
NOTE: current UI must support "getSelectedItems" action.
NOTE: You must not call it in |autocmd|.
NOTE: current UI must update "b:ddu_ui_selected_items" when
select items.

*ddu#ui#multi_actions()*
ddu#ui#multi_actions([{action-name1}[, {params1}], ...][, {ui-name}])
Expand Down Expand Up @@ -628,6 +627,11 @@ ddu#ui#sync_action({action-name}[, {params}[, {ui-name}]])
NOTE: It is slow than |ddu#ui#do_action()|.
NOTE: You must not call it in |autocmd|.

*ddu#ui#update_cursor()*
ddu#ui#update_cursor({name})
Update UI cursor.
{name} is specified ddu name(|ddu-option-name|).

*ddu#ui#visible()*
ddu#ui#visible({name}[, {tabnr}])
Return v:true if UI is visible.
Expand Down Expand Up @@ -1184,6 +1188,10 @@ redraw (function) (Required)
searchItem (function) (Optional)
Called when search item.

*ddu-ui-attribute-updateCursor*
updateCursor (function) (Optional)
Called to update UI cursor.

*ddu-ui-attribute-visible*
visible (function) (Optional)
Called to get UI is visible.
Expand Down

0 comments on commit 33faa70

Please sign in to comment.