Brad
Brad

Reputation: 15879

Is it possible to change the colour of a line of text in Eclipse Java Editor based on it's content?

Specifically for editing Java code, I was wanting to know if I could change the colour of the text of any line of code beginning with the string LOG. which indicates a logging statement.

In this example I would like to be able to make LOG statement appear all in grey for example. It would help when reading code that is heavily logged.

public class Foo
{
    private static final Logger LOG = LoggerFactory.getLogger(Foo.class);

    public void doSomething() {
        LOG.info("About to do something");

        // code
    }
}

It's probably more difficult than just identifying a single line, as logging could be split over multiple lines, and/or even contained within a if(LOG.isDebugEnabled) {...} block.

Visualy I would like these LOG statements/blocks to appear like the default colouring of // comments /* */

Upvotes: 7

Views: 1068

Answers (2)

Lii
Lii

Reputation: 12112

One possibility would be to use the File Search tool and search on the text you want to highlight.

By searching on for example *LOG.* you would get a search result over an entire line.

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

In Eclipse it is also possible to customise the appearance of a search result to look the way you want it to. You do this in Window > Preferences > General > Editors > Text Editors > Annotations > Search Results.

Example result, by searching on *.getLogger()*:

enter image description here

Upvotes: 0

prashant thakre
prashant thakre

Reputation: 5147

You can achieve the same by writing a eclipse custom plugin.
Eclipse works to color the syntex as per rule basis.

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 instantiates a Token object for a particular color:

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

To proceed further to achieve the same you can refer to the Eclipse FAQ

Upvotes: 2

Related Questions