X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fteditor%2FHighlighter.java;h=23ee90014e863bc0568a3526f4e086a793b6465c;hb=d442b96e0b1453c21abf7f34bfaf73dcbed2a7ee;hp=9576ad1e52b0b9ddd854a5693b64122873228373;hpb=e8a11f986bfe2556e450d7b8ad6ef0059b369bbc;p=fanfix.git diff --git a/src/jexer/teditor/Highlighter.java b/src/jexer/teditor/Highlighter.java index 9576ad1..23ee900 100644 --- a/src/jexer/teditor/Highlighter.java +++ b/src/jexer/teditor/Highlighter.java @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (C) 2017 Kevin Lamonte + * Copyright (C) 2019 Kevin Lamonte * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -39,16 +39,51 @@ import jexer.bits.Color; */ public class Highlighter { + // ------------------------------------------------------------------------ + // Variables -------------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * The highlighter colors. */ private SortedMap colors; + // ------------------------------------------------------------------------ + // Constructors ----------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * Public constructor sets the theme to the default. */ public Highlighter() { + // NOP + } + + // ------------------------------------------------------------------------ + // Highlighter ------------------------------------------------------------ + // ------------------------------------------------------------------------ + + /** + * Set keyword highlighting. + * + * @param enabled if true, enable keyword highlighting + */ + public void setEnabled(final boolean enabled) { + if (enabled) { + setJavaColors(); + } else { + colors = null; + } + } + + /** + * Set my field values to that's field. + * + * @param rhs an instance of Highlighter + */ + public void setTo(final Highlighter rhs) { colors = new TreeMap(); + colors.putAll(rhs.colors); } /** @@ -57,11 +92,13 @@ public class Highlighter { * @param ch the character * @return true if the word should be split */ - public boolean shouldSplit(final char ch) { + public boolean shouldSplit(final int ch) { // For now, split on punctuation String punctuation = "'\"\\<>{}[]!@#$%^&*();:.,-+/*?"; - if (punctuation.indexOf(ch) != -1) { - return true; + if (ch < 0x100) { + if (punctuation.indexOf((char) ch) != -1) { + return true; + } } return false; } @@ -73,7 +110,10 @@ public class Highlighter { * @return color associated with name, e.g. bold yellow on blue */ public CellAttributes getColor(final String name) { - CellAttributes attr = (CellAttributes) colors.get(name); + if (colors == null) { + return null; + } + CellAttributes attr = colors.get(name); return attr; } @@ -81,19 +121,41 @@ public class Highlighter { * Sets to defaults that resemble the Borland IDE colors. */ public void setJavaColors() { + colors = new TreeMap(); + CellAttributes color; - String [] keywords = { + String [] types = { "boolean", "byte", "short", "int", "long", "char", "float", - "double", "void", "new", - "static", "final", "volatile", "synchronized", "abstract", - "public", "private", "protected", - "class", "interface", "extends", "implements", + "double", "void", + }; + color = new CellAttributes(); + color.setForeColor(Color.GREEN); + color.setBackColor(Color.BLUE); + color.setBold(true); + for (String str: types) { + colors.put(str, color); + } + + String [] modifiers = { + "abstract", "final", "native", "private", "protected", "public", + "static", "strictfp", "synchronized", "transient", "volatile", + }; + color = new CellAttributes(); + color.setForeColor(Color.WHITE); + color.setBackColor(Color.BLUE); + color.setBold(true); + for (String str: modifiers) { + colors.put(str, color); + } + + String [] keywords = { + "new", "class", "interface", "extends", "implements", "if", "else", "do", "while", "for", "break", "continue", "switch", "case", "default", }; color = new CellAttributes(); - color.setForeColor(Color.WHITE); + color.setForeColor(Color.YELLOW); color.setBackColor(Color.BLUE); color.setBold(true); for (String str: keywords) { @@ -129,5 +191,4 @@ public class Highlighter { } - }