Replace item in list kotlin
Tutorial: Structural search and replace in KotlinStructural search and replace is a powerful tool that can search for a specific pattern in code and replace it in an automated way. Show In this tutorial we will get acquainted with templates and filters, modify a predefined template, and then create a code inspection based on it. The functionality covered in this tutorial is by no means the exhaustive list of what Structural Search and Replace can do. Our goal is to get you started. After that, you can explore various templates, filters, and options and combine them to create your own specific searches. For the tutorial, we will use the following code: class Point constructor(locationX: Int, locationY: Int) { val x: Int = locationX val y: Int = locationY } open class OpenClass { open val openProperty = 0 open fun display() { println("Some text") print("More text") print(0) } } Open the Search Structurally dialog
Use an existing template (property declaration)
Rerun
Now, let's return to our structural search dialog to alter the predefined template a bit. Alter a predefined template
As you can see, the search used the filter this time and only shows the open properties now. With Structural Search, we can run some interesting searches. Let's choose another existing template (Method calls) as our example. Assume we're doing this search to replace all print statements with logging calls and exclude all calls that do not have a String as the argument. Replace function calls
We can save this template for later use if we need. Save template
The template is saved under the User Defined node in the Existing Templates dialog. We can also use our template as an inspection, so when we come across the same code, we'll see a warning and can quickly replace the code. Create a custom inspection
The newly created inspection appears under the Structural search node in Settings/Preferences | Editor | Inspections. To quickly run an inspection, select Code | Analyze Code | Run Inspection by Name... from the main menu or press Ctrl+Alt+Shift+I) and enter the name of the inspection. Last modified: 21 November 2021 Tutorial: Test-driven development with Kotlin Run/Debug Configuration: Kotlin
|