Minor code simplifications + no i18n

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