Skip to content

Commit 4af73a1

Browse files
authored
Merge pull request #14 from Softwee/fix/highlight.line.break
Fixed line break bug
2 parents 4cfaaff + 282c22f commit 4af73a1

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

codeview/src/main/java/io/github/kbiakov/codeview/highlight/CodeHighlighter.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ fun Int.isFound() = this >= 0
219219
fun Int.notFound() = this == -1
220220

221221
/**
222-
* @return String with applied font params
222+
* Apply font params to string.
223+
*
224+
* @param color Color as formatter string
225+
* @return Formatted string
223226
*/
224227
fun String.withFontParams(color: String?): String {
225228
val parametrizedString = StringBuilder()
@@ -232,13 +235,13 @@ fun String.withFontParams(color: String?): String {
232235
else { // may contain multiple lines with line breaks
233236

234237
// put tag on the borders (end & start of line, ..., end of tag)
235-
while (newIdx.isFound()) { // until closing tag is reached
236-
val part = substring(idx..newIdx).inFontTag(color)
238+
do { // until closing tag is reached
239+
val part = substring(idx..newIdx - 1).inFontTag(color).plus("\n")
237240
parametrizedString.append(part)
238241

239-
idx = newIdx
240-
newIdx = indexOf("\n", idx + 1)
241-
}
242+
idx = newIdx + 1
243+
newIdx = indexOf("\n", idx)
244+
} while (newIdx.isFound())
242245

243246
if (idx != indexOf("\n")) // if not replaced only once (for multiline tag coverage)
244247
parametrizedString.append(substring(idx).inFontTag(color))
@@ -251,7 +254,7 @@ fun String.withFontParams(color: String?): String {
251254
* @return String with escaped line break at start
252255
*/
253256
fun String.escLineBreakAtStart() =
254-
if (startsWith("\n") && length >= 2)
257+
if (startsWith("\n") && length >= 1)
255258
substring(1)
256259
else this
257260

example/src/main/java/io/github/kbiakov/codeviewexample/ListingsActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void onCodeLineClicked(int n, @NotNull String line) {
7676
codeView.highlightCode("python");
7777
codeView.removeCodeListener();
7878

79-
diffsAdapter.addFooterEntity(15, new DiffModel(getString(R.string.py_addition_16), true));
80-
diffsAdapter.addFooterEntity(10, new DiffModel(getString(R.string.py_deletion_11), false));
79+
diffsAdapter.addFooterEntity(16, new DiffModel(getString(R.string.py_addition_16), true));
80+
diffsAdapter.addFooterEntity(11, new DiffModel(getString(R.string.py_deletion_11), false));
8181
}
8282
}

example/src/main/res/values/strings.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,21 @@
9898
\n
9999
tmp = \"Python 3.2.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32.\"\n
100100
\n
101-
def case1(): # А. инкрементальные конкатенации в цикле\n
101+
def case1(): # A. Incremental concatenations in cycle\n
102102
s = \"\"\n
103103
for i in range(10000):\n
104104
s += tmp\n
105-
106-
def case2(): # Б. через промежуточный список и метод join\n
105+
\n
106+
def case2(): # Б. temporary list and join\n
107107
s = []\n
108108
for i in range(10000):\n
109109
s.append(tmp)\n
110110
s = \"\".join(s)\n
111111
\n
112-
def case3(): # В. списковое выражение и метод join\n
112+
def case3(): # C. list expression and join\n
113113
return \"\".join([tmp for i in range(10000)])\n
114114
\n
115-
def case4(): # Г. генераторное выражение и метод join\n
115+
def case4(): # D. generator expression join\n
116116
return \"\".join(tmp for i in range(10000))\n
117117
\n
118118
for v in range(1,5):\n

0 commit comments

Comments
 (0)