Skip to content

Instantly share code, notes, and snippets.

@oirodolfo
Forked from liautaud/tailwind-plugin-patcher.py
Created December 21, 2022 15:17
Show Gist options
  • Save oirodolfo/1af7ae2cb67731556c9e4719063981c7 to your computer and use it in GitHub Desktop.
Save oirodolfo/1af7ae2cb67731556c9e4719063981c7 to your computer and use it in GitHub Desktop.
Small Python script to patch the IntelliJ TailwindCSS plugin.
import re
from zipfile import ZipFile, Path
"""
TailwindCSS plugin patcher for IntelliJ IDEA
--------------------------------------------
1. Download the latest ZIP of the plugin compatible with your version of IntelliJ here:
https://plugins.jetbrains.com/plugin/15321-tailwind-css/versions
2. Fill `CLASS_ATTRIBUTES` to specify which XML attributes can contain Tailwind classes.
3. Fill `CLASS_REGEXES` to add completion regexes using the format described here:
https://github.com/tailwindlabs/tailwindcss/issues/7553#issuecomment-735915659
4. Run the script with `python3 tailwind-plugin-patcher.py`.
5. Install the patched ZIP via Preferences > Plugins > Gear Icon > Install plugin from disk.
"""
SOURCE_ARCHIVE_PATH = "tailwindcss-222.3739.24.zip"
PATCHED_ARCHIVE_PATH = "tailwindcss-222.3739.24-patched.zip"
CLASS_ATTRIBUTES = r'["class", "className", "\\w+ClassName"]'
CLASS_REGEXES = r'[]'
# ------------------------------------------
script_path = "tailwindcss/server/tailwindcss-language-server"
class_attributes_search = "(yield s.editor.getConfiguration(l.uri)).tailwindCSS.classAttributes"
class_regexes_search = '[A-Z][a-z]\(f,"tailwindCSS\.experimental\.classRegex",\[\]\)'
with ZipFile(SOURCE_ARCHIVE_PATH, "r") as source_archive:
with ZipFile(PATCHED_ARCHIVE_PATH, "w") as patched_archive:
# Copy everything except the script file.
patched_archive.comment = source_archive.comment
for item in source_archive.infolist():
if item.filename != script_path:
patched_archive.writestr(item, source_archive.read(item.filename))
# Write the new script file.
with Path(source_archive, script_path).open("r") as script:
patched = script.read().replace(class_attributes_search, CLASS_ATTRIBUTES)
patched = re.sub(class_regexes_search, CLASS_REGEXES, patched)
with Path(patched_archive, script_path).open("w") as script:
script.write(patched)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment