Skip to content

Commit 34df3c6

Browse files
authored
Fix Path Resolution (#26)
This PR fixes the Windows file URI formatting and refactors path resolution by using the VS Code workspace root API to get the source directory instead of parsing file URIs for each opened/modified file.
1 parent 84e5076 commit 34df3c6

File tree

4 files changed

+42
-32
lines changed

4 files changed

+42
-32
lines changed
544 Bytes
Binary file not shown.

server/src/main/java/LJDiagnosticsHandler.java

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,18 @@
2121
public class LJDiagnosticsHandler {
2222

2323
private static final String FILE_PREFIX = "file://";
24-
private static final String SRC_SUFFIX = "/src/";
2524
private static final String SOURCE = "liquidjava";
2625

2726
/**
2827
* Generates LJDiagnostics for the given URI
29-
* @param uri
28+
* @param uri the document URI
29+
* @param path the file path
3030
* @return LJDiagnostics
3131
*/
32-
public static LJDiagnostics getLJDiagnostics(String uri) {
32+
public static LJDiagnostics getLJDiagnostics(String uri, String path) {
3333
List<LJError> errors = new ArrayList<>();
3434
List<LJWarning> warnings = new ArrayList<>();
35-
36-
String path = convertUTFtoCharacters(extractBasePath(uri));
35+
3736
CommandLineLauncher.launch(path);
3837
Diagnostics diagnostics = Diagnostics.getInstance();
3938
if (diagnostics.foundWarning()) {
@@ -70,7 +69,7 @@ public static List<PublishDiagnosticsParams> getDiagnostics(List<LJDiagnostic> d
7069
// group diagnostics by file
7170
Map<String, List<Diagnostic>> diagnosticsByFile = diagnostics.stream()
7271
.collect(Collectors.groupingBy(
73-
d -> FILE_PREFIX + d.getFile(),
72+
d -> toFileUri(d.getFile()),
7473
Collectors.mapping(d -> {
7574
Range range = getRangeFromErrorPosition(d.getPosition());
7675
String message = String.format("%s: %s", d.getTitle(), d.getMessage());
@@ -84,6 +83,21 @@ public static List<PublishDiagnosticsParams> getDiagnostics(List<LJDiagnostic> d
8483
.toList();
8584
}
8685

86+
/**
87+
* Converts a file path to a file:// URI
88+
* @param filePath the file path
89+
* @return the file URI
90+
*/
91+
private static String toFileUri(String filePath) {
92+
String normalized = filePath.replace("\\", "/");
93+
// Windows (C:/path)
94+
if (!normalized.isEmpty() && normalized.charAt(1) == ':') {
95+
return FILE_PREFIX + "/" + normalized;
96+
}
97+
// Unix (/path)
98+
return FILE_PREFIX + normalized;
99+
}
100+
87101
/**
88102
* Generates empty diagnostics for the given URI
89103
* @param uri the uri used for the verification
@@ -93,30 +107,6 @@ public static PublishDiagnosticsParams getEmptyDiagnostics(String uri) {
93107
return new PublishDiagnosticsParams(uri, Collections.emptyList());
94108
}
95109

96-
/**
97-
* Extracts the base path from the given full path
98-
* e.g. file://path/to/project/src/main/path/to/File.java => /path/to/project/src/main
99-
* @param fullPath the full path
100-
* @return base path
101-
*/
102-
private static String extractBasePath(String fullPath) {
103-
fullPath = fullPath.replace(FILE_PREFIX, "");
104-
int suffixIndex = fullPath.indexOf(SRC_SUFFIX);
105-
int nextSlashIndex = fullPath.indexOf("/", suffixIndex + SRC_SUFFIX.length());
106-
if (suffixIndex == -1 || nextSlashIndex == -1)
107-
return fullPath; // cannot extract base path
108-
return fullPath.substring(0, nextSlashIndex); // up to and including the next slash after /src/
109-
}
110-
111-
/**
112-
* Converts a UTF-8 encoded string to a regular string
113-
* @param source the UTF-8 encoded string
114-
* @return converted string
115-
*/
116-
private static String convertUTFtoCharacters(String source) {
117-
return java.net.URLDecoder.decode(source, StandardCharsets.UTF_8);
118-
}
119-
120110
/**
121111
* Gets the Range from the given ErrorPosition If the position is null, returns a default Range at (0,0)-(0,0)
122112
* @param pos the ErrorPosition

server/src/main/java/LJDiagnosticsService.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import java.io.File;
22
import java.net.URI;
3+
import java.nio.file.Path;
4+
import java.nio.file.Paths;
35
import java.util.List;
46
import java.util.stream.Collectors;
57
import java.util.stream.Stream;
@@ -18,8 +20,9 @@
1820
import liquidjava.diagnostics.LJDiagnostic;
1921

2022
public class LJDiagnosticsService implements TextDocumentService, WorkspaceService {
21-
23+
2224
private LJLanguageClient client;
25+
private String sourcePath;
2326

2427
/**
2528
* Sets the language client
@@ -29,6 +32,17 @@ public void setClient(LJLanguageClient client) {
2932
this.client = client;
3033
}
3134

35+
/**
36+
* Sets the source path
37+
* Uses workspaceRoot + "/src/main/java" if it exists, otherwise uses workspaceRoot
38+
* @param workspaceRoot the workspace root URI
39+
*/
40+
public void setSourcePath(String workspaceRoot) {
41+
Path workspaceRootPath = Paths.get(URI.create(workspaceRoot));
42+
Path srcMainJava = workspaceRootPath.resolve("src/main/java");
43+
this.sourcePath = srcMainJava.toFile().isDirectory() ? srcMainJava.toString() : workspaceRootPath.toString();
44+
}
45+
3246
/**
3347
* Sends diagnostics notification to the client
3448
* @param diagnostics the diagnostics to send
@@ -49,7 +63,7 @@ public void sendDiagnosticsNotification(List<LJDiagnostic> diagnostics) {
4963
* @param uri the URI of the document
5064
*/
5165
public void generateDiagnostics(String uri) {
52-
LJDiagnostics ljDiagnostics = LJDiagnosticsHandler.getLJDiagnostics(uri);
66+
LJDiagnostics ljDiagnostics = LJDiagnosticsHandler.getLJDiagnostics(uri, sourcePath);
5367
List<PublishDiagnosticsParams> nativeDiagnostics = LJDiagnosticsHandler.getNativeDiagnostics(ljDiagnostics, uri);
5468
nativeDiagnostics.forEach(params -> {
5569
this.client.publishDiagnostics(params);

server/src/main/java/LJLanguageServer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
3030
WorkspaceServerCapabilities workspaceServerCapabilities = new WorkspaceServerCapabilities();
3131
WorkspaceFoldersOptions workspaceFoldersOptions = new WorkspaceFoldersOptions();
3232

33+
// extract workspace root from initialization params
34+
if (params.getWorkspaceFolders() != null && !params.getWorkspaceFolders().isEmpty()) {
35+
String workspaceRoot = params.getWorkspaceFolders().get(0).getUri();
36+
diagnosticsService.setSourcePath(workspaceRoot);
37+
}
38+
3339
// set options
3440
workspaceFoldersOptions.setChangeNotifications(true);
3541
workspaceFoldersOptions.setSupported(true);

0 commit comments

Comments
 (0)