Skip to content

Commit

Permalink
Merge pull request #44 from koxudaxi/fix_element_detection
Browse files Browse the repository at this point in the history
Fix element detection
  • Loading branch information
koxudaxi authored Jan 5, 2023
2 parents 637f03d + 9454d16 commit a1b2ed6
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/com/koxudaxi/ruff/RuffInspection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.intellij.credentialStore.toByteArrayAndClear
import com.intellij.openapi.editor.Document
import com.intellij.openapi.module.ModuleUtil
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.util.PsiTreeUtil
import com.jetbrains.python.inspections.PyInspection
Expand Down Expand Up @@ -41,9 +42,9 @@ class RuffInspection : PyInspection() {
runRuff(sdk, stdin, *args.toTypedArray())
} ?: return
parseJsonResponse(response).forEach {
val pyElement = getPyElement(it, pyFile, document) ?: return@forEach
val psiElement = getPyElement(it, pyFile, document) ?: return@forEach
registerProblem(
pyElement,
psiElement,
it.message,
it.fix?.let { fix ->
RuffQuickFix.create(fix, document)
Expand All @@ -52,10 +53,10 @@ class RuffInspection : PyInspection() {

}

private fun getPyElement(result: Result, pyFile: PyFile, document: Document): PyElement? {
private fun getPyElement(result: Result, pyFile: PyFile, document: Document): PsiElement? {
val start = document.getLineStartOffset(result.location.row - 1) + result.location.column - 1
val end = document.getLineStartOffset(result.endLocation.row - 1) + result.endLocation.column - 1
return PsiTreeUtil.findElementOfClassAtRange(pyFile, start, end, PyElement::class.java)
return PsiTreeUtil.findElementOfClassAtRange(pyFile, start, end, PsiElement::class.java)
}
}

Expand Down

0 comments on commit a1b2ed6

Please sign in to comment.