@NotNull @Override public String getUniqueVirtualFilePath(Project project, VirtualFile file) { final Collection<VirtualFile> filesWithSameName = FilenameIndex.getVirtualFilesByName(project, file.getName(), ProjectScope.getProjectScope(project)); if (filesWithSameName.size() > 1 && filesWithSameName.contains(file)) { String path = project.getBasePath(); path = path == null ? "" : FileUtil.toSystemIndependentName(path); UniqueNameBuilder<VirtualFile> builder = new UniqueNameBuilder<VirtualFile>(path, File.separator, 25); for (VirtualFile virtualFile: filesWithSameName) { builder.addPath(virtualFile, virtualFile.getPath()); } return builder.getShortPath(file); } return file.getName(); }
protected RecentProjectItemRenderer(UniqueNameBuilder<ReopenProjectAction> pathShortener) { super(new VerticalFlowLayout()); myShortener = pathShortener; myPath.setFont(JBUI.Fonts.label(SystemInfo.isMac ? 10f : 11f)); setFocusable(true); layoutComponents(); }
public void testRoot() { UniqueNameBuilder<String> builder = new UniqueNameBuilder<String>("/Users/yole/idea", "/", 100); builder.addPath("A", "/Users/yole/idea/build/scripts/layouts.gant"); builder.addPath("B", "/Users/yole/idea/community/build/scripts/layouts.gant"); assertEquals("build/\u2026/layouts.gant", builder.getShortPath("A")); assertEquals("community/\u2026/layouts.gant", builder.getShortPath("B")); builder = new UniqueNameBuilder<String>("", "/", 100); builder.addPath("A", "build/scripts/layouts.gant"); builder.addPath("B", "community/build/scripts/layouts.gant"); assertEquals("build/\u2026/layouts.gant", builder.getShortPath("A")); assertEquals("community/\u2026/layouts.gant", builder.getShortPath("B")); }
public void testShortenNames() { UniqueNameBuilder<String> builder = new UniqueNameBuilder<String>("/Users/yole/idea", "/", 25); builder.addPath("A", "/Users/yole/idea/build/scripts/layouts.gant"); builder.addPath("B", "/Users/yole/idea/community/build/scripts/layouts.gant"); assertEquals("build/\u2026/layouts.gant", builder.getShortPath("A")); assertEquals("community/\u2026/layouts.gant", builder.getShortPath("B")); }
public void testShortenNamesUnique() { UniqueNameBuilder<String> builder = new UniqueNameBuilder<String>("/Users/yole/idea", "/", 25); builder.addPath("A", "/Users/yole/idea/pycharm/download/index.html"); builder.addPath("B", "/Users/yole/idea/pycharm/documentation/index.html"); builder.addPath("C", "/Users/yole/idea/fabrique/download/index.html"); assertEquals("pycharm/\u2026/index.html", builder.getShortPath("A")); }
public void testShortenNamesUnique2() { UniqueNameBuilder<String> builder = new UniqueNameBuilder<String>("/Users/yole/idea", "/", 25); builder.addPath("A", "source/components/views/something/tmpl/default.php"); builder.addPath("B", "source/components/views/something_else/tmpl/default.php"); assertEquals("something/\u2026/default.php", builder.getShortPath("A")); assertEquals("something_else/\u2026/default.php", builder.getShortPath("B")); }
public void testFilesWithoutExtensions() { UniqueNameBuilder<String> builder = new UniqueNameBuilder<String>("", "/", 25); builder.addPath("A", "foo/.htaccess"); builder.addPath("B", "bar/.htaccess"); assertEquals("foo/.htaccess", UniqueVFilePathBuilderImpl.getEditorTabText("A", builder, false)); assertEquals("foo/.htaccess", UniqueVFilePathBuilderImpl.getEditorTabText("A", builder, true)); }
@Nullable private static UniqueNameBuilder<VirtualFile> filesWithTheSameName(String fileName, Project project, boolean skipNonOpenedFiles, GlobalSearchScope scope) { Collection<VirtualFile> filesWithSameName = skipNonOpenedFiles ? Collections.<VirtualFile>emptySet() : FilenameIndex.getVirtualFilesByName(project, fileName, scope); THashSet<VirtualFile> setOfFilesWithTheSameName = new THashSet<VirtualFile>(filesWithSameName); // add open files out of project scope for(VirtualFile openFile: FileEditorManager.getInstance(project).getOpenFiles()) { if (openFile.getName().equals(fileName)) { setOfFilesWithTheSameName.add(openFile); } } for (VirtualFile recentlyEditedFile : EditorHistoryManager.getInstance(project).getFiles()) { if (recentlyEditedFile.getName().equals(fileName)) { setOfFilesWithTheSameName.add(recentlyEditedFile); } } filesWithSameName = setOfFilesWithTheSameName; if (filesWithSameName.size() > 1) { String path = project.getBasePath(); path = path == null ? "" : FileUtil.toSystemIndependentName(path); UniqueNameBuilder<VirtualFile> builder = new UniqueNameBuilder<VirtualFile>(path, File.separator, 25); for (VirtualFile virtualFile: filesWithSameName) { builder.addPath(virtualFile, virtualFile.getPath()); } return builder; } return null; }
public static <T> String getEditorTabText(T key, UniqueNameBuilder<T> builder, boolean hideKnownExtensionInTabs) { String result = builder.getShortPath(key); if (hideKnownExtensionInTabs) { String withoutExtension = FileUtil.getNameWithoutExtension(result); if (StringUtil.isNotEmpty(withoutExtension) && !withoutExtension.endsWith(builder.getSeparator())) { return withoutExtension; } } return result; }
@Nullable private static UniqueNameBuilder<VirtualFile> filesWithTheSameName(String fileName, Project project, boolean skipNonOpenedFiles, GlobalSearchScope scope) { Collection<VirtualFile> filesWithSameName = skipNonOpenedFiles ? Collections.emptySet() : FilenameIndex.getVirtualFilesByName(project, fileName, scope); THashSet<VirtualFile> setOfFilesWithTheSameName = new THashSet<>(filesWithSameName); // add open files out of project scope for(VirtualFile openFile: FileEditorManager.getInstance(project).getOpenFiles()) { if (openFile.getName().equals(fileName)) { setOfFilesWithTheSameName.add(openFile); } } if (!skipNonOpenedFiles) { for (VirtualFile recentlyEditedFile : EditorHistoryManager.getInstance(project).getFiles()) { if (recentlyEditedFile.getName().equals(fileName)) { setOfFilesWithTheSameName.add(recentlyEditedFile); } } } filesWithSameName = setOfFilesWithTheSameName; if (filesWithSameName.size() > 1) { String path = project.getBasePath(); path = path == null ? "" : FileUtil.toSystemIndependentName(path); UniqueNameBuilder<VirtualFile> builder = new UniqueNameBuilder<>(path, File.separator, 25); for (VirtualFile virtualFile: filesWithSameName) { builder.addPath(virtualFile, virtualFile.getPath()); } return builder; } return null; }
protected ListCellRenderer createRenderer(UniqueNameBuilder<ReopenProjectAction> pathShortener) { return new RecentProjectItemRenderer(pathShortener); }
public void testSimple() { UniqueNameBuilder<String> builder = new UniqueNameBuilder<String>("", "/", 100); builder.addPath("A", "/Users/yole/idea/foo/bar.java"); builder.addPath("B", "/Users/yole/idea/baz/bar.java"); assertEquals("foo/bar.java", builder.getShortPath("A")); }
public void testTwoLevel() { UniqueNameBuilder<String> builder = new UniqueNameBuilder<String>("", "/", 100); builder.addPath("A", "/Users/yole/idea/foo/buy/index.html"); builder.addPath("B", "/Users/yole/idea/bar/buy/index.html"); assertEquals("foo/\u2026/index.html", builder.getShortPath("A")); }
public void testSeparator() { UniqueNameBuilder<String> builder = new UniqueNameBuilder<String>("", "\\", 100); builder.addPath("A", "/Users/yole/idea/foo/buy/index.html"); builder.addPath("B", "/Users/yole/idea/bar/buy/index.html"); assertEquals("foo\\\u2026\\index.html", builder.getShortPath("A")); }
private static String getUniqueVirtualFilePath(final Project project, VirtualFile file, final boolean skipNonOpenedFiles) { Key<CachedValue<ConcurrentMap<String, UniqueNameBuilder<VirtualFile>>>> key = skipNonOpenedFiles ? ourShortNameOpenedBuilderCacheKey:ourShortNameBuilderCacheKey; CachedValue<ConcurrentMap<String, UniqueNameBuilder<VirtualFile>>> data = project.getUserData(key); if (data == null) { project.putUserData(key, data = CachedValuesManager.getManager(project).createCachedValue( new CachedValueProvider<ConcurrentMap<String, UniqueNameBuilder<VirtualFile>>>() { @Nullable @Override public Result<ConcurrentMap<String, UniqueNameBuilder<VirtualFile>>> compute() { return new Result<ConcurrentMap<String, UniqueNameBuilder<VirtualFile>>>( ContainerUtil.<String, UniqueNameBuilder<VirtualFile>>createConcurrentSoftValueMap(), PsiModificationTracker.MODIFICATION_COUNT, //ProjectRootModificationTracker.getInstance(project), //VirtualFileManager.VFS_STRUCTURE_MODIFICATIONS, FileEditorManagerImpl.OPEN_FILE_SET_MODIFICATION_COUNT ); } }, false)); } final ConcurrentMap<String, UniqueNameBuilder<VirtualFile>> valueMap = data.getValue(); final String fileName = file.getName(); UniqueNameBuilder<VirtualFile> uniqueNameBuilderForShortName = valueMap.get(fileName); if (uniqueNameBuilderForShortName == null) { final UniqueNameBuilder<VirtualFile> builder = filesWithTheSameName( fileName, project, skipNonOpenedFiles, ProjectScope.getProjectScope(project) ); valueMap.put(fileName, builder != null ? builder:ourEmptyBuilder); uniqueNameBuilderForShortName = builder; } else if (uniqueNameBuilderForShortName == ourEmptyBuilder) { uniqueNameBuilderForShortName = null; } if (uniqueNameBuilderForShortName != null && uniqueNameBuilderForShortName.contains(file)) { if (file instanceof VirtualFilePathWrapper) { return ((VirtualFilePathWrapper)file).getPresentablePath(); } return getEditorTabText(file, uniqueNameBuilderForShortName, UISettings.getInstance().HIDE_KNOWN_EXTENSION_IN_TABS); } return file.getPresentableName(); }
private static String getUniqueVirtualFilePath(final Project project, VirtualFile file, final boolean skipNonOpenedFiles, GlobalSearchScope scope) { Key<CachedValue<Map<GlobalSearchScope, Map<String, UniqueNameBuilder<VirtualFile>>>>> key = skipNonOpenedFiles ? ourShortNameOpenedBuilderCacheKey:ourShortNameBuilderCacheKey; CachedValue<Map<GlobalSearchScope, Map<String, UniqueNameBuilder<VirtualFile>>>> data = project.getUserData(key); if (data == null) { project.putUserData(key, data = CachedValuesManager.getManager(project).createCachedValue( () -> new CachedValueProvider.Result<Map<GlobalSearchScope, Map<String, UniqueNameBuilder<VirtualFile>>>>( new ConcurrentHashMap<>(2), PsiModificationTracker.MODIFICATION_COUNT, //ProjectRootModificationTracker.getInstance(project), //VirtualFileManager.VFS_STRUCTURE_MODIFICATIONS, FileEditorManagerImpl.OPEN_FILE_SET_MODIFICATION_COUNT ), false)); } ConcurrentMap<GlobalSearchScope, Map<String, UniqueNameBuilder<VirtualFile>>> scope2ValueMap = (ConcurrentMap<GlobalSearchScope, Map<String,UniqueNameBuilder<VirtualFile>>>)data.getValue(); Map<String, UniqueNameBuilder<VirtualFile>> valueMap = scope2ValueMap.get(scope); if (valueMap == null) { valueMap = ConcurrencyUtil.cacheOrGet(scope2ValueMap, scope, ContainerUtil.createConcurrentSoftValueMap()); } final String fileName = file.getName(); UniqueNameBuilder<VirtualFile> uniqueNameBuilderForShortName = valueMap.get(fileName); if (uniqueNameBuilderForShortName == null) { final UniqueNameBuilder<VirtualFile> builder = filesWithTheSameName( fileName, project, skipNonOpenedFiles, scope ); valueMap.put(fileName, builder != null ? builder:ourEmptyBuilder); uniqueNameBuilderForShortName = builder; } else if (uniqueNameBuilderForShortName == ourEmptyBuilder) { uniqueNameBuilderForShortName = null; } if (uniqueNameBuilderForShortName != null && uniqueNameBuilderForShortName.contains(file)) { if (file instanceof VirtualFilePathWrapper) { return ((VirtualFilePathWrapper)file).getPresentablePath(); } return uniqueNameBuilderForShortName.getShortPath(file); } return file.getName(); }