diff --git a/src/api.ts b/src/api.ts index bf697ee..dacfa77 100644 --- a/src/api.ts +++ b/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 + ); } diff --git a/src/components/tooltipWidget.ts b/src/components/tooltipWidget.ts index 2a95462..2365f29 100644 --- a/src/components/tooltipWidget.ts +++ b/src/components/tooltipWidget.ts @@ -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 { diff --git a/src/main.ts b/src/main.ts index 5b34dd8..554bf2f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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 diff --git a/src/modules/tooltipExtension.ts b/src/modules/tooltipExtension.ts index 81ae10b..b5cbae0 100644 --- a/src/modules/tooltipExtension.ts +++ b/src/modules/tooltipExtension.ts @@ -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(); -export function cursorTooltipExtension() { +export function cursorTooltipExtension(app: App) { return StateField.define({ 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,