Browse Source

Remove usages of deprecated Intellij APIs

release/1.2.2 v1.2.2
Alexey Tsvetkov 2 years ago committed by Igor Demin
parent
commit
1a7d74461f
  1. 17
      idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/PreviewFloatingToolbarProvider.kt
  2. 1
      idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/editorUtils.kt
  3. 11
      idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/ijCompatibilityUtils.kt

17
idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/PreviewFloatingToolbarProvider.kt

@ -8,33 +8,18 @@ package org.jetbrains.compose.desktop.ide.preview
import com.intellij.openapi.Disposable
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.application.ReadAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.event.CaretEvent
import com.intellij.openapi.editor.event.CaretListener
import com.intellij.openapi.editor.toolbar.floating.AbstractFloatingToolbarProvider
import com.intellij.openapi.editor.toolbar.floating.FloatingToolbarComponent
import com.intellij.openapi.editor.toolbar.floating.FloatingToolbarComponentImpl
import com.intellij.openapi.project.Project
import com.intellij.util.concurrency.AppExecutorUtil
class PreviewFloatingToolbarProvider : AbstractFloatingToolbarProvider(PREVIEW_EDITOR_TOOLBAR_GROUP_ID) {
override val autoHideable = false
override val priority: Int = 100
// todo: disable if not in Compose JVM module
override fun register(toolbar: FloatingToolbarComponent, parentDisposable: Disposable) {
try {
// todo: use provided data context once 2020.3 is no longer supported
val toolbarClass = FloatingToolbarComponentImpl::class.java
val getDataMethod = toolbarClass.getMethod("getData", String::class.java)
val editor = getDataMethod.invoke(toolbar, CommonDataKeys.EDITOR.name) as? Editor ?: return
registerComponent(toolbar, editor, parentDisposable)
} catch (e: Exception) {
LOG.error(e)
}
}
override fun register(dataContext: DataContext, component: FloatingToolbarComponent, parentDisposable: Disposable) {
val editor = dataContext.getData(CommonDataKeys.EDITOR) ?: return
registerComponent(component, editor, parentDisposable)
@ -59,7 +44,7 @@ internal class PreviewEditorToolbarVisibilityUpdater(
private val editor: Editor
) : CaretListener {
override fun caretPositionChanged(event: CaretEvent) {
ReadAction.nonBlocking { updateVisibility() }
runNonBlocking { updateVisibility() }
.inSmartMode(project)
.submit(AppExecutorUtil.getAppExecutorService())
}

1
idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/editorUtils.kt

@ -9,7 +9,6 @@ import com.intellij.openapi.editor.Editor
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import com.intellij.testFramework.LightVirtualFile
import com.intellij.util.concurrency.annotations.RequiresReadLock
import org.jetbrains.kotlin.idea.KotlinFileType
import org.jetbrains.kotlin.psi.KtNamedFunction

11
idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/ijCompatibilityUtils.kt

@ -6,10 +6,13 @@
package org.jetbrains.compose.desktop.ide.preview
import com.intellij.ide.lightEdit.LightEdit
import com.intellij.openapi.application.NonBlockingReadAction
import com.intellij.openapi.application.ReadAction
import com.intellij.openapi.externalSystem.model.Key
import com.intellij.openapi.externalSystem.model.project.AbstractNamedData
import com.intellij.openapi.project.Project
import java.lang.reflect.Modifier
import java.util.concurrent.Callable
// todo: filter only Compose projects
internal fun isPreviewCompatible(project: Project): Boolean =
@ -33,4 +36,10 @@ internal val kotlinTargetDataKey: Key<out AbstractNamedData> = run {
?: error("Cannot find '${kotlinTargetDataClass.canonicalName}.Companion.getKEY'")
@Suppress("UNCHECKED_CAST")
getKeyMethod.invoke(companionInstance) as Key<out AbstractNamedData>
}
}
internal inline fun runNonBlocking(crossinline fn: () -> Unit): NonBlockingReadAction<Void?> =
ReadAction.nonBlocking(Callable<Void?> {
fn()
null
})
Loading…
Cancel
Save