views:

1541

answers:

7

I'm writing a source-code editor in Java (for Java source code), and I'd like to add simple syntax highlighting (distinctive coloring for keywords would suffice). Any suggestions?

+11  A: 

Something like JSyntaxPane, perhaps?

A very simple to use and extend JEditorKit that supports few languages. The main goal is to make it easy to have nice looking Java Swing Editors with support for Syntax Highlighting.

Michael Myers
+3  A: 

Might want to look at an existing editor (Notepad++ for example - http://notepad-plus.sourceforge.net/uk/site.htm) and see how user-defined syntax highlighting is done (oneo of the plugins to check - Gmod 10 Lua Syntax Highlighter). I'd wager that the Java (and other languages) are done similarly...

newfie_coder
A: 

What about this component? http://fifesoft.com/rsyntaxtextarea/ It's LGPL

ktulur
So bad? I'm using it in a project and I think it's great. Besides, If you want to code your own stuff you can study it as it's opensource.
ktulur
A: 

You should check Google's prettify.js out. Some pretty neat tricks in there, and you might get a more robust feel for syntax highlighting.

Geoffrey Chetwood
That's javaScript, not Java
ktulur
@ktulur: Did you actually look at it? It does syntax highlighting for many languages including Java. This will be a great way to learn how to write a syntax highlighting algorithm. Specifically one that can support multiple languages.
Geoffrey Chetwood
Ok, sorry, I was thinking about using an existing component ,not about writing your own stuff. That's why when I've read ".js" I was like "wtf?"
ktulur
Sometimes it is nice to teach people to learn, not just reuse.
Geoffrey Chetwood
Yep sometimes, but generally, isn't it better reuse? I think that in that way you can spend your time in your own original stuff.
ktulur
If that were the case, why on earth would he be writing a Java editor in the first place?
Geoffrey Chetwood
+1  A: 

You first should think about using a common parser to create an AST (abstract syntax tree) from the sources. There are some tools around, first I find googling the internet was javaparser. It looks like this parser also records line numbers and columns, so the AST from javaparser can be a nice model for the editor.

Just process the tree, define colors for the AST node types and print it.

Andreas_D
A: 

http://www.neathighlighter.com/ is a good JavaScript highlighter

+2  A: 

jhighlight is an open source java library that supports highlighting of Java, html, Groovy, xml etc.

yossis