Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions resources/META-INF/extensions/intentions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
<category>LaTeX</category>
</intentionAction>

<intentionAction>
<language>Latex</language>
<className>nl.hannahsten.texifyidea.intentions.LatexFlipArgumentsIntention</className>
<category>LaTeX</category>
</intentionAction>

<intentionAction>
<language>Latex</language>
<className>nl.hannahsten.texifyidea.intentions.LatexInlineDisplayToggleIntention</className>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
\[
3 + x = y
\]
$ \frac{x}{2} $
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
\begin{displaymath}
3 + x = y
\end{displaymath}
$ \frac{2}{x} $
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
\[
3 + x = y
\]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
\begin{displaymath}
3 + x = y
\end{displaymath}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
Swaps the order of passed arguments
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package nl.hannahsten.texifyidea.intentions

import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiFile
import com.intellij.refactoring.suggested.endOffset
import com.intellij.refactoring.suggested.startOffset
import nl.hannahsten.texifyidea.psi.LatexCommands
import nl.hannahsten.texifyidea.psi.LatexPsiHelper
import nl.hannahsten.texifyidea.psi.LatexRequiredParam
import nl.hannahsten.texifyidea.util.files.getAllRequiredArguments
import nl.hannahsten.texifyidea.util.files.isLatexFile
import nl.hannahsten.texifyidea.util.parser.firstChildOfType
import nl.hannahsten.texifyidea.util.parser.parentOfType
import nl.hannahsten.texifyidea.util.parser.requiredParameters
import nl.hannahsten.texifyidea.util.replaceString

Check warning on line 18 in src/nl/hannahsten/texifyidea/intentions/LatexFlipArgumentsIntention.kt

View workflow job for this annotation

GitHub Actions / Qodana

Unused import directive

Unused import directive

class LatexFlipArgumentsIntention : TexifyIntentionBase("Swap the two arguments of a command") {
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile?): Boolean {
if (editor == null || file == null || !file.isLatexFile()) {
return false
}

val parent = getToken(file, editor) ?: return false

val argCount = parent.getAllRequiredArguments()?.size ?: return false

return argCount == 2
}

private fun getToken(
file: PsiFile,
editor: Editor
): LatexCommands? {
val selected = file.findElementAt(editor.caretModel.offset) ?: return null

return selected.parentOfType(LatexCommands::class)
}

override fun invoke(project: Project, editor: Editor?, file: PsiFile?) {
if (editor == null || file == null || !file.isLatexFile()) {
return
}

val token = getToken(file, editor) ?: return
val tokens = token.requiredParameters()

assert(tokens.size == 2) { "Expected to have only 2 args!" }

val range = TextRange(tokens[0].startOffset, tokens[1].endOffset)

val document = editor.document
val replacements = tokens[1].text + tokens[0].text

runWriteAction {
token.parent.node.addChild(LatexPsiHelper(project).createFromText(tokens[0].node.text).firstChildOfType(LatexRequiredParam::class)!!.node)
tokens[0].delete()
// document.replaceString(range, replacements)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package nl.hannahsten.texifyidea.intentions

import com.intellij.testFramework.fixtures.BasePlatformTestCase
import nl.hannahsten.texifyidea.file.LatexFileType
import nl.hannahsten.texifyidea.testutils.writeCommand

class LatexFlipArgumentsIntentionTest : BasePlatformTestCase() {

fun testInMathEnvironment() {
myFixture.configureByText(
LatexFileType,
"""
\begin{document}
$\frac{a<caret>}{b}$
\end{document}
""".trimIndent()
)
val intentions = myFixture.availableIntentions
writeCommand(myFixture.project) {
intentions.first { i -> i.text == "Swap the two arguments of a command" }.invoke(myFixture.project, myFixture.editor, myFixture.file)
}
myFixture.checkResult(
"""
\begin{document}
$\frac{b<caret>}{a}$
\end{document}
""".trimIndent()
)
}

fun testOnFunctionTOken() {
myFixture.configureByText(
LatexFileType,
"""
\begin{document}
$\fr<caret>ac{a}{b}$
\end{document}
""".trimIndent()
)
val intentions = myFixture.availableIntentions
writeCommand(myFixture.project) {
intentions.first { i -> i.text == "Swap the two arguments of a command" }.invoke(myFixture.project, myFixture.editor, myFixture.file)
}
myFixture.checkResult(
"""
\begin{document}
$\fr<caret>ac{b}{a}$
\end{document}
""".trimIndent()
)
}
}
Loading