|
| 1 | +/* |
| 2 | + * cursor.test.ts |
| 3 | + * |
| 4 | + * Copyright (C) 2026 by Posit Software, PBC |
| 5 | + */ |
| 6 | + |
| 7 | +import assert from "node:assert/strict"; |
| 8 | +import test from "node:test"; |
| 9 | +import { runInNewContext } from "node:vm"; |
| 10 | + |
| 11 | +import { Position, Range } from "vscode-languageserver-types"; |
| 12 | + |
| 13 | +import { Document } from "../src/document"; |
| 14 | +import { mathRange } from "../src/markdown/cursor"; |
| 15 | +import { markdownitParser } from "../src/markdown/parsers/markdownit"; |
| 16 | + |
| 17 | +function document(text: string): Document { |
| 18 | + return { |
| 19 | + uri: "file:///math.qmd", |
| 20 | + languageId: "quarto", |
| 21 | + version: 1, |
| 22 | + lineCount: 1, |
| 23 | + getText: () => text, |
| 24 | + positionAt: (offset) => Position.create(0, offset), |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +for (const [text, character] of [ |
| 29 | + ["Before $$x$$ after", 0], |
| 30 | + ["Before $$x$$ after", 16], |
| 31 | + ["Before $$x$$ and $$y$$ after", 14], |
| 32 | + ["Before $$x$$ and $$y$$ after", 26], |
| 33 | +] as const) { |
| 34 | + test(`returns no math at character ${character} in ${JSON.stringify(text)}`, () => { |
| 35 | + assert.equal( |
| 36 | + runInNewContext( |
| 37 | + "mathRange(parser, doc, position)", |
| 38 | + { mathRange, parser: markdownitParser(), doc: document(text), position: Position.create(0, character) }, |
| 39 | + { timeout: 1000 } |
| 40 | + ), |
| 41 | + null |
| 42 | + ); |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +test("finds later display math and resets the search for repeated requests", () => { |
| 47 | + const parser = markdownitParser(); |
| 48 | + const doc = document("Before $$x$$ and $$y$$ after"); |
| 49 | + for (const character of [22, 12, 22, 12]) { |
| 50 | + assert.deepEqual( |
| 51 | + runInNewContext( |
| 52 | + "mathRange(parser, doc, position)", |
| 53 | + { mathRange, parser, doc, position: Position.create(0, character) }, |
| 54 | + { timeout: 1000 } |
| 55 | + ), |
| 56 | + character === 22 |
| 57 | + ? { math: "y", range: Range.create(0, 17, 0, 22) } |
| 58 | + : { math: "x", range: Range.create(0, 7, 0, 12) } |
| 59 | + ); |
| 60 | + } |
| 61 | +}); |
0 commit comments