diff --git a/platform/platform-api/src/com/intellij/ide/BrowserUtil.java b/platform/platform-api/src/com/intellij/ide/BrowserUtil.java index bebc64dc5e3a..c19643e00dbf 100644 --- a/platform/platform-api/src/com/intellij/ide/BrowserUtil.java +++ b/platform/platform-api/src/com/intellij/ide/BrowserUtil.java @@ -49,7 +49,6 @@ import java.io.InputStream; import java.lang.reflect.InvocationTargetException; import java.net.MalformedURLException; import java.net.URI; -import java.net.URISyntaxException; import java.net.URL; import java.util.Arrays; import java.util.HashSet; @@ -102,15 +101,8 @@ public class BrowserUtil { browse(VfsUtil.toUri(file)); } - public static boolean browse(@NotNull File file) { - try { - browse(new URI(StandardFileSystems.FILE_PROTOCOL, "", file.getAbsolutePath(), null)); - return true; - } - catch (URISyntaxException e) { - LOG.debug(e); - return false; - } + public static void browse(@NotNull File file) { + browse(VfsUtil.toUri(file)); } public static void browse(@NotNull URL url) { @@ -158,10 +150,8 @@ public class BrowserUtil { } } - if (browse(file)) { - return; - } - uri = null; + browse(file); + return; } if (uri == null) { diff --git a/platform/platform-api/src/com/intellij/openapi/vfs/VfsUtil.java b/platform/platform-api/src/com/intellij/openapi/vfs/VfsUtil.java index a2ca45ee9acb..85509d8cfcc6 100644 --- a/platform/platform-api/src/com/intellij/openapi/vfs/VfsUtil.java +++ b/platform/platform-api/src/com/intellij/openapi/vfs/VfsUtil.java @@ -498,7 +498,7 @@ public class VfsUtil extends VfsUtilCore { } /** - * @return correct URI, must be used only for external communication + * @return correct URL, must be used only for external communication */ @NotNull public static URI toUri(@NotNull VirtualFile file) { @@ -517,6 +517,23 @@ public class VfsUtil extends VfsUtilCore { } } + /** + * @return correct URL, must be used only for external communication + */ + @NotNull + public static URI toUri(@NotNull File file) { + String path = file.toURI().getPath(); + try { + if (SystemInfo.isWindows && path.charAt(0) != '/') { + path = '/' + path; + } + return new URI(StandardFileSystems.FILE_PROTOCOL, "", path, null, null); + } + catch (URISyntaxException e) { + throw new IllegalArgumentException(e); + } + } + /** * uri - may be incorrect (escaping or missed "/" before disk name under windows), may be not fully encoded, * may contains query and fragment diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/vfs/VfsUtilTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/vfs/VfsUtilTest.java index ace33665106e..5797dbe4679b 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/vfs/VfsUtilTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/vfs/VfsUtilTest.java @@ -297,6 +297,9 @@ public class VfsUtilTest extends PlatformLangTestCase { } public void testToUri() { + assertEquals("file:///asd", VfsUtil.toUri(new File("/asd")).toASCIIString()); + assertEquals("file:///asd%20/sd", VfsUtil.toUri(new File("/asd /sd")).toASCIIString()); + URI uri = VfsUtil.toUri("file:///asd"); assertNotNull(uri); assertEquals("file", uri.getScheme());