From 46a160f127db16e109c5896c35d0f682e96d64fd Mon Sep 17 00:00:00 2001 From: Mathias Jakobsen Date: Tue, 25 Jun 2024 09:07:47 +0100 Subject: [PATCH] Merge pull request #19072 from overleaf/mj-known-commands-reuse [web] Parse known commands in math mode GitOrigin-RevId: e7937fc46320f13c716d1612eca36aa67c8fb967 --- .../source-editor/lezer-latex/latex.grammar | 43 ++++++++----------- .../utils/tree-operations/commands.ts | 9 ++-- 2 files changed, 21 insertions(+), 31 deletions(-) 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 849c3f4439..b2853905ed 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 @@ -219,7 +219,7 @@ OptionalMacroParameter { // The autocompletion code in services/web/frontend/js/features/source-editor/utils/tree-operations/commands.ts // depends on following the `KnownCommand { Command { CommandCtrlSeq [args] } }` // structure -KnownCommand { +KnownCommand { Title { TitleCtrlSeq optionalWhitespace? OptionalArgument? TextArgument } | @@ -249,10 +249,10 @@ KnownCommand { PackageArgument } | TextColorCommand { - TextColorCtrlSeq optionalWhitespace? ShortTextArgument optionalWhitespace? TextArgument + TextColorCtrlSeq optionalWhitespace? ShortTextArgument optionalWhitespace? ArgumentType } | ColorBoxCommand { - ColorBoxCtrlSeq optionalWhitespace? ShortTextArgument optionalWhitespace? TextArgument + ColorBoxCtrlSeq optionalWhitespace? ShortTextArgument optionalWhitespace? ArgumentType } | HrefCommand { HrefCtrlSeq optionalWhitespace? UrlArgument ShortTextArgument @@ -295,12 +295,6 @@ KnownCommand { Hbox { HboxCtrlSeq optionalWhitespace? TextArgument } | - Left { - LeftCtrlSeq optionalWhitespace? - } | - Right { - RightCtrlSeq optionalWhitespace? - } | NewCommand { NewCommandCtrlSeq optionalWhitespace? (Csname | OpenBrace LiteralArgContent CloseBrace) @@ -350,20 +344,27 @@ KnownCommand { optionalWhitespace? SpanArgument { ShortTextArgument } optionalWhitespace? ColumnArgument { ShortTextArgument } optionalWhitespace? TabularArgument + } | + MathTextCommand { + MathTextCtrlSeq optionalWhitespace? "*"? TextArgument } } UnknownCommand { (CtrlSeq !argument Whitespace (OptionalArgument | TextArgument)+) - | ((CtrlSeq | MathTextCtrlSeq) (OptionalArgument | TextArgument)+) + | (CtrlSeq (OptionalArgument | TextArgument)+) | CtrlSeq Whitespace? | CtrlSym } Command { - KnownCommand + KnownCommand | UnknownCommand | KnownCtrlSym + // Not technically allowed in normal mode, but not worth failing the parse over + | LeftCtrlSeq + | RightCtrlSeq + } KnownCtrlSym { @@ -480,7 +481,7 @@ TikzPictureContent { /// same as Text but with added allowed characters } DefinitionFragment { - ( KnownCommand + ( KnownCommand | CtrlSeq optionalWhitespace? | CtrlSym | Begin @@ -491,6 +492,8 @@ DefinitionFragment { | CloseParenCtrlSym | OpenBracketCtrlSym | CloseBracketCtrlSym + | LeftCtrlSeq + | RightCtrlSeq | KnownCtrlSym | BlankLine | NewLine @@ -686,21 +689,9 @@ Math { MathCommand { - MathKnownCommand + KnownCommand | MathUnknownCommand -} - -MathKnownCommand { - KnownCtrlSym - | Label { - LabelCtrlSeq optionalWhitespace? OptionalArgument? LabelArgument - } - | Ref { - (RefCtrlSeq | RefStarrableCtrlSeq "*"?) optionalWhitespace? OptionalArgument? optionalWhitespace? OptionalArgument? optionalWhitespace? RefArgument - } - | MathTextCommand { - (MathTextCtrlSeq | HboxCtrlSeq) optionalWhitespace? "*"? TextArgument - } + | KnownCtrlSym } @external tokens endOfArgumentListTokenizer from "./tokens.mjs" { diff --git a/services/web/frontend/js/features/source-editor/utils/tree-operations/commands.ts b/services/web/frontend/js/features/source-editor/utils/tree-operations/commands.ts index 46c008492f..ccec68aca9 100644 --- a/services/web/frontend/js/features/source-editor/utils/tree-operations/commands.ts +++ b/services/web/frontend/js/features/source-editor/utils/tree-operations/commands.ts @@ -75,14 +75,13 @@ export const enterNode = ( } else if ( node.type.is('UnknownCommand') || node.type.is('KnownCommand') || - node.type.is('MathUnknownCommand') || - node.type.is('MathKnownCommand') + node.type.is('MathUnknownCommand') ) { let commandNode: SyntaxNode | null = node.node - if (node.type.is('KnownCommand') || node.type.is('MathKnownCommand')) { - // (Math)KnownCommands are defined as + if (node.type.is('KnownCommand')) { + // KnownCommands are defined as // - // (Math)KnownCommand { + // KnownCommand { // CommandName { // CommandCtrlSeq [args] // }