first commit

This commit is contained in:
2025-04-24 13:11:28 +08:00
commit ff9c54d5e4
5960 changed files with 834111 additions and 0 deletions

View File

@@ -0,0 +1,329 @@
import { expect } from 'chai'
import { EditorState, Text } from '@codemirror/state'
import { LaTeXLanguage } from '../../../../../../frontend/js/features/source-editor/languages/latex/latex-language'
import {
ensureSyntaxTree,
foldNodeProp,
LanguageSupport,
} from '@codemirror/language'
import { EditorView } from '@codemirror/view'
const latex = new LanguageSupport(LaTeXLanguage)
const makeView = (lines: string[]): EditorView => {
const text = Text.of(lines)
const view = new EditorView({
state: EditorState.create({
doc: text,
extensions: [latex],
}),
})
return view
}
type Fold = { from: number; to: number }
const _getFolds = (view: EditorView) => {
const ranges: Fold[] = []
const tree = ensureSyntaxTree(view.state, view.state.doc.length)
if (!tree) {
throw new Error("Couldn't get Syntax Tree")
}
tree.iterate({
enter: nodeRef => {
const prop = nodeRef.type.prop(foldNodeProp)
if (prop) {
const hasFold = prop(nodeRef.node, view.state)
if (hasFold) {
ranges.push({ from: hasFold.from, to: hasFold.to })
}
}
},
})
return ranges
}
describe('CodeMirror LaTeX-folding', function () {
describe('With empty document', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = ['']
view = makeView(content)
})
it('should not produce any folds', function () {
const folds = _getFolds(view)
expect(folds).to.be.empty
})
})
describe('Sectioning command folding', function () {
describe('with no foldable sections', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = ['hello', 'test']
view = makeView(content)
})
it('should not produce any folds', function () {
const folds = _getFolds(view)
expect(folds).to.be.empty
})
})
describe('with one foldable section', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = ['hello', '\\section{one}', 'a', 'b', 'c']
view = makeView(content)
})
it('should produce one fold', function () {
const folds = _getFolds(view)
expect(folds.length).to.equal(1)
})
it('should fold from the section line to last line', function () {
const folds = _getFolds(view)
const fold = folds[0]
expect(view.state.doc.lineAt(fold.from).number).to.equal(2)
expect(view.state.doc.lineAt(fold.to).number).to.equal(
view.state.doc.lines
)
})
})
describe('with two foldable sections', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = [
'hello',
'\\section{one}',
'a',
'b',
'\\section{two}',
'c',
'd',
]
view = makeView(content)
})
it('should produce two folds', function () {
const folds = _getFolds(view)
expect(folds.length).to.equal(2)
expect(view.state.doc.lineAt(folds[0].from).number).to.equal(2)
expect(view.state.doc.lineAt(folds[0].to).number).to.equal(4)
expect(view.state.doc.lineAt(folds[1].from).number).to.equal(5)
expect(view.state.doc.lineAt(folds[1].to).number).to.equal(
view.state.doc.lines
)
})
})
describe('with realistic nesting', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = [
'hello',
'\\chapter{1}',
' a',
' \\section{1.1}',
' a',
' \\subsection{1.1.1}',
' a',
' \\section{1.2}',
' a',
' \\subsection{1.2.1}',
' a',
'\\chapter{2}',
' a',
' \\section{2.1}',
' a',
' \\section{2.2}',
' a',
]
view = makeView(content)
})
it('should produce many folds', function () {
const folds = _getFolds(view)
expect(folds.length).to.equal(8)
const foldDescriptions = folds.map(fold => {
const fromLine = view.state.doc.lineAt(fold.from).number
const toLine = view.state.doc.lineAt(fold.to).number
return { fromLine, toLine }
})
expect(foldDescriptions).to.deep.equal([
{ fromLine: 2, toLine: 11 },
{ fromLine: 4, toLine: 7 },
{ fromLine: 6, toLine: 7 },
{ fromLine: 8, toLine: 11 },
{ fromLine: 10, toLine: 11 },
{ fromLine: 12, toLine: 17 },
{ fromLine: 14, toLine: 15 },
{ fromLine: 16, toLine: 17 },
])
})
})
})
describe('Environment folding', function () {
describe('with single environment', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = ['\\begin{foo}', 'content', '\\end{foo}']
view = makeView(content)
})
it('should fold the environment', function () {
const folds = _getFolds(view)
expect(folds.length).to.equal(1)
expect(folds).to.deep.equal([{ from: 11, to: 20 }])
})
})
describe('with nested environment', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = [
'\\begin{foo}',
'\\begin{bar}',
'content',
'\\end{bar}',
'\\end{foo}',
]
view = makeView(content)
})
it('should fold the environment', function () {
const folds = _getFolds(view)
expect(folds.length).to.equal(2)
expect(folds).to.deep.equal([
{ from: 11, to: 42 },
{ from: 23, to: 32 },
])
})
})
})
describe('Comment folding', function () {
describe('with a single set of comments', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = ['Hello', '% {', 'this is folded', '% }', 'End']
view = makeView(content)
})
it('should fold the region marked by comments', function () {
const folds = _getFolds(view)
expect(folds.length).to.equal(1)
expect(folds).to.deep.equal([{ from: 9, to: 27 }])
})
})
describe('with several sets of comments', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = [
'Hello',
'% {',
'this is folded',
'% }',
'',
'% {',
'and this also',
'% }',
'End',
]
view = makeView(content)
})
it('should fold both regions marked by comments', function () {
const folds = _getFolds(view)
expect(folds.length).to.equal(2)
expect(folds).to.deep.equal([
{ from: 9, to: 27 },
{ from: 33, to: 50 },
])
})
})
describe('with nested sets of comments', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = [
'Hello',
'% {',
'one',
'% {',
'two',
'% {',
'three',
'% }',
'two',
'% }',
'one',
'% }',
'End',
]
view = makeView(content)
})
it('should fold all the regions marked by comments, with nesting', function () {
const folds = _getFolds(view)
expect(folds.length).to.equal(3)
expect(folds).to.deep.equal([
{ from: 9, to: 50 },
{ from: 17, to: 42 },
{ from: 25, to: 34 },
])
})
})
describe('with fold comment spanning entire document', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = ['% {', 'Hello', '% }']
view = makeView(content)
})
it('should fold', function () {
const folds = _getFolds(view)
expect(folds.length).to.equal(1)
expect(folds).to.deep.equal([{ from: 3, to: 12 }])
})
})
describe('with fold comment at start of document', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = ['% {', 'Hello', '% }', 'Test']
view = makeView(content)
})
it('should fold', function () {
const folds = _getFolds(view)
expect(folds.length).to.equal(1)
expect(folds).to.deep.equal([{ from: 3, to: 12 }])
})
})
describe('with fold comment at end of document', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = ['Test', '% {', 'Hello', '% }']
view = makeView(content)
})
it('should fold', function () {
const folds = _getFolds(view)
expect(folds.length).to.equal(1)
expect(folds).to.deep.equal([{ from: 8, to: 17 }])
})
})
})
})

