Skip to content

Instantly share code, notes, and snippets.

@IonsInTheEther
Last active April 18, 2024 08:35
Show Gist options
  • Save IonsInTheEther/c1f1fe6af8d83e387436946f6319bcda to your computer and use it in GitHub Desktop.
Save IonsInTheEther/c1f1fe6af8d83e387436946f6319bcda to your computer and use it in GitHub Desktop.
A JetBrains LivePlugin to collapse `class` attributes. Invaluable for Tailwind projects.
import com.intellij.codeInsight.folding.impl.EditorFoldingInfo
import com.intellij.codeInsight.folding.impl.FoldingUtil
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.editor.Document
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.FoldRegion
import com.intellij.openapi.editor.ex.FoldingModelEx
import java.util.regex.Pattern
import static liveplugin.PluginUtil.*
// Notes about this plugin:
// This is a JetBrains LivePlugin to collapse `class` attributes. Invaluable for Tailwind projects.
// Works with standard HTML "class=", ERB "class: ", and JSX "className=" attributes
// Adapted from @denisborovikov's sx collapse LivePlugin found here:
// https://gist.github.com/denisborovikov/e5241c93f0dd9007b3fdb00d8c6e125b
// (looks better if folded text background is the same as normal text; Settings -> Editor -> Colors & Fonts)
// Setup:
// Put this file inside a `.live-plugins` directory at the root of your project
// It can only be executed within LivePlugin -- https://github.com/dkandalov/live-plugin
// Install LivePlugin through the Plugins manager in your JetBrains IDE
// Go to View > Tool Windows > Live Plugins
// In the Live Plugins panel, click the Settings button and enable "Run Plugins on IDE Start" and "Run Project Specific Plugins"
// Restart your JetBrains IDE and your plugin should now be running
// TO USE:
// ctrl-alt-0 (defined below) to collapse/expand class tags
// To make changes to the plugin, edit this file then right-click it in the project browser and select 'Rerun Plugin'
registerAction("collapseClassAttributes", "ctrl alt 0") { AnActionEvent event ->
def editor = currentEditorIn(event.project)
collapseIn(editor, 'class="(\s*.*?\s*)"', { "[~]" })
collapseIn(editor, 'class: ["\'](\s*.*?\s*)["\']', { "[~]" })
collapseIn(editor, 'className=\\"(\s*.*?\s*)\\"', { "[~]" })
}
if (!isIdeStartup) show("Collapse HTML/ERB/JSX class attributes (tailwind helper). Use Ctrl+Alt+0 to run it.")
def collapseIn(Editor editor, String regExp, Closure replacementFor) {
def matches = []
def matcher = Pattern.compile(regExp, Pattern.DOTALL).matcher(editor.document.charsSequence)
while (matcher.find()) {
matches << [start: matcher.start(0), end: matcher.end(0), text: matcher.group(0)]
// If you prefer to keep the "class=" portion visible, change the line above to:
// matches << [start: matcher.start(1), end: matcher.end(1), text: matcher.group(1)]
}
editor.foldingModel.runBatchFoldingOperation(new Runnable() {
@Override
public void run() {
matches.each { foldText(it.start, it.end, replacementFor(it.text), editor) }
}
})
}
/**
* Originally copied from com.intellij.codeInsight.folding.impl.CollapseSelectionHandler
*/
def foldText(int start, int end, String placeHolderText, Editor editor) {
if (start + 1 >= end) return
if (start < end && editor.document.charsSequence.charAt(end - 1) == '\n') end--
FoldRegion region = FoldingUtil.findFoldRegion(editor, start, end)
if (region != null) {
EditorFoldingInfo info = EditorFoldingInfo.get(editor)
if (info.getPsiElement(region) == null) {
editor.foldingModel.removeFoldRegion(region)
info.removeRegion(region)
}
} else {
region = ((FoldingModelEx)editor.foldingModel).addFoldRegion(start, end, placeHolderText)
if (region == null) {
return
}
region.expanded = false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment