11import java .io .File ;
22import java .net .URI ;
3- import java .nio .file .Path ;
4- import java .nio .file .Paths ;
53import java .util .List ;
64import java .util .stream .Collectors ;
75import java .util .stream .Stream ;
1816
1917import dtos .DiagnosticConverter ;
2018import liquidjava .diagnostics .LJDiagnostic ;
19+ import utils .PathUtils ;
2120
2221public class LJDiagnosticsService implements TextDocumentService , WorkspaceService {
2322
2423 private LJLanguageClient client ;
25- private String sourcePath ;
24+ private String workspaceRoot ;
2625
27- /**
28- * Sets the language client
29- * @param client the language client
30- */
3126 public void setClient (LJLanguageClient client ) {
3227 this .client = client ;
3328 }
3429
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 ();
30+ public void setWorkspaceRoot (String workspaceRoot ) {
31+ this .workspaceRoot = workspaceRoot ;
4432 }
4533
4634 /**
4735 * Sends diagnostics notification to the client
4836 * @param diagnostics the diagnostics to send
4937 */
5038 public void sendDiagnosticsNotification (List <LJDiagnostic > diagnostics ) {
51- if (this .client == null )
52- return ;
39+ if (this .client == null ) return ;
5340
5441 System .out .println ("Sending diagnostics notification with " + diagnostics .size () + " diagnostics" );
5542 List <Object > dtos = diagnostics .stream ()
@@ -63,7 +50,8 @@ public void sendDiagnosticsNotification(List<LJDiagnostic> diagnostics) {
6350 * @param uri the URI of the document
6451 */
6552 public void generateDiagnostics (String uri ) {
66- LJDiagnostics ljDiagnostics = LJDiagnosticsHandler .getLJDiagnostics (uri , sourcePath );
53+ String path = PathUtils .extractBasePath (uri );
54+ LJDiagnostics ljDiagnostics = LJDiagnosticsHandler .getLJDiagnostics (path );
6755 List <PublishDiagnosticsParams > nativeDiagnostics = LJDiagnosticsHandler .getNativeDiagnostics (ljDiagnostics , uri );
6856 nativeDiagnostics .forEach (params -> {
6957 this .client .publishDiagnostics (params );
@@ -72,21 +60,6 @@ public void generateDiagnostics(String uri) {
7260 sendDiagnosticsNotification (diagnostics );
7361 }
7462
75- /**
76- * Checks if a file URI is within the source path
77- * @param uri the file URI
78- * @return true if the file is within sourcePath, false otherwise
79- */
80- private boolean isFileInSourcePath (String uri ) {
81- try {
82- Path filePath = Paths .get (new URI (uri ));
83- Path sourcePathObj = Paths .get (sourcePath );
84- return filePath .startsWith (sourcePathObj );
85- } catch (Exception e ) {
86- return false ;
87- }
88- }
89-
9063 /**
9164 * Clear a diagnostic for a specific URI
9265 * @param uri the URI of the document
@@ -104,7 +77,7 @@ public void clearDiagnostic(String uri) {
10477 @ Override
10578 public void didOpen (DidOpenTextDocumentParams params ) {
10679 String uri = params .getTextDocument ().getUri ();
107- if (!isFileInSourcePath (uri )) return ;
80+ if (!PathUtils . isFileInDirectory (uri , workspaceRoot )) return ;
10881 System .out .println ("Document opened — checking diagnostics" );
10982 generateDiagnostics (uri );
11083 }
@@ -116,7 +89,7 @@ public void didOpen(DidOpenTextDocumentParams params) {
11689 @ Override
11790 public void didSave (DidSaveTextDocumentParams params ) {
11891 String uri = params .getTextDocument ().getUri ();
119- if (!isFileInSourcePath (uri )) return ;
92+ if (!PathUtils . isFileInDirectory (uri , workspaceRoot )) return ;
12093 System .out .println ("Document saved — checking diagnostics" );
12194 clearDiagnostic (uri );
12295 generateDiagnostics (uri );
@@ -129,7 +102,7 @@ public void didSave(DidSaveTextDocumentParams params) {
129102 @ Override
130103 public void didClose (DidCloseTextDocumentParams params ) {
131104 String uri = params .getTextDocument ().getUri ();
132- if (!isFileInSourcePath (uri )) return ;
105+ if (!PathUtils . isFileInDirectory (uri , workspaceRoot )) return ;
133106 try {
134107 // check if the file still exists on disk
135108 File file = new File (new URI (uri ));
0 commit comments