Syntax Style Editor

General Discussion on RSyntaxTextArea.

Moderator: robert

Syntax Style Editor

Postby picard09 » Sun Dec 26, 2010 4:23 pm

Hi,

i am trying to implement an editor for the syntax scheme. Any hints how I can connect to a style of a selected character?

thanks

best
Roland
picard09
 

Re: Syntax Style Editor

Postby robert » Sun Dec 26, 2010 8:38 pm

Hi picard,

Can you be more specific about what you're trying to accomplish? Are you trying to implement highlighting for the Scheme programming language? Are you asking how to determine the type of token at a given mouse/caret position?
User avatar
robert
 
Posts: 805
Joined: Sat May 10, 2008 5:16 pm

Re: Syntax Style Editor

Postby picard09 » Sun Jan 02, 2011 1:53 pm

Hi Robert,

to be more specific: I just want to create a gui which allows the user to change the used colors/fonts/font styles used for specific token types.
So, my gui would contain an RSyntaxTextArea with an example code, e.g. Java. The user then marks e.g. a java keyword. How do I determine the syntax scheme set for this marked word? - Having determined that I can provide a setting gui where he can change e.g. font color.

BTW: why not making SyntaxScheme serializable? Or do you already have some serializing mechanism for syntax schemes?

Best and thanks so far.
Roland
picard09
 

Re: Syntax Style Editor

Postby robert » Mon Jan 03, 2011 5:11 am

It's late, so I'll post more later, but here's a start:

To get the style of the token at the current caret position:

Code: Select all
int type = 0;

int line = textArea.getCaretLineNumber();
Token t = textArea.getTokenListForLine(line);
t = RSyntaxUtilities.getTokenAtOffset(t, textArea.getCaretPosition());
if (t!=null) { // Should always be true
    type = t.type;
}
Style style = textArea.getSyntaxScheme().styles[type];


Note that to go with the token at the current mouse location instead of the caret position, you can simply use:

Code: Select all
int offs = textArea.viewToModel(e.getPoint());


in a MouseMotionListener's mouseMoved() callback,
User avatar
robert
 
Posts: 805
Joined: Sat May 10, 2008 5:16 pm

Re: Syntax Style Editor

Postby robert » Tue Jan 04, 2011 7:58 pm

Following up on the rest of the question...

BTW: why not making SyntaxScheme serializable? Or do you already have some serializing mechanism for syntax schemes?


There's no reason it can't be serializable. Feel free to add a Feature Request on the SourceForge site if you would like to see this in the future.

You can currently do something similar to serialization by using these methods:

Code: Select all
public static SyntaxScheme loadFromString(String string);
public String toCommaSeparatedString();


The latter gives you a concise String representation of the scheme (suitable for using as a value in a properties file, for example, to save the scheme), whereas the former is a static utility method to construct a SyntaxScheme from a String saved previously.

There's also a completely undocumented feature for loading a SyntaxScheme from XML. I wanted to develop this further, but quite frankly I've been too lazy to actually follow through with it when there's no real benefit to it (for me at least) when compared with the first method I mentioned above. If you're inclined to use this method, see the method:

Code: Select all
public static SyntaxScheme load(Font baseFont, InputStream in) throws IOException;


in SyntaxScheme.java. Using "null" for the baseFont will result in a system-appropriate monospaced font being used (e.g. Monaco or Menlo on OS X). As for the XML structure, well, there should be a schema for it, but for now you'll have to reverse-engineer it from the XmlParser class at the bottom of SyntaxScheme.java.
User avatar
robert
 
Posts: 805
Joined: Sat May 10, 2008 5:16 pm

Re: Syntax Style Editor

Postby Guest » Fri Jan 07, 2011 4:32 pm

Hi Robert,

thanks a lot. That one helped!
CSV works fine for me.

Best regards
Roland
Guest
 

Re: Syntax Style Editor

Postby robert » Fri Jan 07, 2011 6:51 pm

Great, glad to hear it!

Just for completeness, here's an example XML SyntaxScheme I was working on that mimics Eclipse's default color scheme:

Code: Select all
<?xml version="1.0" encoding="UTF-8" ?>

<!--
   Syntax scheme that mimics Eclipse's defaults.
-->
<SyntaxScheme>

<styles>
   <style token="IDENTIFIER" fg="#000000"/>
   <style token="RESERVED_WORD" fg="#7f0055"/>
   <style token="COMMENT_EOL" fg="#3f7f5f"/>
   <style token="COMMENT_MULTILINE" fg="#3f7f5f"/>
   <style token="COMMENT_DOCUMENTATION" fg="#3f5fbf"/>
   <style token="VARIABLE" fg="#ae9fbf"/>
   <style token="DATA_TYPE" fg="#7f007f" bold="true"/>

</styles>

</SyntaxScheme>
User avatar
robert
 
Posts: 805
Joined: Sat May 10, 2008 5:16 pm


Return to Open Discussion

Who is online

Users browsing this forum: No registered users and 4 guests

cron