Need reformat inspection
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This inspection reports places that are not formatted according to project settings.
|
||||
</body>
|
||||
</html>
|
||||
@@ -1648,6 +1648,15 @@
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.ReformatInspection"
|
||||
displayName="File is not formatted according to project settings"
|
||||
groupPath="Kotlin"
|
||||
groupName="Style issues"
|
||||
enabledByDefault="false"
|
||||
level="WEAK WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.DeprecatedCallableAddReplaceWithInspection"
|
||||
displayName="@Deprecated annotation without 'replaceWith' argument"
|
||||
groupPath="Kotlin"
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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.kotlin.idea.formatter
|
||||
|
||||
import com.intellij.formatting.Block
|
||||
import com.intellij.formatting.FormattingDocumentModel
|
||||
import com.intellij.formatting.FormattingModel
|
||||
import com.intellij.lang.ASTNode
|
||||
import com.intellij.openapi.util.Key
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager
|
||||
import com.intellij.psi.formatter.FormattingDocumentModelImpl
|
||||
import org.jetbrains.kotlin.idea.formatter.FormattingChange.ReplaceWhiteSpace
|
||||
import org.jetbrains.kotlin.idea.formatter.FormattingChange.ShiftIndentInsideRange
|
||||
import org.jetbrains.kotlin.psi.NotNullableCopyableUserDataProperty
|
||||
import org.jetbrains.kotlin.psi.UserDataProperty
|
||||
|
||||
private var PsiFile.collectFormattingChanges: Boolean by NotNullableCopyableUserDataProperty(Key.create("COLLECT_FORMATTING_CHANGES"), false)
|
||||
private var PsiFile.collectChangesFormattingModel: CollectChangesWithoutApplyModel? by UserDataProperty(Key.create("COLLECT_CHANGES_FORMATTING_MODEL"))
|
||||
|
||||
fun createCollectFormattingChangesModel(file: PsiFile, block: Block): FormattingModel? {
|
||||
if (file.collectFormattingChanges) {
|
||||
return CollectChangesWithoutApplyModel(file, block).also {
|
||||
file.collectChangesFormattingModel = it
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
sealed class FormattingChange {
|
||||
data class ShiftIndentInsideRange(val node: ASTNode?, val range: TextRange, val indent: Int) : FormattingChange()
|
||||
data class ReplaceWhiteSpace(val textRange: TextRange, val whiteSpace: String) : FormattingChange()
|
||||
}
|
||||
|
||||
fun collectFormattingChanges(file: PsiFile): Set<FormattingChange> {
|
||||
try {
|
||||
file.collectFormattingChanges = true
|
||||
CodeStyleManager.getInstance(file.project).reformat(file)
|
||||
return file.collectChangesFormattingModel?.requestedChanges ?: emptySet()
|
||||
}
|
||||
finally {
|
||||
file.collectFormattingChanges = false
|
||||
file.collectChangesFormattingModel = null
|
||||
}
|
||||
}
|
||||
|
||||
private class CollectChangesWithoutApplyModel(val file: PsiFile, val block: Block) : FormattingModel {
|
||||
private val documentModel = FormattingDocumentModelImpl.createOn(file)
|
||||
private val changes = HashSet<FormattingChange>()
|
||||
|
||||
val requestedChanges: Set<FormattingChange> get() = changes
|
||||
|
||||
override fun commitChanges() {
|
||||
/* do nothing */
|
||||
}
|
||||
|
||||
override fun getDocumentModel(): FormattingDocumentModel = documentModel
|
||||
override fun getRootBlock(): Block = block
|
||||
|
||||
override fun shiftIndentInsideRange(node: ASTNode?, range: TextRange, indent: Int): TextRange {
|
||||
changes.add(ShiftIndentInsideRange(node, range, indent))
|
||||
return range
|
||||
}
|
||||
|
||||
override fun replaceWhiteSpace(textRange: TextRange, whiteSpace: String): TextRange {
|
||||
changes.add(ReplaceWhiteSpace(textRange, whiteSpace))
|
||||
return textRange
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,10 @@
|
||||
|
||||
package org.jetbrains.kotlin.idea.formatter;
|
||||
|
||||
import com.intellij.formatting.*;
|
||||
import com.intellij.formatting.FormattingModel;
|
||||
import com.intellij.formatting.FormattingModelBuilder;
|
||||
import com.intellij.formatting.FormattingModelProvider;
|
||||
import com.intellij.formatting.Indent;
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.openapi.editor.impl.DocumentImpl;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
@@ -45,6 +48,13 @@ public class KotlinFormattingModelBuilder implements FormattingModelBuilder {
|
||||
return new PsiBasedFormattingModel(containingFile, block, formattingDocumentModel);
|
||||
}
|
||||
|
||||
if (element instanceof PsiFile) {
|
||||
FormattingModel collectChangesModel = CollectChangesWithoutApplyModelKt.createCollectFormattingChangesModel((PsiFile) element, block);
|
||||
if (collectChangesModel != null) {
|
||||
return collectChangesModel;
|
||||
}
|
||||
}
|
||||
|
||||
return FormattingModelProvider.createFormattingModelForPsiFile(element.getContainingFile(), block, settings);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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.kotlin.idea.inspections
|
||||
|
||||
import com.intellij.codeInspection.*
|
||||
import com.intellij.codeInspection.ex.ProblemDescriptorImpl
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.psi.PsiWhiteSpace
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager
|
||||
import org.jetbrains.kotlin.idea.formatter.FormattingChange
|
||||
import org.jetbrains.kotlin.idea.formatter.FormattingChange.ReplaceWhiteSpace
|
||||
import org.jetbrains.kotlin.idea.formatter.FormattingChange.ShiftIndentInsideRange
|
||||
import org.jetbrains.kotlin.idea.formatter.collectFormattingChanges
|
||||
import org.jetbrains.kotlin.idea.util.ProjectRootsUtil
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
|
||||
class ReformatInspection : LocalInspectionTool() {
|
||||
override fun checkFile(file: PsiFile, manager: InspectionManager, isOnTheFly: Boolean): Array<out ProblemDescriptor>? {
|
||||
if (file !is KtFile || !ProjectRootsUtil.isInProjectSource(file)) {
|
||||
return null
|
||||
}
|
||||
|
||||
val changes = collectFormattingChanges(file)
|
||||
if (changes.isEmpty()) return null
|
||||
|
||||
val elements = changes.map {
|
||||
val rangeOffset = when (it) {
|
||||
is ShiftIndentInsideRange -> it.range.startOffset
|
||||
is ReplaceWhiteSpace -> it.textRange.startOffset
|
||||
}
|
||||
|
||||
val leaf = file.findElementAt(rangeOffset) ?: return@map null
|
||||
if (!leaf.isValid) return@map null
|
||||
if (leaf is PsiWhiteSpace && isEmptyLineReformat(leaf, it)) return@map null
|
||||
|
||||
leaf
|
||||
}.filterNotNull()
|
||||
|
||||
return elements.map {
|
||||
ProblemDescriptorImpl(it, it,
|
||||
"File is not properly formatted",
|
||||
arrayOf(ReformatQuickFix),
|
||||
ProblemHighlightType.WEAK_WARNING, false, null,
|
||||
isOnTheFly)
|
||||
}.toTypedArray()
|
||||
}
|
||||
|
||||
private fun isEmptyLineReformat(whitespace: PsiWhiteSpace, change: FormattingChange): Boolean {
|
||||
if (change !is FormattingChange.ReplaceWhiteSpace) return false
|
||||
|
||||
val beforeText = whitespace.text
|
||||
val afterText = change.whiteSpace
|
||||
|
||||
return beforeText.count { it == '\n' } == afterText.count { it == '\n' } &&
|
||||
beforeText.substringAfterLast('\n') == afterText.substringAfterLast('\n')
|
||||
}
|
||||
|
||||
private object ReformatQuickFix : LocalQuickFix {
|
||||
override fun getFamilyName(): String = "Reformat File"
|
||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||
CodeStyleManager.getInstance(project).reformat(descriptor.psiElement.containingFile)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<problems>
|
||||
<problem>
|
||||
<file>reformat.kt</file>
|
||||
<line>4</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/reformat.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">File is not formatted according to project settings</problem_class>
|
||||
<description>File is not properly formatted</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>reformat.kt</file>
|
||||
<line>4</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/reformat.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">File is not formatted according to project settings</problem_class>
|
||||
<description>File is not properly formatted</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>reformat.kt</file>
|
||||
<line>4</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/reformat.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">File is not formatted according to project settings</problem_class>
|
||||
<description>File is not properly formatted</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>reformat.kt</file>
|
||||
<line>5</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/reformat.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">File is not formatted according to project settings</problem_class>
|
||||
<description>File is not properly formatted</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>reformat.kt</file>
|
||||
<line>10</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/reformat.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">File is not formatted according to project settings</problem_class>
|
||||
<description>File is not properly formatted</description>
|
||||
</problem>
|
||||
</problems>
|
||||
@@ -0,0 +1 @@
|
||||
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.ReformatInspection
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package format.inspection
|
||||
|
||||
fun test() {
|
||||
if(true){}
|
||||
}
|
||||
|
||||
|
||||
|
||||
fun other() {
|
||||
val test = 12
|
||||
<caret>
|
||||
}
|
||||
@@ -335,6 +335,12 @@ public class InspectionTestGenerated extends AbstractInspectionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("reformat/inspectionData/inspections.test")
|
||||
public void testReformat_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/reformat/inspectionData/inspections.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("removeSetterParameterType/inspectionData/inspections.test")
|
||||
public void testRemoveSetterParameterType_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/removeSetterParameterType/inspectionData/inspections.test");
|
||||
|
||||
Reference in New Issue
Block a user