52938
|
1 |
/*
|
|
2 |
* Copyright (c) 2002-2016, the original author or authors.
|
|
3 |
*
|
|
4 |
* This software is distributable under the BSD license. See the terms of the
|
|
5 |
* BSD license in the documentation provided with this software.
|
|
6 |
*
|
58903
|
7 |
* https://opensource.org/licenses/BSD-3-Clause
|
52938
|
8 |
*/
|
|
9 |
package jdk.internal.org.jline.reader.impl;
|
|
10 |
|
|
11 |
import jdk.internal.org.jline.reader.LineReader;
|
|
12 |
import jdk.internal.org.jline.reader.LineReader.RegionType;
|
|
13 |
import jdk.internal.org.jline.reader.Highlighter;
|
|
14 |
import jdk.internal.org.jline.utils.AttributedString;
|
|
15 |
import jdk.internal.org.jline.utils.AttributedStringBuilder;
|
|
16 |
import jdk.internal.org.jline.utils.AttributedStyle;
|
|
17 |
import jdk.internal.org.jline.utils.WCWidth;
|
|
18 |
|
|
19 |
public class DefaultHighlighter implements Highlighter {
|
|
20 |
|
|
21 |
@Override
|
|
22 |
public AttributedString highlight(LineReader reader, String buffer) {
|
|
23 |
int underlineStart = -1;
|
|
24 |
int underlineEnd = -1;
|
|
25 |
int negativeStart = -1;
|
|
26 |
int negativeEnd = -1;
|
|
27 |
String search = reader.getSearchTerm();
|
|
28 |
if (search != null && search.length() > 0) {
|
|
29 |
underlineStart = buffer.indexOf(search);
|
|
30 |
if (underlineStart >= 0) {
|
|
31 |
underlineEnd = underlineStart + search.length() - 1;
|
|
32 |
}
|
|
33 |
}
|
|
34 |
if (reader.getRegionActive() != RegionType.NONE) {
|
|
35 |
negativeStart = reader.getRegionMark();
|
|
36 |
negativeEnd = reader.getBuffer().cursor();
|
|
37 |
if (negativeStart > negativeEnd) {
|
|
38 |
int x = negativeEnd;
|
|
39 |
negativeEnd = negativeStart;
|
|
40 |
negativeStart = x;
|
|
41 |
}
|
|
42 |
if (reader.getRegionActive() == RegionType.LINE) {
|
|
43 |
while (negativeStart > 0 && reader.getBuffer().atChar(negativeStart - 1) != '\n') {
|
|
44 |
negativeStart--;
|
|
45 |
}
|
|
46 |
while (negativeEnd < reader.getBuffer().length() - 1 && reader.getBuffer().atChar(negativeEnd + 1) != '\n') {
|
|
47 |
negativeEnd++;
|
|
48 |
}
|
|
49 |
}
|
|
50 |
}
|
|
51 |
|
|
52 |
AttributedStringBuilder sb = new AttributedStringBuilder();
|
|
53 |
for (int i = 0; i < buffer.length(); i++) {
|
|
54 |
if (i == underlineStart) {
|
|
55 |
sb.style(AttributedStyle::underline);
|
|
56 |
}
|
|
57 |
if (i == negativeStart) {
|
|
58 |
sb.style(AttributedStyle::inverse);
|
|
59 |
}
|
|
60 |
char c = buffer.charAt(i);
|
|
61 |
if (c == '\t' || c == '\n') {
|
|
62 |
sb.append(c);
|
|
63 |
} else if (c < 32) {
|
|
64 |
sb.style(AttributedStyle::inverseNeg)
|
|
65 |
.append('^')
|
|
66 |
.append((char) (c + '@'))
|
|
67 |
.style(AttributedStyle::inverseNeg);
|
|
68 |
} else {
|
|
69 |
int w = WCWidth.wcwidth(c);
|
|
70 |
if (w > 0) {
|
|
71 |
sb.append(c);
|
|
72 |
}
|
|
73 |
}
|
|
74 |
if (i == underlineEnd) {
|
|
75 |
sb.style(AttributedStyle::underlineOff);
|
|
76 |
}
|
|
77 |
if (i == negativeEnd) {
|
|
78 |
sb.style(AttributedStyle::inverseOff);
|
|
79 |
}
|
|
80 |
}
|
|
81 |
return sb.toAttributedString();
|
|
82 |
}
|
|
83 |
|
|
84 |
}
|