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

Merge pull request #19072 from overleaf/mj-known-commands-reuse

[web] Parse known commands in math mode

GitOrigin-RevId: e7937fc46320f13c716d1612eca36aa67c8fb967
Mathias Jakobsen 2 лет назад
Родитель
Сommit
46a160f127

+ 17 - 26
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<ArgumentType> {
     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<TextArgument>
     | 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<TextArgument>
     | 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<MathArgument>
   | 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" {

+ 4 - 5
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]
       //    }