Skip to content

Commit 0e36d24

Browse files
committed
chore: cleanup nvim-git-rebase-todo.ts
Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
1 parent a7f0c4d commit 0e36d24

File tree

1 file changed

+48
-156
lines changed

1 file changed

+48
-156
lines changed

nvim-git-rebase-todo/nvim-git-rebase-todo.ts

Lines changed: 48 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import util from "util";
55

66
import { NvimPlugin, Buffer, Window } from "neovim";
77
import { OpenWindowOptions } from "neovim/lib/api/Neovim";
8+
import { AutocmdOptions } from "neovim/lib/host/NvimPlugin";
89

910
const execAsync = util.promisify(cp.exec);
1011

@@ -17,27 +18,6 @@ export default function nvimGitRebaseTodo(plugin: NvimPlugin): void {
1718

1819
plugin.setOptions({ dev: false });
1920

20-
plugin.registerCommand(
21-
"EchoMessage",
22-
async () => {
23-
try {
24-
await vim.outWrite("Dayman (ah-ah-ah) \n");
25-
} catch (err) {
26-
console.error(err);
27-
}
28-
},
29-
{ sync: false }
30-
);
31-
32-
plugin.registerFunction(
33-
"SetLines",
34-
() =>
35-
vim
36-
.setLine("May I offer you an egg in these troubling times")
37-
.then(() => console.log("Line should be set")),
38-
{ sync: false }
39-
);
40-
4121
/**
4222
* TODO make actually configurable
4323
*/
@@ -119,73 +99,25 @@ export default function nvimGitRebaseTodo(plugin: NvimPlugin): void {
11999
width, //
120100
height,
121101
}: SetWindowRelativeToCursorOpts): Promise<OpenWindowOptions> => {
122-
/**
123-
* TODO REFACTOR - the "relative to cursor" behavior
124-
* should be applied when initializing as well
125-
* it's not noticable because you always start at row 0,
126-
* (unless if you come from another buffer!
127-
* but probably unlikely since by that point
128-
* would already be initialized?
129-
*
130-
*
131-
*
132-
* or we .close our gWindow so still matters?)
133-
*
134-
*
135-
*
136-
*/
137-
138102
const cursor = await vim.window.cursor;
139103

140-
// const row = await vim.window.row;
141-
// const col = await vim.window.col;
142-
143104
const relWin = await vim.window;
144-
// const [row, _col] = await relWin.cursor;
145105

146106
return {
147107
// relative: "cursor",
148108
relative: "win" as const,
149-
win: relWin as any,
150-
// row: row - 1,
151-
// col: col,
152-
// col: await relWin.width,
109+
win: (relWin as unknown) as number, // TODO TS fix incorrect types @ upstream
110+
//
153111
bufpos: cursor,
112+
//
154113
row: -1, // TODO investigate when in very last row & fully scrolled down with Ctrl-E
155-
// col: 20,
156114
col: await relWin.width,
115+
//
157116
width,
158117
height,
159118
//
160119
style: "minimal",
161120
};
162-
// await vim.windowConfig(gWindow,);
163-
164-
// ? ({
165-
// // relative: "win",
166-
// // win: relWin.id,
167-
// // relative: "cursor",
168-
// //
169-
// width,
170-
// height,
171-
// //
172-
// // anchor: "NE", // TODO is this needed?
173-
// row: 0,
174-
// // row: relWinHeight,
175-
// col: relWinWidth,
176-
177-
// //
178-
// style: "minimal",
179-
// } as const)
180-
181-
// gWindow.row = _row;
182-
// await gWindow.setOption("row", _row);
183-
// gWindow.cursor = cursor;
184-
// gWindow.width = _row;
185-
186-
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
187-
// @ts-ignore
188-
// gWindow.row = _row;
189121
};
190122

