-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
162 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 121 additions & 2 deletions
123
convex-gui/src/main/java/convex/gui/utils/CVXHighlighter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,126 @@ | ||
package convex.gui.utils; | ||
|
||
import java.awt.Color; | ||
|
||
import javax.swing.JTextPane; | ||
import javax.swing.text.AttributeSet; | ||
import javax.swing.text.SimpleAttributeSet; | ||
import javax.swing.text.StyleConstants; | ||
import javax.swing.text.StyleContext; | ||
import javax.swing.text.StyledDocument; | ||
|
||
import org.antlr.v4.runtime.CharStream; | ||
import org.antlr.v4.runtime.CharStreams; | ||
import org.antlr.v4.runtime.CommonTokenStream; | ||
import org.antlr.v4.runtime.Token; | ||
|
||
import convex.core.lang.reader.antlr.ConvexLexer; | ||
|
||
public class CVXHighlighter { | ||
public static void highlight() { | ||
|
||
static StyleContext sc = StyleContext.getDefaultStyleContext(); | ||
static AttributeSet BASE = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Italic, false); | ||
static AttributeSet ITALIC = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Italic, true); | ||
|
||
static AttributeSet NORMAL = sc.addAttribute(BASE, StyleConstants.Foreground, Color.LIGHT_GRAY); | ||
static AttributeSet STRING = sc.addAttribute(ITALIC, StyleConstants.Foreground, Color.CYAN); | ||
static AttributeSet KEYWORD = sc.addAttribute(BASE, StyleConstants.Foreground, Color.PINK); | ||
static AttributeSet SYMBOL = NORMAL; | ||
static AttributeSet NUMBER = sc.addAttribute(BASE, StyleConstants.Foreground, Color.ORANGE); | ||
|
||
private static final AttributeSet[] PARENS = new AttributeSet[10]; | ||
|
||
static { | ||
for (int i=0; i<PARENS.length; i++) { | ||
Color c=Color.getHSBColor((0.1f*i), 1, 1); | ||
c=c.brighter(); | ||
AttributeSet aset = sc.addAttribute(BASE, StyleConstants.Foreground, c); | ||
PARENS[i]=aset; | ||
} | ||
} | ||
|
||
public static void highlight(JTextPane inputArea, int start, int end) { | ||
try { | ||
// Get range of text to format | ||
StyledDocument d=inputArea.getStyledDocument(); | ||
int dlen=d.getLength(); | ||
end=Math.min(end, dlen); // ensure in bounds | ||
|
||
String input=d.getText(start, end-start); | ||
|
||
CharStream cs=CharStreams.fromString(input); | ||
ConvexLexer lexer=new ConvexLexer(cs); | ||
lexer.removeErrorListeners(); | ||
CommonTokenStream tokens = new CommonTokenStream(lexer); | ||
tokens.fill(); | ||
//ConvexParser parser = new ConvexParser(tokens); | ||
//parser.removeErrorListeners(); | ||
//ParseTree pt = parser.forms(); | ||
|
||
int tcount=tokens.size(); | ||
int nest=0; | ||
for (int i=0; i<tcount; i++) { | ||
Token tok=tokens.get(i); | ||
int tstart=start+tok.getStartIndex(); | ||
int tend=start+tok.getStopIndex()+1; | ||
int tlen=tend-tstart; | ||
|
||
if (tlen>0) { | ||
char c=input.charAt(tstart-start); | ||
|
||
AttributeSet aset=NORMAL; | ||
switch (c) { | ||
case '"': { | ||
aset=STRING; break; | ||
} | ||
case ':': { | ||
aset=KEYWORD; break; | ||
} | ||
case '(':{ | ||
aset=getParen(nest++); break; | ||
} | ||
case ')': { | ||
aset=getParen(--nest); break; | ||
} | ||
case '[':{ | ||
nest+=3; | ||
aset=getParen(nest++); break; | ||
} | ||
case ']': { | ||
aset=getParen(--nest); | ||
nest-=3; break; | ||
} | ||
case '{':{ | ||
nest+=6; | ||
aset=getParen(nest++); break; | ||
} | ||
case '}': { | ||
aset=getParen(--nest); | ||
nest-=6; break; | ||
} | ||
// Whitespace uses paren colour, this colours commas! | ||
case ' ': case '\t': case ',': { | ||
aset=getParen(nest); | ||
break; | ||
} | ||
|
||
default: { | ||
if (Character.isDigit(c)) { | ||
aset=NUMBER; break; | ||
} | ||
} | ||
} | ||
|
||
d.setCharacterAttributes(tstart, tlen, aset, false); | ||
} | ||
} | ||
} catch (Exception t) { | ||
t.printStackTrace(); | ||
} | ||
} | ||
|
||
private static AttributeSet getParen(int i) { | ||
if (i<0) i=0; | ||
return PARENS[i%PARENS.length]; | ||
} | ||
|
||
} |