-
syntax...
I am playing around with making a java editor with syntax highlighting. I got everything to work ok however, when the file gets very large, there is a major speed issue. Anyone have any ideas on how to optimize this? ( the current syntax highlighter is re-highlighting the entire file every time text is inserted or removed. )
-
Maybe a way to split the file in parts that can be independently reparsed. Then only reparse the part where the eit occurred.
-
How is the highlighting mechanism implemented? Do you have a file filled with predefined keywords that are matched to what the user types?
-
Its an interface, actually, with all the java keywords, and operators, created as static final strings. As the user types int the JTextPane, the text is inserted, and then the entire text is searched for occurences of the keywords, operators, comments, quotations, and so on... The only problem is that this process gets too slow once the file reaches larger sizes ( more than a couple of pages ). I thought of trying to make a separate thread to do the highlighting, but for some reason, it even slower then...
-
Are you searcing the whole text all the time? Or just the word that was just written? It can't take that much time to check the last word that was typed....
-
I don't see why it would be necessary to check the whole file everytime a keyword is entered.
Why don't you try opening the file for RandomAccess and position the file pointer to where a word is being typed or retyped?
-
The other suggestions are good ones. Color only the current word, or my favorite, only the visible area.
-
Granted, highlighting only the line affected by the change would be the ideal thing. However, what about quotes and multi-line comments? Those two parts of highlighting affect more than one line, and therefore you wouldn't be able to only check the line of the change. I would love to incorporate a system to only check the visible text, but, I can't figure out how to do that. If you know, please share...
-
If you go by the swing.text model, your document is already divided into areas of coloring: string literals, comments, keywords etc. A whole tree.
Any change would just need to check the current area for changes, if it ends it etc.