changing colors

Questions on using RSyntaxTextArea should go here.

Moderator: robert

changing colors

Postby Nate » Fri Dec 04, 2009 6:45 am

Hi! Thanks for the great RSyntaxTextArea widget. I would like to use Java highlighting, but with different colors. How can I change the colors? Sorry if ths is an easy one, I did poke around quite a bit.

Also, I want to highlight Pnuts script, which is essentially the same as Java code. Could I extend or hack the Java syntax highlighter to add and maybe remove some keywords? Other than that, the only major difference is Pnuts has a `verbatim string` that can contain newlines and other unescaped characters.

Thanks!
Nate
 

Re: changing colors

Postby Guest » Fri Dec 04, 2009 6:48 am

I meant to also mention, I'd like to change the font. I have changed it for the RSyntaxTextArea, but some of the formatting is unfortunately still using the old font. Any ideas?
Guest
 

Re: changing colors

Postby robert » Fri Dec 04, 2009 2:01 pm

Hi Nate,

The code for changing the color scheme used while highlighting is a mess, unfortunately, and the only way to modify it is programmatically, so bear with me...

You can change the colors used by editing the SyntaxScheme of an RSTA instance like this:

Code: Select all
SyntaxScheme ss = textArea.getSyntaxScheme();
ss.styles[Token.RESERVED_WORD].foreground = Color.red;
ss.styles[Token.LITERAL_STRING_DOUBLE_QUOTE ].foreground = Color.blue;
ss.styles[Token.LITERAL_STRING_DOUBLE_QUOTE ].font = new Font(...);


The index into the "styles" array is one of the constants from the Token class. Just find the one that looks like it's used for the token type you want to change and try it out. :)

If you set the "font" field to null for any style, it'll use the default font for the editor (i.e., the value of "textArea.getFont()").
If you set the "foreground" field to null, it'll use textArea.getForeground().
You can also give tokens a background color or underline them, if that suits your fancy.

Also, since some tokens use a different font (bold for keywords, for example), you can't just call setFont() to change all of the fonts in the editor, unfortunately. If you want to globally change it, you'll have to do something like this:

Code: Select all
public static void setFont(RSyntaxTextArea textArea, Font font) {
   if (font!=null) {
      SyntaxScheme ss = textArea.getSyntaxScheme();
      ss = (SyntaxScheme)ss.clone();
      for (int i=0; i<ss.styles.length; i++) {
         if (ss.styles[i]!=null) {
            ss.styles[i].font = font;
         }
      }
      textArea.setSyntaxScheme(ss);
      textArea.setFont(font);
   }
}


It's been on my to-do list for ages to add this to the actual API, but I'm a little too lazy for my own good. :D
User avatar
robert
 
Posts: 774
Joined: Sat May 10, 2008 5:16 pm

Re: changing colors

Postby Guest » Fri Dec 04, 2009 7:41 pm

Exactly what I needed, thanks Robert! My style code is below in case it helps someone else. I'm not at all bothered by this API and I prefer to do it programmatically.

I have only one remaining wish. How can I extend or hack the Java highlighter so I can change the keyword lists? For extra credit, how would I add a verbatim string that uses backtick to the Java highlighter?

Your lib is great. I tried JEditSyntaxPane and a couple others before finding yours. The others just didn't integrate well. FWIW, this is the project your stuff is getting used in:
http://code.google.com/p/pg3b/

Code: Select all
Style black = new Style(new Color(0, 0, 0), null);
Style blackBold = new Style(new Color(0, 0, 0), null, monoBold);
Style greenPlain = new Style(new Color(0, 164, 82), null, plain);
Style darkBlueBold = new Style(new Color(0, 0, 128), null, monoBold);
Style darkBlue = new Style(new Color(0, 0, 128), null);
Style blue = new Style(new Color(0, 0, 255), null);
Style red = new Style(new Color(255, 0, 0), null);
Style darkGreen = new Style(new Color(64, 128, 128), null);

Style[] styles = new Style[Token.NUM_TOKEN_TYPES];
styles[COMMENT_DOCUMENTATION] = greenPlain;
styles[COMMENT_MULTILINE] = greenPlain;
styles[COMMENT_EOL] = greenPlain;
styles[RESERVED_WORD] = darkBlueBold;
styles[FUNCTION] = darkBlueBold;
styles[LITERAL_BOOLEAN] = darkBlueBold;
styles[LITERAL_NUMBER_DECIMAL_INT] = blue;
styles[LITERAL_NUMBER_FLOAT] = blue;
styles[LITERAL_NUMBER_HEXADECIMAL] = blue;
styles[LITERAL_STRING_DOUBLE_QUOTE] = darkGreen;
styles[LITERAL_CHAR] = darkGreen;
styles[LITERAL_BACKQUOTE] = darkGreen;
styles[DATA_TYPE] = darkBlueBold;
styles[VARIABLE] = darkBlue;
styles[IDENTIFIER] = black;
styles[WHITESPACE] = black;
styles[SEPARATOR] = blue;
styles[OPERATOR] = blue;
styles[PREPROCESSOR] = blue;
styles[MARKUP_TAG_DELIMITER] = black;
styles[MARKUP_TAG_NAME] = black;
styles[MARKUP_TAG_ATTRIBUTE] = black;
styles[ERROR_IDENTIFIER] = red;
styles[ERROR_NUMBER_FORMAT] = red;
styles[ERROR_STRING_DOUBLE] = red;
styles[ERROR_CHAR] = red;

SyntaxScheme scheme = new SyntaxScheme(false);
scheme.styles = styles;
codeText.setSyntaxScheme(scheme);
codeText.setFont(mono);
Guest
 

Re: changing colors

Postby Nate » Fri Dec 04, 2009 7:42 pm

Gah! Keep forgetting to login before posting.
Nate
 
Posts: 15
Joined: Fri Dec 04, 2009 6:45 am

Re: changing colors

Postby Nate » Fri Dec 04, 2009 8:01 pm

New question, how do I clear the undo/redo buffers?
Nate
 
Posts: 15
Joined: Fri Dec 04, 2009 6:45 am

Re: changing colors

Postby robert » Sat Dec 05, 2009 4:57 am

Clear the undo buffer by calling textArea.discardAllEdits(). You'll probably need to do this after loading a file into it, so they can't "undo" the load.

As for "hacking" new keywords into the Java highlighter, or adding verbatim strings, there's no easy way to do that yet. The scanner classes are generated from JFlex, with some nasty hand-modifications afterward; this makes it tough to create a new scanner if you haven't been doing it for awhile now.

That said, I do have a WIP "TokenMakerMaker" app that lets you use a GUI to generate the .flex/.java source for a scanner, and even try it out in RSTA. This allows you to create a scanner for your language without learning the peculiarities of RSyntaxTextArea - you can drop your generated .java file into your project and use it via the API. If you're interested in trying out this beta software, just holler at me and I'll send you a link. It doesn't do verbatim strings yet, but I add features as they are needed. You can currently generate a very reasonable Java scanner with it (highlighting keywords, class names, comments, and operators).

Or I can cobble one up for you, and add it into the project proper. :) Then everyone can enjoy Pnuts highlighting
User avatar
robert
 
Posts: 774
Joined: Sat May 10, 2008 5:16 pm

Re: changing colors

Postby Guest » Sat Dec 05, 2009 1:41 pm

RSTA is working fantastic! The PG3B project now has compilation error underline squiggles and autocompletion! :ugeek:

About the keywords, I'll dig in deeper and report back my progress.
Guest
 

Re: changing colors

Postby robert » Sun Dec 06, 2009 5:40 pm

Awesome, glad you're finding it useful! Feedback and suggestions are welcome.

If you really want to dig into the JFlex yourself, look at the class comment for one of the already-generated scanners, say src/org/fife/ui/rsyntaxtextarea/modes/JavaTokenMaker.flex. There should be a list of manual steps you need to do to the .java source file generated by JFlex to get it actually working in RText.

Since Pnuts is largely identical to Java syntax, you could make a copy of JavaTokenMaker.flex, modify the keywords listed in it as needed (should be easy), and add verbatim strings (harder, but I believe C# has them - using """ as delimiters? - so you could probably rip off the code in CSharpTokenMaker.flex for this).

Hope this helps! Let me know if you run into any problems along the way.
User avatar
robert
 
Posts: 774
Joined: Sat May 10, 2008 5:16 pm

Re: changing colors

Postby Guest » Mon Dec 07, 2009 11:04 am

Cool, I'll give it a shot soon.

Not too many suggestions really. I like the part where I download RSyntaxTextArea, put it in my project, and it just works. :o With the AutoCompletion project, you are providing a huge amount of functionality with very little fuss to get going. I suppose the weakest part of the lib is that if it doesn't do what you want out of the box, it doesn't seem trivial to customize.

My favorite text editor feature is word completion. In SciTE, my preferred editor, it shows a popup of all other words in the same file that start with what I've typed so far. Eclipse has arguably better word completion. There it just automatically completes what I've typed using the first word before the caret that starts with the same thing. Hitting word completion again clears the last completion and completes with the next word up. Once the top of the file is hit, it starts using words from the caret to the end of the file. After that it looks in every other open file. However, the right answer is 90% of the time the first or second press. Because it is an automatic insert, this is way fast than regular completion and hitting enter to pick from a list. I'm so used to it, I know what words are going to be automatically completed, so I just press a letter or two, word compeltion (I use shift+space), and suddenly I'm typing the next word. It is fun to amazing people watching me because the words just pop into view! :ugeek: Needless to say, it is one hell of a feature! Too bad Eclipse doesn't have it mapped to a key by default.
Guest
 

Next

Return to Help

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests