Skip to content

Commit

Permalink
feat(選択範囲をコンテキストとして追加する処理を追加): ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
nekowasabi committed Dec 30, 2024
1 parent 0a441f2 commit b8f9d4f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
24 changes: 19 additions & 5 deletions 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 @@ -440,8 +440,22 @@ export async function getFileBuffers(denops: Denops): Promise<undefined | string
* @retuns {Promise<string>} 一時ファイルのパス
*/
export async function getPartialContextFilePath(denops: Denops, start: string, end: string): Promise<string> {
const buf_count = ensure(await fn.bufnr(denops, "$"), is.Number);
const s = ensure(start, is.String);
const e = ensure(end, is.String);
return `${s}:${e}:${buf_count}`;
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;
}
5 changes: 3 additions & 2 deletions denops/aider/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,9 @@ export async function main(denops: Denops): Promise<void> {
"addPartialReadonlyContext",
"*",
async (start: string, end: string) => {
const partialContextFile = buffer.getPartialContextFilePath(denops, start, end);
// aider addする
const partialContextFile = await buffer.getPartialContextFilePath(denops, start, end);
const prompt = `/read-only ${partialContextFile}`;
await buffer.sendPrompt(denops, prompt);
},
{ pattern: "[<line1>, <line2>]", range: true },
),
Expand Down

0 comments on commit b8f9d4f

Please sign in to comment.