Skip to content

Commit

Permalink
feat: ✨ Relation suggestor also detects if your cursor is insideYaml …
Browse files Browse the repository at this point in the history
…or not
  • Loading branch information
SkepticMystic committed Jan 29, 2022
1 parent 957ebe4 commit 4d62eef
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
8 changes: 4 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10006,7 +10006,7 @@ function isInsideYaml(app) {
return false;
const { start, end } = frontmatter.position;
const currOff = editor.posToOffset(editor.getCursor());
if (currOff >= start.offset + 4 && currOff <= end.offset - 4)
if (currOff >= start.offset && currOff <= end.offset)
return true;
else
return false;
Expand Down Expand Up @@ -33518,11 +33518,11 @@ class RelationSuggestor extends obsidian.EditorSuggest {
});
}
selectSuggestion(suggestion) {
const { context } = this;
const { context, plugin } = this;
if (context) {
const trig = this.plugin.settings.relSuggestorTrigger;
const trig = plugin.settings.relSuggestorTrigger;
const { start, end, editor } = context;
editor.replaceRange(suggestion + ":", { ch: start.ch + 1 - trig.length, line: start.line }, end);
editor.replaceRange(suggestion + (isInsideYaml(plugin.app) ? ": " : ":: "), { ch: start.ch + 1 - trig.length, line: start.line }, end);
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/RelationSuggestor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
EditorSuggestTriggerInfo,
TFile,
} from "obsidian";
import { isInsideYaml } from "./Utils/ObsidianUtils";
import type BCPlugin from "./main";
import { escapeRegex } from "./Utils/generalUtils";
import { getFields } from "./Utils/HierUtils";
Expand Down Expand Up @@ -54,12 +55,13 @@ export class RelationSuggestor extends EditorSuggest<string> {
}

selectSuggestion(suggestion: string): void {
const { context } = this;
const { context, plugin } = this;
if (context) {
const trig = this.plugin.settings.relSuggestorTrigger;
const trig = plugin.settings.relSuggestorTrigger;
const { start, end, editor } = context;

editor.replaceRange(
suggestion + ":",
suggestion + (isInsideYaml(plugin.app) ? ": " : ":: "),
{ ch: start.ch + 1 - trig.length, line: start.line },
end
);
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/ObsidianUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,6 @@ export function isInsideYaml(app: App): boolean | null {

const { start, end } = frontmatter.position;
const currOff = editor.posToOffset(editor.getCursor());
if (currOff >= start.offset + 4 && currOff <= end.offset - 4) return true;
if (currOff >= start.offset && currOff <= end.offset) return true;
else return false;
}

0 comments on commit 4d62eef

Please sign in to comment.