Skip to content

Instantly share code, notes, and snippets.

@Mishkun
Last active March 15, 2020 12:21
Show Gist options
  • Save Mishkun/7f39bb762033b8aadc60feffb6fc18d5 to your computer and use it in GitHub Desktop.
Save Mishkun/7f39bb762033b8aadc60feffb6fc18d5 to your computer and use it in GitHub Desktop.
This LivePlugin https://github.com/dkandalov/live-plugin#liveplugin snippet toggles comma character at the end of the line. Combine this with ideavim for optimal experience
# you can use any other character. Just replace `,` with the vim expr of your choice
nmap , :action ToggleEndOfLineComma<cr>
// MIT License
//
// Copyright (c) 2020 Mikhail Levchenko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.util.TextRange
import liveplugin.currentEditor
import liveplugin.registerAction
import liveplugin.runWriteAction
registerAction(id = "ToggleEndOfLineComma", callback = { event: AnActionEvent ->
val project = event.project!!
val editor = project.currentEditor!!
editor.document.runWriteAction(project, description = "Toggle comma at the end of the line", callback = {
val currentLine = editor.caretModel.logicalPosition.line
val lineEndOffset = editor.document.getLineEndOffset(currentLine)
val endChar = editor.document.getText(TextRange.create(lineEndOffset - 1, lineEndOffset)).first()
if (endChar == ',') {
editor.document.deleteString(lineEndOffset - 1, lineEndOffset)
} else {
editor.document.insertString(lineEndOffset, ",")
}
})
})
@Mishkun
Copy link
Author

Mishkun commented Mar 15, 2020

DEMO

ezgif-6-fd8b6ef943e1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment