Просмотр исходного кода

Merge pull request #14245 from overleaf/mj-linebreak-arguments

[lezer+visual] Allow optional arguments for line breaks

GitOrigin-RevId: e59a7c9a2efacc8a0fde887736899bbcbb52a4bc
Mathias Jakobsen 3 лет назад
Родитель
Сommit
56438c32cc

+ 4 - 3
services/web/frontend/js/features/source-editor/components/table-generator/utils.ts

@@ -66,8 +66,9 @@ function parseColumnSpecifications(specification: string): ColumnDefinition[] {
   return columns
 }
 
-const isRowSeparator = (node: SyntaxNode, state: EditorState) =>
-  node.type.is('Command') && state.sliceDoc(node.from, node.to) === '\\\\'
+const isRowSeparator = (node: SyntaxNode) =>
+  node.type.is('Command') &&
+  Boolean(node.getChild('KnownCtrlSym')?.getChild('LineBreak'))
 
 const isHLine = (node: SyntaxNode) =>
   node.type.is('Command') &&
@@ -133,7 +134,7 @@ function parseTabularBody(
     currentChild;
     currentChild = currentChild.nextSibling
   ) {
-    if (isRowSeparator(currentChild, state)) {
+    if (isRowSeparator(currentChild)) {
       const lastRow = getLastRow()
       body.rows.push({
         cells: [],

+ 10 - 6
services/web/frontend/js/features/source-editor/extensions/visual/atomic-decorations.ts

@@ -794,13 +794,17 @@ export const atomicDecorations = (options: Options) => {
               }).range(nodeRef.from, nodeRef.to)
             )
           }
-        } else if (nodeRef.type.is('LineBreakCtrlSym')) {
+        } else if (nodeRef.type.is('LineBreak')) {
           // line break
-          decorations.push(
-            Decoration.replace({
-              widget: new IndicatorWidget('\u21A9'),
-            }).range(nodeRef.from, nodeRef.to)
-          )
+          const optionalArgument = nodeRef.node.getChild('OptionalArgument')
+          if (!optionalArgument || shouldDecorate(state, optionalArgument)) {
+            decorations.push(
+              Decoration.replace({
+                widget: new IndicatorWidget('\u21A9'),
+              }).range(nodeRef.from, nodeRef.to)
+            )
+          }
+          return false
         } else if (nodeRef.type.is('Caption')) {
           if (shouldDecorate(state, nodeRef)) {
             // a caption

+ 1 - 2
services/web/frontend/js/features/source-editor/extensions/visual/utils/typeset-content.ts

@@ -1,6 +1,5 @@
 import { EditorState } from '@codemirror/state'
 import { SyntaxNode } from '@lezer/common'
-import { LineBreakCtrlSym } from '../../../lezer-latex/latex.terms.mjs'
 
 const isUnknownCommandWithName = (
   node: SyntaxNode,
@@ -92,7 +91,7 @@ export function typesetNodeIntoElement(
         // ignoring these commands
         from = childNode.to
         return false
-      } else if (childNode.type.is(LineBreakCtrlSym)) {
+      } else if (childNode.type.is('LineBreak')) {
         ancestor().appendChild(document.createElement('br'))
         from = childNode.to
       }

+ 3 - 1
services/web/frontend/js/features/source-editor/lezer-latex/latex.grammar

@@ -360,7 +360,9 @@ Command {
 }
 
 KnownCtrlSym {
-  LineBreakCtrlSym
+    LineBreak {
+        LineBreakCtrlSym OptionalArgument?
+    }
 }
 
 textBase {