@@ -22,6 +22,7 @@ import com.tang.vscode.configuration.ConfigurationManager
2222import com.tang.vscode.utils.computeAsync
2323import com.tang.vscode.utils.getSymbol
2424import org.eclipse.lsp4j.*
25+ import org.eclipse.lsp4j.jsonrpc.CompletableFutures
2526import org.eclipse.lsp4j.jsonrpc.services.JsonRequest
2627import org.eclipse.lsp4j.services.WorkspaceService
2728import java.io.File
@@ -37,6 +38,7 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
3738 private val schemeMap = mutableMapOf<String , IFolder >()
3839 private val configurationManager = ConfigurationManager ()
3940 private var client: LuaLanguageClient ? = null
41+ private var workspaceDiagnoseFuture: CompletableFuture <Unit >? = null
4042
4143 inner class WProject : UserDataHolderBase (), Project {
4244 override fun process (processor : Processor <PsiFile >) {
@@ -68,13 +70,13 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
6870 FileChangeType .Created -> addFile(change.uri)
6971 FileChangeType .Deleted -> removeFile(change.uri)
7072 FileChangeType .Changed -> {
71- if (change.uri.endsWith(" globalStorage" )){
73+ if (change.uri.endsWith(" globalStorage" )) {
7274 return
7375 }
7476 removeFile(change.uri)
7577 addFile(change.uri)
7678 }
77- else -> { }
79+ else -> {}
7880 }
7981 }
8082 }
@@ -83,7 +85,7 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
8385 val settings = params.settings as ? JsonObject ? : return
8486 val ret = VSCodeSettings .update(settings)
8587 if (ret.associationChanged) {
86- loadWorkspace ()
88+ diagnoseWorkspace ()
8789 }
8890 }
8991
@@ -95,14 +97,15 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
9597 fun updateConfig (params : UpdateConfigParams ): CompletableFuture <Void > {
9698 configurationManager.updateConfiguration(params)
9799 loadWorkspace()
100+ diagnoseWorkspace()
98101 return CompletableFuture ()
99102 }
100103
101104 override fun symbol (params : WorkspaceSymbolParams ): CompletableFuture <MutableList <out SymbolInformation >> {
102105 if (params.query.isBlank())
103106 return CompletableFuture .completedFuture(mutableListOf ())
104107 val matcher = CamelHumpMatcher (params.query, false )
105- return computeAsync { cancel->
108+ return computeAsync { cancel ->
106109 val list = mutableListOf<SymbolInformation >()
107110 LuaShortNameIndex .processValues(project, GlobalSearchScope .projectScope(project), Processor {
108111 cancel.checkCanceled()
@@ -123,8 +126,10 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
123126 params.event.removed.forEach {
124127 removeRoot(it.uri)
125128 }
126- if (params.event.added.isNotEmpty())
129+ if (params.event.added.isNotEmpty()) {
127130 loadWorkspace()
131+ diagnoseWorkspace()
132+ }
128133 }
129134
130135 override fun eachRoot (processor : (ws: IFolder ) -> Boolean ) {
@@ -226,6 +231,10 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
226231 })
227232 }
228233
234+ fun diagnoseWorkspace () {
235+ sendAllDiagnostics()
236+ }
237+
229238 private fun loadWorkspace (monitor : IProgressMonitor ) {
230239 monitor.setProgress(" load workspace folders" , 0f )
231240 val collections = fileManager.findAllFiles()
@@ -238,29 +247,39 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
238247 processedCount++
239248 val file = uri.toFile()
240249 if (file != null ) {
241- monitor.setProgress(" Emmy parse file[${(processedCount / totalFileCount * 100 ).toInt()} %]: ${file.canonicalPath} " ,
242- processedCount / totalFileCount)
250+ monitor.setProgress(
251+ " Emmy parse file[${(processedCount / totalFileCount * 100 ).toInt()} %]: ${file.canonicalPath} " ,
252+ processedCount / totalFileCount
253+ )
243254 }
244255 addFile(uri, null )
245256 }
246257 }
247258 monitor.done()
248- sendAllDiagnostics()
249259 }
250260
251261 /* *
252262 * send all diagnostics of the workspace
253263 */
254264 private fun sendAllDiagnostics () {
255- project.process {
256- val file = it.virtualFile
257- if (file is LuaFile ) {
258- file.diagnose()
259- if (file.diagnostics.isNotEmpty()) {
260- client?.publishDiagnostics(PublishDiagnosticsParams (file.uri.toString(), file.diagnostics))
265+ workspaceDiagnoseFuture?.cancel(true )
266+
267+ workspaceDiagnoseFuture = CompletableFutures .computeAsync { cancel ->
268+ project.process {
269+ if (cancel.isCanceled) {
270+ return @process false
271+ }
272+
273+ val file = it.virtualFile
274+ if (file is LuaFile ) {
275+ file.diagnose()
276+ if (file.diagnostics.isNotEmpty()) {
277+ client?.publishDiagnostics(PublishDiagnosticsParams (file.uri.toString(), file.diagnostics))
278+ }
261279 }
280+ true
262281 }
263- true
282+ workspaceDiagnoseFuture = null
264283 }
265284 }
266285
0 commit comments