Skip to content

Commit

Permalink
feature: threading support (#66)
Browse files Browse the repository at this point in the history
accepted!
  • Loading branch information
WalrusSoup authored Feb 14, 2023
1 parent e008590 commit 9b51cc8
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 55 deletions.
29 changes: 28 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,31 @@ jobs:
- name: Upload Release Asset
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: gh release upload ${{ github.event.release.tag_name }} ./build/distributions/*
run: gh release upload ${{ github.event.release.tag_name }} ./build/distributions/*

# Create pull request
- name: Create Pull Request
if: ${{ steps.properties.outputs.changelog != '' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
VERSION="${{ github.event.release.tag_name }}"
BRANCH="changelog-update-$VERSION"
LABEL="release changelog"
git config user.email "[email protected]"
git config user.name "GitHub Action"
git checkout -b $BRANCH
git commit -am "Changelog update - $VERSION"
git push --set-upstream origin $BRANCH
gh label create "$LABEL" \
--description "Pull requests with release changelog update" \
|| true
gh pr create \
--title "Changelog update - \`$VERSION\`" \
--body "Current pull request contains patched \`CHANGELOG.md\` file for the \`$VERSION\` version." \
--label "$LABEL" \
--head $BRANCH
8 changes: 4 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- Keep a Changelog guide -> https://keepachangelog.com -->

# tailwind-formatter-next Changelog
# Tailwind Formatter Changelog

## [Unreleased]

## [2.0.4]
### Added
- Initial scaffold created from [IntelliJ Platform Plugin Template](https://github.com/JetBrains/intellij-platform-plugin-template)
- Performance enhancement: tasks now run in background threads with full progress indication
10 changes: 6 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ plugins {
// Java support
id("java")
// Kotlin support
id("org.jetbrains.kotlin.jvm") version "1.7.10"
id("org.jetbrains.kotlin.jvm") version "1.8.0"
// Gradle IntelliJ Plugin
id("org.jetbrains.intellij") version "1.8.0"
id("org.jetbrains.intellij") version "1.12.0"
// Gradle Changelog Plugin
id("org.jetbrains.changelog") version "1.3.1"
id("org.jetbrains.changelog") version "2.0.0"
// Gradle Qodana Plugin
id("org.jetbrains.qodana") version "0.1.13"
// Gradle Kover Plugin
id("org.jetbrains.kotlinx.kover") version "0.6.1"
}

group = properties("pluginGroup")
Expand All @@ -23,7 +25,7 @@ repositories {
mavenCentral()
}

// Set the JVM language level used to compile sources and generate files - Java 11 is required since 2020.3
// Set the JVM language level used to compile sources and generate files - Java 17 is required since 2022.3
kotlin {
jvmToolchain {
languageVersion.set(JavaLanguageVersion.of(11))
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pluginGroup = com.github.walrussoup.tailwindformatternext
pluginName = tailwind-formatter-next
# SemVer format -> https://semver.org
pluginVersion = 2.0.3
pluginVersion = 2.0.4

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 213
Expand All @@ -18,7 +18,7 @@ platformVersion = 2021.3.3
platformPlugins =

# Gradle Releases -> https://github.com/gradle/gradle/releases
gradleVersion = 7.5.1
gradleVersion = 7.6

# Opt-out flag for bundling Kotlin standard library -> https://plugins.jetbrains.com/docs/intellij/kotlin.html#kotlin-standard-library
# suppress inspection "UnusedProperty"
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rootProject.name = "tailwind-formatter-next"
rootProject.name = "Tailwind Formatter"
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.Document
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.progress.Task
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VfsUtilCore
import com.intellij.openapi.vfs.VirtualFile
import org.jetbrains.annotations.NotNull


class FormatFileAction : AnAction("Format Current File")
Expand All @@ -34,11 +38,16 @@ class FormatFileAction : AnAction("Format Current File")
val project: Project = e.getRequiredData(CommonDataKeys.PROJECT);
val document: Document = editor.document;
LOG.info("Building Parser");
val parser = TailwindParser(TailwindSorter(getClassOrder(project), isCustomConfiguration))
val body = parser.processBody(document.text);
LOG.info("Writing sorted output");
WriteCommandAction.runWriteCommandAction(project) { document.setText(body) }
TailwindFormatterStatusBarWidget.updateText("Finished", "Finished Formatting ${currentFile.name}");

ProgressManager.getInstance().run(object : Task.Backgroundable(null, "Format file") {
override fun run(@NotNull progressIndicator: ProgressIndicator) {
val parser = TailwindParser(TailwindSorter(getClassOrder(project), isCustomConfiguration))
val body = parser.processBody(document.text);
LOG.info("Writing sorted output");
WriteCommandAction.runWriteCommandAction(project) { document.setText(body) }
TailwindFormatterStatusBarWidget.updateText("Finished", "Finished Formatting ${currentFile.name}");
}
})
}

private fun getClassOrder(project: Project): List<String> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ import com.github.walrussoup.tailwindformatternext.support.TailwindUtility
import com.github.walrussoup.tailwindformatternext.ui.TailwindFormatterStatusBarWidget
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.progress.Task
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectFileIndex
import com.intellij.openapi.vfs.VfsUtilCore
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.search.FilenameIndex
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.annotations.NotNull

class FormatProjectAction : AnAction("Format Project")
{
Expand All @@ -24,45 +30,60 @@ class FormatProjectAction : AnAction("Format Project")

override fun actionPerformed(e: AnActionEvent)
{
TailwindFormatterStatusBarWidget.updateText("Starting", "Starting Project Wide Format");
// Not using com.intellij.openapi.progress.runBackgroundableTask as we lose scope of the project
ProgressManager.getInstance().run(object : Task.Backgroundable(null, "Format file") {
override fun run(@NotNull progressIndicator: ProgressIndicator) {
val project = e.project;
if (project == null) {
LOG.info("No project found, returning");
return;
}
TailwindFormatterStatusBarWidget.updateText("Starting", "Starting Project Wide Format");
progressIndicator.isIndeterminate = false;
LOG.info("Invoking project-wide formatting");
val parser = TailwindParser(TailwindSorter(getClassOrder(project), isCustomConfiguration))

LOG.info("Invoking project-wide formatting");
val project = e.project;
if(project == null) {
LOG.info("No project found, returning");
// List all files in the project
val allFiles = kotlin.runCatching {
runReadAction { getListOfAllProjectVFiles(project) }
}.getOrDefault(listOf<VirtualFile>());

return;
}
val parser = TailwindParser(TailwindSorter(getClassOrder(project), isCustomConfiguration))
LOG.info("Discovering vue files...");
val files = getListOfProjectVirtualFilesByExt(project, "vue");
LOG.info("${files.size} discovered")
val allFiles = getListOfAllProjectVFiles(project);
LOG.info("${allFiles.size} all files discovered")
LOG.info(extensions.toString());
var fileCount = 0;
allFiles.forEach {
if(it.extension == null) {
return@forEach;
}
if(!extensions.contains(it.extension)) {
LOG.info("Extension list does not contain: ${it.extension}")
return@forEach;
}
LOG.info("${it.canonicalPath} is valid for formatting");
val document = FileDocumentManager.getInstance().getDocument(it);
if(document == null) {
LOG.info("${it.canonicalPath} failed opening, skipping?");
return@forEach;
}
LOG.info("Processing ${it.name}...");
val body = parser.processBody(document.text);
WriteCommandAction.runWriteCommandAction(project) { document.setText(body) }
TailwindFormatterStatusBarWidget.updateText("Running", "Formatting ${it.name}");
fileCount++;
}
LOG.info("Found ${allFiles.size} files in project");
var fileCount = 0;
val totalFiles = allFiles.size;

allFiles.forEach {
if (progressIndicator.isCanceled) {
LOG.info("User cancelled formatting");
return@forEach;
}
if (it.extension == null || !extensions.contains(it.extension)) {
return@forEach;
}
progressIndicator.text = "Checking ${it.name}";
progressIndicator.fraction = fileCount.toDouble() / totalFiles.toDouble();
LOG.info("${it.canonicalPath} is valid for formatting");

TailwindFormatterStatusBarWidget.updateText("Finished Project Format", "Finished formatting $fileCount files.");
val document = kotlin.runCatching {
runReadAction { FileDocumentManager.getInstance().getDocument(it) }
}.getOrDefault(null);

if (document == null) {
LOG.info("No document found for ${it.name}, skipping");
return@forEach;
}
LOG.info("Processing ${it.name}...");
val body = parser.processBody(document.text);
WriteCommandAction.runWriteCommandAction(project) { document.setText(body) }
TailwindFormatterStatusBarWidget.updateText("Running", "Formatting ${it.name}");
fileCount++;
}
TailwindFormatterStatusBarWidget.updateText(
"Finished Project Format",
"Finished formatting $fileCount files."
);
}
});
}

override fun update(e: AnActionEvent)
Expand All @@ -80,14 +101,14 @@ class FormatProjectAction : AnAction("Format Project")
/**
* Builds a collection, containing of all files in the project.
*/
fun getListOfAllProjectVFiles(project: Project): MutableCollection<VirtualFile> {
fun getListOfAllProjectVFiles(project: Project): List<VirtualFile> {
val collection = mutableListOf<VirtualFile>()
ProjectFileIndex.getInstance(project).iterateContent {
collection += it
// Return true to process all the files (no early escape).
true
}
return collection
return collection.toList()
}

// TODO: This should not be copy pasted
Expand Down

0 comments on commit 9b51cc8

Please sign in to comment.