Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

eclipse

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 /* */

like image 933
Brad Avatar asked May 13 '15 07:05

Brad


People also ask

How to change the color of current selected lines (text) in Eclipse?

To change the color of current selected lines (TEXT), follow these steps Eclipse-- In Eclipse: go to Windows -> preference-> General -> Editors -> Text Editors ->Appearance color options ->Selection foreground Color

How to change the skin of Eclipse using color theme?

Now that we have installed the color theme plugin, it is time to use this plugin and change the skin of Eclipse. Go to Menu->Windows->Preference: In the left sidebar, go to General->Appearance->Color Theme. When you select that, it will show you different available themes in the right pane. You can choose the one you like and press OK.

How to change the color of current selected line background?

To change the color of current selected line background, follow these steps Eclipse-- In Eclipse: go to Windows -> preference-> General -> Editors -> Text Editors ->Appearance color options ->Selection Background Color.

How do I change the color of the highlight text?

The one you are after are probably in General -> Editors -> Text Editors ===> Then Appearance Color options -> Current line highlight & Selection background Share Improve this answer Follow answered Dec 28 '11 at 6:21


1 Answers

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

like image 182
prashant thakre Avatar answered Oct 23 '22 18:10

prashant thakre