View File

@@ -0,0 +1,786 @@
import { assert } from 'chai'
import LintWorker from '../../../../../../frontend/js/features/source-editor/languages/latex/linter/latex-linter.worker.js'
import { errorsToDiagnostics } from '../../../../../../frontend/js/features/source-editor/languages/latex/linter/errors-to-diagnostics'
import { Diagnostic } from '@codemirror/lint'
import { mergeCompatibleOverlappingDiagnostics } from '../../../../../../frontend/js/features/source-editor/languages/latex/linter/merge-overlapping-diagnostics'
const { Parse } = new LintWorker()
describe('LatexLinter', function () {
it('should accept a simple environment match without errors', function () {
const { errors } = Parse('\\begin{foo}\n' + '\\end{foo}\n')
assert.equal(errors.length, 0)
})
it('should accept an invalid \\it* command', function () {
const { errors } = Parse('\\it*hello\n' + '\\bye\n')
assert.equal(errors.length, 0)
})
it('should accept newcomlumntype', function () {
const { errors } = Parse(
'hello\n' +
'\\newcolumntype{M}[1]{>{\\begin{varwidth}[t]{#1}}l<{\\end{varwidth}}}\n' +
'bye'
)
assert.equal(errors.length, 0)
})
it('should accept newenvironment', function () {
const { errors } = Parse(
'\\newenvironment{Algorithm}[2][tbh]%\n' +
'{\\begin{myalgo}[#1]\n' +
'\\centering\n' +
'\\part{title}\\begin{minipage}{#2}\n' +
'\\begin{algorithm}[H]}%\n' +
'{\\end{algorithm}\n' +
'\\end{minipage}\n' +
'\\end{myalgo}}'
)
assert.equal(errors.length, 0)
})
it('should accept newenvironment II', function () {
const { errors } = Parse(
'\\newenvironment{claimproof}[1][\\myproofname]{\\begin{proof}[#1]\\renewcommand*{\\qedsymbol}{\\(\\diamondsuit\\)}}{\\end{proof}}'
)
assert.equal(errors.length, 0)
})
it('should accept superscript inside math mode', function () {
const { errors } = Parse('this is $a^b$ test')
assert.equal(errors.length, 0)
})
it('should accept subscript inside math mode', function () {
const { errors } = Parse('this is $a_b$ test')
assert.equal(errors.length, 0)
})
it('should return an error for superscript outside math mode', function () {
const { errors } = Parse('this is a^b test')
assert.equal(errors.length, 1)
assert.equal(errors[0].text, '^ must be inside math mode')
assert.equal(errors[0].type, 'error')
})
it('should return an error subscript outside math mode', function () {
const { errors } = Parse('this is a_b test')
assert.equal(errors.length, 1)
assert.equal(errors[0].text, '_ must be inside math mode')
assert.equal(errors[0].type, 'error')
})
it('should accept math mode inside \\hbox outside math mode', function () {
const { errors } = Parse('this is \\hbox{for every $bar$}')
assert.equal(errors.length, 0)
})
it('should accept math mode inside \\hbox inside math mode', function () {
const { errors } = Parse('this is $foo = \\hbox{for every $bar$}$ test')
assert.equal(errors.length, 0)
})
it('should accept math mode inside \\text inside math mode', function () {
const { errors } = Parse('this is $foo = \\text{for every $bar$}$ test')
assert.equal(errors.length, 0)
})
it('should accept verbatim', function () {
const { errors } = Parse(
'this is text\n' +
'\\begin{verbatim}\n' +
'this is verbatim\n' +
'\\end{verbatim}\n' +
'this is more text\n'
)
assert.equal(errors.length, 0)
})
it('should accept verbatim with environment inside', function () {
const { errors } = Parse(
'this is text\n' +
'\\begin{verbatim}\n' +
'this is verbatim\n' +
'\\begin{foo}\n' +
'this is verbatim too\n' +
'\\end{foo}\n' +
'\\end{verbatim}\n' +
'this is more text\n'
)
assert.equal(errors.length, 0)
})
it('should accept verbatim with \\begin{verbatim} inside', function () {
const { errors } = Parse(
'this is text\n' +
'\\begin{verbatim}\n' +
'this is verbatim\n' +
'\\begin{verbatim}\n' +
'this is verbatim too\n' +
'\\end{verbatim}\n' +
'this is more text\n'
)
assert.equal(errors.length, 0)
})
it('should accept equation', function () {
const { errors } = Parse(
'this is text\n' +
'\\begin{equation}\n' +
'\\alpha^2 + b^2 = c^2\n' +
'\\end{equation}\n' +
'this is more text\n'
)
assert.equal(errors.length, 0)
})
it('should accept $$', function () {
const { errors } = Parse(
'this is text\n' +
'$$\n' +
'\\alpha^2 + b^2 = c^2\n' +
'$$\n' +
'this is more text\n'
)
assert.equal(errors.length, 0)
})
it('should accept $', function () {
const { errors } = Parse(
'this is text $\\alpha^2 + b^2 = c^2$' + ' this is more text\n'
)
assert.equal(errors.length, 0)
})
it('should accept \\[', function () {
const { errors } = Parse(
'this is text\n' +
'\\[\n' +
'\\alpha^2 + b^2 = c^2\n' +
'\\]\n' +
'this is more text\n'
)
assert.equal(errors.length, 0)
})
it('should accept \\(', function () {
const { errors } = Parse(
'this is text \\(\\alpha^2 + b^2 = c^2\\)' + ' this is more text\n'
)
assert.equal(errors.length, 0)
})
it('should accept \\begin{foo}', function () {
const { errors } = Parse(
'this is text\n' +
'\\begin{foo}\n' +
'this is foo\n' +
'\\end{foo}\n' +
'this is more text\n'
)
assert.equal(errors.length, 0)
})
it('should accept \\begin{foo_bar}', function () {
const { errors } = Parse(
'this is text\n' +
'\\begin{foo_bar}\n' +
'this is foo bar\n' +
'\\end{foo_bar}\n' +
'this is more text\n'
)
assert.equal(errors.length, 0)
})
it('should accept \\begin{foo} \\begin{bar}', function () {
const { errors } = Parse(
'this is text\n' +
'\\begin{foo}\n' +
'\\begin{bar}\n' +
'\\begin{baz}\n' +
'this is foo bar baz\n' +
'\\end{baz}\n' +
'\\end{bar}\n' +
'\\end{foo}\n' +
'this is more text\n'
)
assert.equal(errors.length, 0)
})
it('should accept \\verb|...|', function () {
const { errors } = Parse('this is text \\verb|hello| and more\n')
assert.equal(errors.length, 0)
})
it('should accept \\verb|...| with special chars', function () {
const { errors } = Parse('this is text \\verb|{}()^_@$xhello| and more\n')
assert.equal(errors.length, 0)
})
it('should accept \\url|...|', function () {
const { errors } = Parse(
'this is text \\url|http://www.overleaf.com/| and more\n'
)
assert.equal(errors.length, 0)
})
it('should accept \\url{...}', function () {
const { errors } = Parse(
'this is text \\url{http://www.overleaf.com/} and more\n'
)
assert.equal(errors.length, 0)
})
it('should accept \\url{...} with % chars', function () {
const { errors } = Parse(
'this is text \\url{http://www.overleaf.com/hello%20world} and more\n'
)
assert.equal(errors.length, 0)
})
it('should accept \\href{...}{...}', function () {
const { errors } = Parse(
'this is text \\href{http://www.overleaf.com/}{test} and more\n'
)
assert.equal(errors.length, 0)
})
it('should accept \\href{...}{...} with dollarsign in url', function () {
const { errors } = Parse(
'this is text \\href{http://www.overleaf.com/foo=$bar}{test} and more\n'
)
assert.equal(errors.length, 0)
})
it('should not accept \\href|...|{...}', function () {
const { errors } = Parse(
'this is text \\href|http://www.overleaf.com|{test} and more\n'
)
assert.equal(errors.length, 1)
assert.equal(errors[0].text, 'invalid href command')
assert.equal(errors[0].type, 'error')
})
it('should catch error in text argument of \\href{...}{...}', function () {
const { errors } = Parse(
'this is text \\href{http://www.overleaf.com/foo=$bar}{i have made an $error} and more\n'
)
assert.equal(errors.length, 2)
assert.equal(errors[0].text, 'unclosed $ found at close group }')
assert.equal(errors[0].type, 'error')
assert.equal(errors[1].text, 'unexpected close group } after $')
assert.equal(errors[1].type, 'error')
})
it('should accept \\left( and \\right)', function () {
const { errors } = Parse('math $\\left( x + y \\right) = y + x$ and more\n')
assert.equal(errors.length, 0)
})
it('should accept \\left( and \\right.', function () {
const { errors } = Parse('math $\\left( x + y \\right. = y + x$ and more\n')
assert.equal(errors.length, 0)
})
it('should accept \\left. and \\right)', function () {
const { errors } = Parse('math $\\left. x + y \\right) = y + x$ and more\n')
assert.equal(errors.length, 0)
})
it('should accept complex math nesting', function () {
const { errors } = Parse(
'math $\\left( {x + {y + z} + x} \\right\\} = \\left[y + x\\right.$ and more\n'
)
assert.equal(errors.length, 0)
})
it('should accept math toggling $a$$b$', function () {
const { errors } = Parse('math $a$$b$ and more\n')
assert.equal(errors.length, 0)
})
it('should accept math toggling $$display$$$inline$', function () {
const { errors } = Parse('math $$display$$$inline$ and more\n')
assert.equal(errors.length, 0)
})
it('should accept math definition commands', function () {
const { errors } = Parse(
'\\let\\originalleft\\left\n' +
'\\let\\originalright\\right\n' +
'\\renewcommand{\\left}{\\mathopen{}\\mathclose\\bgroup\\originalleft}\n' +
'\\renewcommand{\\right}{\\aftergroup\\egroup\\originalright}\n'
)
assert.equal(errors.length, 0)
})
it('should accept math reflectbox commands', function () {
const { errors } = Parse('$\\reflectbox{$alpha$}$\n')
assert.equal(errors.length, 0)
})
it('should accept math scalebox commands', function () {
const { errors } = Parse('$\\scalebox{2}{$alpha$}$\n')
assert.equal(errors.length, 0)
})
it('should accept math rotatebox commands', function () {
const { errors } = Parse('$\\rotatebox{60}{$alpha$}$\n')
assert.equal(errors.length, 0)
})
it('should accept math resizebox commands', function () {
const { errors } = Parse('$\\resizebox{2}{3}{$alpha$}$\n')
assert.equal(errors.length, 0)
})
it('should accept all math box commands', function () {
const { errors } = Parse(
'\\[ \\left(\n' +
'\\shiftright{2ex}{\\raisebox{-2ex}{\\scalebox{2}{$\\ast$}}}\n' +
'\\reflectbox{$ddots$}\n' +
'\\right). \\]\n'
)
assert.equal(errors.length, 0)
})
it('should accept math tag commands', function () {
const { errors } = Parse('$\\tag{$alpha$}$\n')
assert.equal(errors.length, 0)
})
it('should accept math \\def commands', function () {
const { errors } = Parse(
'\\def\\peb[#1]{{\\left\\lfloor #1\\right\\rfloor}}'
)
assert.equal(errors.length, 0)
})
it('should accept math \\def commands II', function () {
const { errors } = Parse('\\def\\foo#1{\\gamma^#1}')
assert.equal(errors.length, 0)
})
it('should accept DeclareMathOperator', function () {
const { errors } = Parse('\\DeclareMathOperator{\\var}{\\Delta^2\\!}')
assert.equal(errors.length, 0)
})
it('should accept DeclarePairedDelimiter', function () {
const { errors } = Parse(
'\\DeclarePairedDelimiter{\\spro}{\\left(}{\\right)^{\\ast}}'
)
assert.equal(errors.length, 0)
})
it('should accept nested user-defined math commands', function () {
const { errors } = Parse(
'$\\foo{$\\alpha \\bar{x^y}{\\cite{hello}}$}{\\gamma}{$\\beta\\baz{\\alpha}$}{\\cite{foo}}$'
)
assert.equal(errors.length, 0)
})
it('should accept nested user-defined math commands II', function () {
const { errors } = Parse(
'\\foo{$\\alpha \\bar{x^y}{\\cite{hello}}$}{\\gamma}{$\\beta\\baz{\\alpha}$}{\\cite{foo}}'
)
assert.equal(errors.length, 0)
})
it('should accept newenvironment with multiple parameters', function () {
const { errors } = Parse(
'\\newenvironment{case}[1][\\textsc{Case}]\n' +
'{\\begin{trivlist}\\item[\\hskip \\labelsep {\\textsc{#1}}]}{\\end{trivlist}}'
)
assert.equal(errors.length, 0)
})
it('should accept newenvironment with no parameters', function () {
const { errors } = Parse(
'\\newenvironment{case}{\\begin{trivlist}\\item[\\hskip \\labelsep {\\textsc{#1}}]}{\\end{trivlist}}'
)
assert.equal(errors.length, 0)
})
it('should accept tikzfeynman', function () {
const { errors } = Parse(
'\\begin{equation*}\n' +
'\\feynmandiagram[layered layout, medium, horizontal=a to b] {\n' +
' a [particle=\\(H\\)] -- [scalar] b [dot] -- [photon] f1 [particle=\\(W^{\\pm}\\)],\n' +
' b -- [boson, edge label=\\(W^{\\mp}\\)] c [dot],\n' +
' c -- [fermion] f2 [particle=\\(f\\)],\n' +
" c -- [anti fermion] f3 [particle=\\(\\bar{f}'\\)],\n" +
' };this is a change\n' +
'\\end{equation*}'
)
assert.equal(errors.length, 0)
})
it('should return errors from malformed \\end', function () {
const { errors } = Parse(
'this is text\n' +
'\\begin{foo}\n' +
'\\begin{bar}\n' +
'this is foo bar baz\n' +
'\\end{bar\n' +
'\\end{foo}\n' +
'this is more text\n'
)
assert.equal(errors.length, 4)
assert.equal(errors[0].text, 'unclosed \\begin{bar} found at \\end{foo}')
assert.equal(errors[1].text, 'invalid environment command \\end{bar')
assert.equal(errors[2].text, 'unclosed open group { found at \\end{foo}')
assert.equal(errors[3].text, 'unexpected \\end{foo} after \\begin{bar}')
})
it('should accept \\newcommand*', function () {
const { errors } = Parse('\\newcommand*{\\foo}{\\bar}')
assert.equal(errors.length, 0)
})
it('should accept incomplete \\newcommand*', function () {
const { errors } = Parse('\\newcommand*{\\beq' + '}')
assert.equal(errors.length, 0)
})
it('should accept a plain hyperref command', function () {
const { errors } = Parse('\\hyperref{http://www.overleaf.com/}')
assert.equal(errors.length, 0)
})
it('should accept a hyperref command with underscores in the url ', function () {
const { errors } = Parse('\\hyperref{http://www.overleaf.com/my_page.html}')
assert.equal(errors.length, 0)
})
it('should accept a hyperref command with category, name and text arguments ', function () {
const { errors } = Parse(
'\\hyperref{http://www.overleaf.com/}{category}{name}{text}'
)
assert.equal(errors.length, 0)
})
it('should accept an underscore in a hyperref label', function () {
const { errors } = Parse('\\hyperref[foo_bar]{foo bar}')
assert.equal(errors.length, 0)
})
it('should reject a $ in a hyperref label', function () {
const { errors } = Parse('\\hyperref[foo$bar]{foo bar}')
assert.equal(errors.length, 1)
})
it('should reject an unclosed hyperref label', function () {
const { errors } = Parse('\\hyperref[foo_bar{foo bar}')
assert.equal(errors.length, 2)
assert.equal(errors[0].text, 'invalid hyperref label')
assert.equal(errors[1].text, 'unexpected close group }')
})
it('should accept a hyperref command without an optional argument', function () {
const { errors } = Parse('{\\hyperref{hello}}')
assert.equal(errors.length, 0)
})
it('should accept a hyperref command without an optional argument and multiple other arguments', function () {
const { errors } = Parse('{\\hyperref{}{}{fig411}}')
assert.equal(errors.length, 0)
})
it('should accept a hyperref command without an optional argument in an unclosed group', function () {
const { errors } = Parse('{\\hyperref{}{}{fig411}')
assert.equal(errors.length, 1)
assert.equal(errors[0].text, 'unclosed group {')
})
it('should accept documentclass with no options', function () {
const { errors } = Parse('\\documentclass{article}')
assert.equal(errors.length, 0)
})
it('should accept documentclass with options', function () {
const { errors } = Parse('\\documentclass[a4paper]{article}')
assert.equal(errors.length, 0)
})
it('should accept documentclass with underscore in options', function () {
const { errors } = Parse(
'\\documentclass[my_custom_document_class_option]{my-custom-class}'
)
assert.equal(errors.length, 0)
})
// %novalidate
// %begin novalidate
// %end novalidate
// \begin{foo}
// \begin{new_theorem}
// \begin{foo invalid environment command
// \newcommand{\foo}{\bar}
// \newcommand[1]{\foo}{\bar #1}
// \renewcommand...
// \def
// \DeclareRobustCommand
// \newcolumntype
// \newenvironment
// \renewenvironment
// \verb|....|
// \url|...|
// \url{...}
// \left( \right)
// \left. \right.
// $...$
// $$....$$
// $...$$...$
// $a^b$ vs a^b
// $$a^b$$ vs a^b
// Matrix for envs for {} left/right \[ \] \( \) $ $$ begin end
// begin equation
// align(*)
// equation(*)
// ]
// array(*)
// eqnarray(*)
// split
// aligned
// cases
// pmatrix
// gathered
// matrix
// alignedat
// smallmatrix
// subarray
// vmatrix
// shortintertext
it('should return math mode contexts', function () {
const { contexts } = Parse(
'\\begin{document}\n' +
'$$\n' +
'\\begin{array}\n' +
'\\left( \\foo{bar} \\right] & 2\n' +
'\\end{array}\n' +
'$$\n' +
'\\end{document}'
)
assert.equal(contexts.length, 1)
assert.equal(contexts[0].type, 'math')
assert.equal(contexts[0].range.start.row, 1)
assert.equal(contexts[0].range.start.column, 0)
assert.equal(contexts[0].range.end.row, 5)
assert.equal(contexts[0].range.end.column, 2)
})
it('should remove error when cursor is inside incomplete command', function () {
const { errors } = Parse('\\begin{}')
const diagnostics = errorsToDiagnostics(errors, 7, 9)
assert.equal(errors.length, 1)
assert.equal(diagnostics.length, 0)
})
it('should show an error when cursor is outside incomplete command', function () {
const { errors } = Parse('\\begin{}')
const diagnostics = errorsToDiagnostics(errors, 6, 9)
assert.equal(errors.length, 1)
assert.equal(diagnostics.length, 1)
assert.equal(diagnostics[0].from, 0)
assert.equal(diagnostics[0].to, 6)
})
it('should adjust an error range when the cursor is inside that range', function () {
const { errors } = Parse('\\begin{}')
const diagnostics = errorsToDiagnostics(errors, 4, 7)
assert.equal(errors.length, 1)
assert.equal(errors[0].startPos, 0)
assert.equal(errors[0].endPos, 7)
assert.equal(diagnostics.length, 1)
assert.equal(diagnostics[0].from, 0)
assert.equal(diagnostics[0].to, 4)
})
it('should reject an error when part of the error range is outside of the document boundaries', function () {
const { errors } = Parse('\\begin{}')
const diagnostics = errorsToDiagnostics(errors, 8, 6)
assert.equal(errors.length, 1)
assert.equal(diagnostics.length, 0)
})
it('should merge two overlapping identical diagnostics', function () {
const diagnostics: Diagnostic[] = [
{
from: 0,
to: 2,
message: 'Message 1',
severity: 'error',
},
{
from: 1,
to: 3,
message: 'Message 1',
severity: 'error',
},
]
const mergedDiagnostics = mergeCompatibleOverlappingDiagnostics(diagnostics)
assert.deepEqual(mergedDiagnostics, [
{
from: 0,
to: 3,
message: 'Message 1',
severity: 'error',
},
])
})
it('should merge two touching identical diagnostics', function () {
const diagnostics: Diagnostic[] = [
{
from: 0,
to: 2,
message: 'Message 1',
severity: 'error',
},
{
from: 2,
to: 3,
message: 'Message 1',
severity: 'error',
},
]
const mergedDiagnostics = mergeCompatibleOverlappingDiagnostics(diagnostics)
assert.deepEqual(mergedDiagnostics, [
{
from: 0,
to: 3,
message: 'Message 1',
severity: 'error',
},
])
})
it('should not merge two overlapping diagnostics with different messages', function () {
const diagnostics: Diagnostic[] = [
{
from: 0,
to: 2,
message: 'Message 1',
severity: 'error',
},
{
from: 1,
to: 3,
message: 'Message 2',
severity: 'error',
},
]
const mergedDiagnostics = mergeCompatibleOverlappingDiagnostics(diagnostics)
assert.deepEqual(diagnostics, mergedDiagnostics)
})
it('should not merge two overlapping diagnostics with different severities', function () {
const diagnostics: Diagnostic[] = [
{
from: 0,
to: 2,
message: 'Message 1',
severity: 'error',
},
{
from: 1,
to: 3,
message: 'Message 1',
severity: 'warning',
},
]
const mergedDiagnostics = mergeCompatibleOverlappingDiagnostics(diagnostics)
assert.deepEqual(diagnostics, mergedDiagnostics)
})
it('should merge three overlapping identical diagnostics', function () {
const diagnostics: Diagnostic[] = [
{
from: 0,
to: 2,
message: 'Message 1',
severity: 'error',
},
{
from: 1,
to: 4,
message: 'Message 1',
severity: 'error',
},
{
from: 3,
to: 5,
message: 'Message 1',
severity: 'error',
},
]
const mergedDiagnostics = mergeCompatibleOverlappingDiagnostics(diagnostics)
assert.deepEqual(mergedDiagnostics, [
{
from: 0,
to: 5,
message: 'Message 1',
severity: 'error',
},
])
})
it('should merge two separate sets of overlapping identical diagnostics', function () {
const diagnostics: Diagnostic[] = [
{
from: 0,
to: 2,
message: 'Message 1',
severity: 'error',
},
{
from: 2,
to: 3,
message: 'Message 1',
severity: 'error',
},
{
from: 2,
to: 5,
message: 'Message 2',
severity: 'error',
},
{
from: 4,
to: 6,
message: 'Message 3',
severity: 'error',
},
{
from: 5,
to: 7,
message: 'Message 3',
severity: 'error',
},
]
const mergedDiagnostics = mergeCompatibleOverlappingDiagnostics(diagnostics)
assert.deepEqual(mergedDiagnostics, [
{
from: 0,
to: 3,
message: 'Message 1',
severity: 'error',
},
{
from: 2,
to: 5,
message: 'Message 2',
severity: 'error',
},
{
from: 4,
to: 7,
message: 'Message 3',
severity: 'error',
},
])
})
})

