Skip to content

Commit

Permalink
Fix construction of paths for compatibility on Windows #1279
Browse files Browse the repository at this point in the history
The direct construction of paths from URIs ensures the correct handling
of drive letters on Windows.
  • Loading branch information
oleosterhagen authored and donat committed Dec 19, 2023
1 parent 3421100 commit af0501b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -44,7 +46,13 @@ public ContentInFile getContentByUri(String uri) {
}

public boolean openFile(String uri) {
Path path = getPathFromUri(uri);
Path path;
try {
path = getPathFromUri(uri);
} catch (URISyntaxException e) {
LOGGER.error("Malformed uri:" + uri, e);
return false;
}
try {
String content = Files.readString(path);
contentByUri.put(uri, new ContentInFile(content, 0));
Expand Down Expand Up @@ -92,9 +100,8 @@ public void closeFile(String uri) {
}


private Path getPathFromUri(String uri) {
String trimmedUri = uri.substring("file://".length());
return Paths.get(trimmedUri);
private Path getPathFromUri(String uri) throws URISyntaxException {
return Paths.get(new URI(uri));
}

private String applyChange(String content, TextDocumentContentChangeEvent change)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
package org.eclipse.buildship.gradleprop.provider;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
Expand Down Expand Up @@ -43,12 +42,10 @@ public class GradlePropertiesConnectionProvider extends ProcessStreamConnectionP
implements StreamConnectionProvider {

public GradlePropertiesConnectionProvider() {
URL localFileURL;
Bundle bundle = FrameworkUtil.getBundle(GradlePropertiesConnectionProvider.class);
try {
localFileURL = FileLocator.toFileURL(bundle.getEntry("/"));
URI localFileURI = new URI(localFileURL.toExternalForm());
Path pathToPlugin = Paths.get(localFileURI.getPath());
URL localFileURL = FileLocator.toFileURL(bundle.getEntry("/"));
Path pathToPlugin = Paths.get(localFileURL.toURI());

String pathToServer = pathToPlugin.resolve("libs/language-server.jar").toString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.Bundle;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -42,12 +41,10 @@ public class KotlinDSLConnectionProvider extends ProcessStreamConnectionProvider
implements StreamConnectionProvider {

public KotlinDSLConnectionProvider() {
URL localFileURL;
Bundle bundle = FrameworkUtil.getBundle(KotlinDSLConnectionProvider.class);
try {
localFileURL = FileLocator.toFileURL(bundle.getEntry("/"));
URI localFileURI = new URI(localFileURL.toExternalForm());
Path pathToPlugin = Paths.get(localFileURI.getPath());
URL localFileURL = FileLocator.toFileURL(bundle.getEntry("/"));
Path pathToPlugin = Paths.get(localFileURL.toURI());

String pathToServer = pathToPlugin.resolve("libs/server-1.0.0-all.jar").toString();

Expand Down

0 comments on commit af0501b

Please sign in to comment.