|
| 1 | +/* |
| 2 | + * ©2009-2018 南京擎盾信息科技有限公司 All rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +package com.wuhao.code.check.inspection.visitor |
| 6 | + |
| 7 | +import com.intellij.application.options.CodeStyle |
| 8 | +import com.intellij.codeInspection.LocalQuickFix |
| 9 | +import com.intellij.codeInspection.ProblemDescriptor |
| 10 | +import com.intellij.codeInspection.ProblemHighlightType |
| 11 | +import com.intellij.codeInspection.ProblemsHolder |
| 12 | +import com.intellij.lang.Language |
| 13 | +import com.intellij.lang.java.JavaLanguage |
| 14 | +import com.intellij.lang.javascript.JavascriptLanguage |
| 15 | +import com.intellij.lang.javascript.dialects.ECMA6LanguageDialect |
| 16 | +import com.intellij.lang.javascript.dialects.TypeScriptLanguageDialect |
| 17 | +import com.intellij.openapi.project.Project |
| 18 | +import com.intellij.psi.PsiElement |
| 19 | +import com.intellij.psi.PsiElementVisitor |
| 20 | +import com.intellij.psi.PsiFile |
| 21 | +import com.intellij.psi.codeStyle.JavaCodeStyleSettings |
| 22 | +import com.wuhao.code.check.DEFAULT_CONTINUATION_INDENT_SPACE_COUNT |
| 23 | +import com.wuhao.code.check.DEFAULT_INDENT_SPACE_COUNT |
| 24 | +import com.wuhao.code.check.PostStart |
| 25 | +import org.apache.xmlbeans.XmlLanguage |
| 26 | +import org.jetbrains.kotlin.idea.KotlinLanguage |
| 27 | +import org.jetbrains.vuejs.VueLanguage |
| 28 | +import org.jetbrains.vuejs.language.VueJSLanguage |
| 29 | +import java.nio.charset.StandardCharsets |
| 30 | + |
| 31 | +/** |
| 32 | + * 基本的代码格式检查访问器,主要检查了文件缩进及文件编码 |
| 33 | + * 文件缩进强制为2个空格,持续缩进为4个空格,文件编码为UTF-8 |
| 34 | + * |
| 35 | + * Created by 吴昊 on 18-4-26. |
| 36 | + * @author 吴昊 |
| 37 | + * @since 1.1 |
| 38 | + */ |
| 39 | +class CommonCodeFormatVisitor(protected val holder: ProblemsHolder) : PsiElementVisitor(), BaseCodeFormatVisitor { |
| 40 | + |
| 41 | + override fun support(language: Language): Boolean { |
| 42 | + return language is KotlinLanguage |
| 43 | + || language is JavaLanguage |
| 44 | + || language is JavascriptLanguage |
| 45 | + || language is TypeScriptLanguageDialect |
| 46 | + || language is ECMA6LanguageDialect |
| 47 | + || language is VueLanguage |
| 48 | + || language is XmlLanguage |
| 49 | + || language is VueJSLanguage |
| 50 | + } |
| 51 | + |
| 52 | + override fun visitFile(file: PsiFile) { |
| 53 | + this.checkEncoding(file) |
| 54 | + this.checkIndent(file) |
| 55 | + } |
| 56 | + |
| 57 | + private fun checkEncoding(element: PsiElement) { |
| 58 | + if (element is PsiFile && element.virtualFile != null && element.virtualFile.charset != StandardCharsets.UTF_8) { |
| 59 | + holder.registerProblem(element, "${element.name}的编码为${element.virtualFile.charset},应该使用UTF-8", |
| 60 | + ProblemHighlightType.ERROR) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private fun checkIndent(element: PsiElement) { |
| 65 | + if (element is PsiFile) { |
| 66 | + val styleContainer = JavaCodeStyleSettings.getInstance(element.project) |
| 67 | + .container |
| 68 | + val indent = styleContainer.getIndentSize(element.fileType) |
| 69 | + val continuationIndent = styleContainer.getContinuationIndentSize(element.fileType) |
| 70 | + if (indent != DEFAULT_INDENT_SPACE_COUNT) { |
| 71 | + val indentFix = object : LocalQuickFix { |
| 72 | + override fun applyFix(project: Project, descriptor: ProblemDescriptor) { |
| 73 | + PostStart.setIndent(element.fileType, CodeStyle.getSettings(element.project)) |
| 74 | + } |
| 75 | + |
| 76 | + override fun getFamilyName(): String { |
| 77 | + return "设置缩进" |
| 78 | + } |
| 79 | + } |
| 80 | + holder.registerProblem(element, "${element.fileType.name}文件的缩进必须为${DEFAULT_INDENT_SPACE_COUNT}个空格", |
| 81 | + ProblemHighlightType.ERROR, indentFix) |
| 82 | + } |
| 83 | + if (continuationIndent != DEFAULT_CONTINUATION_INDENT_SPACE_COUNT) { |
| 84 | + holder.registerProblem(element, "${element.fileType.name}文件的持续缩进必须为${DEFAULT_CONTINUATION_INDENT_SPACE_COUNT}个空格", |
| 85 | + ProblemHighlightType.ERROR) |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + companion object { |
| 91 | + const val ACTION_PREFIX = "@" |
| 92 | + const val CUSTOM_ATTR_PREFIX = ":" |
| 93 | + const val DIRECTIVE_PREFIX = "v-" |
| 94 | + } |
| 95 | +} |
| 96 | + |
0 commit comments