2121public 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
0 commit comments