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?
-
1Tried to improve this in a way that supports mmyers' answer.Shog9– Shog92009-05-14 17:40:38 +00:00Commented May 14, 2009 at 17:40
-
1@Shog9: editing the question to fit an answer? Is that kosher?Paul Sonier– Paul Sonier2009-05-14 17:46:07 +00:00Commented May 14, 2009 at 17:46
-
1Voted down, you shouldn't use SO for questions that are easily answered after a google search. Also it was almost impossible to readmarcgg– marcgg2009-05-14 17:49:51 +00:00Commented May 14, 2009 at 17:49
-
4@MarcG: Not sure where you got that opinion, but you are rather wrong. That is /exactly/ what SO is for.GEOCHET– GEOCHET2009-05-14 17:52:59 +00:00Commented May 14, 2009 at 17:52
-
4I had voted it down previously because in its original form the question was utterly incomprehensible. And Marc G is completely wrong on his assertion that SO isn't for simple questions. Please refer to the newest podcast for Jeff & Joel's take.TheTXI– TheTXI2009-05-14 18:02:52 +00:00Commented May 14, 2009 at 18:02
6 Answers
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.
1 Comment
RSyntaxTextArea for new projects.What about RSyntaxTextArea? It uses a modified BSD license.
4 Comments
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.
Comments
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...
Comments
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.