@@ -35,14 +35,12 @@ import com.redhat.devtools.intellij.kubernetes.editor.util.getDocument
35
35
import com.redhat.devtools.intellij.kubernetes.editor.util.isKubernetesResource
36
36
import com.redhat.devtools.intellij.kubernetes.model.IResourceModel
37
37
import com.redhat.devtools.intellij.kubernetes.model.IResourceModelListener
38
- import com.redhat.devtools.intellij.kubernetes.model.Notification
39
38
import com.redhat.devtools.intellij.kubernetes.model.context.IActiveContext
40
- import com.redhat.devtools.intellij.kubernetes.model.util.KubernetesClientExceptionUtils.statusMessage
41
39
import com.redhat.devtools.intellij.kubernetes.model.util.ResourceException
42
40
import com.redhat.devtools.intellij.kubernetes.model.util.toMessage
43
41
import com.redhat.devtools.intellij.kubernetes.model.util.toTitle
44
- import com.redhat.devtools.intellij.kubernetes.model.util.trimWithEllipsis
45
42
import io.fabric8.kubernetes.api.model.HasMetadata
43
+ import java.util.concurrent.CompletableFuture
46
44
47
45
/* *
48
46
* A Decorator for [FileEditor] instances that allows to push or pull the editor content to/from a remote cluster.
@@ -77,14 +75,14 @@ open class ResourceEditor(
77
75
private val getPsiDocumentManager : (Project ) -> PsiDocumentManager = { PsiDocumentManager .getInstance(project) },
78
76
// for mocking purposes
79
77
@Suppress(" NAME_SHADOWING" )
80
- private val getKubernetesResourceInfo : (VirtualFile ? , Project ) -> KubernetesResourceInfo ? = {
81
- file, project -> com.redhat.devtools.intellij.kubernetes.editor.util.getKubernetesResourceInfo(file,project)
78
+ private val getKubernetesResourceInfo : (VirtualFile ? , Project ) -> KubernetesResourceInfo ? = { file, project ->
79
+ com.redhat.devtools.intellij.kubernetes.editor.util.getKubernetesResourceInfo(file, project)
82
80
},
83
81
// for mocking purposes
84
82
private val diff : ResourceDiff = ResourceDiff (project),
85
83
// for mocking purposes
86
84
protected val editorResources : EditorResources = EditorResources (resourceModel)
87
- ): Disposable {
85
+ ) : Disposable {
88
86
89
87
init {
90
88
Disposer .register(editor, this )
@@ -112,14 +110,15 @@ open class ResourceEditor(
112
110
val resources = createResources(
113
111
getDocument(editor),
114
112
editor.file?.fileType,
115
- resourceModel.getCurrentNamespace())
113
+ resourceModel.getCurrentNamespace()
114
+ )
116
115
val editorResources = editorResources.setResources(resources)
117
116
118
117
if (editorResources.size == 1 ) {
119
118
// show notification for 1 resource
120
119
val editorResource = editorResources.firstOrNull() ? : return @runAsync
121
120
showNotification(editorResource)
122
- } else if (editorResources.size > 1 ){
121
+ } else if (editorResources.size > 1 ) {
123
122
// show notification for multiple resources
124
123
showNotification(editorResources)
125
124
}
@@ -155,6 +154,7 @@ open class ResourceEditor(
155
154
*/
156
155
is Modified ->
157
156
showPushNotification(true , listOf (editorResource))
157
+
158
158
else ->
159
159
runInUI {
160
160
hideNotifications()
@@ -170,7 +170,7 @@ open class ResourceEditor(
170
170
}
171
171
val inError = editorResources.filter(FILTER_ERROR )
172
172
if (inError.isNotEmpty()) {
173
- showErrorNotification(inError)
173
+ showErrorNotification(inError)
174
174
} else {
175
175
runInUI {
176
176
hideNotifications()
@@ -300,43 +300,38 @@ open class ResourceEditor(
300
300
}
301
301
}
302
302
303
- fun diff () {
304
- val manager = getPsiDocumentManager.invoke(project)
305
- val file = editor.file ? : return
306
- var fileType: FileType ? = null
307
- var beforeDiff: String? = null
308
- runInUI {
309
- val document = getDocument.invoke(editor) ? : return @runInUI
310
- fileType = getFileType(document, manager) ? : return @runInUI
311
- beforeDiff = document.text
312
- }
313
- if (fileType == null
314
- || beforeDiff == null
315
- ) {
316
- return
317
- }
318
- runAsync {
319
- try {
303
+ open fun diff (): CompletableFuture <Unit > {
304
+ return CompletableFuture
305
+ .supplyAsync(
306
+ // UI thread required
307
+ {
308
+ val file = editor.file ? : throw ResourceException (" Editor ${editor.name} has no file." )
309
+ val manager = getPsiDocumentManager.invoke(project)
310
+ val document = getDocument.invoke(editor) ? : throw ResourceException (" Could not get document for editor ${editor.name} ." )
311
+ val fileType = getFileType(document, manager) ? : throw ResourceException (" Could not determine file type for editor ${editor.name} ." )
312
+ DiffContext (file, fileType, document.text)
313
+ },
314
+ { runInUI { it.run () } }
315
+ ).thenApplyAsync { context ->
320
316
val resourcesOnCluster = editorResources.getAllResourcesOnCluster()
321
- val serialized = serialize(resourcesOnCluster, fileType) ? : return @runAsync
322
- runInUI {
323
- diff.open(file, serialized) { onDiffClosed(beforeDiff) }
324
- }
325
- } catch (e: ResourceException ) {
326
- val message = trimWithEllipsis(e.message, 100 )
327
- val causeMessage = statusMessage(e.cause)
328
- Notification ()
329
- .error(" Could not open diff" ,
330
- if (causeMessage == null ) {
331
- message ? : " "
332
- } else if (message == null ) {
333
- causeMessage
334
- } else {
335
- " $message : $causeMessage "
336
- }
337
- )
338
- }
339
- }
317
+ val serialized = serialize(resourcesOnCluster, context.fileType) ? : throw ResourceException (" Could not serialize cluster resources for editor ${editor.name} ." )
318
+ context.clusterResources = serialized
319
+ context
320
+ }.thenApplyAsync(
321
+ // UI thread required
322
+ { context ->
323
+ diff.open(context.file, context.clusterResources) { onDiffClosed(context.document) }
324
+ },
325
+ { runInUI { it.run () } }
326
+ )
327
+ }
328
+
329
+ private class DiffContext (
330
+ val file : VirtualFile ,
331
+ val fileType : FileType ,
332
+ val document : String
333
+ ) {
334
+ lateinit var clusterResources: String
340
335
}
341
336
342
337
/*
@@ -423,13 +418,14 @@ open class ResourceEditor(
423
418
}
424
419
}
425
420
426
- override fun currentNamespaceChanged (new : IActiveContext <* ,* >? , old : IActiveContext <* ,* >? ) {
421
+ override fun currentNamespaceChanged (new : IActiveContext <* , * >? , old : IActiveContext <* , * >? ) {
427
422
// current namespace in same context has changed, recreate cluster resource
428
423
editorResources.disposeAll()
429
424
update()
430
425
}
431
426
}
432
427
}
428
+
433
429
/* *
434
430
* Closes this instance and cleans up references to it.
435
431
* - Removes the resource model listener,
@@ -452,7 +448,9 @@ open class ResourceEditor(
452
448
protected open fun enableEditingNonProjectFile () {
453
449
if (editor.file == null
454
450
|| ! isKubernetesResource(
455
- getKubernetesResourceInfo.invoke(editor.file, project))){
451
+ getKubernetesResourceInfo.invoke(editor.file, project)
452
+ )
453
+ ) {
456
454
return
457
455
}
458
456
createResourceFileForVirtual(editor.file)?.enableEditingNonProjectFile()
@@ -477,7 +475,7 @@ open class ResourceEditor(
477
475
}
478
476
479
477
/* * for testing purposes */
480
- protected open fun <R : Any > runReadCommand (runnable : () -> R ? ): R ? {
478
+ protected open fun <R : Any > runReadCommand (runnable : () -> R ? ): R ? {
481
479
return ReadAction .compute<R , Exception >(runnable)
482
480
}
483
481
0 commit comments