13
13
package org .eclipse .jdt .ls .core .internal .handlers ;
14
14
15
15
import java .util .Collections ;
16
- import java .util .List ;
17
- import java .util .stream .Collectors ;
16
+ import java .util .concurrent .LinkedBlockingQueue ;
18
17
19
18
import org .eclipse .core .resources .IResource ;
20
19
import org .eclipse .core .resources .IWorkspaceRunnable ;
@@ -49,11 +48,23 @@ public class WorkspaceEventsHandler {
49
48
private final ProjectsManager pm ;
50
49
private final JavaClientConnection connection ;
51
50
private final BaseDocumentLifeCycleHandler handler ;
51
+ private final LinkedBlockingQueue <FileEvent > queue = new LinkedBlockingQueue <>();
52
52
53
53
public WorkspaceEventsHandler (ProjectsManager projects , JavaClientConnection connection , BaseDocumentLifeCycleHandler handler ) {
54
54
this .pm = projects ;
55
55
this .connection = connection ;
56
56
this .handler = handler ;
57
+ Thread eventThread = new Thread (() -> {
58
+ while (true ) {
59
+ try {
60
+ FileEvent event = queue .take ();
61
+ handleFileEvent (event );
62
+ } catch (InterruptedException e ) {
63
+ break ;
64
+ }
65
+ }
66
+ }, "WorkspaceEventsHandler" );
67
+ eventThread .start ();
57
68
}
58
69
59
70
private CHANGE_TYPE toChangeType (FileChangeType vtype ) {
@@ -70,50 +81,64 @@ private CHANGE_TYPE toChangeType(FileChangeType vtype) {
70
81
}
71
82
72
83
public void didChangeWatchedFiles (DidChangeWatchedFilesParams param ) {
73
- List <FileEvent > changes = param .getChanges ().stream ().distinct ().collect (Collectors .toList ());
74
- for (FileEvent fileEvent : changes ) {
75
- CHANGE_TYPE changeType = toChangeType (fileEvent .getType ());
76
- if (changeType == CHANGE_TYPE .DELETED ) {
77
- cleanUpDiagnostics (fileEvent .getUri ());
78
- handler .didClose (new DidCloseTextDocumentParams (new TextDocumentIdentifier (fileEvent .getUri ())));
79
- discardWorkingCopies (fileEvent .getUri ());
84
+ param .getChanges ().stream ().distinct ().forEach (event -> {
85
+ try {
86
+ queue .put (event );
87
+ } catch (InterruptedException e ) {
88
+ // do nothing
89
+ }
90
+ });
91
+ }
92
+
93
+ // for test only
94
+ public void handleFileEvents (FileEvent ... fileEvents ) {
95
+ for (FileEvent fileEvent : fileEvents ) {
96
+ handleFileEvent (fileEvent );
97
+ }
98
+ }
99
+
100
+ private void handleFileEvent (FileEvent fileEvent ) {
101
+ CHANGE_TYPE changeType = toChangeType (fileEvent .getType ());
102
+ if (changeType == CHANGE_TYPE .DELETED ) {
103
+ cleanUpDiagnostics (fileEvent .getUri ());
104
+ handler .didClose (new DidCloseTextDocumentParams (new TextDocumentIdentifier (fileEvent .getUri ())));
105
+ discardWorkingCopies (fileEvent .getUri ());
106
+ }
107
+ ICompilationUnit unit = JDTUtils .resolveCompilationUnit (fileEvent .getUri ());
108
+ if (unit != null && changeType == CHANGE_TYPE .CREATED && !unit .exists ()) {
109
+ final ICompilationUnit [] units = new ICompilationUnit [1 ];
110
+ units [0 ] = unit ;
111
+ try {
112
+ ResourcesPlugin .getWorkspace ().run (new IWorkspaceRunnable () {
113
+ @ Override
114
+ public void run (IProgressMonitor monitor ) throws CoreException {
115
+ units [0 ] = createCompilationUnit (units [0 ]);
116
+ }
117
+ }, new NullProgressMonitor ());
118
+ } catch (CoreException e ) {
119
+ JavaLanguageServerPlugin .logException (e .getMessage (), e );
80
120
}
81
- ICompilationUnit unit = JDTUtils . resolveCompilationUnit ( fileEvent . getUri ()) ;
82
- if ( unit != null && changeType == CHANGE_TYPE . CREATED && ! unit . exists ()) {
83
- final ICompilationUnit [] units = new ICompilationUnit [ 1 ];
84
- units [ 0 ] = unit ;
121
+ unit = units [ 0 ] ;
122
+ }
123
+ if ( unit != null ) {
124
+ if ( unit . isWorkingCopy ()) {
85
125
try {
86
- ResourcesPlugin .getWorkspace ().run (new IWorkspaceRunnable () {
87
- @ Override
88
- public void run (IProgressMonitor monitor ) throws CoreException {
89
- units [0 ] = createCompilationUnit (units [0 ]);
90
- }
91
- }, new NullProgressMonitor ());
126
+ IResource resource = unit .getUnderlyingResource ();
127
+ if (resource != null && resource .exists ()) {
128
+ resource .refreshLocal (IResource .DEPTH_ZERO , new NullProgressMonitor ());
129
+ }
92
130
} catch (CoreException e ) {
93
131
JavaLanguageServerPlugin .logException (e .getMessage (), e );
94
132
}
95
- unit = units [ 0 ] ;
133
+ return ;
96
134
}
97
- if (unit != null ) {
98
- if (unit .isWorkingCopy ()) {
99
- try {
100
- IResource resource = unit .getUnderlyingResource ();
101
- if (resource != null && resource .exists ()) {
102
- resource .refreshLocal (IResource .DEPTH_ZERO , new NullProgressMonitor ());
103
- }
104
- } catch (CoreException e ) {
105
- JavaLanguageServerPlugin .logException (e .getMessage (), e );
106
- }
107
- continue ;
108
- }
109
- if (changeType == CHANGE_TYPE .DELETED || changeType == CHANGE_TYPE .CHANGED ) {
110
- if (unit .equals (CoreASTProvider .getInstance ().getActiveJavaElement ())) {
111
- CoreASTProvider .getInstance ().disposeAST ();
112
- }
135
+ if (changeType == CHANGE_TYPE .DELETED || changeType == CHANGE_TYPE .CHANGED ) {
136
+ if (unit .equals (CoreASTProvider .getInstance ().getActiveJavaElement ())) {
137
+ CoreASTProvider .getInstance ().disposeAST ();
113
138
}
114
139
}
115
- pm .fileChanged (fileEvent .getUri (), changeType );
116
140
}
141
+ pm .fileChanged (fileEvent .getUri (), changeType );
117
142
}
118
143
119
144
private ICompilationUnit createCompilationUnit (ICompilationUnit unit ) {
0 commit comments