191123
type InitWindowOpts = WH & {
@@ -199,11 +131,7 @@ export default function nvimGitRebaseTodo(plugin: NvimPlugin): void {
199131
*/
200132

201133
const relWin: Window = await vim.getWindow();
202-
203134
const relWinWidth: number = await relWin.width;
204-
// const relWinHeight: number = await gRelWin.height;
205-
206-
const enter = false;
207135

208136
const opts: OpenWindowOptions = config.relativeToCursor
209137
? {
@@ -223,6 +151,7 @@ export default function nvimGitRebaseTodo(plugin: NvimPlugin): void {
223151
style: "minimal",
224152
} as const);
225153

154+
const enter = false;
226155
const window: number | Window = await vim.openWindow(buffer, enter, opts);
227156

228157
if (typeof window === "number") {
@@ -243,8 +172,9 @@ export default function nvimGitRebaseTodo(plugin: NvimPlugin): void {
243172

244173
/**
245174
* TODO REFACTOR
175+
* the `hideWindow` should be a prop probably,
176+
* instead of implying it like this w/ `0x0`
246177
*/
247-
// if (width === 0 && height === 0 && !config.relativeToCursor) {
248178
if (width === 0 && height === 0) {
249179
await hideWindow();
250180
} else {
@@ -257,19 +187,6 @@ export default function nvimGitRebaseTodo(plugin: NvimPlugin): void {
257187
} else {
258188
gWindow.width = width;
259189
gWindow.height = height;
260-
261-
// await vim.windowConfig(gWindow, {
262-
// relative: "win",
263-
// win: relWin,
264-
// // row: row - 1,
265-
// // col: col,
266-
// // col: await relWin.width,
267-
// row: 0,
268-
// // col: 0,
269-
// col: await relWin.width,
270-
// width,
271-
// height,
272-
// });
273190
}
274191
}
275192
};
@@ -279,10 +196,6 @@ export default function nvimGitRebaseTodo(plugin: NvimPlugin): void {
279196
*/
280197

281198
const getCommittishOfCurrentLine = async (): Promise<Committish> => {
282-
// const line: string | null = await new Promise<string>((r) => r(vim.line)).catch(() => null);
283-
// const line: string | null = await new Promise<string>((r) => r(vim.line)).catch(() => null);
284-
// console.log({ line });
285-
286199
let line: string | null;
287200
try {
288201
line = await vim.line;
@@ -309,12 +222,6 @@ export default function nvimGitRebaseTodo(plugin: NvimPlugin): void {
309222

310223
return committish;
311224
};
312-
// /**
313-
// * TODO make private, allow to be changed only in `drawLinesOfCommittishStat`
314-
// * (factory fn),
315-
// * and create a getter fn to allow comparing
316-
// */
317-
// let gCommittish: null | string = null;
318225

319226
let count: number = 0;
320227
/**
@@ -328,7 +235,6 @@ export default function nvimGitRebaseTodo(plugin: NvimPlugin): void {
328235
*
329236
*/
330237
const getStatLines = async (committish: NonNullable<Committish>): Promise<string[]> => {
331-
// const gitShowCmd: string = `git show --stat ${committish}`;
332238
/**
333239
* remove everything except the `/path/to/file | N +-`
334240
*/
@@ -343,7 +249,14 @@ export default function nvimGitRebaseTodo(plugin: NvimPlugin): void {
343249
].join(" ");
344250

345251
const stat: string[] = await execAsync(gitShowCmd, { encoding: "utf-8" }).then((x) =>
346-
x.stderr ? [] : x.stdout.split("\n")
252+
/**
253+
* if stderr, it's an error.
254+
* TODO ideally would be more explicit,
255+
* i.e. "hideWindow" or smthn
256+
*/
257+
x.stderr //
258+
? []
259+
: x.stdout.split("\n")
347260
);
348261
if (!stat.length) {
349262
return [];
@@ -356,20 +269,6 @@ export default function nvimGitRebaseTodo(plugin: NvimPlugin): void {
356269
return stat;
357270
};
358271

359-
// type DrawLinesOfCommittishStatRet = {
360-
// width: number;
361-
// height: number;
362-
363-
// committish: string | null;
364-
365-
// bufStart: number;
366-
// bufEnd: number;
367-
// };
368-
369-
// let previousState: DrawLinesOfCommittishStatRet | null = null;
370-
371-
// const drawLinesOfCommittishStat = async (): Promise<DrawLinesOfCommittishStatRet> => {
372-
373272
type Committish = string | null;
374273
let previousCommittish: Committish;
375274

@@ -382,6 +281,8 @@ export default function nvimGitRebaseTodo(plugin: NvimPlugin): void {
382281

383282
/**
384283
* TODO OPTIMIZE
284+
* could cache already computed Committish'es
285+
* see also the above idea for pre-computing all lines in the file
385286
*/
386287
const commitState = async (opts: Omit<InitWindowOpts, "buffer"> & { lines: string[] }): Promise<Committish> => {
387288
const { lines, ...rest } = opts;
@@ -446,51 +347,42 @@ export default function nvimGitRebaseTodo(plugin: NvimPlugin): void {
446347
});
447348
};
448349

350+
/**
351+
*
352+
*/
353+
449354
const pattern = "git-rebase-todo" as const;
355+
const commonOptions: AutocmdOptions = {
356+
sync: false, //
357+
pattern,
358+
eval: 'expand("<afile>")', // i don't know what this does
359+
};
450360

361+
/**
362+
* :help events
363+
*/
451364
plugin.registerAutocmd(
452-
/**
453-
* :help events
454-
*
455-
* CursorMoved?
456-
*/
457-
"BufEnter",
458-
async (_fileName: string) => {
459-
// await vim.buffer.append("BufEnter for git-rebase-todo File?");
460-
// const w = new vim.Window({});
461-
// w.setOption("width", 10);
462-
//
463-
// const w = await vim.window;
464-
// console.log(w);
465-
466-
// const { width, height } = await drawLinesOfCommittishStat();
467-
// await initWindow({ width, height });
468-
await drawLinesOfCommittishStat();
469-
},
470-
{ sync: false, pattern: pattern, eval: 'expand("<afile>")' }
365+
"BufEnter", //
366+
() => drawLinesOfCommittishStat(),
367+
{
368+
...commonOptions,
369+
}
471370
);
472371

473372
plugin.registerAutocmd(
474-
"BufLeave",
475-
async () => {
476-
await hideWindow();
477-
},
373+
"BufLeave", //
374+
() => hideWindow(),
478375
{
479-
sync: false,
480-
pattern: pattern,
481-
eval: 'expand("<afile>")',
376+
...commonOptions,
482377
}
483378
);
484379

485-
/**
486-
* :help events
487-
*/
488380
plugin.registerAutocmd(
489-
"CursorMoved",
490-
() => {
491-
drawLinesOfCommittishStat();
492-
},
493-
{ sync: false, pattern: pattern, eval: 'expand("<afile>")' }
381+
"CursorMoved", //
382+
() => drawLinesOfCommittishStat(),
383+
{
384+
...commonOptions,
385+
}
494386
);
495387

496388
/**
@@ -500,10 +392,10 @@ export default function nvimGitRebaseTodo(plugin: NvimPlugin): void {
500392
* TODO OPTIMIZE
501393
*/
502394
plugin.registerAutocmd(
503-
"CursorMovedI",
504-
() => {
505-
drawLinesOfCommittishStat();
506-
},
507-
{ sync: false, pattern: pattern, eval: 'expand("<afile>")' }
395+
"CursorMovedI", //
396+
() => drawLinesOfCommittishStat(),
397+
{
398+
...commonOptions,
399+
}
508400
);
509401
}

0 commit comments

Comments
 (0)