View File

@@ -0,0 +1,537 @@
import { LanguageSupport } from '@codemirror/language'
import { EditorState, Text } from '@codemirror/state'
import { EditorView } from '@codemirror/view'
import { expect } from 'chai'
import { documentOutline } from '../../../../../../frontend/js/features/source-editor/languages/latex/document-outline'
import {
FlatOutline,
getNestingLevel,
} from '../../../../../../frontend/js/features/source-editor/utils/tree-query'
import { LaTeXLanguage } from '../../../../../../frontend/js/features/source-editor/languages/latex/latex-language'
import {
Book,
Chapter,
Paragraph,
Part,
Section,
SubParagraph,
SubSection,
SubSubSection,
} from '../../../../../../frontend/js/features/source-editor/lezer-latex/latex.terms.mjs'
const latex = new LanguageSupport(LaTeXLanguage, documentOutline.extension)
const makeView = (lines: string[]): EditorView => {
const text = Text.of(lines)
const view = new EditorView({
state: EditorState.create({
doc: text,
extensions: [latex],
}),
})
return view
}
const BOOK_LEVEL = getNestingLevel(Book)
const PART_LEVEL = getNestingLevel(Part)
const CHAPTER_LEVEL = getNestingLevel(Chapter)
const SECTION_LEVEL = getNestingLevel(Section)
const SUB_SECTION_LEVEL = getNestingLevel(SubSection)
const SUB_SUB_SECTION_LEVEL = getNestingLevel(SubSubSection)
const PARAGRAPH_LEVEL = getNestingLevel(Paragraph)
const SUB_PARAGRAPH_LEVEL = getNestingLevel(SubParagraph)
const FRAME_LEVEL = getNestingLevel('frame')
const insertText = (view: EditorView, position: number, text: string) => {
view.dispatch({
changes: [{ from: position, insert: text }],
})
}
const deleteText = (view: EditorView, position: number, length: number) => {
view.dispatch({
changes: [{ from: position - length, to: position }],
})
}
const getOutline = (view: EditorView): FlatOutline | null => {
return view.state.field(documentOutline)?.items || null
}
describe('CodeMirror LaTeX-FileOutline', function () {
describe('with no update', function () {
describe('an empty document', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = ['']
view = makeView(content)
})
it('should have empty outline', function () {
const outline = getOutline(view)
expect(outline).to.be.empty
})
})
describe('a document with nested sections', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = [
'line 1',
'\\section{sec title}',
'content',
'\\subsection{subsec title}',
]
view = makeView(content)
})
it('should have outline with different levels', function () {
const outline = getOutline(view)
expect(outline).to.be.deep.equal([
{
from: 7,
to: 26,
level: SECTION_LEVEL,
title: 'sec title',
line: 2,
},
{
from: 35,
to: 60,
level: SUB_SECTION_LEVEL,
title: 'subsec title',
line: 4,
},
])
})
})
describe('a document with sibling sections', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = [
'line 1',
'\\section{sec title 1}',
'content',
'\\section{sec title 2}',
]
view = makeView(content)
})
it('should have outline with same levels for siblings', function () {
const outline = getOutline(view)
expect(outline).to.be.deep.equal([
{
from: 7,
to: 28,
level: SECTION_LEVEL,
title: 'sec title 1',
line: 2,
},
{
from: 37,
to: 58,
level: SECTION_LEVEL,
title: 'sec title 2',
line: 4,
},
])
})
})
})
describe('with change to title', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = ['\\section{title }']
view = makeView(content)
const initialOutline = getOutline(view)
expect(initialOutline).to.deep.equal([
{
from: 0,
to: 16,
title: 'title ',
line: 1,
level: SECTION_LEVEL,
},
])
})
describe('for appending to title', function () {
beforeEach(function () {
insertText(view, 15, '1')
})
it('should update title in outline', function () {
const updatedOutline = getOutline(view)
expect(updatedOutline).to.deep.equal([
{
from: 0,
to: 17,
title: 'title 1',
line: 1,
level: SECTION_LEVEL,
},
])
})
})
describe('for removing from title', function () {
beforeEach(function () {
deleteText(view, 15, 1)
})
it('should update title in outline', function () {
const updatedOutline = getOutline(view)
expect(updatedOutline).to.deep.equal([
{
from: 0,
to: 15,
title: 'title',
line: 1,
level: SECTION_LEVEL,
},
])
})
})
})
describe('for moving section', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = ['\\section{title}', '\\subsection{subtitle}']
view = makeView(content)
const initialOutline = getOutline(view)
expect(initialOutline).to.deep.equal([
{
from: 0,
to: 15,
title: 'title',
line: 1,
level: SECTION_LEVEL,
},
{
from: 16,
to: 37,
title: 'subtitle',
line: 2,
level: SUB_SECTION_LEVEL,
},
])
insertText(view, 15, '\n')
})
it('should update position for moved section', function () {
const updatedOutline = getOutline(view)
expect(updatedOutline).to.deep.equal([
{
from: 0,
to: 15,
title: 'title',
line: 1,
level: SECTION_LEVEL,
},
{
from: 17,
to: 38,
title: 'subtitle',
line: 3,
level: SUB_SECTION_LEVEL,
},
])
})
})
describe('for removing a section', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = ['\\section{title}']
view = makeView(content)
const initialOutline = getOutline(view)
expect(initialOutline).to.deep.equal([
{
from: 0,
to: 15,
title: 'title',
line: 1,
level: SECTION_LEVEL,
},
])
deleteText(view, 4, 1)
})
it('should remove the section from the outline', function () {
const updatedOutline = getOutline(view)
expect(updatedOutline).to.be.empty
})
})
describe('for changing parent section', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = [
'\\section{section}',
'%\\subsection{subsection}', // initially commented out
'\\subsubsection{subsubsection}',
]
view = makeView(content)
const initialOutline = getOutline(view)
expect(initialOutline).to.deep.equal([
{
from: 0,
to: 17,
title: 'section',
line: 1,
level: SECTION_LEVEL,
},
{
from: 43,
to: 72,
title: 'subsubsection',
line: 3,
level: SUB_SUB_SECTION_LEVEL,
},
])
// Remove the %
deleteText(view, 19, 1)
})
it('should be nested properly', function () {
const updatedOutline = getOutline(view)
expect(updatedOutline).to.deep.equal([
{
from: 0,
to: 17,
title: 'section',
line: 1,
level: SECTION_LEVEL,
},
{
from: 18,
to: 41,
title: 'subsection',
line: 2,
level: SUB_SECTION_LEVEL,
},
{
from: 42,
to: 71,
title: 'subsubsection',
line: 3,
level: SUB_SUB_SECTION_LEVEL,
},
])
})
})
describe('for a sectioning command inside a newcommand or renewcommand', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = [
'\\section{section}',
'\\newcommand{\\test}{\\section{should not display}}',
'\\renewcommand{\\test}{\\section{should still not display}}',
]
view = makeView(content)
})
it('should not include them in the outline', function () {
const outline = getOutline(view)
expect(outline?.length).to.equal(1)
expect(outline).to.deep.equal([
{
from: 0,
to: 17,
title: 'section',
line: 1,
level: SECTION_LEVEL,
},
])
})
})
describe('for all section types', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = [
'\\book{book}',
'\\part{part}',
'\\chapter{chapter}',
'\\section{section}',
'\\subsection{subsection}',
'\\subsubsection{subsubsection}',
'\\paragraph{paragraph}',
'\\subparagraph{subparagraph}',
]
view = makeView(content)
})
it('should include them in the file outline', function () {
const outline = getOutline(view)
expect(outline).to.deep.equal([
{
from: 0,
to: 11,
title: 'book',
line: 1,
level: BOOK_LEVEL,
},
{
from: 12,
to: 23,
title: 'part',
line: 2,
level: PART_LEVEL,
},
{
from: 24,
to: 41,
title: 'chapter',
line: 3,
level: CHAPTER_LEVEL,
},
{
from: 42,
to: 59,
title: 'section',
line: 4,
level: SECTION_LEVEL,
},
{
from: 60,
to: 83,
title: 'subsection',
line: 5,
level: SUB_SECTION_LEVEL,
},
{
from: 84,
to: 113,
title: 'subsubsection',
line: 6,
level: SUB_SUB_SECTION_LEVEL,
},
{
from: 114,
to: 135,
title: 'paragraph',
line: 7,
level: PARAGRAPH_LEVEL,
},
{
from: 136,
to: 163,
title: 'subparagraph',
line: 8,
level: SUB_PARAGRAPH_LEVEL,
},
])
})
})
describe('sectioning commands with optional arguments', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = ['\\section[short title]{section}']
view = makeView(content)
})
it('should use the long argument as title', function () {
const outline = getOutline(view)
expect(outline).to.deep.equal([
{
from: 0,
to: 30,
title: 'section',
line: 1,
level: SECTION_LEVEL,
},
])
})
})
describe('for labels using texorpdfstring', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = [
'\\section{The \\texorpdfstring{function $f(x) = x^2$}{function f(x) = x^2}: Properties of \\texorpdfstring{$x$}{x}.}',
]
view = makeView(content)
})
it('should use the text argument as title', function () {
const outline = getOutline(view)
expect(outline).to.deep.equal([
{
from: 0,
to: 113,
title: 'The function f(x) = x^2: Properties of x.',
line: 1,
level: SECTION_LEVEL,
},
])
})
})
describe('for ill-formed \\def command', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = ['\\def\\x{', '\\section{test}', '\\subsection{test2}']
view = makeView(content)
})
it('still shows an outline', function () {
const outline = getOutline(view)
expect(outline).to.deep.equal([
{
from: 8,
to: 22,
title: 'test',
line: 2,
level: SECTION_LEVEL,
},
{
from: 23,
to: 41,
title: 'test2',
line: 3,
level: SUB_SECTION_LEVEL,
},
])
})
})
describe('for beamer frames', function () {
describe('with titles', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = ['\\begin{frame}{frame title}{}', '\\end{frame}']
view = makeView(content)
})
it('should show up in the file outline', function () {
const outline = getOutline(view)
expect(outline).to.deep.equal([
{
from: 0,
to: 28,
title: 'frame title',
line: 1,
level: FRAME_LEVEL,
},
])
})
})
describe('without titles', function () {
let view: EditorView, content: string[]
beforeEach(function () {
content = ['\\begin{frame}', '\\end{frame}']
view = makeView(content)
})
it('should not show up in the file outline', function () {
const outline = getOutline(view)
expect(outline).to.be.empty
})
})
})
})