Skip to content

Commit

Permalink
Merge branch 'extract_functions'
Browse files Browse the repository at this point in the history
  • Loading branch information
tamuratak committed Aug 25, 2023
2 parents 8cdebde + fe9e1f4 commit 9b75594
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 51 deletions.
90 changes: 45 additions & 45 deletions src/components/mathpreviewlib/cursorrenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,62 @@ import { convertPositionToOffset, findPrevNextNode, isSubOrSuper, toLuPos } from
import type { UtensilsParser } from '../utensilsparser'


export class CursorRenderer {
private currentTeXString: string | undefined
private currentAst: latexParser.LatexAst | undefined
// Test whether cursor is in tex command strings
// like \begin{...} \end{...} \xxxx{ \[ \] \( \) or \\
function isCursorInTeXCommand(document: ITextDocumentLike): boolean {
const editor = vscode.window.activeTextEditor
if (!editor) {
return false
}
const cursor = editor.selection.active
const r = document.getWordRangeAtPosition(cursor, /\\(?:begin|end|label)\{.*?\}|\\[a-zA-Z]+\{?|\\[()[\]]|\\\\/)
if (r && r.start.isBefore(cursor) && r.end.isAfter(cursor) ) {
return true
}
return false
}

constructor(private readonly extension: {
readonly utensilsParser: UtensilsParser
}) { }
function getCursorPosInSnippet(texMath: TexMathEnv, cursorPos: vscode.Position): vscode.Position {
const line = cursorPos.line - texMath.range.start.line
const character = line === 0 ? cursorPos.character - texMath.range.start.character : cursorPos.character
return new vscode.Position(line, character)
}

// Test whether cursor is in tex command strings
// like \begin{...} \end{...} \xxxx{ \[ \] \( \) or \\
isCursorInTeXCommand(document: ITextDocumentLike): boolean {
const editor = vscode.window.activeTextEditor
if (!editor) {
return false
}
const cursor = editor.selection.active
const r = document.getWordRangeAtPosition(cursor, /\\(?:begin|end|label)\{.*?\}|\\[a-zA-Z]+\{?|\\[()[\]]|\\\\/)
if (r && r.start.isBefore(cursor) && r.end.isAfter(cursor) ) {
function isInNonMathCommand(findResult: latexParser.FindResult<latexParser.Node> | undefined): boolean {
let parent = findResult?.parent
while (parent) {
const node = parent.node
if (
latexParser.isAmsMathTextCommand(parent.node) ||
latexParser.isCommand(node) && node.name === 'tag'
) {
return true
}
return false
parent = parent.parent
}
return false
}

cursorPosInSnippet(texMath: TexMathEnv, cursorPos: vscode.Position): vscode.Position {
const line = cursorPos.line - texMath.range.start.line
const character = line === 0 ? cursorPos.character - texMath.range.start.character : cursorPos.character
return new vscode.Position(line, character)
}
export function isCursorInsideTexMath(texMathRange: vscode.Range, cursorPos: vscode.Position): boolean {
return texMathRange.contains(cursorPos) && !texMathRange.start.isEqual(cursorPos) && !texMathRange.end.isEqual(cursorPos)
}

isInNonMathCommand(findResult: latexParser.FindResult<latexParser.Node> | undefined): boolean {
let parent = findResult?.parent
while (parent) {
const node = parent.node
if (
latexParser.isAmsMathTextCommand(parent.node) ||
latexParser.isCommand(node) && node.name === 'tag'
) {
return true
}
parent = parent.parent
}
return false
}
export class CursorRenderer {
private currentTeXString: string | undefined
private currentAst: latexParser.LatexAst | undefined

constructor(private readonly extension: {
readonly utensilsParser: UtensilsParser
}) { }

async insertCursor(texMath: TexMathEnv, originalCursorPos: vscode.Position, cursor: string): Promise<string | undefined> {
const cursorPos = this.cursorPosInSnippet(texMath, originalCursorPos)
const cursorPos = getCursorPosInSnippet(texMath, originalCursorPos)
const findResult = await this.findNodeAt(texMath, cursorPos)
if (!findResult) {
return
}
const cursorNode = findResult.node
if (this.isInNonMathCommand(findResult)) {
if (isInNonMathCommand(findResult)) {
return
}
if (cursorNode && latexParser.isCommand(cursorNode)) {
Expand Down Expand Up @@ -111,7 +115,7 @@ export class CursorRenderer {
}
}

async findNodeAt(texMath: TexMathEnv, cursorPosInSnippet: vscode.Position) {
private async findNodeAt(texMath: TexMathEnv, cursorPosInSnippet: vscode.Position) {
let ast: latexParser.LatexAst | undefined
if (texMath.texString === this.currentTeXString && this.currentAst) {
ast = this.currentAst
Expand Down Expand Up @@ -144,10 +148,10 @@ export class CursorRenderer {
if (!cursorPos) {
return
}
if (!this.isCursorInsideTexMath(texMathRange, cursorPos)) {
if (!isCursorInsideTexMath(texMathRange, cursorPos)) {
return
}
if (this.isCursorInTeXCommand(document)) {
if (isCursorInTeXCommand(document)) {
return
}
const symbol = configuration.get('hover.preview.cursor.symbol') as string
Expand All @@ -157,8 +161,4 @@ export class CursorRenderer {
return ret
}

isCursorInsideTexMath(texMathRange: vscode.Range, cursorPos: vscode.Position): boolean {
return texMathRange.contains(cursorPos) && !texMathRange.start.isEqual(cursorPos) && !texMathRange.end.isEqual(cursorPos)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as vscode from 'vscode'
import { runUnitTestWithFixture } from '../../../../utils/ciutils'
import { UtensilsParser } from '../../../../../src/components/utensilsparser'
import { TeXMathEnvFinder } from '../../../../../src/components/mathpreviewlib/texmathenvfinder'
import { CursorRenderer } from '../../../../../src/components/mathpreviewlib/cursorrenderer'
import { CursorRenderer, isCursorInsideTexMath } from '../../../../../src/components/mathpreviewlib/cursorrenderer'
import { TextDocumentLike } from '../../../../../src/components/mathpreviewlib/textdocumentlike'

const utensilsParser = new UtensilsParser()
Expand Down Expand Up @@ -34,21 +34,20 @@ suite('unit test suite: mathpreviewlib/cursorrenderer', () => {
const cursorPos = new vscode.Position(0, 0)
const texMath = finder.findMathEnvIncludingPosition(doc, cursorPos)
assert(texMath)
const renderer = new CursorRenderer({utensilsParser})

const result = renderer.isCursorInsideTexMath(texMath.range, cursorPos)
const result = isCursorInsideTexMath(texMath.range, cursorPos)
assert.strictEqual(result, false)

const cursorPos1 = new vscode.Position(0, 1)
const result1 = renderer.isCursorInsideTexMath(texMath.range, cursorPos1)
const result1 = isCursorInsideTexMath(texMath.range, cursorPos1)
assert.strictEqual(result1, true)

const cursorPos4 = new vscode.Position(0, 4)
const result4 = renderer.isCursorInsideTexMath(texMath.range, cursorPos4)
const result4 = isCursorInsideTexMath(texMath.range, cursorPos4)
assert.strictEqual(result4, true)

const cursorPos5 = new vscode.Position(0, 5)
const result5 = renderer.isCursorInsideTexMath(texMath.range, cursorPos5)
const result5 = isCursorInsideTexMath(texMath.range, cursorPos5)
assert.strictEqual(result5, false)

})
Expand Down

0 comments on commit 9b75594

Please sign in to comment.