Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ dependencies {

testImplementation(libs.junit)
testImplementation(libs.junit.jupiter.api)
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
}

// Set the JVM language level used to build the project.
Expand Down Expand Up @@ -206,6 +207,7 @@ changelog {
}

tasks.withType<Test> {
useJUnitPlatform()
environment("GRADLE_RELEASE_REPOSITORY","https://services.gradle.org/distributions")
systemProperty("idea.log.leaked.projects.in.tests", "false")
systemProperty( "idea.maven.test.mirror", "https://repo1.maven.org/maven2")
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/com/redhat/devtools/lsp4ij/LSPIJUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -490,12 +490,16 @@ private static Language doGetFileLanguage(@NotNull VirtualFile file, @NotNull Pr
}

public static @NotNull URI toUri(@NotNull File file) {
// URI scheme specified by language server protocol and LSP
try {
return new URI("file", "", file.getAbsoluteFile().toURI().getPath(), null); //$NON-NLS-1$ //$NON-NLS-2$
} catch (URISyntaxException e) {
LOGGER.warn(e.getLocalizedMessage(), e);
return file.getAbsoluteFile().toURI();
return file.toPath().toUri();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment Hich explains why path is used by adding in comments some url sample

} catch (Exception e) {
LOGGER.debug("Failed to convert file '{}' to URI, using fallback", file.getAbsolutePath(), e);
try {
return new URI("file", "", file.getAbsoluteFile().toURI().getPath(), null); //$NON-NLS-1$ //$NON-NLS-2$
} catch (URISyntaxException e2) {
LOGGER.warn(e2.getLocalizedMessage(), e2);
return file.getAbsoluteFile().toURI();
}
}
}

Expand Down Expand Up @@ -1059,6 +1063,10 @@ private static void applyRenameFile(RenameFile renameFile) {
return VfsUtil.findFileByIoFile(newFile, true);
}

public static @Nullable VirtualFile findResourceFor(@NotNull File file) {
return findResourceFor(toUri(file).toString());
}

public static @Nullable VirtualFile findResourceFor(@NotNull URI uri) {
return LocalFileSystem.getInstance().findFileByIoFile(Paths.get(uri).toFile());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected FormattingTask createFormattingTask(@NotNull AsyncFormattingRequest fo
if (ioFile == null) {
return null;
}
VirtualFile file = LSPIJUtils.findResourceFor(ioFile.toURI());
VirtualFile file = LSPIJUtils.findResourceFor(ioFile);
if (file == null) {
return null;
}
Expand Down
116 changes: 116 additions & 0 deletions src/test/java/com/redhat/devtools/lsp4ij/LSPIJUtils_toUriTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*******************************************************************************
* Copyright (c) 2024 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
* SAP SE - additional test cases
******************************************************************************/
package com.redhat.devtools.lsp4ij;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;

import java.io.File;
import java.net.URI;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;

/**
* Tests for {@link LSPIJUtils#toUri(File)} method.
*/
public class LSPIJUtils_toUriTest {

@Test
@EnabledOnOs(OS.WINDOWS)
void testToUriWithWslPath() {
// Given: Create a File object from a UNC path string
// Note: File object may normalize the path, so we test with the actual behavior
String wslPath = "\\\\wsl$\\Ubuntu\\home\\user\\project\\file.txt";
File wslFile = new File(wslPath);

// When
URI resultUri = LSPIJUtils.toUri(wslFile);

// Then
assertNotNull(resultUri);
assertEquals("file", resultUri.getScheme());

// The URI should either:
// 1. Have authority="wsl$" and path="/Ubuntu/home/user/project/file.txt" (correct UNC handling)
// 2. Or at minimum, not produce the old broken format with 4 slashes
String uriString = resultUri.toString();
assertNotNull(uriString);

// Verify it doesn't have the broken 4-slash format
if (uriString.contains("file:////")) {
fail("URI should not contain file://// (broken UNC format): " + uriString);
}
}

@Test
@EnabledOnOs(OS.WINDOWS)
void testToUriWithLocalPathOnWindows() {
// Given
File localFile = new File("C:\\Users\\user\\project\\file.txt");

// When
URI resultUri = LSPIJUtils.toUri(localFile);

// Then
assertNotNull(resultUri);
assertEquals("file", resultUri.getScheme());
assertEquals("/C:/Users/user/project/file.txt", resultUri.getPath());
}

@Test
@EnabledOnOs({OS.LINUX, OS.MAC})
void testToUriWithLocalPathOnUnix() {
// Given
File localFile = new File("/home/user/project/file.txt");

// When
URI resultUri = LSPIJUtils.toUri(localFile);

// Then
assertNotNull(resultUri);
assertEquals("file", resultUri.getScheme());
assertEquals("/home/user/project/file.txt", resultUri.getPath());
}

@Test
@EnabledOnOs(OS.WINDOWS)
void testToUriWithSpacesOnWindows() {
// Given
File fileWithSpaces = new File("C:\\Users\\user name\\project name\\file.txt");

// When
URI resultUri = LSPIJUtils.toUri(fileWithSpaces);

// Then
assertNotNull(resultUri);
assertEquals("file", resultUri.getScheme());
assertEquals("/C:/Users/user%20name/project%20name/file.txt", resultUri.getRawPath());
}

@Test
@EnabledOnOs({OS.LINUX, OS.MAC})
void testToUriWithSpacesOnUnix() {
// Given
File fileWithSpaces = new File("/home/user name/project name/file.txt");

// When
URI resultUri = LSPIJUtils.toUri(fileWithSpaces);

// Then
assertNotNull(resultUri);
assertEquals("file", resultUri.getScheme());
assertEquals("/home/user%20name/project%20name/file.txt", resultUri.getRawPath());
}
}
Loading