From 382e61ef1a8183194b7fb7bfa22d569cb33b0b80 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Thu, 4 May 2017 11:10:30 +0200 Subject: [PATCH] move getPresentableName to ProjectUtil --- .../openapi/compiler/CompilerPaths.java | 4 +- .../intellij/openapi/project/ProjectUtil.kt | 25 +++++++- .../openapi/project/ProjectUtilCore.kt | 58 +++++-------------- 3 files changed, 40 insertions(+), 47 deletions(-) diff --git a/java/compiler/openapi/src/com/intellij/openapi/compiler/CompilerPaths.java b/java/compiler/openapi/src/com/intellij/openapi/compiler/CompilerPaths.java index a7aaf85c7366..d0d493a1f045 100644 --- a/java/compiler/openapi/src/com/intellij/openapi/compiler/CompilerPaths.java +++ b/java/compiler/openapi/src/com/intellij/openapi/compiler/CompilerPaths.java @@ -22,7 +22,7 @@ import com.intellij.openapi.application.PathManager; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; -import com.intellij.openapi.project.ProjectUtilCore; +import com.intellij.openapi.project.ProjectUtil; import com.intellij.openapi.roots.CompilerModuleExtension; import com.intellij.openapi.roots.ModuleRootManager; import com.intellij.openapi.util.Computable; @@ -92,7 +92,7 @@ public class CompilerPaths { public static String getCompilerSystemDirectoryName(Project project) { // todo: use ProjectKt.getSystemCacheFileName() - return ProjectUtilCore.getPresentableName(project) + "." + project.getLocationHash(); + return ProjectUtil.getPresentableName(project) + "." + project.getLocationHash(); } public static File getCompilerSystemDirectory() { diff --git a/platform/platform-api/src/com/intellij/openapi/project/ProjectUtil.kt b/platform/platform-api/src/com/intellij/openapi/project/ProjectUtil.kt index 4c5e3025dbba..1f3ced3f28a2 100644 --- a/platform/platform-api/src/com/intellij/openapi/project/ProjectUtil.kt +++ b/platform/platform-api/src/com/intellij/openapi/project/ProjectUtil.kt @@ -17,6 +17,7 @@ package com.intellij.openapi.project import com.intellij.ide.DataManager +import com.intellij.ide.highlighter.ProjectFileType import com.intellij.openapi.actionSystem.CommonDataKeys import com.intellij.openapi.application.runWriteAction import com.intellij.openapi.fileEditor.UniqueVFilePathBuilder @@ -33,6 +34,7 @@ import com.intellij.openapi.vfs.VirtualFilePathWrapper import com.intellij.util.io.exists import java.nio.file.InvalidPathException import java.nio.file.Paths +import java.util.* import javax.swing.JComponent val Module.rootManager: ModuleRootManager @@ -57,7 +59,7 @@ fun calcRelativeToProjectPath(file: VirtualFile, project: Project?, includeFileP if (project == null) { return url } - return ProjectUtilCore.displayUrlRelativeToProject(file, url, project, includeFilePath, keepModuleAlwaysOnTheLeft) + return displayUrlRelativeToProject(file, url, project, includeFilePath, keepModuleAlwaysOnTheLeft) } fun guessProjectForFile(file: VirtualFile): Project? = ProjectLocator.getInstance().guessProjectForFile(file) @@ -133,4 +135,25 @@ fun Project.guessProjectDir() : VirtualFile { } } return this.baseDir!! +} + +fun getPresentableName(project: Project): String? { + if (project.isDefault) { + return project.name + } + + val location = project.presentableUrl ?: return null + + var projectName = FileUtil.toSystemIndependentName(location).trimEnd('/') + val lastSlash = projectName.lastIndexOf('/') + if (lastSlash >= 0 && lastSlash + 1 < projectName.length) { + projectName = projectName.substring(lastSlash + 1) + } + + if (projectName.endsWith(ProjectFileType.DOT_DEFAULT_EXTENSION, ignoreCase = true)) { + projectName = projectName.substring(0, projectName.length - ProjectFileType.DOT_DEFAULT_EXTENSION.length) + } + + // replace ':' from windows drive names + return projectName.toLowerCase(Locale.US).replace(':', '_') } \ No newline at end of file diff --git a/platform/projectModel-api/src/com/intellij/openapi/project/ProjectUtilCore.kt b/platform/projectModel-api/src/com/intellij/openapi/project/ProjectUtilCore.kt index 0c05a5a92f71..d31f9a1c383d 100644 --- a/platform/projectModel-api/src/com/intellij/openapi/project/ProjectUtilCore.kt +++ b/platform/projectModel-api/src/com/intellij/openapi/project/ProjectUtilCore.kt @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2017 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. @@ -16,29 +16,20 @@ @file:JvmName("ProjectUtilCore") package com.intellij.openapi.project -import com.intellij.ide.highlighter.ProjectFileType import com.intellij.openapi.module.ModuleUtilCore import com.intellij.openapi.roots.JdkOrderEntry import com.intellij.openapi.roots.libraries.LibraryUtil import com.intellij.openapi.util.SystemInfo -import com.intellij.openapi.util.io.FileUtil -import com.intellij.openapi.util.text.StringUtil import com.intellij.openapi.vfs.LocalFileProvider import com.intellij.openapi.vfs.VirtualFile -import java.util.* -fun displayUrlRelativeToProject(file: VirtualFile, - url: String, - project: Project, - includeFilePath: Boolean, - keepModuleAlwaysOnTheLeft: Boolean): String { - var url = url +fun displayUrlRelativeToProject(file: VirtualFile, url: String, project: Project, includeFilePath: Boolean, keepModuleAlwaysOnTheLeft: Boolean): String { + var result = url val baseDir = project.baseDir if (baseDir != null && includeFilePath) { - val projectHomeUrl = baseDir.presentableUrl - if (url.startsWith(projectHomeUrl)) { - url = "..." + url.substring(projectHomeUrl.length) + if (result.startsWith(projectHomeUrl)) { + result = "...${result.substring(projectHomeUrl.length)}" } } @@ -48,44 +39,23 @@ fun displayUrlRelativeToProject(file: VirtualFile, val libraryEntry = LibraryUtil.findLibraryEntry(file, project) if (libraryEntry != null) { if (libraryEntry is JdkOrderEntry) { - url = url + " - [" + libraryEntry.jdkName + "]" + result = "$result - [${libraryEntry.jdkName}]" } else { - url = url + " - [" + libraryEntry.presentableName + "]" + result = "$result - [${libraryEntry.presentableName}]" } } else { - url = url + " - [" + fileForJar.name + "]" + result = "$result - [${fileForJar.name}]" } } } - val module = ModuleUtilCore.findModuleForFile(file, project) ?: return url - return if (!keepModuleAlwaysOnTheLeft && SystemInfo.isMac) - url + " - [" + module.name + "]" - else - "[" + module.name + "] - " + url -} - -fun getPresentableName(project: Project): String? { - if (project.isDefault) { - return project.name + val module = ModuleUtilCore.findModuleForFile(file, project) ?: return result + return if (!keepModuleAlwaysOnTheLeft && SystemInfo.isMac) { + "$result - [${module.name}]" } - - val location = project.presentableUrl ?: return null - - var projectName = FileUtil.toSystemIndependentName(location) - projectName = StringUtil.trimEnd(projectName, "/") - - val lastSlash = projectName.lastIndexOf('/') - if (lastSlash >= 0 && lastSlash + 1 < projectName.length) { - projectName = projectName.substring(lastSlash + 1) + else { + "[${module.name}] - $result" } - - if (StringUtil.endsWithIgnoreCase(projectName, ProjectFileType.DOT_DEFAULT_EXTENSION)) { - projectName = projectName.substring(0, projectName.length - ProjectFileType.DOT_DEFAULT_EXTENSION.length) - } - - projectName = projectName.toLowerCase(Locale.US).replace(':', '_') // replace ':' from windows drive names - return projectName -} +} \ No newline at end of file