Skip to content

Commit

Permalink
feat: ✨ Fix #4: Loop back to start if no next instance is found
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Oct 22, 2021
1 parent 7d0b170 commit aada0ff
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 65 deletions.
74 changes: 44 additions & 30 deletions main.js

Large diffs are not rendered by default.

101 changes: 66 additions & 35 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,62 +72,93 @@ export default class MyPlugin extends Plugin {
this.addSettingTab(new SettingTab(this.app, this));
}

createSelection(
editor: Editor,
nextI: number,
currSelection: string
): EditorSelectionOrCaret {
const { line, ch } = editor.offsetToPos(nextI);
const anchor: EditorPosition = {
ch,
line,
};
const head: EditorPosition = {
ch: ch + currSelection.length,
line,
};
return { anchor, head };
}

reconstructCurrentSelections(selections: EditorSelectionOrCaret[]) {
const newSelections: EditorSelectionOrCaret[] = [];
selections.forEach((selection) => {
newSelections.push({
anchor: selection.anchor,
head: selection.head,
});
});
return newSelections;
}

setSelections(
appendQ: boolean,
editor: Editor,
editorSelection: EditorSelectionOrCaret
) {
if (appendQ) {
const currSelections: EditorSelectionOrCaret[] = editor.listSelections();

const reconstructedSelections =
this.reconstructCurrentSelections(currSelections);
reconstructedSelections.push(editorSelection);
editor.setSelections(reconstructedSelections);
} else {
editor.setSelections([editorSelection]);
}
}

async selectNextInstance(editor: Editor, appendQ = false) {
const currFile = this.app.workspace.getActiveFile();
const content = await this.app.vault.read(currFile);

// const currSelection = editor.getSelection();

const lastSelection = editor.listSelections().last();
const currSelection = editor.getRange(
lastSelection.anchor,
lastSelection.head
);

const currOffset = editor.posToOffset(lastSelection.head);

const nextI = content.indexOf(currSelection, currOffset);

console.log({ currOffset, nextI });

if (nextI > -1) {
const { line, ch } = editor.offsetToPos(nextI);
const anchor: EditorPosition = {
ch,
line,
};
const head: EditorPosition = {
ch: ch + currSelection.length,
line,
};

if (appendQ) {
const currSelections: EditorSelectionOrCaret[] =
editor.listSelections();

const reconstructedSelections =
this.reconstructCurrentSelections(currSelections);
reconstructedSelections.push({ anchor, head });
editor.setSelections(reconstructedSelections);
const editorSelection = this.createSelection(
editor,
nextI,
currSelection
);
this.setSelections(appendQ, editor, editorSelection);
} else {
const loopedI = content.indexOf(currSelection);
if (loopedI > -1) {
const editorSelection = this.createSelection(
editor,
loopedI,
currSelection
);
this.setSelections(appendQ, editor, editorSelection);
new Notice(
`Cannot find next instance of "${currSelection}", looping back to start.`
);
} else {
editor.setSelections([{ anchor, head }]);
new Notice(
`Cannot find next instance of "${currSelection}" anywhere else in file.`
);
}
} else {
new Notice(`Cannot find next instance of "${currSelection}"`);
}
}

reconstructCurrentSelections(selections: EditorSelectionOrCaret[]) {
const newSelections: EditorSelectionOrCaret[] = [];
selections.forEach((selection) => {
newSelections.push({
anchor: selection.anchor,
head: selection.head,
});
});
return newSelections;
}

onunload() {}

async loadSettings() {
Expand Down

0 comments on commit aada0ff

Please sign in to comment.