Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ScoutTemplateContextType #13

Draft
wants to merge 1 commit into
base: releases/13.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the transformation from PsiClass to IType really necessary? Because this loads the classpath and is therefore quite expensive just to perform a check of the super-hierarchy. Maybe use InheritanceUtil on clazz instead?
Then the IdeaEnvironment must not be created explicitly and the code becomes much simpler?

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