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