Skip to content

Commit

Permalink
Prevent padding on lines from confusing moveVertically and posAtCoords
Browse files Browse the repository at this point in the history
FIX: Fix an issue where having a bunch of padding on lines could cause vertical cursor
motion and `posAtCoords` to jump over lines.

See https://discuss.codemirror.net/t/different-heights-for-lines/6706
  • Loading branch information
marijnh committed Jun 20, 2023
1 parent c77629d commit 657615e
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export function posAtCoords(view: EditorView, coords: {x: number, y: number}, pr
if (yOffset > docHeight) return view.state.doc.length

// Scan for a text block near the queried y position
for (let halfLine = view.defaultLineHeight / 2, bounced = false;;) {
for (let halfLine = view.viewState.heightOracle.textHeight / 2, bounced = false;;) {
block = view.elementAtHeight(yOffset)
if (block.type == BlockType.Text) break
for (;;) {
Expand Down Expand Up @@ -206,7 +206,8 @@ export function posAtCoords(view: EditorView, coords: {x: number, y: number}, pr
function posAtCoordsImprecise(view: EditorView, contentRect: Rect, block: BlockInfo, x: number, y: number) {
let into = Math.round((x - contentRect.left) * view.defaultCharacterWidth)
if (view.lineWrapping && block.height > view.defaultLineHeight * 1.5) {
let line = Math.floor((y - block.top) / view.defaultLineHeight)
let textHeight = view.viewState.heightOracle.textHeight
let line = Math.floor((y - block.top - (view.defaultLineHeight - textHeight) * 0.5) / textHeight)
into += line * view.viewState.heightOracle.lineLength
}
let content = view.state.sliceDoc(block.from, block.to)
Expand Down Expand Up @@ -309,7 +310,7 @@ export function moveVertically(view: EditorView, start: SelectionRange, forward:
startY = (dir < 0 ? line.top : line.bottom) + docTop
}
let resolvedGoal = rect.left + goal
let dist = distance ?? (view.defaultLineHeight >> 1)
let dist = distance ?? (view.viewState.heightOracle.textHeight >> 1)
for (let extra = 0;; extra += 10) {
let curY = startY + (dist + extra) * dir
let pos = posAtCoords(view, {x: resolvedGoal, y: curY}, false, dir)!
Expand Down

0 comments on commit 657615e

Please sign in to comment.