EclipseCodeFormatter icon indicating copy to clipboard operation
EclipseCodeFormatter copied to clipboard

EclipseCodeStyleManager not honoring plugin listeners

Open spacedvoid opened this issue 1 year ago • 4 comments
trafficstars

What steps will reproduce the issue?

Create an IDE Kotlin script as below:

import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ProjectManager
import com.intellij.psi.PsiFile
import com.intellij.psi.codeStyle.CodeStyleManager

class ReformatListener(private val project: Project) : CodeStyleManager.Listener {
	init {
		LOGGER.info("Listener created for project " + project.name)
	}

	override fun beforeReformatText(file: PsiFile) {
		LOGGER.info("beforeReformatText: " + project.name)
	}

	override fun afterReformatText(file: PsiFile) {
		LOGGER.info("afterReformatText: " + project.name)
	}

	companion object {
		private val LOGGER: Logger = Logger.getInstance(ReformatListener::class.java)
	}
}

var project = ProjectManager.getInstance().openProjects[0] // Or the project instance you know
project.messageBus.connect().subscribe(CodeStyleManager.Listener.TOPIC, ReformatListener(project))

Run the script, and reformat any file.

What is the expected result?

Intellij log file idea.log shows the logs Listener created for project <projectName>, beforeReformatText: <projectName>, and afterReformatText: <projectName>, just like when the default formatter of Intellij is used.

What happens instead?

No logs are printed.

Paste information about IDE and OS (it can be copied from Help | About dialog).

IntelliJ IDEA 2023.3.1 (Community Edition) Build #IC-233.11799.300, built on December 12, 2023 Runtime version: 17.0.9+7-b1087.7 amd64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. Windows 11.0 GC: G1 Young Generation, G1 Old Generation Memory: 2048M Cores: 16 Registry: debugger.new.tool.window.layout=true ide.experimental.ui=true Non-Bundled Plugins: DevKit (233.11799.300) EclipseCodeFormatter (23.3.223.000.0-Eclipse_2023-03) com.demonwav.minecraft-dev (2023.3-1.6.12) Kotlin: 233.11799.300-IJ

> Applicable fixes

As used in com.intellij.psi.impl.source.codeStyle.CodeStyleManagerImpl#reformatText(com.intellij.psi.PsiFile, com.intellij.formatting.FormatTextRanges), include the lines

this.original.getProject().getMessageBus().syncPublisher(Listener.TOPIC).beforeReformatText(file);

and

this.original.getProject().getMessageBus().syncPublisher(Listener.TOPIC).afterReformatText(file);

before and after formatting in EclipseCodeStyleManager.

spacedvoid avatar Jan 13 '24 12:01 spacedvoid

Seems like the dependent IDE version is stuck at 2022.3, and listeners were presented at 2023.3.2... Requires a version bump to fix this.

spacedvoid avatar Jan 15 '24 12:01 spacedvoid

Not planning to create a merge request unless the plugin uses the latest api. https://github.com/krasa/EclipseCodeFormatter/blob/8596658c943a6b63c301d973aba1db986f8849b1/src/main/java/krasa/formatter/plugin/ProjectCodeStyleInstaller.java#L75-L82 Guess they were right...

spacedvoid avatar Jan 15 '24 14:01 spacedvoid

Maybe I should switch to com.intellij.formatting.service.FormattingService

krasa avatar Jan 15 '24 14:01 krasa

Guess they were right...

There was no alternative at that time.

krasa avatar Jan 15 '24 14:01 krasa