Android CodeView: Auto Intending, Find and replace
Tags: android, java, kotlin
Published: 2022-01-09 21:13:57

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](https://github.com/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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oqoi8exa80uhgttw26pj.png)

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 `{`.

```java
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 `}`.

```java
Set<Character> indentationEnds = new HashSet<>(); indentationEnds.add('}'); codeView.setIndentationEnds(indentationEnds);
```

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

```java
codeView.setTabLength(4);
codeView.setEnableAutoIndentation(true);
```

You can easily change the highlighting color

```java
codeView.setMatchingHighlightColor(color);
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5229wr637a3in0u2sute.gif)

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.

```java
List<Token> tokens = codeView.findMatches(regex);
```

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

```java
Token token = codeView.findNextMatch();
```

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

```java
Token token = codeView.findPrevMatch();
```

To Clear all matching highlighted token.

```java
codeView.clearMatches();
```

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

```java
codeView.replaceFirstMatch(regex, replacement);
```

To Replace all strings that match regex with other string.

```java
codeView.replaceAllMatches(regex, replacement);
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mf6ifnur557ad801eg5q.gif)

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

Enjoy Programming 😋.