generated from JetBrains/intellij-platform-plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Add linter and guard, apply fixes and suppress by scope #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
WalterWoshid
wants to merge
2
commits into
j-plugins:main
Choose a base branch
from
WalterWoshid:feat/apply-fix-and-suppress
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 0 additions & 12 deletions
12
src/main/kotlin/com/github/xepozz/mago/formatter/MagoReformatFileAction.kt
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
src/main/kotlin/com/github/xepozz/mago/qualityTool/MagoApplyEditAction.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| package com.github.xepozz.mago.qualityTool | ||
|
|
||
| import com.github.xepozz.mago.configuration.MagoProjectConfiguration | ||
| import com.github.xepozz.mago.formatter.MagoExternalFormatter | ||
| import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer | ||
| import com.intellij.codeInsight.intention.FileModifier | ||
| import com.intellij.codeInsight.intention.IntentionAction | ||
| import com.intellij.codeInsight.intention.IntentionActionWithOptions | ||
| import com.intellij.codeInsight.intention.PriorityAction | ||
| import com.intellij.codeInsight.intention.preview.IntentionPreviewUtils | ||
| import com.intellij.openapi.command.WriteCommandAction | ||
| import com.intellij.openapi.editor.Editor | ||
| import com.intellij.openapi.project.Project | ||
| import com.intellij.openapi.util.io.FileUtil | ||
| import com.intellij.psi.PsiDocumentManager | ||
| import com.intellij.psi.PsiElement | ||
| import com.intellij.psi.PsiFile | ||
| import java.nio.charset.StandardCharsets | ||
| import java.nio.file.Paths | ||
|
|
||
| enum class ApplyAllScope(val maxSafetyLevel: Int, val label: String) { | ||
| SAFE_ONLY(0, "fixes only safe"), | ||
| POTENTIALLY_UNSAFE(1, "potentially unsafe"), | ||
| UNSAFE(2, "unsafe") | ||
| } | ||
|
|
||
| fun safetyLevel(safety: String): Int = when (safety) { | ||
| "unsafe" -> 2 | ||
| "potentiallyunsafe" -> 1 | ||
| else -> 0 | ||
| } | ||
|
|
||
| fun MagoEdit.maxSafetyLevel(): Int = replacements.maxOfOrNull { safetyLevel(it.safety) } ?: 0 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. put maxSafetyLevel in the |
||
|
|
||
| /** Normalize path for comparison so edit path (e.g. with ./) matches IDE path. */ | ||
| private fun normalizePath(path: String): String = FileUtil.toCanonicalPath(path) | ||
|
|
||
| /** True if this edit applies to the given file (path or name match, paths normalized). */ | ||
| private fun editMatchesFile(edit: MagoEdit, filePath: String?, fileName: String): Boolean { | ||
| if (filePath != null) { | ||
| if (FileUtil.pathsEqual(normalizePath(filePath), normalizePath(edit.path))) return true | ||
| } | ||
| if (edit.name == fileName) return true | ||
| val editLastName = Paths.get(edit.name).fileName?.toString() | ||
| if (editLastName == fileName) return true | ||
| if (edit.name.endsWith("/$fileName") || edit.name.endsWith("\\$fileName")) return true | ||
| return false | ||
| } | ||
|
|
||
| /** Keep only replacements with exactly this safety level (safe=0, potentially unsafe=1, unsafe=2). */ | ||
| fun filterEditsByExactSafety(edits: List<MagoEdit>, level: Int): List<MagoEdit> = edits | ||
| .map { edit -> | ||
| edit.copy(replacements = edit.replacements.filter { safetyLevel(it.safety) == level }) | ||
| } | ||
| .filter { it.replacements.isNotEmpty() } | ||
|
|
||
| class MagoApplyEditAction( | ||
| private val edits: List<MagoEdit>, | ||
| private val isApplyAll: Boolean = false, | ||
| private val applyAllScope: ApplyAllScope? = null, | ||
| private val fixDescription: String? = null | ||
| ) : IntentionAction, PriorityAction, FileModifier { | ||
|
|
||
| override fun getElementToMakeWritable(currentFile: PsiFile): PsiElement = currentFile | ||
|
|
||
| override fun getFileModifierForPreview(target: PsiFile): FileModifier { | ||
| return MagoApplyEditAction(edits, isApplyAll, applyAllScope, fixDescription) | ||
| } | ||
| override fun getFamilyName() = "Mago" | ||
|
|
||
| override fun getPriority(): PriorityAction.Priority { | ||
| return if (isApplyAll) PriorityAction.Priority.LOW else PriorityAction.Priority.HIGH | ||
| } | ||
|
|
||
| override fun getText(): String { | ||
| if (applyAllScope != null) { | ||
| return "Mago: Apply all suggested fixes (${applyAllScope.label})" | ||
| } | ||
| val maxSafetyValue = edits.flatMap { it.replacements }.maxOfOrNull { safetyLevel(it.safety) } ?: 0 | ||
| val safetySuffix = when (maxSafetyValue) { | ||
| 2 -> " (unsafe)" | ||
| 1 -> " (potentially unsafe)" | ||
| else -> "" | ||
| } | ||
| return when { | ||
| !fixDescription.isNullOrBlank() -> "Mago: " + fixDescription.trim() + safetySuffix | ||
| isApplyAll -> "Mago: Apply all suggested fixes$safetySuffix" | ||
| else -> "Mago: Apply suggested fix$safetySuffix" | ||
| } | ||
| } | ||
|
Comment on lines
+79
to
+90
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use string builder instead |
||
|
|
||
| override fun invoke(project: Project, editor: Editor, file: PsiFile) { | ||
| val filePath = file.virtualFile?.path?.let { normalizePath(it) } | ||
| val fileName = file.name | ||
| val currentFileEdits = edits.filter { editMatchesFile(it, filePath, fileName) } | ||
| if (currentFileEdits.isEmpty()) return | ||
| val fileText = file.text | ||
| val doc = editor.document | ||
| val allReplacements = currentFileEdits.flatMap { edit -> | ||
| edit.replacements.map { r -> | ||
| val startChar = byteOffsetToCharOffset(fileText, r.start) | ||
| val endChar = byteOffsetToCharOffset(fileText, r.end) | ||
| Triple(startChar, endChar, r.newText) | ||
| } | ||
| }.sortedByDescending { it.first } | ||
| val inPreview = IntentionPreviewUtils.isIntentionPreviewActive() | ||
| if (inPreview) { | ||
| for ((startChar, endChar, newText) in allReplacements) { | ||
| if (startChar in 0..endChar && endChar <= doc.textLength) { | ||
| doc.replaceString(startChar, endChar, newText) | ||
| } | ||
| } | ||
| } else { | ||
| WriteCommandAction.runWriteCommandAction(project) { | ||
| for ((startChar, endChar, newText) in allReplacements) { | ||
| if (startChar in 0..endChar && endChar <= doc.textLength) { | ||
| doc.replaceString(startChar, endChar, newText) | ||
| } | ||
| } | ||
| PsiDocumentManager.getInstance(project).commitDocument(doc) | ||
| file.putUserData(MagoGlobalInspection.MAGO_ANNOTATOR_INFO, null) | ||
| MagoHtmlAnnotator.clearProblemCache(file) | ||
| val settings = project.getService(MagoProjectConfiguration::class.java) | ||
| if (settings.formatAfterFix && settings.formatterEnabled) { | ||
| val formatter = MagoExternalFormatter() | ||
| if (formatter.activeForFile(file)) { | ||
| formatter.format(file, file.textRange, | ||
| canChangeWhiteSpacesOnly = false, | ||
| keepLineBreaks = false, | ||
| enableBulkUpdate = false, | ||
| cursorOffset = 0 | ||
| ) | ||
| } | ||
| } | ||
| DaemonCodeAnalyzer.getInstance(project).restart(file, "Mago fix applied") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun byteOffsetToCharOffset(text: String, byteOffset: Int): Int { | ||
| val bytes = text.toByteArray(StandardCharsets.UTF_8) | ||
| if (byteOffset <= 0) return 0 | ||
| if (byteOffset >= bytes.size) return text.length | ||
| return String(bytes.copyOf(byteOffset), StandardCharsets.UTF_8).length | ||
| } | ||
|
|
||
| override fun startInWriteAction() = true | ||
| override fun isAvailable(project: Project, editor: Editor, file: PsiFile) = edits.isNotEmpty() | ||
| } | ||
|
|
||
| class MagoApplyEditSubmenuAction( | ||
| private val mainAction: MagoApplyEditAction, | ||
| private val subActions: List<IntentionAction> | ||
| ) : IntentionAction, IntentionActionWithOptions, PriorityAction, FileModifier { | ||
| override fun getFamilyName() = mainAction.familyName | ||
| override fun getText() = mainAction.text | ||
| override fun invoke(project: Project, editor: Editor, file: PsiFile) { | ||
| mainAction.invoke(project, editor, file) | ||
| } | ||
|
|
||
| override fun getOptions(): List<IntentionAction> { | ||
| // Don't duplicate the main entry in the submenu (IntentionOptionsOnly shows only getOptions()) | ||
| return subActions.filter { it != mainAction } | ||
| } | ||
|
|
||
| override fun getCombiningPolicy(): IntentionActionWithOptions.CombiningPolicy { | ||
| return IntentionActionWithOptions.CombiningPolicy.IntentionOptionsOnly | ||
| } | ||
| override fun getPriority() = mainAction.priority | ||
| override fun startInWriteAction() = mainAction.startInWriteAction() | ||
| override fun isAvailable(project: Project, editor: Editor, file: PsiFile) = mainAction.isAvailable(project, editor, file) | ||
|
|
||
| override fun getElementToMakeWritable(currentFile: PsiFile): PsiElement = mainAction.getElementToMakeWritable(currentFile) | ||
|
|
||
| override fun getFileModifierForPreview(target: PsiFile): FileModifier? { | ||
| val mainCopy = mainAction.getFileModifierForPreview(target) as? MagoApplyEditAction ?: return null | ||
| val subCopies = subActions.mapNotNull { (it as? FileModifier)?.getFileModifierForPreview(target) } | ||
| if (subCopies.size != subActions.size) return null | ||
| return MagoApplyEditSubmenuAction(mainCopy, subCopies.filterIsInstance<IntentionAction>()) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please keep 1 class in 1 file