Skip to content
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
116 changes: 39 additions & 77 deletions packages/pi-fff/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
/**
* pi-fff: FFF-powered file search extension for pi
*
* Overrides built-in `find` and `grep` tools with FFF and can also replace
* @-mention autocomplete suggestions in the interactive editor.
* Overrides built-in `find` and `grep` tools with FFF and adds FFF-backed
* @-mention autocomplete suggestions to the interactive editor.
*/

import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
import { CustomEditor } from "@earendil-works/pi-coding-agent";
import {
Text,
type AutocompleteItem,
type AutocompleteProvider,
Text,
} from "@earendil-works/pi-tui";
import { Type } from "@sinclair/typebox";
import { FileFinder } from "@ff-labs/fff-node";
import type {
GrepCursor,
GrepMode,
GrepResult,
SearchResult,
MixedItem,
SearchResult,
} from "@ff-labs/fff-node";
import { FileFinder } from "@ff-labs/fff-node";
import { Type } from "@sinclair/typebox";
import { buildQuery } from "./query";

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -273,12 +272,6 @@ function createFffMentionProvider(
};
}

// FffEditor is defined inside fffExtension() so it can capture `getMentionItems`
// via closure rather than via a 4th constructor parameter. This makes the class
// safe to subclass via `new SubClass(tui, theme, keybindings)` -- the pattern
// pi-vim and pi-image-attachments use to compose editors. See:
// https://github.com/badlogic/pi-mono/issues/3935

// ---------------------------------------------------------------------------
// Extension
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -391,72 +384,44 @@ export default function fffExtension(pi: ExtensionAPI) {
});
}

// Editor wrapper that injects FFF @-mention autocomplete alongside base provider.
// Defined inside fffExtension() so the class methods capture `getMentionItems`
// via closure. Subclasses constructed as `new Sub(tui, theme, keybindings)` by
// composability wrappers (pi-vim, pi-image-attachments) still get a working
// mention provider because the closure binding is preserved across subclassing.
class FffEditor extends CustomEditor {
private baseProvider: AutocompleteProvider | undefined;

override setAutocompleteProvider(provider: AutocompleteProvider): void {
this.baseProvider = provider;
// Create composite provider that handles @-mentions and falls back to base
function registerAutocompleteProvider(ctx: {
ui: {
addAutocompleteProvider: (
factory: (current: AutocompleteProvider) => AutocompleteProvider,
) => void;
};
}) {
ctx.ui.addAutocompleteProvider((current) => {
const mentionProvider = createFffMentionProvider(getMentionItems);
const compositeProvider: AutocompleteProvider = {
getSuggestions: async (lines, cursorLine, cursorCol, options) => {
// Try @-mention first
const mentionResult = await mentionProvider.getSuggestions(
lines,
cursorLine,
cursorCol,
options,
);
if (mentionResult) return mentionResult;
// Fall back to base provider
return (
this.baseProvider?.getSuggestions(lines, cursorLine, cursorCol, options) ??
null
);
},
applyCompletion: (lines, cursorLine, cursorCol, item, prefix) => {
// Let mention provider handle @ completions, base provider for others
if (prefix?.startsWith("@")) {
return mentionProvider.applyCompletion!(
lines,
cursorLine,
cursorCol,
item,
prefix,
);

return {
async getSuggestions(lines, cursorLine, cursorCol, options) {
if (shouldEnableMentions()) {
try {
const mentionResult = await mentionProvider.getSuggestions(
lines,
cursorLine,
cursorCol,
options,
);
if (mentionResult) return mentionResult;
} catch {
// Delegate when FFF lookup is unavailable.
}
}

return current.getSuggestions(lines, cursorLine, cursorCol, options);
},
applyCompletion(lines, cursorLine, cursorCol, item, prefix) {
return current.applyCompletion(lines, cursorLine, cursorCol, item, prefix);
},
shouldTriggerFileCompletion(lines, cursorLine, cursorCol) {
return (
this.baseProvider?.applyCompletion?.(
lines,
cursorLine,
cursorCol,
item,
prefix,
) ?? { lines, cursorLine, cursorCol }
current.shouldTriggerFileCompletion?.(lines, cursorLine, cursorCol) ?? true
);
},
};
super.setAutocompleteProvider(compositeProvider);
}
}

function applyEditorMode(ctx: {
ui: {
setEditorComponent: (
factory: ((tui: any, theme: any, keybindings: any) => any) | undefined,
) => void;
};
}) {
if (!shouldEnableMentions()) return;

ctx.ui.setEditorComponent(
(tui: any, theme: any, keybindings: any) => new FffEditor(tui, theme, keybindings),
);
});
}

// --- Flags / lifecycle ---
Expand All @@ -479,7 +444,7 @@ export default function fffExtension(pi: ExtensionAPI) {
pi.on("session_start", async (_event, ctx) => {
try {
activeCwd = ctx.cwd;
if (shouldEnableMentions()) applyEditorMode(ctx);
registerAutocompleteProvider(ctx);
await ensureFinder(activeCwd);
} catch (e: unknown) {
ctx.ui.notify(
Expand Down Expand Up @@ -947,9 +912,6 @@ export default function fffExtension(pi: ExtensionAPI) {
const oldMode = getMode();
setMode(newMode);

// Apply immediately using the shared function
applyEditorMode(ctx);

const note =
(oldMode === "override") !== (newMode === "override")
? " (tool name change requires restart)"
Expand Down
Loading
Loading