Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
137 changes: 137 additions & 0 deletions src/engine/MacroChoiceEngine.editorCommands.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import type { App } from "obsidian";
import { beforeEach, describe, expect, it, vi } from "vitest";

vi.mock("../formatters/completeFormatter", () => ({
CompleteFormatter: class CompleteFormatterMock {},
}));

vi.mock("obsidian-dataview", () => ({
getAPI: vi.fn(),
}));

vi.mock("../main", () => ({
default: class QuickAddMock {},
}));

import { MacroChoiceEngine } from "./MacroChoiceEngine";
import { EditorCommandType } from "../types/macros/EditorCommands/EditorCommandType";
import { MoveCursorToFileStartCommand } from "../types/macros/EditorCommands/MoveCursorToFileStartCommand";
import { MoveCursorToFileEndCommand } from "../types/macros/EditorCommands/MoveCursorToFileEndCommand";
import { MoveCursorToLineStartCommand } from "../types/macros/EditorCommands/MoveCursorToLineStartCommand";
import { MoveCursorToLineEndCommand } from "../types/macros/EditorCommands/MoveCursorToLineEndCommand";

const callExecuteEditorCommand = async (editorCommandType: EditorCommandType) => {
const executeEditorCommand = (
MacroChoiceEngine.prototype as unknown as {
executeEditorCommand: (
command: { editorCommandType: EditorCommandType },
) => Promise<void>;
}
).executeEditorCommand;
const app = {} as App;

await executeEditorCommand.call({ app }, { editorCommandType });
return app;
};

describe("MacroChoiceEngine editor command dispatch", () => {
beforeEach(() => {
vi.restoreAllMocks();
});

it("dispatches MoveCursorToFileStart", async () => {
const fileStartSpy = vi
.spyOn(MoveCursorToFileStartCommand, "run")
.mockImplementation(() => undefined);
const fileEndSpy = vi
.spyOn(MoveCursorToFileEndCommand, "run")
.mockImplementation(() => undefined);
const lineStartSpy = vi
.spyOn(MoveCursorToLineStartCommand, "run")
.mockImplementation(() => undefined);
const lineEndSpy = vi
.spyOn(MoveCursorToLineEndCommand, "run")
.mockImplementation(() => undefined);

const app = await callExecuteEditorCommand(
EditorCommandType.MoveCursorToFileStart
);

expect(fileStartSpy).toHaveBeenCalledWith(app);
expect(fileEndSpy).not.toHaveBeenCalled();
expect(lineStartSpy).not.toHaveBeenCalled();
expect(lineEndSpy).not.toHaveBeenCalled();
});

it("dispatches MoveCursorToFileEnd", async () => {
const fileStartSpy = vi
.spyOn(MoveCursorToFileStartCommand, "run")
.mockImplementation(() => undefined);
const fileEndSpy = vi
.spyOn(MoveCursorToFileEndCommand, "run")
.mockImplementation(() => undefined);
const lineStartSpy = vi
.spyOn(MoveCursorToLineStartCommand, "run")
.mockImplementation(() => undefined);
const lineEndSpy = vi
.spyOn(MoveCursorToLineEndCommand, "run")
.mockImplementation(() => undefined);

const app = await callExecuteEditorCommand(
EditorCommandType.MoveCursorToFileEnd
);

expect(fileStartSpy).not.toHaveBeenCalled();
expect(fileEndSpy).toHaveBeenCalledWith(app);
expect(lineStartSpy).not.toHaveBeenCalled();
expect(lineEndSpy).not.toHaveBeenCalled();
});

it("dispatches MoveCursorToLineStart", async () => {
const fileStartSpy = vi
.spyOn(MoveCursorToFileStartCommand, "run")
.mockImplementation(() => undefined);
const fileEndSpy = vi
.spyOn(MoveCursorToFileEndCommand, "run")
.mockImplementation(() => undefined);
const lineStartSpy = vi
.spyOn(MoveCursorToLineStartCommand, "run")
.mockImplementation(() => undefined);
const lineEndSpy = vi
.spyOn(MoveCursorToLineEndCommand, "run")
.mockImplementation(() => undefined);

const app = await callExecuteEditorCommand(
EditorCommandType.MoveCursorToLineStart
);

expect(fileStartSpy).not.toHaveBeenCalled();
expect(fileEndSpy).not.toHaveBeenCalled();
expect(lineStartSpy).toHaveBeenCalledWith(app);
expect(lineEndSpy).not.toHaveBeenCalled();
});

it("dispatches MoveCursorToLineEnd", async () => {
const fileStartSpy = vi
.spyOn(MoveCursorToFileStartCommand, "run")
.mockImplementation(() => undefined);
const fileEndSpy = vi
.spyOn(MoveCursorToFileEndCommand, "run")
.mockImplementation(() => undefined);
const lineStartSpy = vi
.spyOn(MoveCursorToLineStartCommand, "run")
.mockImplementation(() => undefined);
const lineEndSpy = vi
.spyOn(MoveCursorToLineEndCommand, "run")
.mockImplementation(() => undefined);

const app = await callExecuteEditorCommand(
EditorCommandType.MoveCursorToLineEnd
);

expect(fileStartSpy).not.toHaveBeenCalled();
expect(fileEndSpy).not.toHaveBeenCalled();
expect(lineStartSpy).not.toHaveBeenCalled();
expect(lineEndSpy).toHaveBeenCalledWith(app);
});
});
16 changes: 16 additions & 0 deletions src/engine/MacroChoiceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import { PasteCommand } from "../types/macros/EditorCommands/PasteCommand";
import { PasteWithFormatCommand } from "../types/macros/EditorCommands/PasteWithFormatCommand";
import { SelectActiveLineCommand } from "../types/macros/EditorCommands/SelectActiveLineCommand";
import { SelectLinkOnActiveLineCommand } from "../types/macros/EditorCommands/SelectLinkOnActiveLineCommand";
import { MoveCursorToFileStartCommand } from "../types/macros/EditorCommands/MoveCursorToFileStartCommand";
import { MoveCursorToFileEndCommand } from "../types/macros/EditorCommands/MoveCursorToFileEndCommand";
import { MoveCursorToLineStartCommand } from "../types/macros/EditorCommands/MoveCursorToLineStartCommand";
import { MoveCursorToLineEndCommand } from "../types/macros/EditorCommands/MoveCursorToLineEndCommand";
import { waitFor } from "src/utility";
import type { IAIAssistantCommand } from "src/types/macros/QuickCommands/IAIAssistantCommand";
import { runAIAssistant } from "src/ai/AIAssistant";
Expand Down Expand Up @@ -448,6 +452,18 @@ export class MacroChoiceEngine extends QuickAddChoiceEngine {
case EditorCommandType.SelectLinkOnActiveLine:
SelectLinkOnActiveLineCommand.run(this.app);
break;
case EditorCommandType.MoveCursorToFileStart:
MoveCursorToFileStartCommand.run(this.app);
break;
case EditorCommandType.MoveCursorToFileEnd:
MoveCursorToFileEndCommand.run(this.app);
break;
case EditorCommandType.MoveCursorToLineStart:
MoveCursorToLineStartCommand.run(this.app);
break;
case EditorCommandType.MoveCursorToLineEnd:
MoveCursorToLineEndCommand.run(this.app);
break;
}
}

