forked from cheptsov/AdvancedExpressionFolding
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
abeb157
commit e1917da
Showing
8 changed files
with
236 additions
and
6 deletions.
There are no files selected for viewing
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
32 changes: 32 additions & 0 deletions
32
resources/intentionDescriptions/AddDynamicMethodFoldingIntention/description.html
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>AJF2: Dynamic Method Folding</title> | ||
</head> | ||
<body> | ||
<h1>AJF2: Dynamic Method Folding</h1> | ||
<p>This intention allows you to dynamically fold methods in your code. With this intention, you can easily rename or remove method calls using a simple dialog interface.</p> | ||
|
||
<h2>Usage</h2> | ||
<ol> | ||
<li>Place the caret on the method call you wish to fold.</li> | ||
<li>Invoke the intention by pressing <strong>Alt+Enter</strong> and select <em>AJF2: Dynamic method folding</em>.</li> | ||
<li>A dialog will appear prompting you to choose an action:</li> | ||
<ul> | ||
<li><strong>Rename:</strong> Enter a new method name.</li> | ||
<li><strong>Remove:</strong> Remove the method call.</li> | ||
<li><strong>Cancel:</strong> Cancel the operation.</li> | ||
</ul> | ||
</ol> | ||
|
||
<h2>Dialog Options</h2> | ||
<p>When the dialog is shown, you can choose to rename the method, remove it, or cancel the operation. If you choose to rename the method, you must provide a new name for it. If you choose to remove the method, it will be removed from the configuration file.</p> | ||
|
||
<h2>Implementation Details</h2> | ||
<p>The implementation leverages the <code>DialogWrapper</code> class to create a custom dialog for method folding. The actions are defined in the <code>Action</code> enum, and the results are handled appropriately based on the user's selection.</p> | ||
|
||
<h2>Conclusion</h2> | ||
<p>With the AJF2: Dynamic Method Folding intention, managing method calls in your code becomes simpler and more efficient. You can easily rename or remove method calls without manually editing the configuration file.</p> | ||
</body> | ||
</html> |
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
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
81 changes: 81 additions & 0 deletions
81
...dvancedExpressionFolding/extension/methodcall/dynamic/AddDynamicMethodFoldingIntention.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.intellij.advancedExpressionFolding.extension.methodcall.dynamic | ||
|
||
import com.intellij.advancedExpressionFolding.FoldingService | ||
import com.intellij.advancedExpressionFolding.extension.methodcall.MethodCallFactory | ||
import com.intellij.advancedExpressionFolding.extension.methodcall.MethodName | ||
import com.intellij.codeInsight.folding.CodeFoldingManager | ||
import com.intellij.codeInsight.intention.IntentionAction | ||
import com.intellij.openapi.application.runInEdt | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.ui.Messages | ||
import com.intellij.psi.PsiFile | ||
import com.intellij.psi.PsiMethodCallExpression | ||
import com.intellij.psi.util.PsiTreeUtil | ||
import org.jetbrains.annotations.Nls | ||
|
||
class AddDynamicMethodFoldingIntention : IntentionAction { | ||
|
||
override fun getText(): @Nls(capitalization = Nls.Capitalization.Sentence) String = "AJF2: Dynamic method folding" | ||
|
||
override fun getFamilyName(): @Nls(capitalization = Nls.Capitalization.Sentence) String = "AJF2" | ||
|
||
override fun isAvailable(project: Project, editor: Editor, file: PsiFile): Boolean { | ||
val element = file.findElementAt(editor.caretModel.offset) | ||
val methodCall = PsiTreeUtil.getParentOfType(element, PsiMethodCallExpression::class.java) | ||
return methodCall != null | ||
} | ||
|
||
override fun invoke(project: Project, editor: Editor, file: PsiFile) { | ||
|
||
val element = file.findElementAt(editor.caretModel.offset) | ||
val methodCall = PsiTreeUtil.getParentOfType(element, PsiMethodCallExpression::class.java) | ||
|
||
methodCall?.methodExpression?.referenceName?.let { methodName -> | ||
when { | ||
methodName.exists() -> { | ||
val dialogResult = methodName.showRenameDialog() | ||
dialogResult?.run { | ||
val (action, newMethodName) = dialogResult | ||
when (action) { | ||
Action.RENAME -> { | ||
ConfigurationParser.addOrUpdateMethod(methodName, newMethodName) | ||
} | ||
Action.REMOVE -> methodName.remove() | ||
Action.CANCEL -> return | ||
} | ||
runInEdt { | ||
FoldingService.get().clearAllKeys(project) | ||
MethodCallFactory.refreshMethodCallMappings() | ||
CodeFoldingManager.getInstance(project).updateFoldRegions(editor) | ||
} | ||
} | ||
} | ||
else -> { | ||
val newName = methodName.getNewNameFromUser() ?: return | ||
ConfigurationParser.addOrUpdateMethod(methodName, newName) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun startInWriteAction() = true | ||
|
||
private fun MethodName.exists() = MethodCallFactory.findByMethodName(this)?.any { | ||
it is DynamicMethodCall | ||
} == true | ||
|
||
private fun MethodName.getNewNameFromUser(): String? { | ||
return Messages.showInputDialog( | ||
"Enter new method name:", | ||
"Add Dynamic Method Folding", | ||
Messages.getQuestionIcon(), | ||
this, | ||
null | ||
) | ||
} | ||
|
||
private fun MethodName.remove() = ConfigurationParser.remove(this) | ||
|
||
} | ||
|
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
65 changes: 65 additions & 0 deletions
65
src/com/intellij/advancedExpressionFolding/extension/methodcall/dynamic/DynamicDialog.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.intellij.advancedExpressionFolding.extension.methodcall.dynamic | ||
|
||
import com.intellij.advancedExpressionFolding.extension.methodcall.MethodName | ||
import com.intellij.openapi.ui.DialogWrapper | ||
import com.intellij.ui.components.JBTextField | ||
import com.intellij.ui.dsl.builder.AlignX | ||
import com.intellij.ui.dsl.builder.panel | ||
|
||
enum class Action { | ||
RENAME, REMOVE, CANCEL | ||
} | ||
|
||
fun MethodName.showRenameDialog(): Pair<Action, String>? { | ||
var selectedAction: Action? = null | ||
var newMethodName: String? = null | ||
|
||
val name = this | ||
val textField = JBTextField(name, 20) | ||
|
||
val dialogWrapper = object : DialogWrapper(true) { | ||
init { | ||
title = "Choose a new folding for method $name" | ||
init() | ||
} | ||
|
||
override fun createCenterPanel() = panel { | ||
row("") { | ||
cell(textField) | ||
.align(AlignX.LEFT) | ||
.focused() | ||
} | ||
row("") { | ||
button("Remove") { | ||
selectedAction = Action.REMOVE | ||
close(OK_EXIT_CODE) | ||
} | ||
} | ||
} | ||
|
||
override fun getPreferredFocusedComponent() = textField | ||
|
||
override fun doOKAction() { | ||
selectedAction = Action.RENAME | ||
newMethodName = textField.text | ||
super.doOKAction() | ||
} | ||
|
||
override fun doCancelAction() { | ||
selectedAction = Action.CANCEL | ||
super.doCancelAction() | ||
} | ||
} | ||
|
||
dialogWrapper.show() | ||
|
||
return when (selectedAction) { | ||
Action.RENAME -> newMethodName?.takeIf { | ||
it.isNotBlank() | ||
}?.let { | ||
Action.RENAME to it | ||
} | ||
Action.REMOVE -> Action.REMOVE to name | ||
else -> null | ||
} | ||
} |
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