Skip to content

Commit

Permalink
show inline math tooltips when cursor is just outside $ symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
artisticat1 committed Oct 1, 2022
1 parent 61cd456 commit 83ba59b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/editor_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,31 @@ export function isWithinInlineEquationState(state: EditorState):boolean {
}


export function isTouchingInlineEquation(state: EditorState, pos: number):number {
// Returns 0 if pos is not touching a $ that marks an inline equation
// Returns 1 if pos+1 is inside an inline eqn
// Returns -1 if pos-1 is inside an inline eqn

const tree = syntaxTree(state);
const prevToken = tree.resolveInner(pos-1, 1).name;
const token = tree.resolveInner(pos, 1).name;
const nextToken = tree.resolveInner(pos+1, 1).name;

if (token.contains("math-end") && !(prevToken.contains("math-end")) && !(nextToken.contains("math-end"))) {
return -1;
}
else if (!(token.contains("math-begin")) && nextToken.contains("math-begin")) {
const nextNextToken = tree.resolveInner(pos+2, 1).name;

if (!(nextNextToken.contains("math-begin"))) {
return 1;
}
}

return 0;
}


export function getEquationBounds(view: EditorView | EditorState, pos?: number):{start: number, end: number} {
const s = view instanceof EditorView ? view.state : view;

Expand Down
22 changes: 19 additions & 3 deletions src/inline_math_tooltip.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Tooltip, showTooltip, EditorView} from "@codemirror/view"
import {StateField, EditorState} from "@codemirror/state"
import { getEquationBounds, isWithinEquation, isWithinInlineEquationState } from "./editor_helpers";
import { getEquationBounds, isTouchingInlineEquation, isWithinEquation, isWithinInlineEquationState } from "./editor_helpers";
import { MarkdownRenderer } from "obsidian";

export const cursorTooltipField = StateField.define<readonly Tooltip[]>({
Expand All @@ -17,9 +17,25 @@ export const cursorTooltipField = StateField.define<readonly Tooltip[]>({


function getCursorTooltips(state: EditorState): readonly Tooltip[] {
const isInsideInlineEqn = (isWithinEquation(state) && isWithinInlineEquationState(state));
let shouldShowTooltip = isInsideInlineEqn;
let isTouchingInlineEqn;
let pos = state.selection.main.from;

if (!isInsideInlineEqn) {
isTouchingInlineEqn = isTouchingInlineEquation(state, pos - 1);

if (isWithinEquation(state) && isWithinInlineEquationState(state)) {
const bounds = getEquationBounds(state);
if (isTouchingInlineEqn != 0) {
pos += isTouchingInlineEqn;
shouldShowTooltip = true;
}
}



if (shouldShowTooltip) {

const bounds = getEquationBounds(state, pos);
if (!bounds) return [];

// Don't render an empty equation
Expand Down

0 comments on commit 83ba59b

Please sign in to comment.