IDEA-144300 Copy Reference for source folder/content root copies empty string

CPP-4315 "Edit | Copy Reference" result doesn't contain the file name
This commit is contained in:
Anton Makeev
2015-08-25 18:36:16 +02:00
parent bcfbe7bb4a
commit f226b597e4
3 changed files with 149 additions and 12 deletions
@@ -0,0 +1,127 @@
/*
* Copyright 2000-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 com.intellij.codeInsight
import com.intellij.ide.actions.CopyReferenceAction
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.psi.PsiManager
import com.intellij.testFramework.PsiTestUtil
public class CopyReferenceActionFilesTest extends CodeInsightTestCase {
VirtualFile additionalRoot
@Override
protected void setUp() throws Exception {
super.setUp();
ApplicationManager.application.runWriteAction(new Runnable() {
@Override
void run() {
additionalRoot = VirtualFileManager.getInstance().findFileByUrl("temp:///").createChildDirectory(this, "newRoot");
}
})
}
@Override
protected void tearDown() throws Exception {
try {
ApplicationManager.application.runWriteAction(new Runnable() {
@Override
void run() {
additionalRoot.delete(this)
}
})
}
finally {
super.tearDown();
}
}
public void testCopyFile_RegisteredAsSourceRoots_ShouldContainItsName() throws Exception {
// CPP-4315 "Edit | Copy Reference" result doesn't contain the file name
VirtualFile dir
VirtualFile dir_subfile
VirtualFile file
ApplicationManager.application.runWriteAction(new Runnable() {
@Override
void run() {
dir = additionalRoot.createChildDirectory(this, "dir");
dir_subfile = dir.createChildData(this, "dir_subfile.txt");
file = additionalRoot.createChildData(this, "file.txt");
PsiTestUtil.addContentRoot(getModule(), additionalRoot);
PsiTestUtil.addSourceRoot(getModule(), dir);
PsiTestUtil.addSourceRoot(getModule(), file);
}
})
assertEquals("dir", CopyReferenceAction.elementToFqn(PsiManager.getInstance(project).findDirectory(dir)));
assertEquals("dir_subfile.txt", CopyReferenceAction.elementToFqn(PsiManager.getInstance(project).findFile(dir_subfile)));
assertEquals("file.txt", CopyReferenceAction.elementToFqn(PsiManager.getInstance(project).findFile(file)));
}
public void testCopyFile_RegisteredAsContentRoot_ShouldContainItsFullPath() throws Exception {
// IDEA-144300 Copy Reference for source folder/content root copies empty string
VirtualFile dir
VirtualFile dir_subfile
VirtualFile file
ApplicationManager.application.runWriteAction(new Runnable() {
@Override
void run() {
dir = additionalRoot.createChildDirectory(this, "dir");
dir_subfile = dir.createChildData(this, "dir_subfile.txt");
file = additionalRoot.createChildData(this, "file.txt");
PsiTestUtil.addContentRoot(getModule(), dir);
PsiTestUtil.addContentRoot(getModule(), file);
}
})
assertEquals(dir.getPath(), CopyReferenceAction.elementToFqn(PsiManager.getInstance(project).findDirectory(dir)));
assertEquals("dir_subfile.txt", CopyReferenceAction.elementToFqn(PsiManager.getInstance(project).findFile(dir_subfile)));
assertEquals(file.getPath(), CopyReferenceAction.elementToFqn(PsiManager.getInstance(project).findFile(file)));
}
public void testCopyFile_RegisteredAsNestedContentRoot_ShouldContainPathFromOuterMostRoot() throws Exception {
// IDEA-144300 Copy Reference for source folder/content root copies empty string
VirtualFile dir
VirtualFile dir_dir
VirtualFile dir_dir_file
ApplicationManager.application.runWriteAction(new Runnable() {
@Override
void run() {
dir = additionalRoot.createChildDirectory(this, "dir");
dir_dir = dir.createChildDirectory(this, "dir_dir");
dir_dir_file = dir_dir.createChildData(this, "file.txt");
PsiTestUtil.addContentRoot(getModule(), dir);
PsiTestUtil.addContentRoot(getModule(), dir_dir);
}
})
assertEquals(dir.getPath(), CopyReferenceAction.elementToFqn(PsiManager.getInstance(project).findDirectory(dir)));
assertEquals("dir_dir", CopyReferenceAction.elementToFqn(PsiManager.getInstance(project).findDirectory(dir_dir)));
assertEquals("dir_dir/file.txt", CopyReferenceAction.elementToFqn(PsiManager.getInstance(project).findFile(dir_dir_file)));
}
}
@@ -1,14 +1,14 @@
package com.intellij.codeInsight
import com.intellij.JavaTestUtil;
import com.intellij.ide.actions.CopyReferenceAction;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.IdeActions;
import com.intellij.psi.PsiFile;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
import com.intellij.JavaTestUtil
import com.intellij.ide.actions.CopyReferenceAction
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.IdeActions
import com.intellij.psi.PsiFile
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
import org.jetbrains.annotations.NonNls
public class CopyReferenceTest extends LightCodeInsightFixtureTestCase {
public class CopyReferenceActionTest extends LightCodeInsightFixtureTestCase {
@NonNls private static final String BASE_PATH = "/codeInsight/copyReference";
protected int oldSetting;
@@ -35,6 +35,7 @@ import com.intellij.openapi.ide.CopyPasteManager;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
@@ -282,14 +283,23 @@ public class CopyReferenceAction extends DumbAwareAction {
private static String getVirtualFileFqn(@NotNull VirtualFile virtualFile, @NotNull Project project) {
final LogicalRoot logicalRoot = LogicalRootsManager.getLogicalRootsManager(project).findLogicalRoot(virtualFile);
if (logicalRoot != null && logicalRoot.getVirtualFile() != null) {
return ObjectUtils.assertNotNull(VfsUtilCore.getRelativePath(virtualFile, logicalRoot.getVirtualFile(), '/'));
VirtualFile logicalRootFile = logicalRoot != null ? logicalRoot.getVirtualFile() : null;
if (logicalRootFile != null && !virtualFile.equals(logicalRootFile)) {
return ObjectUtils.assertNotNull(VfsUtilCore.getRelativePath(virtualFile, logicalRootFile, '/'));
}
final VirtualFile contentRoot = ProjectRootManager.getInstance(project).getFileIndex().getContentRootForFile(virtualFile);
if (contentRoot != null) {
return ObjectUtils.assertNotNull(VfsUtilCore.getRelativePath(virtualFile, contentRoot, '/'));
VirtualFile outerMostRoot = null;
VirtualFile each = virtualFile;
ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();
while (each != null && (each = index.getContentRootForFile(each)) != null) {
outerMostRoot = each;
each = each.getParent();
}
if (outerMostRoot != null && !outerMostRoot.equals(virtualFile)) {
return ObjectUtils.assertNotNull(VfsUtilCore.getRelativePath(virtualFile, outerMostRoot, '/'));
}
return virtualFile.getPath();
}
}