[groovy] logback: use new imports API

No need in separate aliased import processing since aliased imports can
be contributed.
This commit is contained in:
Daniil Ovchinnikov
2017-11-27 19:40:37 +03:00
parent abdb9be673
commit 7ea6506b94
4 changed files with 33 additions and 114 deletions
@@ -3,7 +3,6 @@
<importContributor implementation="org.jetbrains.plugins.groovy.ext.logback.LogbackImportContributor"/>
<membersContributor implementation="org.jetbrains.plugins.groovy.ext.logback.LogbackTopLevelMemberContributor"/>
<membersContributor implementation="org.jetbrains.plugins.groovy.ext.logback.LogbackDelegateMemberContributor"/>
<membersContributor implementation="org.jetbrains.plugins.groovy.ext.logback.LogbackAliasedImportedMemberContributor"/>
<delegatesToProvider implementation="org.jetbrains.plugins.groovy.ext.logback.LogbackDelegatesToProvider"/>
</extensions>
@@ -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<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
}
}
}
}
@@ -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<GroovyImport> by lazy(::buildImports)
override fun getImports(file: GroovyFile) = if (file.isLogbackConfig()) imports else emptyList()
}
override fun getFileImports(file: GroovyFile) = if (file.isLogbackConfig()) imports else emptyList()
}
@@ -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<String, GrLiteral> {
accept(visitor)
return result
}
/**
* see ch.qos.logback.classic.gaffer.GafferConfigurator#importCustomizer()
*/
internal fun buildImports(): List<GroovyImport> {
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<GroovyImport>().apply {
packageImports.mapTo(this, ::StarImport)
this += RegularImport("ch.qos.logback.classic.encoder.PatternLayoutEncoder")
this += StaticStarImport(levelFqn)
levels.mapTo(this) {
StaticImport(levelFqn, it, it.toLowerCase())
}
}
}