Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor openAiderBuffer, small fix getAiderBuffer #24

Merged
merged 5 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 51 additions & 23 deletions denops/aider/bufferOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,23 @@ export async function exitAiderBuffer(denops: Denops): Promise<void> {
*/
export async function openAiderBuffer(
denops: Denops,
aiderBuf: AiderBuffer | undefined,
openBufferType: BufferLayout,
): Promise<undefined | boolean> {
// TODO openBufferTypeで大きく分岐するようリファクタすべき
// return typeもbooleanにできる
if (aiderBuf && openBufferType === "floating") {
await openFloatingWindow(denops, aiderBuf.bufnr);
return true;
): Promise<void> {
const aiderBuf = await getAiderBuffer(denops);
if (openBufferType === "floating") {
if (aiderBuf === undefined) {
const bufnr = ensure(
await n.nvim_create_buf(denops, false, true),
is.Number,
);
await openFloatingWindow(denops, bufnr);
await aider().run(denops);
return;
}
if (aiderBuf !== undefined) {
await openFloatingWindow(denops, aiderBuf.bufnr);
return;
}
Comment on lines +58 to +73
Copy link
Contributor

@coderabbitai coderabbitai bot Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Simplify conditional logic in 'openAiderBuffer' function

After checking if (aiderBuf === undefined) and returning, the subsequent check if (aiderBuf !== undefined) is redundant and can be replaced with an else statement for improved readability.

Apply this diff to simplify the conditional logic:

if (openBufferType === "floating") {
  if (aiderBuf === undefined) {
    // ... existing code ...
    return;
- }
- if (aiderBuf !== undefined) {
+ } else {
    await openFloatingWindow(denops, aiderBuf.bufnr);
    return;
  }
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
): Promise<void> {
const aiderBuf = await getAiderBuffer(denops);
if (openBufferType === "floating") {
if (aiderBuf === undefined) {
const bufnr = ensure(
await n.nvim_create_buf(denops, false, true),
is.Number,
);
await openFloatingWindow(denops, bufnr);
await aider().run(denops);
return;
}
if (aiderBuf !== undefined) {
await openFloatingWindow(denops, aiderBuf.bufnr);
return;
}
): Promise<void> {
const aiderBuf = await getAiderBuffer(denops);
if (openBufferType === "floating") {
if (aiderBuf === undefined) {
const bufnr = ensure(
await n.nvim_create_buf(denops, false, true),
is.Number,
);
await openFloatingWindow(denops, bufnr);
await aider().run(denops);
return;
} else {
await openFloatingWindow(denops, aiderBuf.bufnr);
return;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

biomeくんがelseを消そうとするんじゃ

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tsukimizake、ご指摘ありがとうございます。Biomeがelseを削除するとのこと、承知しました。それではこのままにしておきます。

/(=´x`=)\


✏️ Learnings added
Learnt from: tsukimizake
PR: nekowasabi/aider.vim#24
File: denops/aider/bufferOperation.ts:58-73
Timestamp: 2024-10-10T13:35:53.623Z
Learning: このTypeScriptコードでは、`if`ブロック内で`return`がある場合、その後の`else`文は使用しません。Biomeが自動的に削除するためです。

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

}

if (openBufferType === "split" || openBufferType === "vsplit") {
Expand All @@ -70,14 +79,9 @@ export async function openAiderBuffer(
} else {
await openSplitWindow(denops);
}
await aider().run(denops);
return;
}

const bufnr = ensure(await n.nvim_create_buf(denops, false, true), is.Number);

await openFloatingWindow(denops, bufnr);

return;
}

export async function sendPromptWithInput(
Expand All @@ -94,7 +98,7 @@ export async function sendPromptWithInput(
const openBufferType = await getOpenBufferType(denops);

if (openBufferType === "floating") {
await openAiderBuffer(denops, aiderBuf, openBufferType);
await openAiderBuffer(denops, openBufferType);
await sendPromptFromFloatingWindow(denops, input);
return;
}
Expand Down Expand Up @@ -142,7 +146,10 @@ export async function openFloatingWindowWithSelectedCode(
return;
}
}
const backupPrompt = await getPromptFromVimVariable(denops, "aider_visual_select_buffer_prompt");
const backupPrompt = await getPromptFromVimVariable(
denops,
"aider_visual_select_buffer_prompt",
);
const bufnr = ensure(await n.nvim_create_buf(denops, false, true), is.Number);
await openFloatingWindow(denops, bufnr);

Expand All @@ -157,9 +164,16 @@ export async function openFloatingWindowWithSelectedCode(
await n.nvim_buf_set_keymap(denops, bufnr, "n", "q", "<cmd>close!<CR>", {
silent: true,
});
await n.nvim_buf_set_keymap(denops, bufnr, "n", "Q", "<cmd>AiderHideVisualSelectFloatingWindow<CR>", {
silent: true,
});
await n.nvim_buf_set_keymap(
denops,
bufnr,
"n",
"Q",
"<cmd>AiderHideVisualSelectFloatingWindow<CR>",
{
silent: true,
},
);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

biomeデフォルトの80文字/行でformatさせると改行増えすぎるなあという気持ち

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

biome教に殉じるか、設定増やすかですねえ。
( ;´。 `;)←ここはあまりこだわりがないので、多くてもまぁええか…となっていた顔
ですが、80文字ってモニタの解像度が小さなときの時代のルールなので、120文字くらいまでは増やしてもよさそうです。

await n.nvim_buf_set_keymap(
denops,
bufnr,
Expand All @@ -182,7 +196,11 @@ export async function openFloatingWindowWithSelectedCode(
* @param {number} bufnr - バッファ番号。
* @param {string[]} backupPrompt - バックアッププロンプトの内容。
*/
async function handleBackupPrompt(denops: Denops, bufnr: number, backupPrompt: string[]) {
async function handleBackupPrompt(
denops: Denops,
bufnr: number,
backupPrompt: string[],
) {
await v.g.set(denops, "aider_visual_select_buffer_prompt", undefined);
await n.nvim_buf_set_lines(denops, bufnr, 0, -1, true, backupPrompt);
await feedkeys(denops, "Gi");
Expand All @@ -198,7 +216,11 @@ async function handleBackupPrompt(denops: Denops, bufnr: number, backupPrompt: s
* @param {number} bufnr - バッファ番号。
* @param {string[]} words - バッファに設定するコード行。
*/
async function handleNoBackupPrompt(denops: Denops, bufnr: number, words: string[]) {
async function handleNoBackupPrompt(
denops: Denops,
bufnr: number,
words: string[],
) {
const filetype = ensure(
await fn.getbufvar(denops, "%", "&filetype"),
is.String,
Expand All @@ -211,7 +233,10 @@ async function handleNoBackupPrompt(denops: Denops, bufnr: number, words: string
await n.nvim_buf_set_lines(denops, bufnr, 0, 1, true, []);
await n.nvim_buf_set_lines(denops, bufnr, -1, -1, true, [""]);

const additionalPrompt = await getPromptFromVimVariable(denops, "aider_additional_prompt");
const additionalPrompt = await getPromptFromVimVariable(
denops,
"aider_additional_prompt",
);
if (additionalPrompt) {
await n.nvim_buf_set_lines(denops, bufnr, -1, -1, true, ["# rule"]);
await n.nvim_buf_set_lines(denops, bufnr, -1, -1, true, additionalPrompt);
Expand All @@ -222,7 +247,9 @@ async function handleNoBackupPrompt(denops: Denops, bufnr: number, words: string
await feedkeys(denops, "Gi");
}

export async function hideVisualSelectFloatingWindow(denops: Denops): Promise<void> {
export async function hideVisualSelectFloatingWindow(
denops: Denops,
): Promise<void> {
const bufferContent = ensure(
await denops.call("getbufline", "%", 1, "$"),
is.ArrayOf(is.String),
Expand Down Expand Up @@ -386,7 +413,8 @@ export async function getAiderBuffer(
// プロセスが動いていない場合(session復元時など)はバッファを削除
if (!aider().isTestMode() && jobId === 0) {
await denops.cmd(`b ${bufnr}`);
await denops.cmd("bp | sp | bn | bd!"); // delete buffer without changing window
await denops.cmd("b#");
await denops.cmd("bd! #");
continue;
}

Expand Down
16 changes: 3 additions & 13 deletions denops/aider/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,7 @@ export async function main(denops: Denops): Promise<void> {
}),

await command("run", "0", async () => {
const aiderBuf = await buffer.getAiderBuffer(denops);
await buffer.openAiderBuffer(denops, aiderBuf, openBufferType);

if (aiderBuf === undefined) {
await aider().run(denops);
return;
}
await buffer.openAiderBuffer(denops, openBufferType);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Action Required: Remaining usages of aiderBuf found in the codebase.

The search revealed that aiderBuf is still used in denops/aider/bufferOperation.ts at multiple locations. Please review and refactor these instances to ensure complete removal or proper handling as per the PR objectives.

🔗 Analysis chain

LGTM: Simplified implementation of the run command.

The change directly calls openAiderBuffer without checking for the existence of aiderBuf, which aligns with the PR objectives of removing the aiderBuf argument and streamlining the code.

To ensure this change doesn't introduce any regressions, let's verify that aiderBuf is not used elsewhere in the codebase:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any remaining usage of 'aiderBuf' in the codebase

# Test: Search for 'aiderBuf' in all TypeScript files
rg --type typescript 'aiderBuf'

Length of output: 66


Script:

#!/bin/bash
# Description: Check for any remaining usage of 'aiderBuf' in TypeScript files

# Test: Search for 'aiderBuf' in all TypeScript files using the correct type alias
rg --type ts 'aiderBuf'

Length of output: 1323

}),

await command("silentRun", "0", () => aider().silentRun(denops)),
Expand Down Expand Up @@ -129,9 +123,7 @@ export async function main(denops: Denops): Promise<void> {
if (openBufferType === "floating") {
await aider().silentRun(denops);
} else {
const aiderBuf = await buffer.getAiderBuffer(denops);
await buffer.openAiderBuffer(denops, aiderBuf, openBufferType);
await aider().run(denops);
await buffer.openAiderBuffer(denops, openBufferType);
await denops.cmd("wincmd p");
console.log("Run AiderAddCurrentFile again.");
return;
Expand Down Expand Up @@ -162,9 +154,7 @@ export async function main(denops: Denops): Promise<void> {
if (openBufferType === "floating") {
await aider().silentRun(denops);
} else {
const aiderBuf = await buffer.getAiderBuffer(denops);
await buffer.openAiderBuffer(denops, aiderBuf, openBufferType);
await aider().run(denops);
await buffer.openAiderBuffer(denops, openBufferType);
await denops.cmd("wincmd p");
console.log("Run AiderAddReadCurrentFile again.");
return;
Expand Down