Expand Down
32 changes: 32 additions & 0 deletions src/gui/MacroGUIs/CommandSequenceEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ import { PasteCommand } from "../../types/macros/EditorCommands/PasteCommand";
import { PasteWithFormatCommand } from "../../types/macros/EditorCommands/PasteWithFormatCommand";
import { SelectActiveLineCommand } from "../../types/macros/EditorCommands/SelectActiveLineCommand";
import { SelectLinkOnActiveLineCommand } from "../../types/macros/EditorCommands/SelectLinkOnActiveLineCommand";
import { MoveCursorToFileStartCommand } from "../../types/macros/EditorCommands/MoveCursorToFileStartCommand";
import { MoveCursorToFileEndCommand } from "../../types/macros/EditorCommands/MoveCursorToFileEndCommand";
import { MoveCursorToLineStartCommand } from "../../types/macros/EditorCommands/MoveCursorToLineStartCommand";
import { MoveCursorToLineEndCommand } from "../../types/macros/EditorCommands/MoveCursorToLineEndCommand";
import { AIAssistantCommand } from "../../types/macros/QuickCommands/AIAssistantCommand";
import type { IconType } from "../../types/IconType";
import { settingsStore } from "../../settingsStore";
Expand Down Expand Up @@ -289,6 +293,18 @@ export class CommandSequenceEditor {
case EditorCommandType.SelectLinkOnActiveLine:
command = new SelectLinkOnActiveLineCommand();
break;
case EditorCommandType.MoveCursorToFileStart:
command = new MoveCursorToFileStartCommand();
break;
case EditorCommandType.MoveCursorToFileEnd:
command = new MoveCursorToFileEndCommand();
break;
case EditorCommandType.MoveCursorToLineStart:
command = new MoveCursorToLineStartCommand();
break;
case EditorCommandType.MoveCursorToLineEnd:
command = new MoveCursorToLineEndCommand();
break;
default:
log.logError("invalid editor command type");
throw new Error("invalid editor command type");
Expand Down Expand Up @@ -320,6 +336,22 @@ export class CommandSequenceEditor {
.addOption(
EditorCommandType.SelectLinkOnActiveLine,
EditorCommandType.SelectLinkOnActiveLine
)
.addOption(
EditorCommandType.MoveCursorToFileStart,
EditorCommandType.MoveCursorToFileStart
)
.addOption(
EditorCommandType.MoveCursorToFileEnd,
EditorCommandType.MoveCursorToFileEnd
)
.addOption(
EditorCommandType.MoveCursorToLineStart,
EditorCommandType.MoveCursorToLineStart
)
.addOption(
EditorCommandType.MoveCursorToLineEnd,
EditorCommandType.MoveCursorToLineEnd
);
})
.addButton((button) =>
Expand Down
4 changes: 4 additions & 0 deletions src/types/macros/EditorCommands/EditorCommandType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ export enum EditorCommandType {
PasteWithFormat = "Paste with format",
SelectActiveLine = "Select active line",
SelectLinkOnActiveLine = "Select link on active line",
MoveCursorToFileStart = "Move cursor to file start",
MoveCursorToFileEnd = "Move cursor to file end",
MoveCursorToLineStart = "Move cursor to line start",
MoveCursorToLineEnd = "Move cursor to line end",
}
17 changes: 17 additions & 0 deletions src/types/macros/EditorCommands/MoveCursorToFileEndCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { App } from "obsidian";
import { EditorCommand } from "./EditorCommand";
import { EditorCommandType } from "./EditorCommandType";

export class MoveCursorToFileEndCommand extends EditorCommand {
constructor() {
super(EditorCommandType.MoveCursorToFileEnd);
}

static run(app: App) {
const activeView = EditorCommand.getActiveMarkdownView(app);
const lastLine = activeView.editor.lastLine();
const lineLength = activeView.editor.getLine(lastLine).length;

activeView.editor.setCursor({ line: lastLine, ch: lineLength });
}
}
14 changes: 14 additions & 0 deletions src/types/macros/EditorCommands/MoveCursorToFileStartCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { App } from "obsidian";
import { EditorCommand } from "./EditorCommand";
import { EditorCommandType } from "./EditorCommandType";

export class MoveCursorToFileStartCommand extends EditorCommand {
constructor() {
super(EditorCommandType.MoveCursorToFileStart);
}

static run(app: App) {
const activeView = EditorCommand.getActiveMarkdownView(app);
activeView.editor.setCursor({ line: 0, ch: 0 });
}
}
17 changes: 17 additions & 0 deletions src/types/macros/EditorCommands/MoveCursorToLineEndCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { App } from "obsidian";
import { EditorCommand } from "./EditorCommand";
import { EditorCommandType } from "./EditorCommandType";

export class MoveCursorToLineEndCommand extends EditorCommand {
constructor() {
super(EditorCommandType.MoveCursorToLineEnd);
}

static run(app: App) {
const activeView = EditorCommand.getActiveMarkdownView(app);
const { line: lineNumber } = activeView.editor.getCursor();
const lineLength = activeView.editor.getLine(lineNumber).length;

activeView.editor.setCursor({ line: lineNumber, ch: lineLength });
}
}
16 changes: 16 additions & 0 deletions src/types/macros/EditorCommands/MoveCursorToLineStartCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { App } from "obsidian";
import { EditorCommand } from "./EditorCommand";
import { EditorCommandType } from "./EditorCommandType";

export class MoveCursorToLineStartCommand extends EditorCommand {
constructor() {
super(EditorCommandType.MoveCursorToLineStart);
}

static run(app: App) {
const activeView = EditorCommand.getActiveMarkdownView(app);
const { line: lineNumber } = activeView.editor.getCursor();

activeView.editor.setCursor({ line: lineNumber, ch: 0 });
}
}
Loading