Skip to content

Commit

Permalink
Merge pull request #357 from koxudaxi/vscode_style_inspection
Browse files Browse the repository at this point in the history
All inspections are displayed correctly
  • Loading branch information
koxudaxi committed Mar 24, 2024
2 parents ec3b03a + db6d592 commit 076cafe
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Changelog

## [Unreleased]
- Ignore lsp restart error [[#398(https://github.com/koxudaxi/ruff-pycharm-plugin/pull/398)]
- Ignore lsp restart error [[#398](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/398)]
- All inspections are displayed correctly [[#357](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/357)]

## [0.0.30] - 2024-02-26

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pluginGroup = com.koxudaxi.ruff
pluginName = Ruff
pluginRepositoryUrl = https://github.com/koxudaxi/ruff-pycharm-plugin
# SemVer format -> https://semver.org
pluginVersion = 0.0.30
pluginVersion = 0.0.31

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 233.11799.241
Expand Down
28 changes: 17 additions & 11 deletions src/com/koxudaxi/ruff/RuffExternalAnnotator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.intellij.profile.codeInspection.InspectionProjectProfileManager
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiWhiteSpace

Check warning on line 17 in src/com/koxudaxi/ruff/RuffExternalAnnotator.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import com.intellij.psi.util.PsiTreeUtil
import com.jetbrains.python.psi.PyFile
import com.jetbrains.python.psi.impl.PyFileImpl
Expand Down Expand Up @@ -70,12 +71,12 @@ class RuffExternalAnnotator :
val project = file.project
val showRuleCode = annotationResult.showRuleCode
val result = annotationResult.result
val trimOriginalLength = file.text.trimEnd().length
val document = PsiDocumentManager.getInstance(project).getDocument(file) ?: return
val inspectionManager = InspectionManager.getInstance(file.project)

result.forEach {
val range = document.getStartEndRange(it.location, it.endLocation, -1)
if (range.startOffset == 0 && range.endOffset == 0) return@forEach
val psiElement = getPyElement(file, range) ?: return@forEach
val builder = holder.newAnnotation(
annotationResult.highlightDisplayLevel,
Expand Down Expand Up @@ -104,10 +105,9 @@ class RuffExternalAnnotator :
)
builder.newLocalQuickFix(quickFix, problemDescriptor).registerFix()
}
if (isForFile(document, it, trimOriginalLength, range)) {
builder.fileLevel()
} else {
builder.range(psiElement)
builder.range(range)
if (range.startOffset == range.endOffset && (range.endOffset == file.textLength || file.text.substring(range.startOffset, range.endOffset + 1) == "\n")) {
builder.afterEndOfLine()
}
builder.create()
}
Expand All @@ -124,14 +124,20 @@ class RuffExternalAnnotator :
return false
}

private fun getPyElement(psiFile: PsiFile, range: TextRange): PsiElement? =
PsiTreeUtil.findElementOfClassAtRange(
psiFile,
range.startOffset,
range.endOffset,
PsiElement::class.java
private fun getPyElement(psiFile: PsiFile, range: TextRange): PsiElement? {
val psiElement = PsiTreeUtil.findElementOfClassAtRange(
psiFile,
range.startOffset,
range.endOffset,
PsiElement::class.java
)

if (psiElement != null) return psiElement
if (range.startOffset == range.endOffset && range.endOffset == psiFile.textLength) {
return psiFile.findElementAt(range.endOffset - 1)
}
return psiFile.findElementAt(range.startOffset)
}

data class RuffExternalAnnotatorInfo(
val showRuleCode: Boolean,
Expand Down

0 comments on commit 076cafe

Please sign in to comment.