Skip to content

Commit

Permalink
Merge pull request #391 from koxudaxi/migrate_formatter_to_AsyncDocum…
Browse files Browse the repository at this point in the history
…entFormattingService

Migrate formatter to AsyncDocumentFormattingService
  • Loading branch information
koxudaxi committed Sep 17, 2024
2 parents 9ea2edd + f593614 commit 1676adc
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 129 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ jobs:

# Run Qodana inspections
- name: Qodana - Code Inspection
uses: JetBrains/qodana-action@v2024.1.9
uses: JetBrains/qodana-action@v2024.2
with:
cache-default-branch-only: true

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## [Unreleased]
- Migrate formatter to AsyncDocumentFormattingService [[#391](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/391)]
- Remove projectRuffExecutablePath in config file [[#505](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/505)]

## [0.0.39] - 2024-09-12
Expand Down
4 changes: 2 additions & 2 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
<extensions defaultExtensionNs="com.intellij">
<postStartupActivity implementation="com.koxudaxi.ruff.RuffProjectInitializer"/>
<projectConfigurable groupId="tools" instance="com.koxudaxi.ruff.RuffConfigurable"/>
<postFormatProcessor implementation="com.koxudaxi.ruff.RuffFixProcessor"/>
<postFormatProcessor implementation="com.koxudaxi.ruff.RuffFormatProcessor"/>
<localInspection language="Python" shortName="RuffInspection" suppressId="Ruff"
displayName="Ruff inspection"
enabledByDefault="true" level="WARNING"
implementationClass="com.koxudaxi.ruff.RuffInspection"/>
<externalAnnotator language="Python" implementationClass="com.koxudaxi.ruff.RuffExternalAnnotator"/>
<formattingService implementation="com.koxudaxi.ruff.RuffAsyncFixFormatter"/>
<formattingService implementation="com.koxudaxi.ruff.RuffAsyncFormatFormatter" order="last"/>
<platform.backend.documentation.targetProvider
implementation="com.koxudaxi.ruff.RuffNoqaDocumentationTargetProvider"/>
<actionOnSave id="BlackFormatterActionOnSave" implementation="com.koxudaxi.ruff.RuffActionOnSave" order="last"/>
Expand Down
4 changes: 2 additions & 2 deletions src/com/koxudaxi/ruff/Ruff.kt
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,15 @@ fun runCommand(
commandArgs.executable, commandArgs.project, commandArgs.stdin, *commandArgs.args.toTypedArray()
)


private fun getGeneralCommandLine(command: List<String>, projectPath: String?): GeneralCommandLine =
GeneralCommandLine(command).withWorkDirectory(projectPath).withCharset(Charsets.UTF_8)

fun getGeneralCommandLine(executable: File, project: Project?, vararg args: String): GeneralCommandLine? {
val projectPath = project?.basePath ?: return null
if (!WslPath.isWslUncPath(executable.path)) {
return getGeneralCommandLine(listOf(executable.path) + args, projectPath)
return getGeneralCommandLine(listOf(executable.path) + args, projectPath)
}

val windowsUncPath = WslPath.parseWindowsUncPath(executable.path) ?: return null
val configArgIndex = args.indexOf(CONFIG_ARG)
val injectedArgs = if (configArgIndex != -1 && configArgIndex < args.size - 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package com.koxudaxi.ruff
import com.intellij.openapi.project.Project


class RuffFixProcessor : RuffPostFormatProcessor() {
class RuffAsyncFixFormatter : RuffAsyncFormatterBase() {
override fun isEnabled(project: Project): Boolean =
RuffConfigService.getInstance(project).runRuffOnReformatCode

override fun process(sourceFile: SourceFile): String? = fix(sourceFile)
override fun getArgs(project: Project): List<String> = project.FIX_ARGS
override fun getName(): String = "Ruff Fix Formatter"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package com.koxudaxi.ruff

import com.intellij.openapi.project.Project


class RuffFormatProcessor : RuffPostFormatProcessor() {
class RuffAsyncFormatFormatter : RuffAsyncFormatterBase() {
override fun isEnabled(project: Project): Boolean {
val ruffConfigService = RuffConfigService.getInstance(project)
return ruffConfigService.runRuffOnReformatCode && ruffConfigService.useRuffFormat && RuffCacheService.hasFormatter(project)
return ruffConfigService.runRuffOnReformatCode && ruffConfigService.useRuffFormat
}
override fun process(sourceFile: SourceFile): String? = format(sourceFile)

override fun getArgs(project: Project): List<String> = FORMAT_ARGS
override fun getName(): String = "Ruff Format Formatter"

}
79 changes: 79 additions & 0 deletions src/com/koxudaxi/ruff/RuffAsyncFormatter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.koxudaxi.ruff

import com.intellij.execution.ExecutionException
import com.intellij.execution.process.CapturingProcessAdapter
import com.intellij.execution.process.OSProcessHandler
import com.intellij.execution.process.ProcessEvent
import com.intellij.formatting.FormattingContext
import com.intellij.formatting.service.AsyncDocumentFormattingService
import com.intellij.formatting.service.AsyncFormattingRequest
import com.intellij.formatting.service.FormattingService
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile
import java.nio.charset.StandardCharsets
import java.util.*


abstract class RuffAsyncFormatterBase : AsyncDocumentFormattingService() {
abstract fun isEnabled(project: Project): Boolean
abstract fun getArgs(project: Project): List<String>
private val FEATURES: MutableSet<FormattingService.Feature> = EnumSet.noneOf(

Check notice on line 20 in src/com/koxudaxi/ruff/RuffAsyncFormatter.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Private property naming convention

Private property name `FEATURES` should not start with an uppercase letter
FormattingService.Feature::class.java
)

override fun getFeatures(): MutableSet<FormattingService.Feature> {
return FEATURES
}

override fun canFormat(file: PsiFile): Boolean {
return isEnabled(file.project) && file.isApplicableTo
}

override fun createFormattingTask(request: AsyncFormattingRequest): FormattingTask? {
val formattingContext: FormattingContext = request.context
val ioFile = request.ioFile ?: return null
val sourceFile = formattingContext.containingFile.sourceFile
val commandArgs = generateCommandArgs(sourceFile, getArgs(formattingContext.project)) ?: return null
try {
val commandLine =
getGeneralCommandLine(commandArgs.executable, commandArgs.project, *commandArgs.args.toTypedArray())
?: return null
val handler = OSProcessHandler(commandLine.withCharset(StandardCharsets.UTF_8))
with(handler) {
processInput.write(ioFile.readText().toByteArray())
processInput.close()
}
return object : FormattingTask {
override fun run() {
handler.addProcessListener(object : CapturingProcessAdapter() {
override fun processTerminated(event: ProcessEvent) {
val exitCode = event.exitCode
if (exitCode == 0) {
request.onTextReady(output.stdout)
} else {
request.onError("Ruff Error", output.stderr)

Check warning on line 54 in src/com/koxudaxi/ruff/RuffAsyncFormatter.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Incorrect string capitalization

String 'Ruff Error' is not properly capitalized. It should have sentence capitalization
}
}
})
handler.startNotify()
}

override fun cancel(): Boolean {
handler.destroyProcess()
return true
}

override fun isRunUnderProgress(): Boolean {
return true
}
}
} catch (e: ExecutionException) {
e.message?.let { request.onError("Ruff Error", it) }

Check warning on line 71 in src/com/koxudaxi/ruff/RuffAsyncFormatter.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Incorrect string capitalization

String 'Ruff Error' is not properly capitalized. It should have sentence capitalization
return null
}
}

override fun getNotificationGroupId(): String {
return "Ruff"
}
}
61 changes: 0 additions & 61 deletions src/com/koxudaxi/ruff/RuffPostFormatProcessor.kt

This file was deleted.

57 changes: 0 additions & 57 deletions testSrc/com/koxudaxi/ruff/RuffPostFormatProcessorTest.kt

This file was deleted.

0 comments on commit 1676adc

Please sign in to comment.