environments.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import { ensureSyntaxTree } from '@codemirror/language'
  2. import { EditorState } from '@codemirror/state'
  3. import { SyntaxNode, SyntaxNodeRef } from '@lezer/common'
  4. import { previousSiblingIs } from './common'
  5. import { NodeIntersectsChangeFn, ProjectionItem } from './projection'
  6. import { FigureData } from '../../extensions/figure-modal'
  7. const HUNDRED_MS = 100
  8. export class EnvironmentName extends ProjectionItem {
  9. readonly title: string = ''
  10. }
  11. export const enterNode = (
  12. state: EditorState,
  13. node: SyntaxNodeRef,
  14. items: EnvironmentName[],
  15. nodeIntersectsChange: NodeIntersectsChangeFn
  16. ): any => {
  17. if (node.type.is('EnvNameGroup')) {
  18. if (!nodeIntersectsChange(node.node)) {
  19. return false
  20. }
  21. if (!node.node.prevSibling?.type.is('Begin')) {
  22. return false
  23. }
  24. const openBraceNode = node.node.getChild('OpenBrace')
  25. if (!openBraceNode) {
  26. return false
  27. }
  28. const envNameNode = openBraceNode.node.nextSibling
  29. if (!envNameNode) {
  30. return false
  31. }
  32. const envNameText = state.doc.sliceString(envNameNode.from, envNameNode.to)
  33. if (envNameText.length < 1) {
  34. return false
  35. }
  36. const thisEnvironmentName = {
  37. title: envNameText,
  38. from: envNameNode.from,
  39. to: envNameNode.to,
  40. line: state.doc.lineAt(envNameNode.from).number,
  41. }
  42. items.push(thisEnvironmentName)
  43. } else if (
  44. node.type.is('NewEnvironment') ||
  45. node.type.is('RenewEnvironment')
  46. ) {
  47. if (!nodeIntersectsChange(node.node)) {
  48. // This should already be in `items`
  49. return false
  50. }
  51. const envNameNode = node.node.getChild('LiteralArgContent')
  52. if (!envNameNode) {
  53. return
  54. }
  55. const envNameText = state.doc.sliceString(envNameNode.from, envNameNode.to)
  56. if (!envNameText) {
  57. return
  58. }
  59. const thisEnvironmentName = {
  60. title: envNameText,
  61. from: envNameNode.from,
  62. to: envNameNode.to,
  63. line: state.doc.lineAt(envNameNode.from).number,
  64. }
  65. items.push(thisEnvironmentName)
  66. }
  67. }
  68. export const cursorIsAtBeginEnvironment = (
  69. state: EditorState,
  70. pos: number
  71. ): boolean | undefined => {
  72. const tree = ensureSyntaxTree(state, pos, HUNDRED_MS)
  73. if (!tree) {
  74. return
  75. }
  76. let thisNode = tree.resolve(pos)
  77. if (!thisNode) {
  78. return
  79. }
  80. if (
  81. thisNode.type.is('EnvNameGroup') &&
  82. previousSiblingIs(state, pos, 'Begin')
  83. ) {
  84. return true
  85. } else if (
  86. thisNode.type.is('$Environment') ||
  87. (thisNode.type.is('LaTeX') && pos === state.doc.length) // We're at the end of the document
  88. ) {
  89. // We're at a malformed `\begin{`, resolve leftward
  90. thisNode = tree.resolve(pos, -1)
  91. if (!thisNode) {
  92. return
  93. }
  94. // TODO: may need to handle various envnames
  95. if (thisNode.type.is('OpenBrace') || thisNode.type.is('$EnvName')) {
  96. return true
  97. }
  98. }
  99. }
  100. export const cursorIsAtEndEnvironment = (
  101. state: EditorState,
  102. pos: number
  103. ): boolean | undefined => {
  104. const tree = ensureSyntaxTree(state, pos, HUNDRED_MS)
  105. if (!tree) {
  106. return
  107. }
  108. let thisNode = tree.resolve(pos)
  109. if (!thisNode) {
  110. return
  111. }
  112. if (
  113. thisNode.type.is('EnvNameGroup') &&
  114. previousSiblingIs(state, pos, 'End')
  115. ) {
  116. return true
  117. } else if (thisNode.type.is('$Environment') || thisNode.type.is('Content')) {
  118. // We're at a malformed `\end{`, resolve leftward
  119. thisNode = tree.resolve(pos, -1)
  120. if (!thisNode) {
  121. return
  122. }
  123. // TODO: may need to handle various envnames
  124. if (thisNode.type.is('OpenBrace') || thisNode.type.is('EnvName')) {
  125. return true
  126. }
  127. }
  128. }
  129. /**
  130. *
  131. * @param node A node of type `$Environment`, `BeginEnv`, or `EndEnv`
  132. * @param state The editor state to read the name from
  133. * @returns The editor name or null if a name cannot be found
  134. */
  135. export function getEnvironmentName(
  136. node: SyntaxNode | null,
  137. state: EditorState
  138. ): string | null {
  139. if (node?.type.is('$Environment')) {
  140. node = node.getChild('BeginEnv')
  141. }
  142. if (!node?.type.is('BeginEnv') && !node?.type.is('EndEnv')) {
  143. return null
  144. }
  145. const nameNode = node
  146. ?.getChild('EnvNameGroup')
  147. ?.getChild('OpenBrace')?.nextSibling
  148. if (!nameNode) {
  149. return null
  150. }
  151. // the name node is a parameter in the grammar, so we have no good way to
  152. // target the specific type
  153. if (nameNode.type.is('CloseBrace')) {
  154. return null
  155. }
  156. return state.sliceDoc(nameNode.from, nameNode.to)
  157. }
  158. export const getUnstarredEnvironmentName = (
  159. node: SyntaxNode | null,
  160. state: EditorState
  161. ): string | undefined => getEnvironmentName(node, state)?.replace(/\*$/, '')
  162. export function getEnvironmentArguments(environmentNode: SyntaxNode) {
  163. return environmentNode.getChild('BeginEnv')?.getChildren('TextArgument')
  164. }
  165. export function parseFigureData(
  166. figureEnvironmentNode: SyntaxNode,
  167. state: EditorState
  168. ): FigureData | null {
  169. let caption: FigureData['caption'] = null
  170. let label: FigureData['label'] = null
  171. let file: FigureData['file'] | undefined
  172. let width: FigureData['width']
  173. let unknownGraphicsArguments: FigureData['unknownGraphicsArguments']
  174. let graphicsCommand: FigureData['graphicsCommand'] | undefined
  175. let graphicsCommandArguments: FigureData['graphicsCommandArguments'] = null
  176. const from = figureEnvironmentNode.from
  177. const to = figureEnvironmentNode.to
  178. let error = false
  179. figureEnvironmentNode.cursor().iterate((node: SyntaxNodeRef) => {
  180. if (error) {
  181. return false
  182. }
  183. if (node.type.is('Caption')) {
  184. if (caption) {
  185. // Multiple captions
  186. error = true
  187. return false
  188. }
  189. caption = {
  190. from: node.from,
  191. to: node.to,
  192. }
  193. }
  194. if (node.type.is('Label')) {
  195. if (label) {
  196. // Multiple labels
  197. error = true
  198. return false
  199. }
  200. label = {
  201. from: node.from,
  202. to: node.to,
  203. }
  204. }
  205. if (node.type.is('IncludeGraphics')) {
  206. if (file) {
  207. // Multiple figure
  208. error = true
  209. return false
  210. }
  211. graphicsCommand = {
  212. from: node.from,
  213. to: node.to,
  214. }
  215. const content = node.node
  216. .getChild('IncludeGraphicsArgument')
  217. ?.getChild('FilePathArgument')
  218. ?.getChild('LiteralArgContent')
  219. if (!content) {
  220. error = true
  221. return false
  222. }
  223. file = {
  224. from: content.from,
  225. to: content.to,
  226. path: state.sliceDoc(content.from, content.to),
  227. }
  228. const optionalArgs = node.node
  229. .getChild('OptionalArgument')
  230. ?.getChild('ShortOptionalArg')
  231. if (!optionalArgs) {
  232. width = undefined
  233. return false
  234. }
  235. graphicsCommandArguments = {
  236. from: optionalArgs.from,
  237. to: optionalArgs.to,
  238. }
  239. const optionalArgContent = state.sliceDoc(
  240. optionalArgs.from,
  241. optionalArgs.to
  242. )
  243. const widthMatch = optionalArgContent.match(
  244. /^width=([0-9]|(?:[0-9]*\.[0-9]+)|(?:[0-9]+\.))\\(linewidth|pagewidth|textwidth|hsize|columnwidth)$/
  245. )
  246. if (widthMatch) {
  247. width = parseFloat(widthMatch[1])
  248. if (widthMatch[2] !== 'linewidth') {
  249. // We shouldn't edit any width other that linewidth
  250. unknownGraphicsArguments = optionalArgContent
  251. }
  252. } else {
  253. unknownGraphicsArguments = optionalArgContent
  254. }
  255. }
  256. })
  257. if (error) {
  258. return null
  259. }
  260. if (graphicsCommand === undefined || file === undefined) {
  261. return null
  262. }
  263. return new FigureData({
  264. caption,
  265. label,
  266. file,
  267. from,
  268. to,
  269. width,
  270. unknownGraphicsArguments,
  271. graphicsCommand,
  272. graphicsCommandArguments,
  273. })
  274. }
  275. export const getBeginEnvSuffix = (state: EditorState, node: SyntaxNode) => {
  276. const argumentNode = node
  277. .getChild('OptionalArgument')
  278. ?.getChild('ShortOptionalArg')
  279. return argumentNode && state.sliceDoc(argumentNode.from, argumentNode.to)
  280. }