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

GitOrigin-RevId: 15c5e5afdee59badc833fe2deb13a80b3c409862
This commit is contained in:
Alf Eaton 2024-11-11 12:09:45 +00:00 committed by Copybot
parent 54ec174331
commit 66fee7a794
4 changed files with 13 additions and 23 deletions

View file

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

View file

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

View file

@ -1,18 +1,10 @@
import { indentService } from '@codemirror/language' import { indentService } from '@codemirror/language'
import { debugConsole } from '@/utils/debugging'
export const latexIndentService = () => { export const latexIndentService = () =>
return indentService.of((indentContext, pos) => { indentService.of((indentContext, pos) => {
try { // only use this for insertNewLineAndIndent
if (indentContext.simulatedBreak) {
// match the indentation of the previous line (if present) // 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
})
} }
})

View file

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