Skip to content
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

add GoToParametrizeArgsAction #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.github.takemikami.intellij.plugin.pytestparametrize


import com.intellij.openapi.actionSystem.*
import com.intellij.openapi.editor.CaretModel
import com.intellij.openapi.editor.ScrollType
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiRecursiveElementVisitor
import com.intellij.refactoring.suggested.endOffset
import com.intellij.refactoring.suggested.startOffset
import com.jetbrains.python.psi.*


class GoToParametrizeArgsAction : AnAction() {
override fun update(event: AnActionEvent) {
val vf = event.getData(CommonDataKeys.VIRTUAL_FILE) ?: return
val active = "py".equals(vf.extension) && vf.name.startsWith("test")
event.presentation.isEnabledAndVisible = active
}

class TestVisitor() : PsiRecursiveElementVisitor() {
override fun visitElement(element: PsiElement) {
if (element is PyDecorator
&& "pytest.mark.parametrize".equals(element.qualifiedName.toString())
&& element.hasArgumentList()
&& element.arguments.size >= 2
) {
val valList = element.arguments[1]
if (valList !is PyListLiteralExpression) return

// detect selected id
val idsArguments = element.arguments.filter { it.name.equals("ids") }
if (idsArguments.isEmpty()) return

val targetIndex = idsArguments.first().children.first().children.map {
currentOffset >= it.startOffset && currentOffset <= it.endOffset
}.indexOfFirst { it }
if (targetIndex == -1) return

// detect args offset
val argsOffsets = valList.elements.map { it.startOffset }
if (targetIndex + 1 > argsOffsets.size) return
offset = argsOffsets[targetIndex]
}
super.visitElement(element)
}

var offset = -1
var currentOffset = -1
}

override fun actionPerformed(event: AnActionEvent) {
val editor = event.getData(CommonDataKeys.EDITOR)
val caretModel: CaretModel = editor?.caretModel ?: return
val logicalPosition = caretModel.logicalPosition

// get offset to goto
val psiFile = event.getData(PlatformDataKeys.PSI_FILE)
val visitor: TestVisitor = TestVisitor()
visitor.currentOffset = caretModel.offset
psiFile?.accept(visitor)
if (visitor.offset == -1) return

// move caret
logicalPosition.leanForward(true);
caretModel.moveToOffset(visitor.offset)
editor.scrollingModel.scrollToCaret(ScrollType.CENTER_DOWN)
editor.selectionModel.removeSelection()
}
}
6 changes: 6 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@
<extensions defaultExtensionNs="com.intellij">
<codeInsight.inlayProvider language="Python" implementationClass="com.github.takemikami.intellij.plugin.pytestparametrize.PytestParametrizeInlayHintsProvider"/>
</extensions>

<actions>
<action id="go_to_parameter" class="com.github.takemikami.intellij.plugin.pytestparametrize.GoToParametrizeArgsAction" text="Pytest Parametrize Args">
<add-to-group group-id="EditorPopupMenu.GoTo" anchor="last" />
</action>
</actions>
</idea-plugin>
Loading