Skip to content

Commit

Permalink
Merge pull request #329 from akashnagesh/fix-npe-fileutils
Browse files Browse the repository at this point in the history
Encode special characters in filepath before converting path to URI
  • Loading branch information
NipunaRanasinghe authored Jul 28, 2023
2 parents e194fe9 + 0e0865c commit 3fedd43
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
9 changes: 2 additions & 7 deletions src/main/java/org/wso2/lsp4intellij/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,7 @@ public static VirtualFile virtualFileFromEditor(Editor editor) {
* @return the URI
*/
public static String VFSToURI(VirtualFile file) {
try {
return sanitizeURI(new URL(file.getUrl().replace(" ", SPACE_ENCODED)).toURI().toString());
} catch (MalformedURLException | URISyntaxException e) {
LOG.warn(e);
return null;
}
return file == null? null : pathToUri(file.getPath());
}

/**
Expand Down Expand Up @@ -286,7 +281,7 @@ public static String editorToProjectFolderPath(Editor editor) {
* @return The uri
*/
public static String pathToUri(@Nullable String path) {
return path != null ? sanitizeURI(new File(path.replace(" ", SPACE_ENCODED)).toURI().toString()) : null;
return path != null ? sanitizeURI(new File(path).toURI().toString()) : null;
}

public static String projectToUri(Project project) {
Expand Down
11 changes: 10 additions & 1 deletion src/test/java/org/wso2/lsp4intellij/utils/FileUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,17 @@ public void testVFSToURI() {
}

@Test
@Ignore
public void testVFSToURINull() {
Assert.assertNull(FileUtils.VFSToURI((new LightVirtualFile())));
PowerMockito.mockStatic(System.class);
PowerMockito.when(System.getProperty(Mockito.anyString())).thenReturn("Linux");

// LightVirtualFile returns '/' as path
String uri = FileUtils.VFSToURI((new LightVirtualFile()));
Assert.assertNotNull(uri);

String expectedUri = "file:///";
Assert.assertEquals(expectedUri, uri);
}

@Test
Expand Down

0 comments on commit 3fedd43

Please sign in to comment.