From 52e813985dd14a464857056bcbd05f70d063a15a Mon Sep 17 00:00:00 2001 From: Mathias Jakobsen Date: Mon, 14 Aug 2023 09:16:27 +0100 Subject: [PATCH] Merge pull request #14245 from overleaf/mj-linebreak-arguments [lezer+visual] Allow optional arguments for line breaks GitOrigin-RevId: e59a7c9a2efacc8a0fde887736899bbcbb52a4bc --- .../components/table-generator/utils.ts | 7 ++++--- .../extensions/visual/atomic-decorations.ts | 16 ++++++++++------ .../extensions/visual/utils/typeset-content.ts | 3 +-- .../source-editor/lezer-latex/latex.grammar | 4 +++- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/services/web/frontend/js/features/source-editor/components/table-generator/utils.ts b/services/web/frontend/js/features/source-editor/components/table-generator/utils.ts index 4ed3f87101..167e0aef02 100644 --- a/services/web/frontend/js/features/source-editor/components/table-generator/utils.ts +++ b/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: [], diff --git a/services/web/frontend/js/features/source-editor/extensions/visual/atomic-decorations.ts b/services/web/frontend/js/features/source-editor/extensions/visual/atomic-decorations.ts index 5f7c704763..a2bbd6d415 100644 --- a/services/web/frontend/js/features/source-editor/extensions/visual/atomic-decorations.ts +++ b/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 diff --git a/services/web/frontend/js/features/source-editor/extensions/visual/utils/typeset-content.ts b/services/web/frontend/js/features/source-editor/extensions/visual/utils/typeset-content.ts index 6e6a826e62..aed41c838e 100644 --- a/services/web/frontend/js/features/source-editor/extensions/visual/utils/typeset-content.ts +++ b/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 } diff --git a/services/web/frontend/js/features/source-editor/lezer-latex/latex.grammar b/services/web/frontend/js/features/source-editor/lezer-latex/latex.grammar index 44294fbd79..4eeaf90820 100644 --- a/services/web/frontend/js/features/source-editor/lezer-latex/latex.grammar +++ b/services/web/frontend/js/features/source-editor/lezer-latex/latex.grammar @@ -360,7 +360,9 @@ Command { } KnownCtrlSym { - LineBreakCtrlSym + LineBreak { + LineBreakCtrlSym OptionalArgument? + } } textBase {