From b67868afdee051b63359d7a3b537ffc2b711ac6b Mon Sep 17 00:00:00 2001 From: Alex Plate Date: Thu, 28 Dec 2023 10:03:50 +0200 Subject: [PATCH] Extract the companion object into the top level As the inspection says, due to eager companion object initialization, it's better to keep such things on the top level --- .../surround/VimSurroundExtension.kt | 171 +++++++++--------- 1 file changed, 86 insertions(+), 85 deletions(-) diff --git a/src/main/java/com/maddyhome/idea/vim/extension/surround/VimSurroundExtension.kt b/src/main/java/com/maddyhome/idea/vim/extension/surround/VimSurroundExtension.kt index 43c7cf0207..60b7014a10 100644 --- a/src/main/java/com/maddyhome/idea/vim/extension/surround/VimSurroundExtension.kt +++ b/src/main/java/com/maddyhome/idea/vim/extension/surround/VimSurroundExtension.kt @@ -18,10 +18,7 @@ import com.maddyhome.idea.vim.api.getLeadingCharacterOffset import com.maddyhome.idea.vim.api.injector import com.maddyhome.idea.vim.api.setChangeMarks import com.maddyhome.idea.vim.command.MappingMode -import com.maddyhome.idea.vim.state.mode.Mode import com.maddyhome.idea.vim.command.OperatorArguments -import com.maddyhome.idea.vim.state.mode.SelectionType -import com.maddyhome.idea.vim.state.mode.selectionType import com.maddyhome.idea.vim.common.TextRange import com.maddyhome.idea.vim.extension.ExtensionHandler import com.maddyhome.idea.vim.extension.VimExtension @@ -33,12 +30,15 @@ import com.maddyhome.idea.vim.extension.VimExtensionFacade.putExtensionHandlerMa import com.maddyhome.idea.vim.extension.VimExtensionFacade.putKeyMappingIfMissing import com.maddyhome.idea.vim.extension.VimExtensionFacade.setOperatorFunction import com.maddyhome.idea.vim.extension.VimExtensionFacade.setRegisterForCaret -import com.maddyhome.idea.vim.state.mode.mode import com.maddyhome.idea.vim.key.OperatorFunction import com.maddyhome.idea.vim.newapi.ij import com.maddyhome.idea.vim.newapi.vim import com.maddyhome.idea.vim.options.helpers.ClipboardOptionHelper import com.maddyhome.idea.vim.put.PutData +import com.maddyhome.idea.vim.state.mode.Mode +import com.maddyhome.idea.vim.state.mode.SelectionType +import com.maddyhome.idea.vim.state.mode.mode +import com.maddyhome.idea.vim.state.mode.selectionType import org.jetbrains.annotations.NonNls import java.awt.event.KeyEvent import javax.swing.KeyStroke @@ -280,96 +280,97 @@ internal class VimSurroundExtension : VimExtension { } } } +} - companion object { - private const val REGISTER = '"' - - private val tagNameAndAttributesCapturePattern = "(\\S+)([^>]*)>".toPattern() - - private val SURROUND_PAIRS = mapOf( - 'b' to ("(" to ")"), - '(' to ("( " to " )"), - ')' to ("(" to ")"), - 'B' to ("{" to "}"), - '{' to ("{ " to " }"), - '}' to ("{" to "}"), - 'r' to ("[" to "]"), - '[' to ("[ " to " ]"), - ']' to ("[" to "]"), - 'a' to ("<" to ">"), - '>' to ("<" to ">"), - 's' to (" " to ""), - ) +private const val REGISTER = '"' + +private val tagNameAndAttributesCapturePattern = "(\\S+)([^>]*)>".toPattern() + +private val SURROUND_PAIRS = mapOf( + 'b' to ("(" to ")"), + '(' to ("( " to " )"), + ')' to ("(" to ")"), + 'B' to ("{" to "}"), + '{' to ("{ " to " }"), + '}' to ("{" to "}"), + 'r' to ("[" to "]"), + '[' to ("[ " to " ]"), + ']' to ("[" to "]"), + 'a' to ("<" to ">"), + '>' to ("<" to ">"), + 's' to (" " to ""), +) + +private fun getSurroundPair(c: Char): Pair? = if (c in SURROUND_PAIRS) { + SURROUND_PAIRS[c] +} else if (!c.isLetter()) { + val s = c.toString() + s to s +} else { + null +} - private fun getSurroundPair(c: Char): Pair? = if (c in SURROUND_PAIRS) { - SURROUND_PAIRS[c] - } else if (!c.isLetter()) { - val s = c.toString() - s to s - } else { - null - } +private fun inputTagPair(editor: Editor): Pair? { + val tagInput = inputString(editor, "<", '>') + val matcher = tagNameAndAttributesCapturePattern.matcher(tagInput) + return if (matcher.find()) { + val tagName = matcher.group(1) + val tagAttributes = matcher.group(2) + "<$tagName$tagAttributes>" to "" + } else { + null + } +} - private fun inputTagPair(editor: Editor): Pair? { - val tagInput = inputString(editor, "<", '>') - val matcher = tagNameAndAttributesCapturePattern.matcher(tagInput) - return if (matcher.find()) { - val tagName = matcher.group(1) - val tagAttributes = matcher.group(2) - "<$tagName$tagAttributes>" to "" - } else { - null - } - } +private fun inputFunctionName( + editor: Editor, + withInternalSpaces: Boolean, +): Pair? { + val functionNameInput = inputString(editor, "function: ", null) + if (functionNameInput.isEmpty()) return null + return if (withInternalSpaces) "$functionNameInput( " to " )" else "$functionNameInput(" to ")" +} - private fun inputFunctionName( - editor: Editor, - withInternalSpaces: Boolean, - ): Pair? { - val functionNameInput = inputString(editor, "function: ", null) - if (functionNameInput.isEmpty()) return null - return if (withInternalSpaces) "$functionNameInput( " to " )" else "$functionNameInput(" to ")" - } +private fun getOrInputPair(c: Char, editor: Editor): Pair? = when (c) { + '<', 't' -> inputTagPair(editor) + 'f' -> inputFunctionName(editor, false) + 'F' -> inputFunctionName(editor, true) + else -> getSurroundPair(c) +} - private fun getOrInputPair(c: Char, editor: Editor): Pair? = when (c) { - '<', 't' -> inputTagPair(editor) - 'f' -> inputFunctionName(editor, false) - 'F' -> inputFunctionName(editor, true) - else -> getSurroundPair(c) - } +private fun getChar(editor: Editor): Char { + val key = inputKeyStroke(editor) + val keyChar = key.keyChar + return if (keyChar == KeyEvent.CHAR_UNDEFINED || keyChar.code == KeyEvent.VK_ESCAPE) { + 0.toChar() + } else { + keyChar + } +} - private fun getChar(editor: Editor): Char { - val key = inputKeyStroke(editor) - val keyChar = key.keyChar - return if (keyChar == KeyEvent.CHAR_UNDEFINED || keyChar.code == KeyEvent.VK_ESCAPE) { - 0.toChar() +private fun performSurround(pair: Pair, range: TextRange, caret: VimCaret, tagsOnNewLines: Boolean = false) { + runWriteAction { + val editor = caret.editor + val change = VimPlugin.getChange() + val leftSurround = pair.first + if (tagsOnNewLines) "\n" else "" + + val isEOF = range.endOffset == editor.text().length + val hasNewLine = editor.endsWithNewLine() + val rightSurround = if (tagsOnNewLines) { + if (isEOF && !hasNewLine) { + "\n" + pair.second } else { - keyChar + pair.second + "\n" } + } else { + pair.second } - private fun performSurround(pair: Pair, range: TextRange, caret: VimCaret, tagsOnNewLines: Boolean = false) { - runWriteAction { - val editor = caret.editor - val change = VimPlugin.getChange() - val leftSurround = pair.first + if (tagsOnNewLines) "\n" else "" - - val isEOF = range.endOffset == editor.text().length - val hasNewLine = editor.endsWithNewLine() - val rightSurround = if (tagsOnNewLines) { - if (isEOF && !hasNewLine) { - "\n" + pair.second - } else { - pair.second + "\n" - } - } else { - pair.second - } - - change.insertText(editor, caret, range.startOffset, leftSurround) - change.insertText(editor, caret, range.endOffset + leftSurround.length, rightSurround) - injector.markService.setChangeMarks(caret, TextRange(range.startOffset, range.endOffset + leftSurround.length + rightSurround.length)) - } - } + change.insertText(editor, caret, range.startOffset, leftSurround) + change.insertText(editor, caret, range.endOffset + leftSurround.length, rightSurround) + injector.markService.setChangeMarks( + caret, + TextRange(range.startOffset, range.endOffset + leftSurround.length + rightSurround.length) + ) } }