Explorar o código

Only use the LaTeX indent service for when inserting a line break (#21530)

GitOrigin-RevId: 15c5e5afdee59badc833fe2deb13a80b3c409862
Alf Eaton hai 1 ano
pai
achega
66fee7a794

+ 4 - 4
services/web/frontend/js/features/source-editor/extensions/toolbar/lists.ts

@@ -6,7 +6,6 @@ import {
   SelectionRange,
 } from '@codemirror/state'
 import {
-  getIndentation,
   getIndentUnit,
   IndentContext,
   indentString,
@@ -38,7 +37,7 @@ const wrapRangeInList = (
   prefix = ''
 ) => {
   const cx = new IndentContext(state)
-  const columns = getIndentation(cx, range.from) ?? 0
+  const columns = cx.lineIndent(range.from)
   const unit = getIndentUnit(state)
   const indent = indentString(state, columns)
   const itemIndent = indentString(state, columns + unit)
@@ -113,7 +112,7 @@ const unwrapRangeFromList = (
   const listToLine = state.doc.lineAt(list.to)
 
   const cx = new IndentContext(state)
-  const columns = getIndentation(cx, range.from) ?? 0
+  const columns = cx.lineIndent(range.from)
   const unit = getIndentUnit(state)
   const indent = indentString(state, columns - unit) // decrease indent depth
 
@@ -270,7 +269,8 @@ const toggleListForRange = (
 
               // if the line before the command is empty, remove one unit of indentation
               if (lineBeforeCommand.trim().length === 0) {
-                const indentation = getIndentation(view.state, itemNode.from)
+                const cx = new IndentContext(view.state)
+                const indentation = cx.lineIndent(itemNode.from)
                 change.from -= Math.min(indentation ?? 0, indentUnit)
               }
 

+ 2 - 6
services/web/frontend/js/features/source-editor/extensions/visual/utils/list-item.ts

@@ -1,13 +1,9 @@
 import { EditorState } from '@codemirror/state'
-import {
-  getIndentation,
-  IndentContext,
-  indentString,
-} from '@codemirror/language'
+import { IndentContext, indentString } from '@codemirror/language'
 
 export const createListItem = (state: EditorState, pos: number) => {
   const cx = new IndentContext(state)
-  const columns = getIndentation(cx, pos) ?? 0
+  const columns = cx.lineIndent(pos)
   const indent = indentString(state, columns)
   return `${indent}\\item `
 }

+ 5 - 13
services/web/frontend/js/features/source-editor/languages/latex/latex-indent-service.ts

@@ -1,18 +1,10 @@
 import { indentService } from '@codemirror/language'
-import { debugConsole } from '@/utils/debugging'
 
-export const latexIndentService = () => {
-  return indentService.of((indentContext, pos) => {
-    try {
+export const latexIndentService = () =>
+  indentService.of((indentContext, pos) => {
+    // only use this for insertNewLineAndIndent
+    if (indentContext.simulatedBreak) {
       // match the indentation of the previous line (if present)
-      const previousLine = indentContext.state.doc.lineAt(pos)
-      const whitespace = previousLine.text.match(/^\s*/)
-      if (whitespace) {
-        return whitespace[0].length
-      }
-    } catch (err) {
-      debugConsole.error('Error in CM indentService', err)
+      return null
     }
-    return null
   })
-}

+ 2 - 0
services/web/test/frontend/features/source-editor/components/codemirror-editor-shortcuts.spec.tsx

@@ -126,6 +126,7 @@ describe('emacs keybindings', { scrollBehavior: false }, function () {
   beforeEach(function () {
     window.metaAttributesCache.set('ol-preventCompileOnLoad', true)
     cy.interceptEvents()
+    cy.interceptMetadata()
 
     const shortDoc = `
 \\documentclass{article}
@@ -227,6 +228,7 @@ describe('vim keybindings', { scrollBehavior: false }, function () {
   beforeEach(function () {
     window.metaAttributesCache.set('ol-preventCompileOnLoad', true)
     cy.interceptEvents()
+    cy.interceptMetadata()
 
     // Make a short doc that will fit entirely into the dom tree, so that
     // index() corresponds to line number - 1