Minor code simplifications + no i18n
This commit is contained in:
@@ -102,10 +102,6 @@ livetemplate.description.anonymous=Anonymous class
|
|||||||
livetemplate.description.exfun=Extension function
|
livetemplate.description.exfun=Extension function
|
||||||
livetemplate.description.exval=Extension read-only property
|
livetemplate.description.exval=Extension read-only property
|
||||||
livetemplate.description.exvar=Extension read-write property
|
livetemplate.description.exvar=Extension read-write property
|
||||||
macro.variable.of.type=kotlinVariable()
|
|
||||||
macro.iterable.variable=kotlinIterableVariable()
|
|
||||||
macro.suggest.variable.name=kotlinSuggestVariableName()
|
|
||||||
macro.fun.parameters=functionParameters()
|
|
||||||
change.visibility.modifier=Change visibility modifier
|
change.visibility.modifier=Change visibility modifier
|
||||||
|
|
||||||
options.kotlin.attribute.descriptor.builtin.annotation=Built-in annotation
|
options.kotlin.attribute.descriptor.builtin.annotation=Built-in annotation
|
||||||
|
|||||||
@@ -48,26 +48,25 @@ class AnonymousSuperMacro : Macro() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val vars = getSupertypes(params, context)
|
val vars = getSupertypes(params, context)
|
||||||
if (vars == null || vars.size == 0) return null
|
if (vars.isEmpty()) return null
|
||||||
return KotlinPsiElementResult(vars.first())
|
return KotlinPsiElementResult(vars.first())
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun calculateLookupItems(params: Array<Expression>, context: ExpressionContext): Array<LookupElement>? {
|
override fun calculateLookupItems(params: Array<Expression>, context: ExpressionContext): Array<LookupElement>? {
|
||||||
val superTypes = getSupertypes(params, context)
|
val superTypes = getSupertypes(params, context)
|
||||||
if (superTypes == null || superTypes.size < 2) return null
|
if (superTypes.size < 2) return null
|
||||||
return superTypes.map { LookupElementBuilder.create(it) }.toTypedArray()
|
return superTypes.map { LookupElementBuilder.create(it) }.toTypedArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getSupertypes(params: Array<Expression>, context: ExpressionContext): Array<PsiNamedElement>? {
|
private fun getSupertypes(params: Array<Expression>, context: ExpressionContext): Collection<PsiNamedElement> {
|
||||||
if (params.size != 0) return null
|
if (params.size != 0) return emptyList()
|
||||||
|
|
||||||
val project = context.project
|
val psiDocumentManager = PsiDocumentManager.getInstance(context.project)
|
||||||
PsiDocumentManager.getInstance(project).commitAllDocuments()
|
psiDocumentManager.commitAllDocuments()
|
||||||
|
|
||||||
val psiFile = PsiDocumentManager.getInstance(project).getPsiFile(context.editor!!.document)
|
val psiFile = psiDocumentManager.getPsiFile(context.editor!!.document) as? KtFile ?: return emptyList()
|
||||||
if (psiFile !is KtFile) return null
|
|
||||||
|
|
||||||
val expression = PsiTreeUtil.getParentOfType(psiFile.findElementAt(context.startOffset), KtExpression::class.java) ?: return null
|
val expression = PsiTreeUtil.getParentOfType(psiFile.findElementAt(context.startOffset), KtExpression::class.java) ?: return emptyList()
|
||||||
|
|
||||||
val bindingContext = expression.analyze(BodyResolveMode.FULL)
|
val bindingContext = expression.analyze(BodyResolveMode.FULL)
|
||||||
val resolutionScope = expression.getResolutionScope(bindingContext, expression.getResolutionFacade())
|
val resolutionScope = expression.getResolutionScope(bindingContext, expression.getResolutionFacade())
|
||||||
@@ -76,6 +75,5 @@ class AnonymousSuperMacro : Macro() {
|
|||||||
.collectDescriptorsFiltered(DescriptorKindFilter.NON_SINGLETON_CLASSIFIERS)
|
.collectDescriptorsFiltered(DescriptorKindFilter.NON_SINGLETON_CLASSIFIERS)
|
||||||
.filter { it is ClassDescriptor && it.modality.isOverridable && (it.kind == ClassKind.CLASS || it.kind == ClassKind.INTERFACE) }
|
.filter { it is ClassDescriptor && it.modality.isOverridable && (it.kind == ClassKind.CLASS || it.kind == ClassKind.INTERFACE) }
|
||||||
.mapNotNull { DescriptorToSourceUtils.descriptorToDeclaration(it) as PsiNamedElement? }
|
.mapNotNull { DescriptorToSourceUtils.descriptorToDeclaration(it) as PsiNamedElement? }
|
||||||
.toTypedArray()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-3
@@ -53,9 +53,7 @@ internal class AnonymousTemplateEditingListener(private val psiFile: PsiFile, pr
|
|||||||
|
|
||||||
override fun templateFinished(template: Template?, brokenOff: Boolean) {
|
override fun templateFinished(template: Template?, brokenOff: Boolean) {
|
||||||
editor.putUserData(LISTENER_KEY, null)
|
editor.putUserData(LISTENER_KEY, null)
|
||||||
if (brokenOff) {
|
if (brokenOff) return
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (classDescriptor != null) {
|
if (classDescriptor != null) {
|
||||||
if (classDescriptor!!.kind == ClassKind.CLASS) {
|
if (classDescriptor!!.kind == ClassKind.CLASS) {
|
||||||
|
|||||||
@@ -18,22 +18,11 @@ package org.jetbrains.kotlin.idea.liveTemplates.macro
|
|||||||
|
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import org.jetbrains.kotlin.descriptors.VariableDescriptor
|
import org.jetbrains.kotlin.descriptors.VariableDescriptor
|
||||||
import org.jetbrains.kotlin.idea.KotlinBundle
|
|
||||||
import org.jetbrains.kotlin.idea.core.IterableTypesDetector
|
import org.jetbrains.kotlin.idea.core.IterableTypesDetector
|
||||||
|
|
||||||
class AnyVariableMacro : BaseKotlinVariableMacro() {
|
class AnyVariableMacro : BaseKotlinVariableMacro() {
|
||||||
override fun getName(): String {
|
override fun getName() = "kotlinVariable"
|
||||||
return "kotlinVariable"
|
override fun getPresentableName() = "kotlinVariable()"
|
||||||
}
|
|
||||||
|
|
||||||
override fun getPresentableName(): String {
|
override fun isSuitable(variableDescriptor: VariableDescriptor, project: Project, iterableTypesDetector: IterableTypesDetector) = true
|
||||||
return KotlinBundle.message("macro.variable.of.type")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun isSuitable(
|
|
||||||
variableDescriptor: VariableDescriptor,
|
|
||||||
project: Project,
|
|
||||||
iterableTypesDetector: IterableTypesDetector): Boolean {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,16 +47,15 @@ import org.jetbrains.kotlin.resolve.scopes.utils.getImplicitReceiversHierarchy
|
|||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
abstract class BaseKotlinVariableMacro : Macro() {
|
abstract class BaseKotlinVariableMacro : Macro() {
|
||||||
private fun getVariables(params: Array<Expression>, context: ExpressionContext): Array<KtNamedDeclaration>? {
|
private fun getVariables(params: Array<Expression>, context: ExpressionContext): Collection<KtNamedDeclaration> {
|
||||||
if (params.size != 0) return null
|
if (params.size != 0) return emptyList()
|
||||||
|
|
||||||
val project = context.project
|
val project = context.project
|
||||||
PsiDocumentManager.getInstance(project).commitAllDocuments()
|
PsiDocumentManager.getInstance(project).commitAllDocuments()
|
||||||
|
|
||||||
val psiFile = PsiDocumentManager.getInstance(project).getPsiFile(context.editor!!.document)
|
val psiFile = PsiDocumentManager.getInstance(project).getPsiFile(context.editor!!.document) as? KtFile ?: return emptyList()
|
||||||
if (psiFile !is KtFile) return null
|
|
||||||
|
|
||||||
val contextExpression = findContextExpression(psiFile, context.startOffset) ?: return null
|
val contextExpression = findContextExpression(psiFile, context.startOffset) ?: return emptyList()
|
||||||
|
|
||||||
val resolutionFacade = contextExpression.getResolutionFacade()
|
val resolutionFacade = contextExpression.getResolutionFacade()
|
||||||
|
|
||||||
@@ -95,7 +94,7 @@ abstract class BaseKotlinVariableMacro : Macro() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return declarations.toTypedArray()
|
return declarations
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getAllVariables(scope: LexicalScope): Collection<DeclarationDescriptor> {
|
private fun getAllVariables(scope: LexicalScope): Collection<DeclarationDescriptor> {
|
||||||
@@ -125,17 +124,13 @@ abstract class BaseKotlinVariableMacro : Macro() {
|
|||||||
|
|
||||||
override fun calculateResult(params: Array<Expression>, context: ExpressionContext): Result? {
|
override fun calculateResult(params: Array<Expression>, context: ExpressionContext): Result? {
|
||||||
val vars = getVariables(params, context)
|
val vars = getVariables(params, context)
|
||||||
if (vars == null || vars.size == 0) return null
|
if (vars.isEmpty()) return null
|
||||||
return KotlinPsiElementResult(vars[0])
|
return KotlinPsiElementResult(vars.first())
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun calculateLookupItems(params: Array<Expression>, context: ExpressionContext): Array<LookupElement>? {
|
override fun calculateLookupItems(params: Array<Expression>, context: ExpressionContext): Array<LookupElement>? {
|
||||||
val vars = getVariables(params, context)
|
val vars = getVariables(params, context)
|
||||||
if (vars == null || vars.size < 2) return null
|
if (vars.size < 2) return null
|
||||||
val set = LinkedHashSet<LookupElement>()
|
return vars.map { LookupElementBuilder.create(it) }.toTypedArray()
|
||||||
for (`var` in vars) {
|
|
||||||
set.add(LookupElementBuilder.create(`var`))
|
|
||||||
}
|
|
||||||
return set.toArray<LookupElement>(arrayOfNulls<LookupElement>(set.size))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,18 +18,12 @@ package org.jetbrains.kotlin.idea.liveTemplates.macro
|
|||||||
|
|
||||||
import com.intellij.codeInsight.template.*
|
import com.intellij.codeInsight.template.*
|
||||||
import com.intellij.psi.PsiDocumentManager
|
import com.intellij.psi.PsiDocumentManager
|
||||||
import org.jetbrains.kotlin.idea.KotlinBundle
|
|
||||||
import org.jetbrains.kotlin.psi.KtFunction
|
import org.jetbrains.kotlin.psi.KtFunction
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class FunctionParametersMacro : Macro() {
|
class FunctionParametersMacro : Macro() {
|
||||||
override fun getName(): String {
|
override fun getName() = "functionParameters"
|
||||||
return "functionParameters"
|
override fun getPresentableName() = "functionParameters()"
|
||||||
}
|
|
||||||
|
|
||||||
override fun getPresentableName(): String {
|
|
||||||
return KotlinBundle.message("macro.fun.parameters")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun calculateResult(params: Array<Expression>, context: ExpressionContext): Result? {
|
override fun calculateResult(params: Array<Expression>, context: ExpressionContext): Result? {
|
||||||
val project = context.project
|
val project = context.project
|
||||||
@@ -53,8 +47,6 @@ class FunctionParametersMacro : Macro() {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun isAcceptableInContext(context: TemplateContextType?): Boolean {
|
override fun isAcceptableInContext(context: TemplateContextType?) = context is JavaCodeContextType
|
||||||
return context is JavaCodeContextType
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,18 +18,11 @@ package org.jetbrains.kotlin.idea.liveTemplates.macro
|
|||||||
|
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import org.jetbrains.kotlin.descriptors.VariableDescriptor
|
import org.jetbrains.kotlin.descriptors.VariableDescriptor
|
||||||
import org.jetbrains.kotlin.idea.KotlinBundle
|
|
||||||
import org.jetbrains.kotlin.idea.core.IterableTypesDetector
|
import org.jetbrains.kotlin.idea.core.IterableTypesDetector
|
||||||
|
|
||||||
class IterableVariableMacro : BaseKotlinVariableMacro() {
|
class IterableVariableMacro : BaseKotlinVariableMacro() {
|
||||||
|
override fun getName() = "kotlinIterableVariable"
|
||||||
override fun getName(): String {
|
override fun getPresentableName() = "kotlinIterableVariable()"
|
||||||
return "kotlinIterableVariable"
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getPresentableName(): String {
|
|
||||||
return KotlinBundle.message("macro.iterable.variable")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun isSuitable(
|
override fun isSuitable(
|
||||||
variableDescriptor: VariableDescriptor,
|
variableDescriptor: VariableDescriptor,
|
||||||
|
|||||||
@@ -20,7 +20,5 @@ import com.intellij.codeInsight.template.JavaPsiElementResult
|
|||||||
import com.intellij.psi.PsiNamedElement
|
import com.intellij.psi.PsiNamedElement
|
||||||
|
|
||||||
class KotlinPsiElementResult(element: PsiNamedElement) : JavaPsiElementResult(element) {
|
class KotlinPsiElementResult(element: PsiNamedElement) : JavaPsiElementResult(element) {
|
||||||
override fun toString(): String {
|
override fun toString() = (element as PsiNamedElement).name ?: ""
|
||||||
return (element as PsiNamedElement).name ?: ""
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,17 +18,11 @@ package org.jetbrains.kotlin.idea.liveTemplates.macro
|
|||||||
|
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import org.jetbrains.kotlin.descriptors.VariableDescriptor
|
import org.jetbrains.kotlin.descriptors.VariableDescriptor
|
||||||
import org.jetbrains.kotlin.idea.KotlinBundle
|
|
||||||
import org.jetbrains.kotlin.idea.core.IterableTypesDetector
|
import org.jetbrains.kotlin.idea.core.IterableTypesDetector
|
||||||
|
|
||||||
class SuggestVariableNameMacro : BaseKotlinVariableMacro() {
|
class SuggestVariableNameMacro : BaseKotlinVariableMacro() {
|
||||||
override fun getName(): String {
|
override fun getName() = "kotlinSuggestVariableName"
|
||||||
return "kotlinSuggestVariableName"
|
override fun getPresentableName() = "kotlinSuggestVariableName()"
|
||||||
}
|
|
||||||
|
|
||||||
override fun getPresentableName(): String {
|
|
||||||
return KotlinBundle.message("macro.suggest.variable.name")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun isSuitable(
|
override fun isSuitable(
|
||||||
variableDescriptor: VariableDescriptor,
|
variableDescriptor: VariableDescriptor,
|
||||||
|
|||||||
Reference in New Issue
Block a user