introduce quickfix for change visiblity from private to internal for private top-level declarations

This commit is contained in:
Michael Nedzelsky
2015-09-02 15:35:43 +03:00
parent 93776b711e
commit 9f5bbf94d3
19 changed files with 161 additions and 0 deletions
@@ -0,0 +1,44 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.quickfix
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.JetFile
import org.jetbrains.kotlin.psi.JetModifierListOwner
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
public class ChangePrivateTopLevelToInternalFix(element: JetModifierListOwner, private val elementName: String) : JetIntentionAction<JetModifierListOwner>(element) {
override fun getText() = "Make $elementName internal"
override fun getFamilyName() = "Make top-level declaration internal"
override fun invoke(project: Project, editor: Editor?, file: JetFile) {
element.addModifier(JetTokens.INTERNAL_KEYWORD)
}
companion object : JetSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val descriptor = Errors.ACCESS_TO_PRIVATE_TOP_LEVEL_FROM_ANOTHER_FILE.cast(diagnostic).a
val declaration = DescriptorToSourceUtils.getSourceFromDescriptor(descriptor) as? JetModifierListOwner ?: return null
return ChangePrivateTopLevelToInternalFix(declaration, descriptor.name.asString())
}
}
}
@@ -64,6 +64,8 @@ public class QuickFixRegistrar : QuickFixContributor {
ABSTRACT_MEMBER_NOT_IMPLEMENTED.registerFactory(addAbstractModifierFactory)
MANY_IMPL_MEMBER_NOT_IMPLEMENTED.registerFactory(addAbstractModifierFactory)
ACCESS_TO_PRIVATE_TOP_LEVEL_FROM_ANOTHER_FILE.registerFactory(ChangePrivateTopLevelToInternalFix)
val removeFinalModifierFactory = RemoveModifierFix.createRemoveModifierFromListOwnerFactory(FINAL_KEYWORD)
val addAbstractToClassFactory = AddModifierFix.createFactory(ABSTRACT_KEYWORD, javaClass<JetClass>())
ABSTRACT_PROPERTY_IN_NON_ABSTRACT_CLASS.registerFactory(removeAbstractModifierFactory, addAbstractToClassFactory)
@@ -0,0 +1,3 @@
package test
internal fun f(): Int = 10
@@ -0,0 +1,7 @@
// "Make f internal" "true"
package test
fun foo() {
val x = f()
}
@@ -0,0 +1,3 @@
package test
private fun f(): Int = 10
@@ -0,0 +1,7 @@
// "Make f internal" "true"
package test
fun foo() {
val x = <caret>f()
}
@@ -0,0 +1,3 @@
package test
internal val prop: Int = 10
@@ -0,0 +1,7 @@
// "Make prop internal" "true"
package test
fun foo() {
val x = prop
}
@@ -0,0 +1,3 @@
package test
private val prop: Int = 10
@@ -0,0 +1,7 @@
// "Make prop internal" "true"
package test
fun foo() {
val x = <caret>prop
}
@@ -0,0 +1,3 @@
package test
internal var prop: Int = 10
@@ -0,0 +1,7 @@
// "Make prop internal" "true"
package test
fun foo() {
prop = 20
}
@@ -0,0 +1,3 @@
package test
private var prop: Int = 10
@@ -0,0 +1,7 @@
// "Make prop internal" "true"
package test
fun foo() {
<caret>prop = 20
}
@@ -0,0 +1,4 @@
package test
var prop: Int = 10
internal set(value: Int) {}
@@ -0,0 +1,7 @@
// "Make <set-prop> internal" "true"
package test
fun foo() {
prop = 20
}
@@ -0,0 +1,4 @@
package test
var prop: Int = 10
private set(value: Int) {}
@@ -0,0 +1,7 @@
// "Make <set-prop> internal" "true"
package test
fun foo() {
<caret>prop = 20
}
@@ -1239,6 +1239,39 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
}
@TestMetadata("idea/testData/quickfix/privateInFiles")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class PrivateInFiles extends AbstractQuickFixMultiFileTest {
public void testAllFilesPresentInPrivateInFiles() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/privateInFiles"), Pattern.compile("^(\\w+)\\.before\\.Main\\.kt$"), true);
}
@TestMetadata("privateTopLevelFunInFile.before.Main.kt")
public void testPrivateTopLevelFunInFile() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/privateInFiles/privateTopLevelFunInFile.before.Main.kt");
doTestWithExtraFile(fileName);
}
@TestMetadata("privateTopLevelValInFile.before.Main.kt")
public void testPrivateTopLevelValInFile() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/privateInFiles/privateTopLevelValInFile.before.Main.kt");
doTestWithExtraFile(fileName);
}
@TestMetadata("privateTopLevelVarInFile.before.Main.kt")
public void testPrivateTopLevelVarInFile() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/privateInFiles/privateTopLevelVarInFile.before.Main.kt");
doTestWithExtraFile(fileName);
}
@TestMetadata("privateTopLevelVarWithSetterInFile.before.Main.kt")
public void testPrivateTopLevelVarWithSetterInFile() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/privateInFiles/privateTopLevelVarWithSetterInFile.before.Main.kt");
doTestWithExtraFile(fileName);
}
}
@TestMetadata("idea/testData/quickfix/suppress")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)