|
| 1 | +package net.sjrx.intellij.plugins.systemdunitfiles.inspections |
| 2 | + |
| 3 | +import com.intellij.codeInspection.LocalInspectionTool |
| 4 | +import com.intellij.codeInspection.ProblemHighlightType |
| 5 | +import com.intellij.codeInspection.ProblemsHolder |
| 6 | +import com.intellij.openapi.util.TextRange |
| 7 | +import com.intellij.psi.PsiElementVisitor |
| 8 | +import com.intellij.psi.util.PsiTreeUtil |
| 9 | +import net.sjrx.intellij.plugins.systemdunitfiles.UnitFileLanguage |
| 10 | +import net.sjrx.intellij.plugins.systemdunitfiles.intentions.CanonicalizeIpv6QuickFix |
| 11 | +import net.sjrx.intellij.plugins.systemdunitfiles.psi.UnitFile |
| 12 | +import net.sjrx.intellij.plugins.systemdunitfiles.psi.UnitFilePropertyType |
| 13 | +import net.sjrx.intellij.plugins.systemdunitfiles.psi.UnitFileSectionGroups |
| 14 | +import net.sjrx.intellij.plugins.systemdunitfiles.psi.UnitFileVisitor |
| 15 | +import net.sjrx.intellij.plugins.systemdunitfiles.semanticdata.SemanticDataRepository |
| 16 | +import net.sjrx.intellij.plugins.systemdunitfiles.semanticdata.fileClass |
| 17 | +import net.sjrx.intellij.plugins.systemdunitfiles.semanticdata.optionvalues.grammar.GrammarOptionValue |
| 18 | +import net.sjrx.intellij.plugins.systemdunitfiles.semanticdata.optionvalues.grammar.canonicalizeIpv6 |
| 19 | +import net.sjrx.intellij.plugins.systemdunitfiles.semanticdata.optionvalues.grammar.labeledRegions |
| 20 | +import net.sjrx.intellij.plugins.systemdunitfiles.settings.ExperimentalSettings |
| 21 | + |
| 22 | +/** |
| 23 | + * Suggests rewriting an IPv6 address to its RFC 5952 canonical form (#363), behind the experimental |
| 24 | + * flag. It walks the grammar's labeled value spans (e.g. a whole IP address) and offers a quick-fix |
| 25 | + * for any that aren't already canonical. |
| 26 | + */ |
| 27 | +class Ipv6CanonicalFormInspection : LocalInspectionTool() { |
| 28 | + |
| 29 | + override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { |
| 30 | + val file = holder.file |
| 31 | + if (file !is UnitFile || !file.language.isKindOf(UnitFileLanguage.INSTANCE)) return PsiElementVisitor.EMPTY_VISITOR |
| 32 | + if (!ExperimentalSettings.getInstance(file.project).state.useGrammarParseEngine) return PsiElementVisitor.EMPTY_VISITOR |
| 33 | + return MyVisitor(holder) |
| 34 | + } |
| 35 | + |
| 36 | + private class MyVisitor(private val holder: ProblemsHolder) : UnitFileVisitor() { |
| 37 | + override fun visitPropertyType(property: UnitFilePropertyType) { |
| 38 | + val section = PsiTreeUtil.getParentOfType(property, UnitFileSectionGroups::class.java) ?: return |
| 39 | + val value = property.valueText ?: return |
| 40 | + val validator = SemanticDataRepository.instance |
| 41 | + .getOptionValidator(section.containingFile.fileClass(), section.sectionName, property.key) |
| 42 | + if (validator !is GrammarOptionValue) return |
| 43 | + |
| 44 | + for (region in validator.combinator.labeledRegions(value)) { |
| 45 | + val text = value.substring(region.start, region.end) |
| 46 | + val canonical = canonicalizeIpv6(text) ?: continue // null = not a (pure) IPv6 address |
| 47 | + if (canonical == text) continue |
| 48 | + holder.registerProblem( |
| 49 | + property.valueNode.psi, |
| 50 | + "IPv6 address is not in canonical form (RFC 5952); use '$canonical'", |
| 51 | + ProblemHighlightType.WEAK_WARNING, |
| 52 | + TextRange(region.start, region.end), |
| 53 | + CanonicalizeIpv6QuickFix(region.start, text, canonical), |
| 54 | + ) |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments