Multiline string fixes
This commit is contained in:
@@ -357,22 +357,40 @@ func highlightStreamPosition2(stream: string, position: int): string =
|
||||
if position < 0: return stream
|
||||
if position > stream.len - 1: return stream
|
||||
|
||||
let startIndex = stream.rfind("\n", 0, position)
|
||||
let ch = stream[position]
|
||||
|
||||
let (lineStartPosition, lineEndPosition) =
|
||||
case ch:
|
||||
# of '\n':
|
||||
# let lineStartPosition = (position - 1).max(0)
|
||||
# let lineEndPosition = (position).min(stream.len - 1)
|
||||
# (lineStartPosition, lineEndPosition)
|
||||
else:
|
||||
(position, position)
|
||||
|
||||
|
||||
debugEcho "char: " & $ch
|
||||
debugEcho "position: " & $position
|
||||
|
||||
let startIndex = stream.rfind("\n", 0, lineStartPosition)
|
||||
let lineStartIndex =
|
||||
case startIndex:
|
||||
of -1: 0
|
||||
else: startIndex
|
||||
else: startIndex + 1
|
||||
|
||||
let endIndex = stream.find("\n", position)
|
||||
debugEcho "startIndex: " & $lineStartIndex
|
||||
|
||||
let endIndex = stream.find("\n", lineEndPosition)
|
||||
let lineEndIndex =
|
||||
case endIndex:
|
||||
of -1: stream.len - 1
|
||||
of -1: stream.len
|
||||
else: endIndex
|
||||
|
||||
let spaceChars = " ".repeat(position - lineStartIndex)
|
||||
let lineChars = "_".repeat((lineEndIndex - position) + 10)
|
||||
debugEcho "endIndex: " & $lineEndIndex
|
||||
|
||||
let spaceChars = " ".repeat((position - lineStartIndex).max(0))
|
||||
let lineChars = "_".repeat((lineEndIndex - position).max(0) + 10)
|
||||
|
||||
let ch = stream[position]
|
||||
let escapedChar =
|
||||
case ch:
|
||||
of '\n': "\\n"
|
||||
@@ -382,9 +400,11 @@ func highlightStreamPosition2(stream: string, position: int): string =
|
||||
let insertMessageAtIndex =
|
||||
case ch:
|
||||
# Print indicator for newlines on the previous line, which looks better for the reader
|
||||
of '\n': (lineEndIndex - 1).max(0)
|
||||
of '\n': (lineEndIndex - 1).max(1)
|
||||
else: lineEndIndex
|
||||
|
||||
debugEcho "insertMessageAtIndex: " & $insertMessageAtIndex
|
||||
|
||||
stream.dup(insert(&"\n{spaceChars}^{lineChars} Char at \"{escapedChar}\"\n", insertMessageAtIndex))
|
||||
|
||||
proc `$`*(x: ParserState): string =
|
||||
@@ -432,12 +452,24 @@ proc `$`*(x: ParserError): string =
|
||||
else: "ParseError"
|
||||
|
||||
when isMainModule:
|
||||
block highlightStreamPosition:
|
||||
let s = "abc"
|
||||
# Out of bounds
|
||||
assert s.highlightStreamPosition(-1) == s
|
||||
assert s.highlightStreamPosition(s.len) == s
|
||||
# Regular highlighting
|
||||
assert s.highlightStreamPosition(0) == LEFT_HIGHLIGHT_CHAR & "a" & RIGHT_HIGHLIGHT_CHAR & "bc"
|
||||
assert s.highlightStreamPosition(1) == "a" & LEFT_HIGHLIGHT_CHAR & "b" & RIGHT_HIGHLIGHT_CHAR & "c"
|
||||
assert s.highlightStreamPosition(2) == "ab" & LEFT_HIGHLIGHT_CHAR & "c" & RIGHT_HIGHLIGHT_CHAR
|
||||
let test1 = """AB
|
||||
|
||||
C
|
||||
|
||||
D"""
|
||||
echo test1.highlightStreamPosition2(test1.find("B"))
|
||||
echo "=============="
|
||||
echo test1.highlightStreamPosition2(test1.find("C"))
|
||||
echo "=============="
|
||||
echo test1.highlightStreamPosition2(test1.find("D"))
|
||||
# echo "\n1\n".rfind('\n', 0, 2)
|
||||
|
||||
# block highlightStreamPosition:
|
||||
# let s = "abc"
|
||||
# # Out of bounds
|
||||
# assert s.highlightStreamPosition(-1) == s
|
||||
# assert s.highlightStreamPosition(s.len) == s
|
||||
# # Regular highlighting
|
||||
# assert s.highlightStreamPosition(0) == LEFT_HIGHLIGHT_CHAR & "a" & RIGHT_HIGHLIGHT_CHAR & "bc"
|
||||
# assert s.highlightStreamPosition(1) == "a" & LEFT_HIGHLIGHT_CHAR & "b" & RIGHT_HIGHLIGHT_CHAR & "c"
|
||||
# assert s.highlightStreamPosition(2) == "ab" & LEFT_HIGHLIGHT_CHAR & "c" & RIGHT_HIGHLIGHT_CHAR
|
||||
|
||||
Reference in New Issue
Block a user