Skip to content

Commit

Permalink
Add ScoutTemplateContextType
Browse files Browse the repository at this point in the history
Restrict liveTemplates for DoNodes to DoEntities.
  • Loading branch information
fschinkel committed Apr 18, 2023
1 parent ed1833e commit 696d06d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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<out TemplateContextType>?) : 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() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
<category>Scout</category>
</intentionAction>

<liveTemplateContext implementation="org.eclipse.scout.sdk.s2i.template.context.ScoutTemplateContextType$DoEntity"/>
<defaultLiveTemplates file="liveTemplates/Scout.xml"/>
</extensions>
<extensions defaultExtensionNs="org.jetbrains.idea.maven">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<variable name="TYPE" expression="&quot;java.lang.Object&quot;" defaultValue="" alwaysStopAt="true"/>
<variable name="NAME" expression="&quot;myDoValue&quot;" defaultValue="" alwaysStopAt="true"/>
<context>
<option name="JAVA_DECLARATION" value="true"/>
<option name="DO_ENTITY" value="true"/>
</context>
</template>
<template resource-bundle="messages.EclipseScoutBundle" key="live.template.description.dol"
Expand All @@ -23,7 +23,7 @@
<variable name="TYPE" expression="&quot;java.lang.Object&quot;" defaultValue="" alwaysStopAt="true"/>
<variable name="NAME" expression="&quot;myDoValue&quot;" defaultValue="" alwaysStopAt="true"/>
<context>
<option name="JAVA_DECLARATION" value="true"/>
<option name="DO_ENTITY" value="true"/>
</context>
</template>
<template resource-bundle="messages.EclipseScoutBundle" key="live.template.description.dos"
Expand All @@ -32,7 +32,7 @@
<variable name="TYPE" expression="&quot;java.lang.Object&quot;" defaultValue="" alwaysStopAt="true"/>
<variable name="NAME" expression="&quot;myDoValue&quot;" defaultValue="" alwaysStopAt="true"/>
<context>
<option name="JAVA_DECLARATION" value="true"/>
<option name="DO_ENTITY" value="true"/>
</context>
</template>
<template resource-bundle="messages.EclipseScoutBundle" key="live.template.description.doc"
Expand All @@ -41,7 +41,7 @@
<variable name="TYPE" expression="&quot;java.lang.Object&quot;" defaultValue="" alwaysStopAt="true"/>
<variable name="NAME" expression="&quot;myDoValue&quot;" defaultValue="" alwaysStopAt="true"/>
<context>
<option name="JAVA_DECLARATION" value="true"/>
<option name="DO_ENTITY" value="true"/>
</context>
</template>
</templateSet>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 696d06d

Please sign in to comment.