-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "Make private" and "Make public" action (#145)
* Add "Make private" and "Make public" actions * Add "Make private" and "Make public" intentions
- Loading branch information
Showing
11 changed files
with
154 additions
and
0 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
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/de/mariushoefler/flutterenhancementsuite/intentions/MakePrivateIntention.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,19 @@ | ||
package de.mariushoefler.flutterenhancementsuite.intentions | ||
|
||
import com.intellij.psi.PsiElement | ||
|
||
/** | ||
* Makes selected class, function or variable private | ||
* | ||
* @author Marius Höfler | ||
* @since v1.6.0 | ||
*/ | ||
class MakePrivateIntention : AbstractModifyVisibilityIntentionAction() { | ||
override fun getText(): String = "Make private" | ||
|
||
override fun getModifiedName(element: PsiElement): String = PRIVATE_MODIFIER + element.text | ||
|
||
override fun isAvailable(element: PsiElement): Boolean { | ||
return !element.text.startsWith(PRIVATE_MODIFIER) | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/de/mariushoefler/flutterenhancementsuite/intentions/MakePublicIntention.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,19 @@ | ||
package de.mariushoefler.flutterenhancementsuite.intentions | ||
|
||
import com.intellij.psi.PsiElement | ||
|
||
/** | ||
* Makes selected class, function or variable public | ||
* | ||
* @author Marius Höfler | ||
* @since v1.6.0 | ||
*/ | ||
class MakePublicIntention : AbstractModifyVisibilityIntentionAction() { | ||
override fun getText(): String = "Make public" | ||
|
||
override fun getModifiedName(element: PsiElement): String = element.text.removePrefix(PRIVATE_MODIFIER) | ||
|
||
override fun isAvailable(element: PsiElement): Boolean { | ||
return element.text.startsWith(PRIVATE_MODIFIER) | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
.../kotlin/de/mariushoefler/flutterenhancementsuite/intentions/ModifyVisibilityInspection.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,92 @@ | ||
package de.mariushoefler.flutterenhancementsuite.intentions | ||
|
||
import com.intellij.CommonBundle | ||
import com.intellij.codeInsight.FileModificationService | ||
import com.intellij.codeInsight.intention.IntentionAction | ||
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction | ||
import com.intellij.openapi.application.WriteAction | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.ui.Messages | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.refactoring.util.CommonRefactoringUtil | ||
import com.jetbrains.lang.dart.assists.AssistUtils | ||
import com.jetbrains.lang.dart.assists.DartSourceEditException | ||
import com.jetbrains.lang.dart.ide.refactoring.ServerRenameRefactoring | ||
import com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus | ||
import com.jetbrains.lang.dart.psi.DartComponentName | ||
import com.jetbrains.lang.dart.psi.DartReferenceExpression | ||
import com.jetbrains.lang.dart.psi.DartSimpleType | ||
import com.jetbrains.lang.dart.psi.DartType | ||
|
||
const val PRIVATE_MODIFIER = "_" | ||
|
||
/** | ||
* Provides quick fixes for modifying the visibility of classes, functions and variables | ||
* | ||
* @author Marius Höfler | ||
* @since v1.6.0 | ||
*/ | ||
abstract class AbstractModifyVisibilityIntentionAction : PsiElementBaseIntentionAction(), IntentionAction { | ||
override fun getFamilyName(): String = text | ||
|
||
override fun isAvailable(project: Project, editor: Editor?, element: PsiElement): Boolean { | ||
val parent = element.parent?.parent | ||
val isReference = | ||
parent is DartReferenceExpression && parent.parent !is DartType && parent.parent !is DartSimpleType | ||
return isAvailable(element) && (parent is DartComponentName || isReference) | ||
} | ||
|
||
override fun startInWriteAction() = true | ||
|
||
override fun invoke(project: Project, editor: Editor?, element: PsiElement) { | ||
val virtualFile = element.containingFile?.virtualFile | ||
if (virtualFile == null || !FileModificationService.getInstance() | ||
.preparePsiElementForWrite(element) | ||
) return | ||
|
||
// Create the refactoring. | ||
val refactoring = ServerRenameRefactoring(project, virtualFile, element.textOffset, 0) | ||
// Validate initial status. | ||
val initialStatus: RefactoringStatus = refactoring.checkInitialConditions() ?: return | ||
if (initialStatus.hasError()) { | ||
initialStatus.message?.let { | ||
CommonRefactoringUtil.showErrorHint(project, editor, it, CommonBundle.getErrorTitle(), null) | ||
} | ||
return | ||
} | ||
refactoring.setNewName(getModifiedName(element)) | ||
doRenameRefactoring(project, refactoring, refactoring.potentialEdits)?.let { error -> | ||
Messages.showErrorDialog(project, error, CommonBundle.getErrorTitle()) | ||
} | ||
} | ||
|
||
protected abstract fun getModifiedName(element: PsiElement): String | ||
|
||
protected abstract fun isAvailable(element: PsiElement): Boolean | ||
|
||
private fun doRenameRefactoring( | ||
project: Project, | ||
refactoring: ServerRenameRefactoring, | ||
excludedIds: Set<String> | ||
): String? { | ||
refactoring.checkFinalConditions()?.let { finalStatus -> | ||
if (finalStatus.hasError()) { | ||
return finalStatus.message | ||
} | ||
|
||
refactoring.change?.let { change -> | ||
return WriteAction.compute<String?, RuntimeException> { | ||
try { | ||
AssistUtils.applySourceChange(project, change, false, excludedIds) | ||
} catch (e: DartSourceEditException) { | ||
return@compute e.message | ||
} | ||
null | ||
} | ||
} | ||
} | ||
return 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
1 change: 1 addition & 0 deletions
1
src/main/resources/intentionDescriptions/MakePrivateIntention/after.dart.template
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 @@ | ||
String _foo = 'bar'; |
1 change: 1 addition & 0 deletions
1
src/main/resources/intentionDescriptions/MakePrivateIntention/before.dart.template
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 @@ | ||
String foo = 'bar'; |
5 changes: 5 additions & 0 deletions
5
src/main/resources/intentionDescriptions/MakePrivateIntention/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,5 @@ | ||
<html> | ||
<body> | ||
<p>Makes selected class, function or variable private</p> | ||
</body> | ||
</html> |
1 change: 1 addition & 0 deletions
1
src/main/resources/intentionDescriptions/MakePublicIntention/after.dart.template
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 @@ | ||
String foo = 'bar'; |
1 change: 1 addition & 0 deletions
1
src/main/resources/intentionDescriptions/MakePublicIntention/before.dart.template
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 @@ | ||
String _foo = 'bar'; |
5 changes: 5 additions & 0 deletions
5
src/main/resources/intentionDescriptions/MakePublicIntention/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,5 @@ | ||
<html> | ||
<body> | ||
<p>Makes selected class, function or variable public</p> | ||
</body> | ||
</html> |