Skip to content

Commit

Permalink
Add indentation to wrapped lines to improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed May 13, 2024
1 parent 00ccd11 commit 165cdfd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/text/text_document.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,12 @@ proc getIndentLevelForLine*(self: TextDocument, line: int): int =
let indentWidth = self.indentStyle.indentWidth(self.tabWidth)
return indentLevelForLine(self.lines[line], self.tabWidth, indentWidth)

proc getIndentLevelForLineInSpaces*(self: TextDocument, line: int, offset: int = 0): int =
if line < 0 or line >= self.lines.len:
return 0
let indentWidth = self.indentStyle.indentWidth(self.tabWidth)
return max(0, indentLevelForLine(self.lines[line], self.tabWidth, indentWidth) + offset) * indentWidth

proc getIndentLevelForClosestLine*(self: TextDocument, line: int): int =
for i in line..self.lines.high:
if self.lineLength(i) > 0:
Expand Down
12 changes: 12 additions & 0 deletions src/ui/widget_builder_text_document.nim
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ type LineRenderOptions = object
parentId: Id
cursorLine: int

indentInSpaces: int

when defined(js):
template tokenColor*(theme: Theme, part: StyledText, default: untyped): Color =
if part.scopeIsToken:
Expand Down Expand Up @@ -273,6 +275,10 @@ proc renderLine*(
if partIndex < line.parts.len:
lastTextPartXW = lastPartXW

if subLineIndex > 0:
builder.panel(&{}, w = options.indentInSpaces.float * builder.charWidth):
lastPartXW = currentNode.bounds.xw

while partIndex < line.parts.len: # inner loop for parts within a wrapped line part
template part: StyledText = line.parts[partIndex]

Expand Down Expand Up @@ -726,6 +732,7 @@ proc createTextLines(self: TextDocumentEditor, builder: UINodeBuilder, app: App,
options.lineNumber = k
options.lineId = self.diffDocument.lineIds[k]
options.cursorLine = -1
options.indentInSpaces = self.diffDocument.getIndentLevelForLineInSpaces(k, 2)
discard renderLine(builder, styledLine, self.diffDocument.lines[k], colors, [], options)
let lastLine = builder.currentChild
leftOffsetY = lastLine.bounds.yh
Expand All @@ -735,6 +742,7 @@ proc createTextLines(self: TextDocumentEditor, builder: UINodeBuilder, app: App,
options.lineNumber = otherLine.line
options.lineId = self.diffDocument.lineIds[otherLine.line]
options.cursorLine = otherCursorLine.flatten.get((-1, false)).line
options.indentInSpaces = self.diffDocument.getIndentLevelForLineInSpaces(otherLine.line, 2)
let colors = [(0.RuneIndex, self.diffDocument.lines[otherLine.line].runeLen.RuneIndex, backgroundColor)]
discard renderLine(builder, styledLine, self.diffDocument.lines[otherLine.line], colors, [], options)

Expand All @@ -745,6 +753,7 @@ proc createTextLines(self: TextDocumentEditor, builder: UINodeBuilder, app: App,
options.backgroundColor = deletedTextBackgroundColor
options.lineNumber = k
options.lineId = self.diffDocument.lineIds[k]
options.indentInSpaces = self.diffDocument.getIndentLevelForLineInSpaces(k, 2)
discard renderLine(builder, styledLine, self.diffDocument.lines[k], colors, [], options)

else:
Expand All @@ -753,6 +762,7 @@ proc createTextLines(self: TextDocumentEditor, builder: UINodeBuilder, app: App,
builder.panel(&{SizeToContentY, FillY, FillBackground}, y = leftOffsetY, x = width / 2, w = width / 2, backgroundColor = diffEmptyBackgroundColor):
options.lineId = self.document.lineIds[i]
options.lineNumber = i
options.indentInSpaces = self.document.getIndentLevelForLineInSpaces(i, 2)
options.cursorLine = cursorLine
options.backgroundColor = backgroundColor

Expand All @@ -771,6 +781,7 @@ proc createTextLines(self: TextDocumentEditor, builder: UINodeBuilder, app: App,

else:

options.indentInSpaces = self.document.getIndentLevelForLineInSpaces(i, 2)
let infos = renderLine(builder, styledLine, self.document.lines[i], colors, cursorsPerLine, options)
cursors.add infos.cursors
if infos.hover.isSome:
Expand Down Expand Up @@ -813,6 +824,7 @@ proc createTextLines(self: TextDocumentEditor, builder: UINodeBuilder, app: App,
options.lineId = self.document.lineIds[contextLine]
options.parentId = self.userId
options.cursorLine = cursorLine
options.indentInSpaces = self.document.getIndentLevelForLineInSpaces(contextLine, 2)

let infos = renderLine(builder, styledLine, self.document.lines[contextLine], colors, [], options)
cursors.add infos.cursors
Expand Down

0 comments on commit 165cdfd

Please sign in to comment.