Browse Source

Specify Preview as entry point

So it's not marked as dead code by Intellij
pull/583/head
Alexey Tsvetkov 4 years ago committed by Alexey Tsvetkov
parent
commit
2f0cd93e71
  1. 58
      idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/idea/preview/PreviewEntryPoint.kt
  2. 18
      idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/idea/preview/PreviewRunConfigurationProducer.kt
  3. 20
      idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/idea/preview/PreviewRunLineMarkerContributor.kt
  4. 20
      idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/idea/preview/locationUtils.kt
  5. 1
      idea-plugin/src/main/resources/META-INF/plugin.xml
  6. 6
      license/README.md

58
idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/idea/preview/PreviewEntryPoint.kt

@ -0,0 +1,58 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.compose.desktop.idea.preview
import com.intellij.codeInspection.reference.EntryPoint
import com.intellij.codeInspection.reference.RefElement
import com.intellij.configurationStore.deserializeInto
import com.intellij.configurationStore.serializeObjectInto
import com.intellij.openapi.util.InvalidDataException
import com.intellij.openapi.util.WriteExternalException
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiMethod
import org.jdom.Element
import org.jetbrains.annotations.Nls
/**
* [EntryPoint] implementation to mark `@Preview` functions as entry points and avoid them being flagged as unused.
*
* Based on
* com.android.tools.idea.compose.preview.PreviewEntryPoint from AOSP
* with modifications
*/
class PreviewEntryPoint : EntryPoint() {
private var ADD_PREVIEW_TO_ENTRIES: Boolean = true
override fun isEntryPoint(refElement: RefElement, psiElement: PsiElement): Boolean = isEntryPoint(psiElement)
override fun isEntryPoint(psiElement: PsiElement): Boolean =
psiElement is PsiMethod && psiElement.hasAnnotation(DESKTOP_PREVIEW_ANNOTATION_FQN)
override fun readExternal(element: Element) = element.deserializeInto(this)
override fun writeExternal(element: Element) {
serializeObjectInto(this, element)
}
override fun getDisplayName(): String = "Compose Preview"
override fun isSelected(): Boolean = ADD_PREVIEW_TO_ENTRIES
override fun setSelected(selected: Boolean) {
this.ADD_PREVIEW_TO_ENTRIES = selected
}
}

18
idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/idea/preview/PreviewRunConfigurationProducer.kt

@ -1,6 +1,17 @@
/*
* Copyright 2020-2021 JetBrains s.r.o. and respective authors and developers.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.compose.desktop.idea.preview
@ -22,6 +33,9 @@ import org.jetbrains.plugins.gradle.service.execution.GradleRunConfiguration
*
* The [ConfigurationContext] where the [ComposePreviewRunConfiguration] is created from can be any descendant of the `@Composable` function
* in the PSI tree, such as its annotations, function name or even the keyword "fun".
*
* Based on com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer from AOSP
* with modifications
*/
class PreviewRunConfigurationProducer : LazyRunConfigurationProducer<GradleRunConfiguration>() {
override fun getConfigurationFactory(): ConfigurationFactory =

20
idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/idea/preview/PreviewRunLineMarkerContributor.kt

@ -1,6 +1,17 @@
/*
* Copyright 2020-2021 JetBrains s.r.o. and respective authors and developers.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.compose.desktop.idea.preview
@ -12,6 +23,11 @@ import com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtNamedFunction
/**
* Based on
* com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunLineMarkerContributor from AOSP
* with modifications
*/
class PreviewRunLineMarkerContributor : RunLineMarkerContributor() {
override fun getInfo(element: PsiElement): Info? {
// Marker should be in a single LeafPsiElement. We choose the identifier and return null for other elements within the function.

20
idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/idea/preview/locationUtils.kt

@ -1,6 +1,17 @@
/*
* Copyright 2020-2021 JetBrains s.r.o. and respective authors and developers.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.compose.desktop.idea.preview
@ -18,6 +29,11 @@ import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
internal const val DESKTOP_PREVIEW_ANNOTATION_FQN = "androidx.compose.ui.tooling.desktop.preview.Preview"
/**
* Utils based on functions from AOSP, taken from
* tools/adt/idea/compose-designer/src/com/android/tools/idea/compose/preview/util/PreviewElement.kt
*/
/**
* Whether this function is properly annotated with [PREVIEW_ANNOTATION_FQN] and is defined in a valid location.
*

1
idea-plugin/src/main/resources/META-INF/plugin.xml

@ -15,5 +15,6 @@
implementationClass="org.jetbrains.compose.desktop.idea.preview.PreviewRunLineMarkerContributor"/>
<runConfigurationProducer
implementation="org.jetbrains.compose.desktop.idea.preview.PreviewRunConfigurationProducer"/>
<deadCode implementation="org.jetbrains.compose.desktop.idea.preview.PreviewEntryPoint" />
</extensions>
</idea-plugin>

6
license/README.md

@ -4,13 +4,9 @@ to which different licenses may apply:
## Compose IDEA plugin
* Path: idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/idea/preview/PreviewRunLineMarkerContributor.kt
* License: Apache 2 (license/third_party/aosp_license.txt)
* Origin: Copyright 2021 The Android Open Source Project
* Path: idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/idea/preview/locationUtils.kt
* Path: idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/idea/preview
* License: Apache 2 (license/third_party/aosp_license.txt)
* Origin: Copyright 2021 The Android Open Source Project
* Path: idea-plugin/src/main/resources/icons/compose/compose.svg
* License: Apache 2 (license/third_party/aosp_license.txt)
* Origin: Copyright 2021 The Android Open Source Project

Loading…
Cancel
Save