Skip to content

Commit

Permalink
Add "Make private" and "Make public" action (#145)
Browse files Browse the repository at this point in the history
* Add "Make private" and "Make public" actions

* Add "Make private" and "Make public" intentions
  • Loading branch information
marius-h authored Aug 22, 2021
1 parent 4683e97 commit c2d59fc
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased]
### Added
- Open a package's pub.dev page directly from your pubspec.yaml (#121)
- Make a variable, function or class private or public via alt+enter (#107)
- Custom file templates for "New Flutter widget" menu
- Run `flutter pub get` from menu by right-clicking a file or a directory (#52)
### Changed
Expand Down
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)
}
}
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)
}
}
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
}
}

9 changes: 9 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@
id="PubChangelogProvider"
order="first, before YamlJsonSchemaDocumentationProvider"/>

<intentionAction order="first">
<className>de.mariushoefler.flutterenhancementsuite.intentions.MakePrivateIntention</className>
<category>Dart</category>
</intentionAction>
<intentionAction order="first">
<className>de.mariushoefler.flutterenhancementsuite.intentions.MakePublicIntention</className>
<category>Dart</category>
</intentionAction>

<annotator implementationClass="de.mariushoefler.flutterenhancementsuite.editor.UrlAnnotator"
order="first"
language="yaml"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
String _foo = 'bar';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
String foo = 'bar';
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
String foo = 'bar';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
String _foo = 'bar';
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>

0 comments on commit c2d59fc

Please sign in to comment.