Skip to content

Commit

Permalink
added line wrap char, reverted other change because of a bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Sep 29, 2023
1 parent ea7e01d commit 9a2322d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 22 deletions.
11 changes: 7 additions & 4 deletions src/platform/widget_builder_text_document.nim
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ proc renderLine*(
y: float, sizeToContentX: bool, lineNumberTotalWidth: float, lineNumberWidth: float, pivot: Vec2,
backgroundColor: Color, textColor: Color,
backgroundColors: openArray[tuple[first: RuneIndex, last: RuneIndex, color: Color]], cursors: openArray[int],
wrapLine: bool): seq[CursorLocationInfo] =
wrapLine: bool, wrapLineEndChar: string, wrapLineEndColor: Color): seq[CursorLocationInfo] =

var flagsInner = &{FillX, SizeToContentY}
if sizeToContentX:
Expand Down Expand Up @@ -100,6 +100,9 @@ proc renderLine*(
break

if lastPartXW + wrapWidth + builder.charWidth >= lineWidth:
builder.panel(&{DrawText, FillBackground, SizeToContentX, SizeToContentY}, text = wrapLineEndChar, backgroundColor = backgroundColor, textColor = wrapLineEndColor):
defer:
lastPartXW = currentNode.xw
subLineIndex += 1
subLinePartIndex = 0
break
Expand Down Expand Up @@ -297,7 +300,7 @@ proc createTextLines(self: TextDocumentEditor, builder: UINodeBuilder, app: App,
let charWidth = builder.charWidth

# ↲ ↩ ⤦ ⤶ ⤸ ⮠
let wrapLineEndChar = getOption[string](app, "editor.text.wrap-line-end-char", "")
let wrapLineEndChar = getOption[string](app, "editor.text.wrap-line-end-char", "")
let wrapLines = getOption[bool](app, "editor.text.wrap-lines", true)
let showContextLines = getOption[bool](app, "editor.text.context-lines", true)

Expand Down Expand Up @@ -396,7 +399,7 @@ proc createTextLines(self: TextDocumentEditor, builder: UINodeBuilder, app: App,
cursors.add self.renderLine(builder, app.theme, styledLine, self.document.lines[i], self.document.lineIds[i],
self.userId, cursorLine, i, lineNumbers, y, sizeToContentX, lineNumberWidth, lineNumberBounds.x, pivot,
backgroundColor, textColor,
colors, cursorsPerLine, wrapLine
colors, cursorsPerLine, wrapLine, wrapLineEndChar, wrapLineEndColor,
)

builder.createLines(self.previousBaseIndex, self.scrollOffset, self.document.lines.high, sizeToContentX, sizeToContentY, backgroundColor, handleScroll, handleLine)
Expand All @@ -422,7 +425,7 @@ proc createTextLines(self: TextDocumentEditor, builder: UINodeBuilder, app: App,
cursors.add self.renderLine(builder, app.theme, styledLine, self.document.lines[contextLine], self.document.lineIds[contextLine],
self.userId, cursorLine, contextLine, lineNumbers, y, sizeToContentX, lineNumberWidth, lineNumberBounds.x, vec2(0, 0),
contextBackgroundColor, textColor,
colors, [], false
colors, [], false, wrapLineEndChar, wrapLineEndColor,
)

# let fill = self.scrollOffset mod builder.textHeight
Expand Down
61 changes: 43 additions & 18 deletions src/ui/node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -787,24 +787,25 @@ proc removeFromParent*(node: UINode) =
proc getNextOrNewNode(builder: UINodeBuilder, node: UINode, last: UINode, userId: var UIUserId): UINode =
let insert = true

let firstOrLast = if last.isNotNil: last else: node.first

# search ahead to see if we find a matching node
var matchingNode = UINode.none
if userId.kind != None and node.first.isNotNil:
for _, c in firstOrLast.nextSiblings:
if c.userId == userId:
matchingNode = c.some
break

if matchingNode.isSome:
matchingNode.get.removeFromParent()
node.insert(matchingNode.get, firstOrLast)

assert firstOrLast.next == matchingNode.get
assert firstOrLast.next.userId == userId

return firstOrLast.next
# # search ahead to see if we find a matching node
# let firstOrLast = if last.isNotNil: last else: node.first
# var matchingNode = UINode.none
# if userId.kind != None and firstOrLast.isNotNil:
# for _, c in firstOrLast.nextSiblings:
# if c.userId == userId:
# matchingNode = c.some
# break

# if matchingNode.isSome:
# if builder.useInvalidation:
# node.clearedChildrenBounds = node.clearedChildrenBounds or matchingNode.get.boundsActual.some
# matchingNode.get.removeFromParent()
# node.insert(matchingNode.get, firstOrLast)

# assert firstOrLast.next == matchingNode.get
# assert firstOrLast.next.userId == userId

# return firstOrLast.next

if last.isNil: # Creating/Updating first child
if node.first.isNotNil: # First child already exists
Expand All @@ -829,6 +830,30 @@ proc getNextOrNewNode(builder: UINodeBuilder, node: UINode, last: UINode, userId
if last.next.userId == userId:
return last.next
elif userId.kind != None: # Has user id, doesn't match

# search ahead to see if we find a matching node
var matchingNode = UINode.none
for _, c in last.nextSiblings:
if c.userId == userId:
matchingNode = c.some
break

if matchingNode.isSome:
# echo "found matching node later, delete inbetween"
# remove all nodes in between
for _, c in last.nextSiblings:
if c == matchingNode.get:
break
# echo "delete old node ", c.dump(), ", ", node.clearRect
if builder.useInvalidation:
node.clearedChildrenBounds = node.clearedChildrenBounds or c.boundsActual.some
c.removeFromParent()
builder.returnNode(c)
assert last.next == matchingNode.get
assert last.next.userId == userId

return last.next

let newNode = builder.unpoolNode(userId)

if newNode.parent.isNotNil:
Expand Down

0 comments on commit 9a2322d

Please sign in to comment.