mirror of
https://github.com/fbarrca/obsidian-inlineAI.git
synced 2026-07-22 11:50:24 +00:00
Made the connected InlineAI use the new Diff API
- Update TooltipWidget to accept app instance and replace selected text with API response - Modify callApi to utilize fine-grained conflict generation - Refactor cursorTooltipExtension to pass app instance for improved tooltip handling
This commit is contained in:
parent
b49e682c35
commit
5562c50620
4 changed files with 35 additions and 24 deletions
14
src/api.ts
14
src/api.ts
|
|
@ -1,6 +1,9 @@
|
|||
// api.ts
|
||||
import ollama from "ollama";
|
||||
|
||||
import {
|
||||
generateFineGrainedConflictWithinLine,
|
||||
generateFineGrainedConflictWithinLineString,
|
||||
} from "./helpers/conflictMarkers";
|
||||
export async function callApi(
|
||||
content: string,
|
||||
context: string
|
||||
|
|
@ -16,6 +19,11 @@ export async function callApi(
|
|||
{ role: "user", content: "```" + context + "\n" + content + "```" },
|
||||
],
|
||||
});
|
||||
console.log(response.message.content);
|
||||
return response.message.content;
|
||||
console.log(
|
||||
generateFineGrainedConflictWithinLine(context, response.message.content)
|
||||
);
|
||||
return generateFineGrainedConflictWithinLineString(
|
||||
context,
|
||||
response.message.content
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
// TooltipWidget.ts
|
||||
import { EditorView, placeholder, keymap } from "@codemirror/view";
|
||||
import { EditorState } from "@codemirror/state";
|
||||
import { ButtonComponent, setIcon, Notice } from "obsidian";
|
||||
import { ButtonComponent, setIcon, Notice, App, MarkdownView } from "obsidian";
|
||||
import { callApi } from "../api";
|
||||
|
||||
export class TooltipWidget {
|
||||
|
|
@ -9,10 +8,11 @@ export class TooltipWidget {
|
|||
editorView: EditorView;
|
||||
submitButton: ButtonComponent;
|
||||
loader: HTMLDivElement;
|
||||
selectedText: string; // Add property to store selected text
|
||||
selectedText: string;
|
||||
app: App;
|
||||
|
||||
constructor(selectedText: string) {
|
||||
// Accept selectedText as a parameter
|
||||
constructor(app: App, selectedText: string) {
|
||||
this.app = app;
|
||||
this.selectedText = selectedText;
|
||||
|
||||
this.dom = document.createElement("div");
|
||||
|
|
@ -65,7 +65,6 @@ export class TooltipWidget {
|
|||
async submitAction() {
|
||||
const userInput = this.editorView.state.doc.toString();
|
||||
|
||||
// Log the selected text from the main editor
|
||||
console.log("Selected Text:", this.selectedText);
|
||||
|
||||
this.submitButton.setDisabled(true);
|
||||
|
|
@ -74,7 +73,16 @@ export class TooltipWidget {
|
|||
|
||||
try {
|
||||
const response = await callApi(userInput, this.selectedText);
|
||||
new Notice(response);
|
||||
new Notice("Response received and replacing selected text.");
|
||||
|
||||
const markdownView =
|
||||
this.app.workspace.getActiveViewOfType(MarkdownView);
|
||||
if (markdownView) {
|
||||
const editor = markdownView.editor;
|
||||
editor.replaceSelection(response);
|
||||
} else {
|
||||
new Notice("Failed to find the active Markdown editor.");
|
||||
}
|
||||
} catch (error) {
|
||||
new Notice("Error: " + (error as Error).message);
|
||||
} finally {
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ export default class MyPlugin extends Plugin {
|
|||
await this.loadSettings();
|
||||
|
||||
// Register the cursor tooltip extension
|
||||
this.registerEditorExtension(cursorTooltipExtension());
|
||||
this.registerEditorExtension(cursorTooltipExtension(this.app));
|
||||
this.registerEditorExtension(conflictMarkers());
|
||||
|
||||
// Add command to show tooltip
|
||||
|
|
|
|||
|
|
@ -1,19 +1,18 @@
|
|||
// extensions.ts
|
||||
import { StateField, EditorState } from "@codemirror/state";
|
||||
import { StateField, EditorState, StateEffect } from "@codemirror/state";
|
||||
import { showTooltip, type Tooltip } from "@codemirror/view";
|
||||
import { TooltipWidget } from "../components/tooltipWidget";
|
||||
import { StateEffect } from "@codemirror/state";
|
||||
import { App } from "obsidian";
|
||||
|
||||
export const showTooltipEffect = StateEffect.define<null>();
|
||||
|
||||
export function cursorTooltipExtension() {
|
||||
export function cursorTooltipExtension(app: App) {
|
||||
return StateField.define<Tooltip | null>({
|
||||
create() {
|
||||
return null;
|
||||
},
|
||||
update(tooltip, tr) {
|
||||
if (tr.effects.some((e) => e.is(showTooltipEffect))) {
|
||||
return getCursorTooltip(tr.state);
|
||||
return getCursorTooltip(tr.state, app);
|
||||
}
|
||||
if (tr.docChanged || tr.selection) {
|
||||
return null;
|
||||
|
|
@ -24,7 +23,7 @@ export function cursorTooltipExtension() {
|
|||
});
|
||||
}
|
||||
|
||||
function getCursorTooltip(state: EditorState): Tooltip | null {
|
||||
function getCursorTooltip(state: EditorState, app: App): Tooltip | null {
|
||||
const { selection } = state;
|
||||
const mainSelection = selection.main;
|
||||
const { from, to, head, anchor } = mainSelection;
|
||||
|
|
@ -43,31 +42,27 @@ function getCursorTooltip(state: EditorState): Tooltip | null {
|
|||
const endLine = state.doc.lineAt(to);
|
||||
|
||||
if (endLine.number < state.doc.lines) {
|
||||
// Get the next line's start position
|
||||
const nextLine = state.doc.line(endLine.number + 1);
|
||||
posAt = nextLine.from;
|
||||
} else {
|
||||
// If at the end of the document, place tooltip at the document's end
|
||||
posAt = state.doc.length;
|
||||
}
|
||||
above = false; // Tooltip is below
|
||||
above = false;
|
||||
} else if (head < anchor) {
|
||||
console.log("Putting tooltip above");
|
||||
const startLine = state.doc.lineAt(from);
|
||||
posAt = startLine.from;
|
||||
above = true; // Tooltip is above
|
||||
above = true;
|
||||
} else {
|
||||
// Fallback for any other cases
|
||||
posAt = currentLine.from;
|
||||
above = true;
|
||||
}
|
||||
} else {
|
||||
// When there's no active selection, place tooltip at the start of the current line
|
||||
posAt = currentLine.from;
|
||||
above = true;
|
||||
}
|
||||
|
||||
const tooltipWidget = new TooltipWidget(selectedText); // Pass selectedText
|
||||
const tooltipWidget = new TooltipWidget(app, selectedText);
|
||||
|
||||
return {
|
||||
pos: posAt,
|
||||
|
|
|
|||
Loading…
Reference in a new issue