correctly highlight lines with tabs

This commit is contained in:
2021-11-05 10:22:45 +01:00
parent c4544899e4
commit a7b09a99a5
6 changed files with 85 additions and 14 deletions

30
line.py
View File

@@ -1,3 +1,6 @@
import constants
class Line:
def __init__(self, byte_offset: int, byte_end: int, line: str):
self._byte_offset = byte_offset
@@ -24,12 +27,37 @@ class Line:
prefix_chars = prefix_bytes.decode("utf8", errors="ignore")
return len(prefix_chars)
def column_to_char(self, column_in_line: int) -> int:
i = 0
result = 0
while i < column_in_line:
char = self._line[result]
if char == "\t":
i = i + constants.tab_width - 1 # jump the additional 7 columns of the tab width
if i >= column_in_line:
break;
i = i + 1
result = result + 1
return result
def char_to_column(self, char_in_line: int) -> int:
result = 0
i = 0
while i < char_in_line:
if i < len(self._line) and self._line[i] == "\t":
result = result + constants.tab_width
else:
result = result + 1
i = i + 1
return result
def includes_byte(self, byte: int) -> bool:
return self._byte_offset <= byte <= self._byte_end
def intersects(self, start_byte: int, end_byte: int):
result = start_byte < self._byte_end and end_byte > self._byte_offset
#print("%d,%d in %d,%d" % (start_byte, end_byte, self._byte_offset, self._byte_end))
# print("%d,%d in %d,%d" % (start_byte, end_byte, self._byte_offset, self._byte_end))
return result
def prefix(self, index: int) -> str: