From 696d06d172d49d65f476bebdafd2ad3fcc4ccb6d Mon Sep 17 00:00:00 2001 From: Fritz Schinkel Date: Tue, 18 Apr 2023 16:07:31 +0200 Subject: [PATCH] Add ScoutTemplateContextType Restrict liveTemplates for DoNodes to DoEntities. --- .../context/ScoutTemplateContextType.kt | 43 +++++++++++++++++++ .../src/main/resources/META-INF/plugin.xml | 1 + .../main/resources/liveTemplates/Scout.xml | 8 ++-- .../messages/EclipseScoutBundle.properties | 1 + 4 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 org.eclipse.scout.sdk.s2i/src/main/kotlin/org/eclipse/scout/sdk/s2i/template/context/ScoutTemplateContextType.kt diff --git a/org.eclipse.scout.sdk.s2i/src/main/kotlin/org/eclipse/scout/sdk/s2i/template/context/ScoutTemplateContextType.kt b/org.eclipse.scout.sdk.s2i/src/main/kotlin/org/eclipse/scout/sdk/s2i/template/context/ScoutTemplateContextType.kt new file mode 100644 index 000000000..d689564f5 --- /dev/null +++ b/org.eclipse.scout.sdk.s2i/src/main/kotlin/org/eclipse/scout/sdk/s2i/template/context/ScoutTemplateContextType.kt @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2010, 2023 BSI Business Systems Integration AG + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.eclipse.scout.sdk.s2i.template.context + +import com.intellij.codeInsight.template.JavaCodeContextType +import com.intellij.codeInsight.template.TemplateContextType +import com.intellij.psi.PsiClass +import com.intellij.psi.PsiElement +import com.intellij.psi.util.PsiTreeUtil +import org.eclipse.scout.sdk.core.java.apidef.ITypeNameSupplier +import org.eclipse.scout.sdk.core.s.java.apidef.IScoutApi +import org.eclipse.scout.sdk.s2i.EclipseScoutBundle +import org.eclipse.scout.sdk.s2i.containingModule +import org.eclipse.scout.sdk.s2i.environment.IdeaEnvironment +import org.eclipse.scout.sdk.s2i.environment.IdeaProgress +import org.eclipse.scout.sdk.s2i.toScoutType +import org.eclipse.scout.sdk.s2i.util.ApiHelper + +abstract class ScoutTemplateContextType(id: String, presentableName: String, baseContextType: Class?) : JavaCodeContextType(id, presentableName, baseContextType) { + + protected fun isInContext(element: PsiElement, instanceCheckTypeNameSupplier: (IScoutApi) -> ITypeNameSupplier): Boolean { + val module = element.containingModule() ?: return false + val clazz = PsiTreeUtil.getParentOfType(element, PsiClass::class.java) ?: return false + return IdeaEnvironment.callInIdeaEnvironmentSync(module.project, IdeaProgress.empty()) { env, _ -> + val type = clazz.toScoutType(env) ?: return@callInIdeaEnvironmentSync false + ApiHelper.scoutApiFor(clazz, env) + ?.let { instanceCheckTypeNameSupplier(it) } + ?.let { type.isInstanceOf(it) } == true + } + } + + class DoEntity : ScoutTemplateContextType("DO_ENTITY", EclipseScoutBundle.message("live.template.context.doentity"), Declaration::class.java) { + + override fun isInContext(element: PsiElement) = isInContext(element) { it.DoEntity() } + } +} diff --git a/org.eclipse.scout.sdk.s2i/src/main/resources/META-INF/plugin.xml b/org.eclipse.scout.sdk.s2i/src/main/resources/META-INF/plugin.xml index 77a526b5c..c96e09057 100644 --- a/org.eclipse.scout.sdk.s2i/src/main/resources/META-INF/plugin.xml +++ b/org.eclipse.scout.sdk.s2i/src/main/resources/META-INF/plugin.xml @@ -142,6 +142,7 @@ Scout + diff --git a/org.eclipse.scout.sdk.s2i/src/main/resources/liveTemplates/Scout.xml b/org.eclipse.scout.sdk.s2i/src/main/resources/liveTemplates/Scout.xml index ecf7757a9..1b7beeab7 100644 --- a/org.eclipse.scout.sdk.s2i/src/main/resources/liveTemplates/Scout.xml +++ b/org.eclipse.scout.sdk.s2i/src/main/resources/liveTemplates/Scout.xml @@ -14,7 +14,7 @@ - diff --git a/org.eclipse.scout.sdk.s2i/src/main/resources/messages/EclipseScoutBundle.properties b/org.eclipse.scout.sdk.s2i/src/main/resources/messages/EclipseScoutBundle.properties index 10b2fff83..28c485100 100644 --- a/org.eclipse.scout.sdk.s2i/src/main/resources/messages/EclipseScoutBundle.properties +++ b/org.eclipse.scout.sdk.s2i/src/main/resources/messages/EclipseScoutBundle.properties @@ -184,6 +184,7 @@ also.show.preview.versions=Also show preview versions invalid.scout.version=Invalid Scout version project.settings=Project Settings translations.changed.reload.question=Some translations changed outside the editor.\nWould you like to reload the editor? +live.template.context.doentity=DoEntity live.template.description.dov=DoValue live.template.description.dol=DoList live.template.description.dos=DoSet