Amr Hesham
Posted on January 9, 2022
Hello everyone, In this article, I will talk about two features from the newest version of the CodeView library which are support auto-indentation, and adding support for finding matching and replacing keywords.
Note that you can find full documentation about how to install and use CodeView in the AmrDeveloper/CodeView official Repository.
So first what is Auto Indentation?
Auto indentation helps you to set the level of indentation when you type new text, indentation level will automatically increase or decrease by the tab length for example by 4 when you insert new lines in the end or in the middle of the text.
For example in the modern IDE when you write java when you start writing new multi-lines block with { the indentation level will increase by tab length for example 4 and once you end this block with } the indentation will decrease.
To support this feature in CodeView you can do it only in 3 steps.
Step 1: set the indentations start characters, so when the user type those characters the indentation level should increase for example {
.
Set<Character> indentationStart = new HashSet<>(); indentationStart.add('{'); codeView.setIndentationStarts(indentationStart);
Step 2: set the indentations end characters, so when the user type those characters the indentation level should decrease for example }
.
Set<Character> indentationEnds = new HashSet<>(); indentationEnds.add('}'); codeView.setIndentationEnds(indentationEnds);
Step 3: set the tab length and enable the auto indenting.
codeView.setTabLength(4);
codeView.setEnableAutoIndentation(true);
You can easily change the highlighting color
codeView.setMatchingHighlightColor(color);
Now we have done :D, Enjoying Auto indenting.
The second feature is inspired by a Feature suggestion from a user of CodeView, which is the ability to highlight and get the matched substring and also replace it with another text easily, this feature you can also see in the modern Code Editors and IDE’s like Vs Code, Android Studio…etc.
So now CodeView provide some methods to help you implement find and replace dialog.
To get all the matched substrings from the text you can use findMatches.
List<Token> tokens = codeView.findMatches(regex);
To Find and highlight the next matching token, returns null if not found.
Token token = codeView.findNextMatch();
To Find and highlight the previous matching token, returns null if not found.
Token token = codeView.findPrevMatch();
To Clear all matching highlighted token.
codeView.clearMatches();
To Replace the first string that matches regex with other string.
codeView.replaceFirstMatch(regex, replacement);
To Replace all strings that match regex with other string.
codeView.replaceAllMatches(regex, replacement);
In the example app on GitHub, you will find dialog that use this feature to support find and replacement
Enjoy Programming 😋.
Posted on January 9, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.