latex-outline.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. import { LanguageSupport } from '@codemirror/language'
  2. import { EditorState, Text } from '@codemirror/state'
  3. import { EditorView } from '@codemirror/view'
  4. import { expect } from 'chai'
  5. import { documentOutline } from '../../../../../../frontend/js/features/source-editor/languages/latex/document-outline'
  6. import {
  7. FlatOutline,
  8. getNestingLevel,
  9. } from '../../../../../../frontend/js/features/source-editor/utils/tree-query'
  10. import { LaTeXLanguage } from '../../../../../../frontend/js/features/source-editor/languages/latex/latex-language'
  11. import {
  12. Book,
  13. Chapter,
  14. Paragraph,
  15. Part,
  16. Section,
  17. SubParagraph,
  18. SubSection,
  19. SubSubSection,
  20. } from '../../../../../../frontend/js/features/source-editor/lezer-latex/latex.terms.mjs'
  21. const latex = new LanguageSupport(LaTeXLanguage, documentOutline.extension)
  22. const makeView = (lines: string[]): EditorView => {
  23. const text = Text.of(lines)
  24. const view = new EditorView({
  25. state: EditorState.create({
  26. doc: text,
  27. extensions: [latex],
  28. }),
  29. })
  30. return view
  31. }
  32. const BOOK_LEVEL = getNestingLevel(Book)
  33. const PART_LEVEL = getNestingLevel(Part)
  34. const CHAPTER_LEVEL = getNestingLevel(Chapter)
  35. const SECTION_LEVEL = getNestingLevel(Section)
  36. const SUB_SECTION_LEVEL = getNestingLevel(SubSection)
  37. const SUB_SUB_SECTION_LEVEL = getNestingLevel(SubSubSection)
  38. const PARAGRAPH_LEVEL = getNestingLevel(Paragraph)
  39. const SUB_PARAGRAPH_LEVEL = getNestingLevel(SubParagraph)
  40. const FRAME_LEVEL = getNestingLevel('frame')
  41. const insertText = (view: EditorView, position: number, text: string) => {
  42. view.dispatch({
  43. changes: [{ from: position, insert: text }],
  44. })
  45. }
  46. const deleteText = (view: EditorView, position: number, length: number) => {
  47. view.dispatch({
  48. changes: [{ from: position - length, to: position }],
  49. })
  50. }
  51. const getOutline = (view: EditorView): FlatOutline | null => {
  52. return view.state.field(documentOutline)?.items || null
  53. }
  54. describe('CodeMirror LaTeX-FileOutline', function () {
  55. describe('with no update', function () {
  56. describe('an empty document', function () {
  57. let view: EditorView, content: string[]
  58. beforeEach(function () {
  59. content = ['']
  60. view = makeView(content)
  61. })
  62. it('should have empty outline', function () {
  63. const outline = getOutline(view)
  64. expect(outline).to.be.empty
  65. })
  66. })
  67. describe('a document with nested sections', function () {
  68. let view: EditorView, content: string[]
  69. beforeEach(function () {
  70. content = [
  71. 'line 1',
  72. '\\section{sec title}',
  73. 'content',
  74. '\\subsection{subsec title}',
  75. ]
  76. view = makeView(content)
  77. })
  78. it('should have outline with different levels', function () {
  79. const outline = getOutline(view)
  80. expect(outline).to.be.deep.equal([
  81. {
  82. from: 7,
  83. to: 26,
  84. level: SECTION_LEVEL,
  85. title: 'sec title',
  86. line: 2,
  87. },
  88. {
  89. from: 35,
  90. to: 60,
  91. level: SUB_SECTION_LEVEL,
  92. title: 'subsec title',
  93. line: 4,
  94. },
  95. ])
  96. })
  97. })
  98. describe('a document with sibling sections', function () {
  99. let view: EditorView, content: string[]
  100. beforeEach(function () {
  101. content = [
  102. 'line 1',
  103. '\\section{sec title 1}',
  104. 'content',
  105. '\\section{sec title 2}',
  106. ]
  107. view = makeView(content)
  108. })
  109. it('should have outline with same levels for siblings', function () {
  110. const outline = getOutline(view)
  111. expect(outline).to.be.deep.equal([
  112. {
  113. from: 7,
  114. to: 28,
  115. level: SECTION_LEVEL,
  116. title: 'sec title 1',
  117. line: 2,
  118. },
  119. {
  120. from: 37,
  121. to: 58,
  122. level: SECTION_LEVEL,
  123. title: 'sec title 2',
  124. line: 4,
  125. },
  126. ])
  127. })
  128. })
  129. })
  130. describe('with change to title', function () {
  131. let view: EditorView, content: string[]
  132. beforeEach(function () {
  133. content = ['\\section{title }']
  134. view = makeView(content)
  135. const initialOutline = getOutline(view)
  136. expect(initialOutline).to.deep.equal([
  137. {
  138. from: 0,
  139. to: 16,
  140. title: 'title ',
  141. line: 1,
  142. level: SECTION_LEVEL,
  143. },
  144. ])
  145. })
  146. describe('for appending to title', function () {
  147. beforeEach(function () {
  148. insertText(view, 15, '1')
  149. })
  150. it('should update title in outline', function () {
  151. const updatedOutline = getOutline(view)
  152. expect(updatedOutline).to.deep.equal([
  153. {
  154. from: 0,
  155. to: 17,
  156. title: 'title 1',
  157. line: 1,
  158. level: SECTION_LEVEL,
  159. },
  160. ])
  161. })
  162. })
  163. describe('for removing from title', function () {
  164. beforeEach(function () {
  165. deleteText(view, 15, 1)
  166. })
  167. it('should update title in outline', function () {
  168. const updatedOutline = getOutline(view)
  169. expect(updatedOutline).to.deep.equal([
  170. {
  171. from: 0,
  172. to: 15,
  173. title: 'title',
  174. line: 1,
  175. level: SECTION_LEVEL,
  176. },
  177. ])
  178. })
  179. })
  180. })
  181. describe('for moving section', function () {
  182. let view: EditorView, content: string[]
  183. beforeEach(function () {
  184. content = ['\\section{title}', '\\subsection{subtitle}']
  185. view = makeView(content)
  186. const initialOutline = getOutline(view)
  187. expect(initialOutline).to.deep.equal([
  188. {
  189. from: 0,
  190. to: 15,
  191. title: 'title',
  192. line: 1,
  193. level: SECTION_LEVEL,
  194. },
  195. {
  196. from: 16,
  197. to: 37,
  198. title: 'subtitle',
  199. line: 2,
  200. level: SUB_SECTION_LEVEL,
  201. },
  202. ])
  203. insertText(view, 15, '\n')
  204. })
  205. it('should update position for moved section', function () {
  206. const updatedOutline = getOutline(view)
  207. expect(updatedOutline).to.deep.equal([
  208. {
  209. from: 0,
  210. to: 15,
  211. title: 'title',
  212. line: 1,
  213. level: SECTION_LEVEL,
  214. },
  215. {
  216. from: 17,
  217. to: 38,
  218. title: 'subtitle',
  219. line: 3,
  220. level: SUB_SECTION_LEVEL,
  221. },
  222. ])
  223. })
  224. })
  225. describe('for removing a section', function () {
  226. let view: EditorView, content: string[]
  227. beforeEach(function () {
  228. content = ['\\section{title}']
  229. view = makeView(content)
  230. const initialOutline = getOutline(view)
  231. expect(initialOutline).to.deep.equal([
  232. {
  233. from: 0,
  234. to: 15,
  235. title: 'title',
  236. line: 1,
  237. level: SECTION_LEVEL,
  238. },
  239. ])
  240. deleteText(view, 4, 1)
  241. })
  242. it('should remove the section from the outline', function () {
  243. const updatedOutline = getOutline(view)
  244. expect(updatedOutline).to.be.empty
  245. })
  246. })
  247. describe('for changing parent section', function () {
  248. let view: EditorView, content: string[]
  249. beforeEach(function () {
  250. content = [
  251. '\\section{section}',
  252. '%\\subsection{subsection}', // initially commented out
  253. '\\subsubsection{subsubsection}',
  254. ]
  255. view = makeView(content)
  256. const initialOutline = getOutline(view)
  257. expect(initialOutline).to.deep.equal([
  258. {
  259. from: 0,
  260. to: 17,
  261. title: 'section',
  262. line: 1,
  263. level: SECTION_LEVEL,
  264. },
  265. {
  266. from: 43,
  267. to: 72,
  268. title: 'subsubsection',
  269. line: 3,
  270. level: SUB_SUB_SECTION_LEVEL,
  271. },
  272. ])
  273. // Remove the %
  274. deleteText(view, 19, 1)
  275. })
  276. it('should be nested properly', function () {
  277. const updatedOutline = getOutline(view)
  278. expect(updatedOutline).to.deep.equal([
  279. {
  280. from: 0,
  281. to: 17,
  282. title: 'section',
  283. line: 1,
  284. level: SECTION_LEVEL,
  285. },
  286. {
  287. from: 18,
  288. to: 41,
  289. title: 'subsection',
  290. line: 2,
  291. level: SUB_SECTION_LEVEL,
  292. },
  293. {
  294. from: 42,
  295. to: 71,
  296. title: 'subsubsection',
  297. line: 3,
  298. level: SUB_SUB_SECTION_LEVEL,
  299. },
  300. ])
  301. })
  302. })
  303. describe('for a sectioning command inside a newcommand or renewcommand', function () {
  304. let view: EditorView, content: string[]
  305. beforeEach(function () {
  306. content = [
  307. '\\section{section}',
  308. '\\newcommand{\\test}{\\section{should not display}}',
  309. '\\renewcommand{\\test}{\\section{should still not display}}',
  310. ]
  311. view = makeView(content)
  312. })
  313. it('should not include them in the outline', function () {
  314. const outline = getOutline(view)
  315. expect(outline?.length).to.equal(1)
  316. expect(outline).to.deep.equal([
  317. {
  318. from: 0,
  319. to: 17,
  320. title: 'section',
  321. line: 1,
  322. level: SECTION_LEVEL,
  323. },
  324. ])
  325. })
  326. })
  327. describe('for all section types', function () {
  328. let view: EditorView, content: string[]
  329. beforeEach(function () {
  330. content = [
  331. '\\book{book}',
  332. '\\part{part}',
  333. '\\chapter{chapter}',
  334. '\\section{section}',
  335. '\\subsection{subsection}',
  336. '\\subsubsection{subsubsection}',
  337. '\\paragraph{paragraph}',
  338. '\\subparagraph{subparagraph}',
  339. ]
  340. view = makeView(content)
  341. })
  342. it('should include them in the file outline', function () {
  343. const outline = getOutline(view)
  344. expect(outline).to.deep.equal([
  345. {
  346. from: 0,
  347. to: 11,
  348. title: 'book',
  349. line: 1,
  350. level: BOOK_LEVEL,
  351. },
  352. {
  353. from: 12,
  354. to: 23,
  355. title: 'part',
  356. line: 2,
  357. level: PART_LEVEL,
  358. },
  359. {
  360. from: 24,
  361. to: 41,
  362. title: 'chapter',
  363. line: 3,
  364. level: CHAPTER_LEVEL,
  365. },
  366. {
  367. from: 42,
  368. to: 59,
  369. title: 'section',
  370. line: 4,
  371. level: SECTION_LEVEL,
  372. },
  373. {
  374. from: 60,
  375. to: 83,
  376. title: 'subsection',
  377. line: 5,
  378. level: SUB_SECTION_LEVEL,
  379. },
  380. {
  381. from: 84,
  382. to: 113,
  383. title: 'subsubsection',
  384. line: 6,
  385. level: SUB_SUB_SECTION_LEVEL,
  386. },
  387. {
  388. from: 114,
  389. to: 135,
  390. title: 'paragraph',
  391. line: 7,
  392. level: PARAGRAPH_LEVEL,
  393. },
  394. {
  395. from: 136,
  396. to: 163,
  397. title: 'subparagraph',
  398. line: 8,
  399. level: SUB_PARAGRAPH_LEVEL,
  400. },
  401. ])
  402. })
  403. })
  404. describe('sectioning commands with optional arguments', function () {
  405. let view: EditorView, content: string[]
  406. beforeEach(function () {
  407. content = ['\\section[short title]{section}']
  408. view = makeView(content)
  409. })
  410. it('should use the long argument as title', function () {
  411. const outline = getOutline(view)
  412. expect(outline).to.deep.equal([
  413. {
  414. from: 0,
  415. to: 30,
  416. title: 'section',
  417. line: 1,
  418. level: SECTION_LEVEL,
  419. },
  420. ])
  421. })
  422. })
  423. describe('for labels using texorpdfstring', function () {
  424. let view: EditorView, content: string[]
  425. beforeEach(function () {
  426. content = [
  427. '\\section{The \\texorpdfstring{function $f(x) = x^2$}{function f(x) = x^2}: Properties of \\texorpdfstring{$x$}{x}.}',
  428. ]
  429. view = makeView(content)
  430. })
  431. it('should use the text argument as title', function () {
  432. const outline = getOutline(view)
  433. expect(outline).to.deep.equal([
  434. {
  435. from: 0,
  436. to: 113,
  437. title: 'The function f(x) = x^2: Properties of x.',
  438. line: 1,
  439. level: SECTION_LEVEL,
  440. },
  441. ])
  442. })
  443. })
  444. describe('for ill-formed \\def command', function () {
  445. let view: EditorView, content: string[]
  446. beforeEach(function () {
  447. content = ['\\def\\x{', '\\section{test}', '\\subsection{test2}']
  448. view = makeView(content)
  449. })
  450. it('still shows an outline', function () {
  451. const outline = getOutline(view)
  452. expect(outline).to.deep.equal([
  453. {
  454. from: 8,
  455. to: 22,
  456. title: 'test',
  457. line: 2,
  458. level: SECTION_LEVEL,
  459. },
  460. {
  461. from: 23,
  462. to: 41,
  463. title: 'test2',
  464. line: 3,
  465. level: SUB_SECTION_LEVEL,
  466. },
  467. ])
  468. })
  469. })
  470. describe('for beamer frames', function () {
  471. describe('with titles', function () {
  472. let view: EditorView, content: string[]
  473. beforeEach(function () {
  474. content = ['\\begin{frame}{frame title}{}', '\\end{frame}']
  475. view = makeView(content)
  476. })
  477. it('should show up in the file outline', function () {
  478. const outline = getOutline(view)
  479. expect(outline).to.deep.equal([
  480. {
  481. from: 0,
  482. to: 28,
  483. title: 'frame title',
  484. line: 1,
  485. level: FRAME_LEVEL,
  486. },
  487. ])
  488. })
  489. })
  490. describe('without titles', function () {
  491. let view: EditorView, content: string[]
  492. beforeEach(function () {
  493. content = ['\\begin{frame}', '\\end{frame}']
  494. view = makeView(content)
  495. })
  496. it('should not show up in the file outline', function () {
  497. const outline = getOutline(view)
  498. expect(outline).to.be.empty
  499. })
  500. })
  501. })
  502. })