diff --git a/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/idea/preview/PreviewEntryPoint.kt b/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/idea/preview/PreviewEntryPoint.kt new file mode 100644 index 00000000..3d9f3856 --- /dev/null +++ b/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 + } +} \ No newline at end of file diff --git a/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/idea/preview/PreviewRunConfigurationProducer.kt b/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/idea/preview/PreviewRunConfigurationProducer.kt index 92d17901..6e9153d0 100644 --- a/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/idea/preview/PreviewRunConfigurationProducer.kt +++ b/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() { override fun getConfigurationFactory(): ConfigurationFactory = diff --git a/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/idea/preview/PreviewRunLineMarkerContributor.kt b/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/idea/preview/PreviewRunLineMarkerContributor.kt index d07d60da..471635ce 100644 --- a/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/idea/preview/PreviewRunLineMarkerContributor.kt +++ b/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. diff --git a/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/idea/preview/locationUtils.kt b/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/idea/preview/locationUtils.kt index b140db4e..b6d87664 100644 --- a/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/idea/preview/locationUtils.kt +++ b/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. * diff --git a/idea-plugin/src/main/resources/META-INF/plugin.xml b/idea-plugin/src/main/resources/META-INF/plugin.xml index 398a6f9d..bda5eeae 100644 --- a/idea-plugin/src/main/resources/META-INF/plugin.xml +++ b/idea-plugin/src/main/resources/META-INF/plugin.xml @@ -15,5 +15,6 @@ implementationClass="org.jetbrains.compose.desktop.idea.preview.PreviewRunLineMarkerContributor"/> + diff --git a/license/README.md b/license/README.md index 11a466c8..dbc535c3 100644 --- a/license/README.md +++ b/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 - * 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 + * 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 -