DEV Community

Cover image for Android CodeView: Auto Intending, Find and replace
Amr Hesham
Amr Hesham

Posted on

Android CodeView: Auto Intending, Find and replace

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.

Image description

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);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Step 3: set the tab length and enable the auto indenting.

codeView.setTabLength(4);
codeView.setEnableAutoIndentation(true);
Enter fullscreen mode Exit fullscreen mode

You can easily change the highlighting color

codeView.setMatchingHighlightColor(color);
Enter fullscreen mode Exit fullscreen mode

Image description

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);
Enter fullscreen mode Exit fullscreen mode

To Find and highlight the next matching token, returns null if not found.

Token token = codeView.findNextMatch();
Enter fullscreen mode Exit fullscreen mode

To Find and highlight the previous matching token, returns null if not found.

Token token = codeView.findPrevMatch();
Enter fullscreen mode Exit fullscreen mode

To Clear all matching highlighted token.

codeView.clearMatches();
Enter fullscreen mode Exit fullscreen mode

To Replace the first string that matches regex with other string.

codeView.replaceFirstMatch(regex, replacement);
Enter fullscreen mode Exit fullscreen mode

To Replace all strings that match regex with other string.

codeView.replaceAllMatches(regex, replacement);
Enter fullscreen mode Exit fullscreen mode

Image description

In the example app on GitHub, you will find dialog that use this feature to support find and replacement

Enjoy Programming 😋.

Top comments (0)