From 66fee7a794afef1d8e11bf7c6b0dccf6b615e52d Mon Sep 17 00:00:00 2001 From: Alf Eaton Date: Mon, 11 Nov 2024 12:09:45 +0000 Subject: [PATCH] Only use the LaTeX indent service for when inserting a line break (#21530) GitOrigin-RevId: 15c5e5afdee59badc833fe2deb13a80b3c409862 --- .../source-editor/extensions/toolbar/lists.ts | 8 ++++---- .../extensions/visual/utils/list-item.ts | 8 ++------ .../languages/latex/latex-indent-service.ts | 18 +++++------------- .../codemirror-editor-shortcuts.spec.tsx | 2 ++ 4 files changed, 13 insertions(+), 23 deletions(-) diff --git a/services/web/frontend/js/features/source-editor/extensions/toolbar/lists.ts b/services/web/frontend/js/features/source-editor/extensions/toolbar/lists.ts index 8d4a065895..5ad4587497 100644 --- a/services/web/frontend/js/features/source-editor/extensions/toolbar/lists.ts +++ b/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) } diff --git a/services/web/frontend/js/features/source-editor/extensions/visual/utils/list-item.ts b/services/web/frontend/js/features/source-editor/extensions/visual/utils/list-item.ts index 7fbcfb9f34..d7c7be9432 100644 --- a/services/web/frontend/js/features/source-editor/extensions/visual/utils/list-item.ts +++ b/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 ` } diff --git a/services/web/frontend/js/features/source-editor/languages/latex/latex-indent-service.ts b/services/web/frontend/js/features/source-editor/languages/latex/latex-indent-service.ts index 627895adc4..08c1798032 100644 --- a/services/web/frontend/js/features/source-editor/languages/latex/latex-indent-service.ts +++ b/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 }) -} diff --git a/services/web/test/frontend/features/source-editor/components/codemirror-editor-shortcuts.spec.tsx b/services/web/test/frontend/features/source-editor/components/codemirror-editor-shortcuts.spec.tsx index 3ff720ebe7..6b7f94732c 100644 --- a/services/web/test/frontend/features/source-editor/components/codemirror-editor-shortcuts.spec.tsx +++ b/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