Skip to content

Commit f65bb68

Browse files
committed
fix: properly update state (no stale issue), refactor - move extra lines mods into central place
Signed-off-by: Kipras Melnikovas <[email protected]>
1 parent 0227a8b commit f65bb68

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

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

+40-18
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ export default function nvimGitRebaseTodo(plugin: NvimPlugin): void {
232232
return committish;
233233
};
234234

235-
let count: number = 0;
235+
let gParseStatCount: number = 0;
236236
/**
237237
* TODO consider an option where we precompute stats for all commits,
238238
* set the window to the the longest height out of them,
@@ -267,30 +267,47 @@ export default function nvimGitRebaseTodo(plugin: NvimPlugin): void {
267267
? []
268268
: x.stdout.split("\n")
269269
);
270+
271+
gParseStatCount++;
272+
270273
if (!stat.length) {
271274
return [];
272275
}
273276

274-
if (config.showStatParsingCount) {
275-
stat.unshift((count++).toString());
276-
}
277-
278277
return stat;
279278
};
280279

281280
type Committish = string | null;
282281
type State = {
283282
committish: Committish;
283+
284+
/**
285+
* will not include the extra "informational" lines;
286+
* use `getExtraInfoLines` for that.
287+
*/
284288
lines: string[];
289+
285290
width: number;
286291
height: number;
287292
};
293+
type CommitStateOpts = {
294+
isCacheHit: boolean; //
295+
};
288296

289297
let prevState: State | null = null;
290298
const committishToStatCache: Map<NonNullable<Committish>, State> = new Map();
291-
const commitState = (state: State): Promise<State> =>
299+
const commitState = (
300+
state: State,
301+
{
302+
isCacheHit = false, //
303+
}: Partial<CommitStateOpts> = {}
304+
): Promise<State> =>
292305
Promise.resolve()
293-
.then(() => updateBuffer(state.lines))
306+
.then((): string[] => [
307+
...getExtraInfoLines({ isCacheHit }),
308+
...state.lines, // do not mutate state.lines
309+
])
310+
.then((lines) => updateBuffer(lines))
294311
.then((buffer) =>
295312
updateWindow({
296313
buffer, //
@@ -301,6 +318,19 @@ export default function nvimGitRebaseTodo(plugin: NvimPlugin): void {
301318
.then(() => state.committish && committishToStatCache.set(state.committish, state))
302319
.then(() => (prevState = state));
303320

321+
function getExtraInfoLines(opts: CommitStateOpts): string[] {
322+
const extra: string[] = [];
323+
324+
if (config.showStatParsingCount) {
325+
extra.push(gParseStatCount.toString());
326+
}
327+
if (config.showCacheHitOrMiss && opts.isCacheHit) {
328+
extra.push("cache hit");
329+
}
330+
331+
return extra;
332+
}
333+
304334
const drawLinesOfCommittishStat = async (): Promise<State> => {
305335
const committish: Committish = await getCommittishOfCurrentLine();
306336

@@ -319,17 +349,9 @@ export default function nvimGitRebaseTodo(plugin: NvimPlugin): void {
319349

320350
let tmp: State | undefined;
321351
if ((tmp = committishToStatCache.get(committish))) {
322-
if (!config.showCacheHitOrMiss) {
323-
return commitState(tmp);
324-
}
325-
326-
const cacheHit = "cache hit";
327-
if (!tmp.lines[0].includes(cacheHit)) {
328-
tmp.lines.unshift("");
329-
}
330-
tmp.lines[0] = cacheHit;
331-
332-
return commitState(tmp);
352+
return commitState(tmp, {
353+
isCacheHit: true, //
354+
});
333355
}
334356

335357
const stat: string[] = await getStatLines(committish);

0 commit comments

Comments
 (0)