-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path_component.ts
More file actions
214 lines (191 loc) · 5.58 KB
/
_component.ts
File metadata and controls
214 lines (191 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import type { Denops } from "@denops/std";
import * as popup from "@denops/std/popup";
import type { Border } from "@vim-fall/core/theme";
import type { Dimension } from "@vim-fall/core/coordinator";
const HIGHLIGHT_NORMAL = "FallNormal";
const HIHGLIGHT_BORDER = "FallBorder";
/**
* Properties that define the appearance and behavior of a component.
*/
export type ComponentProperties = {
/** The title of the component */
readonly title?: string;
/** The border style for the component */
readonly border?: Border;
/** The z-index of the component */
readonly zindex?: number;
};
/**
* Information about the component's current state, including window ID and buffer number.
*/
export type ComponentInfo = {
/** The buffer number associated with the component */
readonly bufnr: number;
/** The window ID associated with the component */
readonly winid: number;
/** The dimension (size and position) of the component */
readonly dimension: Readonly<Dimension>;
};
/**
* Options for interacting with the component, such as abort signals.
*/
export type ComponentOptions = {
/** Signal used to abort operations */
signal?: AbortSignal;
};
/**
* The base interface for a component that can be opened, moved, updated, rendered, and closed.
* Provides methods to manipulate the component's window and update its properties.
*/
export type Component = AsyncDisposable & {
/**
* Opens the component window with the specified dimensions.
* @param denops The Denops instance.
* @param dimension The dimensions of the component.
* @param options Additional options such as the abort signal.
* @returns A disposable object to manage the component window's lifecycle.
*/
open(
denops: Denops,
dimension: Readonly<Dimension>,
options?: ComponentOptions,
): Promise<AsyncDisposable>;
/**
* Moves the component window to new dimensions.
* @param denops The Denops instance.
* @param dimension The new dimensions of the component.
* @param options Additional options such as the abort signal.
*/
move(
denops: Denops,
dimension: Readonly<Partial<Dimension>>,
options?: ComponentOptions,
): Promise<void>;
/**
* Updates the component's properties.
* @param denops The Denops instance.
* @param properties The new properties of the component.
* @param options Additional options such as the abort signal.
*/
update(
denops: Denops,
properties: Readonly<ComponentProperties>,
options?: ComponentOptions,
): Promise<void>;
/**
* Renders the component.
* @param denops The Denops instance.
* @param options Additional options such as the abort signal.
* @returns A promise that resolves to `true` or `void` when rendering is complete.
* If the component does not need to render anything, it can resolve with `void`.
*/
render(
denops: Denops,
options?: ComponentOptions,
): Promise<true | void>;
/**
* Closes the component.
*/
close(): Promise<void>;
};
/**
* A base class for a component that can be opened, moved, updated, rendered, and closed.
* This class uses the `popup` module to manage the window and display the component.
*/
export class BaseComponent implements Component {
#opened?: {
readonly window: popup.PopupWindow;
readonly dimension: Readonly<Dimension>;
};
protected properties: Readonly<ComponentProperties>;
constructor(properties: Readonly<ComponentProperties> = {}) {
this.properties = properties;
}
/**
* Returns information about the component's current state (buffer number, window ID, and dimensions).
* If the component is not opened, returns `undefined`.
*/
get info(): Readonly<ComponentInfo> | undefined {
if (!this.#opened) {
return undefined;
}
const { bufnr, winid } = this.#opened.window;
return { bufnr, winid, dimension: this.#opened.dimension };
}
async open(
denops: Denops,
dimension: Readonly<Dimension>,
{ signal }: ComponentOptions = {},
): Promise<AsyncDisposable> {
if (this.#opened) {
return this;
}
signal?.throwIfAborted();
this.#opened = {
window: await popup.open(denops, {
...dimension,
...this.properties,
relative: "editor",
anchor: "NW",
highlight: {
normal: HIGHLIGHT_NORMAL,
border: HIHGLIGHT_BORDER,
},
noRedraw: true,
}),
dimension,
};
return this;
}
async move(
denops: Denops,
dimension: Readonly<Partial<Dimension>>,
{ signal }: ComponentOptions = {},
): Promise<void> {
if (this.#opened) {
this.#opened = {
...this.#opened,
dimension: {
...this.#opened.dimension,
...dimension,
},
};
signal?.throwIfAborted();
await popup.config(denops, this.#opened.window.winid, {
...dimension,
relative: "editor",
noRedraw: true,
});
}
}
async update(
denops: Denops,
properties: Readonly<ComponentProperties>,
{ signal }: ComponentOptions = {},
): Promise<void> {
this.properties = {
...this.properties,
...properties,
};
if (this.#opened) {
signal?.throwIfAborted();
await popup.config(denops, this.#opened.window.winid, {
...properties,
noRedraw: true,
});
}
}
render(
_denops: Denops,
_options: ComponentOptions = {},
): Promise<true | void> {
return Promise.resolve(true);
}
async close(): Promise<void> {
await this.#opened?.window.close();
this.#opened = undefined;
}
async [Symbol.asyncDispose]() {
await this.close();
}
}