Can I change the color of a line of text in a Java Eclipse editor based on its contents?

In particular, for editing Java code, I wanted to know if I can change the text color of any line of code starting with the LOG. line LOG. which indicates the logging statement.

In this example, I would like the LOG operator to display everything in gray, for example. This would help in reading code that was pretty much logged.

 public class Foo { private static final Logger LOG = LoggerFactory.getLogger(Foo.class); public void doSomething() { LOG.info("About to do something"); // code } } 

This is probably harder than just identifying a single line, since logging can be split into multiple lines and / or even contained in an if(LOG.isDebugEnabled) {...} block.

Visualy I would like these LOG statements / blocks to display as the default coloring // comments / * * /

+7
java eclipse
source share
2 answers

You can achieve this by writing a custom eclipse plugin.
Eclipse works to color syntax according to the rule.

 ITokenScanner scanner = new RuleBasedScanner(); IToken string = createToken(colorString); IRule[] rules = new IRule[3]; // Add rule for double quotes rules[0] = new SingleLineRule("\"", "\"", string, '\\'); // Add a rule for single quotes rules[1] = new SingleLineRule("'", "'", string, '\\'); // Add generic whitespace rule. rules[2] = new WhitespaceRule(whitespaceDetector); scanner.setRules(rules); scanner.setDefaultReturnToken(createToken(colorTag)); 

The createToken method creates a Token object for a specific color:

 private IToken createToken(Color color) { return new Token(new TextAttribute(color)); } 

To continue, you can refer to the Eclipse FAQ.

+2
source share

One possibility is to use the file search tool and search the text that you want to highlight.

If you search for, for example, *LOG.* , You will get a search result across the entire string.

You can access the file search tool by selecting a piece of text in the editor and press Ctrl + H You can also easily search for all files in a project or in a subdirectory.

In Eclipse, you can also customize the look of your search result to see how you want it. You do this in Window > Preferences > General > Editors > Text Editors > Annotations > Search Results .

As a result, the result *.getLogger()* :

enter image description here

0
source share

All Articles