Skip to content

Commit

Permalink
Merge pull request #38 from nekowasabi/feat/add-pertical-context
Browse files Browse the repository at this point in the history
  • Loading branch information
nekowasabi authored Dec 30, 2024
2 parents b0445e6 + af86041 commit 5187f60
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ To use aider.vim, you can run the following commands within Vim or Neovim:
- `:AiderSilentAddCurrentFileReadOnly` - Silently adds the current file as read-only to aider's context.
- `:AiderExit` - Exits aider and cleans up the session.
- `:AiderVisualTextWithPrompt` - Edit the selected text in visual mode in a floating window and send it to aider. In the floating window, send to aider with `<CR>` in normal mode, and close the floating window with `Q`. You can also backup prompt with `q`.
- `:AiderAddPartialReadonlyContext` - Adds the selected text in visual mode as read-only context to Aider.
- `:AiderAddWeb` - Displays a prompt for the specified URL and adds it to the aider context.
- `:AiderOpenIgnore` - Opens the `.aiderignore` file in the git root directory if it exists.
- `:AiderAddIgnoreCurrentFile` - Adds the current file to the `.aiderignore`.
Expand Down
31 changes: 30 additions & 1 deletion denops/aider/bufferOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Denops } from "https://deno.land/x/[email protected]/mod.ts";
import * as v from "https://deno.land/x/[email protected]/variable/mod.ts";
import { ensure, is, maybe } from "https://deno.land/x/[email protected]/mod.ts";
import { aider } from "./aiderCommand.ts";
import { getPromptFromVimVariable } from "./utils.ts";
import { getCurrentFilePath, getPromptFromVimVariable } from "./utils.ts";

/**
* Enum representing different buffer layout options.
Expand Down Expand Up @@ -430,3 +430,32 @@ export async function getFileBuffers(denops: Denops): Promise<undefined | string

return buffers.join(" ") ?? undefined;
}

/**
* 選択したテキストを一時ファイルに保存し、ファイルのパスを返します。
*
* @param {Denops} denops - Denopsインスタンス
* @param {string} start - 選択範囲の開始行
* @param {string} end - 選択範囲の終了行
* @retuns {Promise<string>} 一時ファイルのパス
*/
export async function getPartialContextFilePath(denops: Denops, start: string, end: string): Promise<string> {
const context = ensure(await denops.call("getline", start, end), is.ArrayOf(is.String));
const filePath = ensure(await getCurrentFilePath(denops), is.String);

const annotation = ensure([`// Path: ${filePath}`], is.ArrayOf(is.String));

annotation.push(`// Line: ${start}-${end}`);

context.unshift(...annotation);

// 一時ファイルに選択範囲を書き込む
const tempFile = ensure(await denops.call("tempname"), is.String);
await Deno.writeTextFile(tempFile, context.join("\n"));

// set filetype
const fileType = ensure(await fn.getbufvar(denops, "%", "&filetype"), is.String);
await denops.cmd(`setlocal filetype=${fileType}`);

return tempFile;
}
11 changes: 11 additions & 0 deletions denops/aider/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,17 @@ export async function main(denops: Denops): Promise<void> {
}
}),

await command(
"addPartialReadonlyContext",
"*",
async (start: string, end: string) => {
const partialContextFile = await buffer.getPartialContextFilePath(denops, start, end);
const prompt = `/read-only ${partialContextFile}`;
await buffer.sendPrompt(denops, prompt);
},
{ pattern: "[<line1>, <line2>]", range: true },
),

await command(
"visualTextWithPrompt",
"*",
Expand Down

0 comments on commit 5187f60

Please sign in to comment.