Created
September 22, 2022 19:37
-
-
Save ShreyashKore/895128bb1a71c9717ab7875ad3682e0f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.intellij.openapi.diagnostic.Logger | |
import liveplugin.* | |
show("View Binding: ${project?.name}") | |
registerAction(id = "Replace ButterKnife", keyStroke = "ctrl shift H") { event: com.intellij.openapi.actionSystem.AnActionEvent -> | |
val project = event.project ?: return@registerAction | |
val editor = event.editor ?: return@registerAction | |
val logger = Logger.getInstance("HelloLogger") | |
// Document modifications must be done inside "commands" which will support undo/redo functionality. | |
editor.document.executeCommand(project, description = "Replace ButterKnife") { | |
var output = text | |
val viewBindingAnnotations = text.split("@BindView").drop(1) | |
val viewBindingData = viewBindingAnnotations.map { | |
ViewElement( | |
id = it.substringAfter("(") | |
.substringBefore(")") | |
.trim(), | |
initialVarName = it.substringAfter("var ") | |
.substringBefore(":") | |
.trim(), | |
) | |
} | |
val layoutId = getLayoutId(text) | |
if (layoutId == null) { | |
show("Unable to find ButterKnife!") | |
return@executeCommand | |
} | |
show(layoutId) | |
val layoutBinding = getBindingName(layoutId) | |
viewBindingData.forEach { | |
output = output.replace("@BindView[^:]*:.*".toRegex(), "") | |
.replace(it.initialVarName, "$B." + it.finalVarName) | |
} | |
output = output.replace("import butterknife.ButterKnife", "import com.invizio.diva.databinding.$layoutBinding") | |
output = output.replaceFirst("{", "{\n lateinit var $B : $layoutBinding") | |
output = output.replace("ButterKnife.bind(.*)".toRegex(), "$B = $layoutBinding.inflate(inflater)") | |
output = output.replace(".+inflater.inflate\\(.*".toRegex(), "") | |
output = output.replace(ON_CREATE_BLOCK_REGEX) { | |
it.value.replace("return(.|\\n)*?}".toRegex()) { | |
it.value.replace("return.*", "return $B.root") | |
} | |
} | |
println("VIEW IDS ******* \n${output}") | |
setText(output) | |
show("Butter knife is removed from ${project.currentFile?.name}") | |
} | |
} | |
"R.id.sales_quotation_recycler" | |
val ON_CREATE_BLOCK_REGEX = "onCreateView\\((.|\\n)*?return(.|\\n)*?}".toRegex() | |
//ButterKnife.bind(this, v) | |
val B = "b" | |
data class ViewElement( | |
val id : String, | |
val initialVarName: String | |
) { | |
val finalVarName: String = getViewBindingName(id) | |
} | |
fun getOldViewVarName(text : String) : String { | |
return "" | |
} | |
fun getOnCreateBlock(text : String) : String { | |
return ON_CREATE_BLOCK_REGEX.find(text)!!.value | |
} | |
/** | |
* Get name generated by view binding. | |
* @param id id of the view | |
*/ | |
fun getViewBindingName(id: String) : String { | |
var s = "" | |
var indexToBeCapitalized = 1000 | |
val idString = id.substringAfterLast(".") | |
for (i in idString.indices) { | |
if (idString[i] == '_') { | |
s += "" | |
indexToBeCapitalized = i + 1 | |
continue | |
} | |
if (i == indexToBeCapitalized) { | |
s += idString[i].uppercase() | |
continue | |
} | |
s += idString[i] | |
} | |
return s | |
} | |
fun getLayoutId(text: String) : String? { | |
return "\\.inflate\\(.*".toRegex() | |
.find(text)?.value | |
?.substringAfter("(") | |
?.substringBefore(",")?.trim() | |
} | |
fun getBindingName(id: String) : String { | |
return getViewBindingName(id).capitalize() + "Binding" | |
} | |
//@BindView\([A-z.]+\) | |
//fun isFragment() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment