From 7ea6506b94f2838f68301e10da64e73f5cefc35c Mon Sep 17 00:00:00 2001 From: Daniil Ovchinnikov Date: Mon, 27 Nov 2017 19:02:37 +0300 Subject: [PATCH] [groovy] logback: use new imports API No need in separate aliased import processing since aliased imports can be contributed. --- .../src/META-INF/groovy-logback-support.xml | 1 - ...LogbackAliasedImportedMemberContributor.kt | 64 ------------------- .../ext/logback/LogbackImportContributor.kt | 40 ++---------- .../plugins/groovy/ext/logback/util.kt | 42 +++++++----- 4 files changed, 33 insertions(+), 114 deletions(-) delete mode 100644 plugins/groovy/src/org/jetbrains/plugins/groovy/ext/logback/LogbackAliasedImportedMemberContributor.kt diff --git a/plugins/groovy/src/META-INF/groovy-logback-support.xml b/plugins/groovy/src/META-INF/groovy-logback-support.xml index ded087cd5a14..945474f5946e 100644 --- a/plugins/groovy/src/META-INF/groovy-logback-support.xml +++ b/plugins/groovy/src/META-INF/groovy-logback-support.xml @@ -3,7 +3,6 @@ - diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/ext/logback/LogbackAliasedImportedMemberContributor.kt b/plugins/groovy/src/org/jetbrains/plugins/groovy/ext/logback/LogbackAliasedImportedMemberContributor.kt deleted file mode 100644 index eb46af78e543..000000000000 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/ext/logback/LogbackAliasedImportedMemberContributor.kt +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2000-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jetbrains.plugins.groovy.ext.logback - -import com.intellij.psi.* -import com.intellij.psi.impl.light.LightVariableBuilder -import com.intellij.psi.scope.ElementClassHint -import com.intellij.psi.scope.NameHint -import com.intellij.psi.scope.PsiScopeProcessor -import org.jetbrains.plugins.groovy.lang.resolve.NonCodeMembersContributor -import org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil - -class LogbackAliasedImportedMemberContributor : NonCodeMembersContributor() { - - private companion object { - val importedConstants = listOf("off", "error", "warn", "info", "debug", "trace", "all") - val levelFqn = "ch.qos.logback.classic.Level" - } - - override fun getParentClassName() = "logback" - - override fun processDynamicElements(qualifierType: PsiType, - aClass: PsiClass?, - processor: PsiScopeProcessor, - place: PsiElement, - state: ResolveState) { - if (!aClass.isLogbackConfig()) return - if (!ResolveUtil.shouldProcessProperties(processor.getHint(ElementClassHint.KEY))) return - - val levelClass = JavaPsiFacade.getInstance(place.project).findClass(levelFqn, place.resolveScope) ?: return - - fun process(constantName: String): Boolean { - val field = levelClass.findFieldByName(constantName.toUpperCase(), false) ?: return true - val variable = LightVariableBuilder>(constantName, field.type, field) - return processor.execute(variable, state) - } - - val name = processor.getHint(NameHint.KEY)?.getName(state) - - if (name != null) { - if (name in importedConstants) { - process(name) - } - } - else { - for (constantName in importedConstants) { - if (!process(constantName)) return - } - } - } -} \ No newline at end of file diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/ext/logback/LogbackImportContributor.kt b/plugins/groovy/src/org/jetbrains/plugins/groovy/ext/logback/LogbackImportContributor.kt index 091a8f408e9b..e69c8ea26bd0 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/ext/logback/LogbackImportContributor.kt +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/ext/logback/LogbackImportContributor.kt @@ -1,41 +1,13 @@ -/* - * Copyright 2000-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package org.jetbrains.plugins.groovy.ext.logback import org.jetbrains.plugins.groovy.lang.psi.GroovyFile -import org.jetbrains.plugins.groovy.lang.resolve.GrImportContributor -import org.jetbrains.plugins.groovy.lang.resolve.Import -import org.jetbrains.plugins.groovy.lang.resolve.ImportType.STAR -import org.jetbrains.plugins.groovy.lang.resolve.ImportType.STATIC_STAR +import org.jetbrains.plugins.groovy.lang.resolve.imports.GrImportContributor +import org.jetbrains.plugins.groovy.lang.resolve.imports.GroovyImport class LogbackImportContributor : GrImportContributor { - private val imports by lazy { - listOf( - Import("ch.qos.logback.classic.encoder.PatternLayoutEncoder"), - Import("ch.qos.logback.classic.Level", STATIC_STAR) - ) + listOf( - "ch.qos.logback.core", - "ch.qos.logback.core.encoder", - "ch.qos.logback.core.read", - "ch.qos.logback.core.rolling", - "ch.qos.logback.core.status", - "ch.qos.logback.classic.net" - ).map { Import(it, STAR) } - } + private val imports: List by lazy(::buildImports) - override fun getImports(file: GroovyFile) = if (file.isLogbackConfig()) imports else emptyList() -} \ No newline at end of file + override fun getFileImports(file: GroovyFile) = if (file.isLogbackConfig()) imports else emptyList() +} diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/ext/logback/util.kt b/plugins/groovy/src/org/jetbrains/plugins/groovy/ext/logback/util.kt index 4223a6ad845d..4161d7c28315 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/ext/logback/util.kt +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/ext/logback/util.kt @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package org.jetbrains.plugins.groovy.ext.logback import com.intellij.psi.PsiClass @@ -25,6 +11,7 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals import org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass import org.jetbrains.plugins.groovy.lang.psi.patterns.GroovyPatterns.groovyLiteralExpression import org.jetbrains.plugins.groovy.lang.psi.patterns.psiMethod +import org.jetbrains.plugins.groovy.lang.resolve.imports.* internal val configName = "logback.groovy" internal val configDelegateFqn = "ch.qos.logback.classic.gaffer.ConfigurationDelegate" @@ -57,3 +44,28 @@ private fun PsiFile.computeAppenderDeclarations(): Map { accept(visitor) return result } + +/** + * see ch.qos.logback.classic.gaffer.GafferConfigurator#importCustomizer() + */ +internal fun buildImports(): List { + val packageImports = arrayOf( + "ch.qos.logback.core", + "ch.qos.logback.core.encoder", + "ch.qos.logback.core.read", + "ch.qos.logback.core.rolling", + "ch.qos.logback.core.status", + "ch.qos.logback.classic.net" + ) + val levelFqn = "ch.qos.logback.classic.Level" + val levels = arrayOf("OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE", "ALL") + + return mutableListOf().apply { + packageImports.mapTo(this, ::StarImport) + this += RegularImport("ch.qos.logback.classic.encoder.PatternLayoutEncoder") + this += StaticStarImport(levelFqn) + levels.mapTo(this) { + StaticImport(levelFqn, it, it.toLowerCase()) + } + } +}