Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code completion and Syntax Highlighting in Swing

I have started a project that requires syntax highlighting and code completion (a window pops up at the caret location providing suggestions) and can't find a suitable solution. There are multiple Swing components which take text input e.g. JTextArea, JTextPane and JEditorPane which makes it significantly more difficult to figure out which is the correct one to use.

The project is a programmer specific text editor which will have integration with the unity game engine. This meaning code completion and highlighting will be done on JavaScript and C# syntax.

It might be obvious but the general idea is that when the user types, certain words change to a specified colour, these words would be stored in a text file. For the code completion I need to be able to grab the currently typed letters, this detection process will restart by using the space character as the delimiter.

Thanks for your time :)

like image 806
someSortOfProgrammer Avatar asked Feb 17 '23 22:02

someSortOfProgrammer


2 Answers

See my answer here which shows auto completion for JTextComponent, it can show pop up window of auto completion words to the current caret index on JTextPane/JEditorPane (+1 @AndrewThompson for the idea):

For JTextField:

Using JTextField as AutoSugesstor

and for other JTextComponents i.e JTextPane, JEditorPane etc its shown at the current caret index:

enter image description here

To change text colour of words (i.e syntax highlight) have a look here:

enter image description here

Or if you want rather a highlighted word. See here:

enter image description here

like image 178
David Kroukamp Avatar answered Feb 28 '23 11:02

David Kroukamp


you can look at components from jintilla (a JNI wrapper around the widely used scintilla) or jedit if their licensing terms fit your use case.

like image 32
radai Avatar answered Feb 28 '23 12:02

radai