Skip to content

Commit

Permalink
Merge pull request #31 from dprint/cache-can-format-values
Browse files Browse the repository at this point in the history
Cache can format values and tweak can format timeout in external formatter
  • Loading branch information
ryan-rushton authored Jun 22, 2022
2 parents 665b9da + 8dbfefd commit 0bf6067
Show file tree
Hide file tree
Showing 20 changed files with 455 additions and 280 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# dprint-intellij-plugin Changelog

## [Unreleased]
- Reduce timeout when checking if a file can be formatted in the external formatter
- Cache whether files can be formatted by dprint and create an action to clear this
- Remove custom synchronization and move to an IntelliJ background task queue for dprint tasks (this appears to solve the hard to reproduce lock up issues)

## [0.3.3]
- Handle execution exceptions when running can format
Expand Down Expand Up @@ -39,4 +42,4 @@
- Fix issue where the inability to parse the schema would stop a project form opening.

## [0.1.2]
- Release first public version of the plugin.
- Release first public version of the plugin.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ repositories {
mavenCentral()
}
dependencies {
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.19.0")
implementation("org.apache.commons:commons-collections4:4.4")
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.20.0")
testImplementation(kotlin("test"))
}

Expand Down
6 changes: 4 additions & 2 deletions detekt-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ complexity:
LongMethod:
threshold: 80
TooManyFunctions:
thresholdInFiles: 15
thresholdInClasses: 15
thresholdInFiles: 17
thresholdInClasses: 17
LongParameterList:
functionThreshold: 7
ComplexMethod:
threshold: 25
formatting:
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -> https://plugins.jetbrains.com/docs/intellij/intellij-artifacts.html
pluginGroup=com.dprint.intellij.plugin
pluginName=dprint-intellij-plugin
pluginVersion=0.3.3
pluginVersion=0.3.4
# See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
# for insight into build numbers and IntelliJ Platform versions.
pluginSinceBuild=213
Expand Down
25 changes: 25 additions & 0 deletions src/main/kotlin/com/dprint/actions/ClearCacheAction.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.dprint.actions

import com.dprint.config.ProjectConfiguration
import com.dprint.core.Bundle
import com.dprint.core.LogUtils
import com.dprint.services.editorservice.EditorServiceManager
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.logger

private val LOGGER = logger<ClearCacheAction>()

/**
* This action clears the cache of canFormat results. Useful if config changes have been made.
*/
class ClearCacheAction : AnAction() {
override fun actionPerformed(event: AnActionEvent) {
event.project?.let { project ->
if (!project.service<ProjectConfiguration>().state.enabled) return@let
LogUtils.info(Bundle.message("clear.cache.action.run"), project, LOGGER)
project.service<EditorServiceManager>().clearCanFormatCache()
}
}
}
14 changes: 12 additions & 2 deletions src/main/kotlin/com/dprint/actions/OnSaveAction.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.dprint.actions

import com.dprint.config.ProjectConfiguration
import com.dprint.config.UserConfiguration
import com.dprint.core.Bundle
import com.dprint.core.LogUtils
import com.dprint.services.FormatterService
import com.intellij.codeInsight.actions.ReformatCodeProcessor
import com.intellij.ide.actionsOnSave.impl.ActionsOnSaveFileDocumentManagerListener
import com.intellij.openapi.command.CommandProcessor
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.Document
Expand All @@ -13,19 +16,26 @@ import com.intellij.openapi.project.Project

private val LOGGER = logger<OnSaveAction>()

/**
* This action sets up format on save functionality.
*/
class OnSaveAction : ActionsOnSaveFileDocumentManagerListener.ActionOnSave() {

override fun isEnabledForProject(project: Project): Boolean {
return project.service<ProjectConfiguration>().state.enabled
return project.service<ProjectConfiguration>().state.enabled &&
project.service<UserConfiguration>().state.runOnSave
}

override fun processDocuments(project: Project, documents: Array<out Document>) {
if (CommandProcessor.getInstance().currentCommandName == ReformatCodeProcessor.getCommandName()) {
return
}
val formatterService = project.service<FormatterService>()
val manager = FileDocumentManager.getInstance()
for (document in documents) {
manager.getFile(document)?.let {
LogUtils.info(Bundle.message("save.action.run", it.path), project, LOGGER)
formatterService.format(it)
formatterService.format(it.path, document)
}
}
}
Expand Down
51 changes: 43 additions & 8 deletions src/main/kotlin/com/dprint/actions/ReformatAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.PlatformDataKeys
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.Document
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.ReadonlyStatusHandler
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiManager
import java.util.Collections

private val LOGGER = logger<ReformatAction>()

Expand All @@ -25,20 +31,49 @@ class ReformatAction : AnAction() {
event.project?.let { project ->
if (!project.service<ProjectConfiguration>().state.enabled) return@let

val formatterService = project.service<FormatterService>()
val editor = event.getData(PlatformDataKeys.EDITOR)

if (editor != null) {
PsiDocumentManager.getInstance(project).getPsiFile(editor.document)?.virtualFile?.let { virtualFile ->
LogUtils.info(Bundle.message("reformat.action.run", virtualFile.path), project, LOGGER)
formatterService.format(virtualFile)
}
formatDocument(project, editor.document)
} else {
event.getData(PlatformDataKeys.VIRTUAL_FILE)?.let { virtualFile ->
LogUtils.info(Bundle.message("reformat.action.run", virtualFile.path), project, LOGGER)
formatterService.format(virtualFile)
event.getData(PlatformDataKeys.VIRTUAL_FILE)?.let {
formatVirtualFile(project, it)
}
}
}
}

private fun formatDocument(project: Project, document: Document) {
val formatterService = project.service<FormatterService>()
PsiDocumentManager.getInstance(project).getPsiFile(document)?.virtualFile?.let { virtualFile ->
LogUtils.info(Bundle.message("reformat.action.run", virtualFile.path), project, LOGGER)
formatterService.format(virtualFile.path, document)
}
}

private fun formatVirtualFile(project: Project, virtualFile: VirtualFile) {
val formatterService = project.service<FormatterService>()
LogUtils.info(Bundle.message("reformat.action.run", virtualFile.path), project, LOGGER)
getDocument(project, virtualFile)?.let {
formatterService.format(virtualFile.path, it)
}
}

private fun isFileWriteable(project: Project, virtualFile: VirtualFile): Boolean {
val readonlyStatusHandler = ReadonlyStatusHandler.getInstance(project)
return !virtualFile.isDirectory &&
virtualFile.isValid &&
virtualFile.isInLocalFileSystem &&
!readonlyStatusHandler.ensureFilesWritable(Collections.singleton(virtualFile)).hasReadonlyFiles()
}

private fun getDocument(project: Project, virtualFile: VirtualFile): Document? {
if (isFileWriteable(project, virtualFile)) {
PsiManager.getInstance(project).findFile(virtualFile)?.let {
return PsiDocumentManager.getInstance(project).getDocument(it)
}
}

return null
}
}
5 changes: 2 additions & 3 deletions src/main/kotlin/com/dprint/actions/RestartAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.logger

private val LOGGER = logger<RestartAction>()

/**
* This action will restart the editor service when invoked
*/

private val LOGGER = logger<RestartAction>()

class RestartAction : AnAction() {
override fun actionPerformed(event: AnActionEvent) {
event.project?.let { project ->
Expand Down
Loading

0 comments on commit 0bf6067

Please sign in to comment.