Skip to content

Commit

Permalink
Fixed cursor in wrong position when at end of line with tab
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Apr 26, 2024
1 parent bdf2e1d commit 91556a9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
10 changes: 6 additions & 4 deletions src/text/text_document.nim
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ type StyledText* = object
inlayContainCursor*: bool
scopeIsToken*: bool = true
canWrap*: bool = true
modifyCursorAtEndOfLine*: bool = false ## If true and the cursor is at the end of the line
## then the cursor will be behind the part.

type StyledLine* = ref object
index*: int
Expand Down Expand Up @@ -479,12 +481,12 @@ proc overrideUnderline*(self: TextDocument, line: var StyledLine, first: int, la
proc overrideStyleAndText*(self: TextDocument, line: var StyledLine, first: int, text: string, scope: string, priority: int, opacity: Option[float] = float.none, joinNext: bool = false) =
line.overrideStyleAndText(self.lines[line.index].toOpenArray.runeIndex(first, returnLen=true), text, scope, priority, opacity, joinNext)

proc insertText*(self: TextDocument, line: var StyledLine, offset: RuneIndex, text: string, scope: string, containCursor: bool) =
proc insertText*(self: TextDocument, line: var StyledLine, offset: RuneIndex, text: string, scope: string, containCursor: bool, modifyCursorAtEndOfLine: bool = false) =
line.splitAt(offset)
for i in 0..line.parts.high:
if line.parts[i].textRange.getSome(r):
if offset == r.endIndex:
line.parts.insert(StyledText(text: text, scope: scope, scopeC: scope.cstring, priority: 1000000000, inlayContainCursor: containCursor), i + 1)
line.parts.insert(StyledText(text: text, scope: scope, scopeC: scope.cstring, priority: 1000000000, inlayContainCursor: containCursor, modifyCursorAtEndOfLine: modifyCursorAtEndOfLine), i + 1)
return

proc insertTextBefore*(self: TextDocument, line: var StyledLine, offset: RuneIndex, text: string, scope: string) =
Expand Down Expand Up @@ -546,7 +548,7 @@ proc replaceTabs(self: TextDocument, line: var StyledLine) =
let runeIndex = self.lines[line.index].toOpenArray.runeIndex(s.first.column, returnLen=true)
line.overrideStyleAndText(runeIndex, t, "comment", 0, opacity=opacity.some)
if currentTabWidth > 1:
self.insertText(line, runeIndex + 1.RuneCount, " ".repeat(currentTabWidth - 1), "comment", containCursor=false)
self.insertText(line, runeIndex + 1.RuneCount, " ".repeat(currentTabWidth - 1), "comment", containCursor=false, modifyCursorAtEndOfLine=true)

currentOffset += currentTabWidth
previousEnd = s.last.column
Expand Down Expand Up @@ -1099,7 +1101,7 @@ proc moveCursorColumn(self: TextDocument, cursor: Cursor, offset: int, wrap: boo
proc firstNonWhitespace*(str: string): int =
result = 0
for c in str:
if c != ' ':
if c != ' ' and c != '\t':
break
result += 1

Expand Down
14 changes: 7 additions & 7 deletions src/ui/widget_builder_text_document.nim
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ proc renderLine*(
builder.panel(flagsInner + LayoutVertical + FillBackground, y = options.y, pivot = options.pivot, backgroundColor = options.backgroundColor, userId = newSecondaryId(options.parentId, options.lineId)):
let lineWidth = currentNode.bounds.w

var lastNonInlaySubLine: UINode = nil
var lastNonInlayPartXW: float32 = 0
var cursorBaseNode: UINode = nil
var cursorBaseXW: float32 = 0

var lastTextSubLine: UINode = nil
var lastTextPartXW: float32 = 0
Expand Down Expand Up @@ -301,9 +301,9 @@ proc renderLine*(
let (startRune, _) = line.getTextRange(partIndex)
let partNode = renderLinePart(builder, line, backgroundColors, options, partIndex, startRune, partRuneLen)

if part.textRange.isSome:
lastNonInlaySubLine = subLine
lastNonInlayPartXW = partNode.bounds.xw
if part.textRange.isSome or part.modifyCursorAtEndOfLine:
cursorBaseNode = subLine
cursorBaseXW = partNode.bounds.xw

# cursor
for curs in cursors:
Expand Down Expand Up @@ -361,11 +361,11 @@ proc renderLine*(
# cursor after latest char
for curs in cursors:
if curs == lineOriginal.len:
result.cursors.add (lastNonInlaySubLine, "", rect(lastNonInlayPartXW, 0, builder.charWidth, builder.textHeight), (line.index, curs))
result.cursors.add (cursorBaseNode, "", rect(cursorBaseXW, 0, builder.charWidth, builder.textHeight), (line.index, curs))

# set hover info if the hover location is at the end of this line
if line.index == options.hoverLocation.line and options.hoverLocation.column == lineOriginal.len:
result.hover = (lastNonInlaySubLine, "", rect(lastNonInlayPartXW, 0, builder.charWidth, builder.textHeight), options.hoverLocation).some
result.hover = (cursorBaseNode, "", rect(cursorBaseXW, 0, builder.charWidth, builder.textHeight), options.hoverLocation).some

proc blendColorRanges(colors: var seq[tuple[first: RuneIndex, last: RuneIndex, color: Color]], ranges: var seq[tuple[first: RuneIndex, last: RuneIndex]], color: Color, inclusive: bool) =
let inclusiveOffset = if inclusive: 1.RuneCount else: 0.RuneCount
Expand Down

0 comments on commit 91556a9

Please sign in to comment.