[java] renaming modules

This commit is contained in:
Roman Shevchenko
2016-09-22 14:39:30 +03:00
parent 39c2766d17
commit 6c758313b5
2 changed files with 67 additions and 0 deletions
@@ -26,6 +26,7 @@ import com.intellij.psi.impl.file.impl.JavaFileManager;
import com.intellij.psi.impl.source.resolve.ResolveCache;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.ArrayUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
@@ -53,6 +54,14 @@ public class PsiJavaModuleReference extends PsiReferenceBase.Poly<PsiJavaModuleR
return ResolveCache.getInstance(getProject()).resolveWithCaching(this, Resolver.INSTANCE, false, incompleteCode);
}
@Override
public PsiElement handleElementRename(@NotNull String newName) throws IncorrectOperationException {
PsiJavaModuleReferenceElement element = getElement();
PsiElementFactory factory = PsiElementFactory.SERVICE.getInstance(element.getProject());
PsiJavaModuleReferenceElement newElement = factory.createModuleFromText("module " + newName + " {}").getNameElement();
return element.replace(newElement);
}
private Project getProject() {
return getElement().getProject();
}
@@ -0,0 +1,58 @@
/*
* Copyright 2000-2016 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 com.intellij.codeInsight.daemon
import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.components.ServiceManager
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.impl.file.impl.JavaFileManager
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.refactoring.rename.RenameProcessor
import com.intellij.testFramework.LightProjectDescriptor
import com.intellij.testFramework.VfsTestUtil
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
import com.intellij.testFramework.fixtures.MultiModuleJava9ProjectDescriptor
import com.intellij.testFramework.fixtures.MultiModuleJava9ProjectDescriptor.ModuleDescriptor
import com.intellij.testFramework.fixtures.MultiModuleJava9ProjectDescriptor.ModuleDescriptor.M2
import com.intellij.testFramework.fixtures.MultiModuleJava9ProjectDescriptor.ModuleDescriptor.MAIN
import org.junit.Test
class ModuleRenameTest : LightCodeInsightFixtureTestCase() {
override fun getProjectDescriptor(): LightProjectDescriptor = MultiModuleJava9ProjectDescriptor
override fun setUp() {
super.setUp()
addFile("module-info.java", "module M2 { }", M2)
}
override fun tearDown() {
MultiModuleJava9ProjectDescriptor.cleanupSourceRoots()
super.tearDown()
}
@Test fun testRename() {
myFixture.configureByText("module-info.java", "module M { requires M2; }")
val service = ServiceManager.getService(project, JavaFileManager::class.java)
val module = service.findModules("M2", GlobalSearchScope.allScope(project)).first()
runWriteAction {
RenameProcessor(project, module, "M2.bis", false, false).run()
PsiDocumentManager.getInstance(project).commitAllDocuments()
}
myFixture.checkResult("module M { requires M2.bis; }")
}
private fun addFile(path: String, text: String, module: ModuleDescriptor = MAIN) = VfsTestUtil.createFile(module.root(), path, text)
}