Skip to content

Commit db18125

Browse files
authored
Merge pull request #41 from block/mehdi.idea-plugin-config
Make idea-plugin enabled by configuration per project
2 parents 7865707 + ca05b3c commit db18125

File tree

4 files changed

+76
-25
lines changed

4 files changed

+76
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package xyz.block.kotlinformatter.idea
2+
3+
import com.intellij.openapi.components.Service
4+
import com.intellij.openapi.diagnostic.Logger
5+
import com.intellij.openapi.project.Project
6+
import xyz.block.kotlinformatter.idea.FormatOnSavePostStartupActivity.Companion
7+
import java.io.InputStreamReader
8+
import java.util.Properties
9+
10+
@Service(Service.Level.PROJECT)
11+
class FormatConfigurationService(private val project: Project) {
12+
13+
// This is calculated once and on demand.
14+
// Changes to the config file will require a restart.
15+
val formattingEnabled by lazy {
16+
val configFile = project.getFile(CONFIG_FILE_PATH)
17+
val enabled = configFile?.let { file ->
18+
val properties = Properties()
19+
InputStreamReader(file.inputStream).use {
20+
properties.load(it)
21+
properties.getProperty(ENABLED_PROPERTY_NAME)?.toBoolean() == true
22+
}
23+
} ?: false
24+
25+
logger.info("Formatting enabled: $enabled")
26+
enabled
27+
}
28+
29+
companion object {
30+
private const val CONFIG_FILE_PATH: String = ".idea/kotlin-formater.properties"
31+
private const val ENABLED_PROPERTY_NAME: String = "kotlin-formatter.enabled"
32+
private val logger = Logger.getInstance(FormatConfigurationService::class.java.name)
33+
}
34+
}

idea-plugin/src/main/kotlin/xyz/block/kotlinformatter/idea/FormatOnSaveListener.kt

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ class FormatOnSaveListener(private val project: Project, private val formatter:
3030
private val processedDocuments = ConcurrentHashMap.newKeySet<Document>()
3131

3232
override fun beforeDocumentSaving(document: Document) {
33+
if (!project.getService(FormatConfigurationService::class.java).formattingEnabled) {
34+
logger.info("Formatting is not enabled")
35+
return
36+
}
37+
3338
val file: VirtualFile = FileDocumentManager.getInstance().getFile(document) ?: return
3439
// All open projects will receive this event, skip if the file doesn't belong to the current project.
3540
if (!ProjectFileIndex.getInstance(project).isInProject(file)) return

idea-plugin/src/main/kotlin/xyz/block/kotlinformatter/idea/KotlinReformatService.kt

+6-25
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@ import com.intellij.formatting.service.AsyncFormattingRequest
55
import com.intellij.formatting.service.FormattingService
66
import com.intellij.openapi.application.ApplicationManager
77
import com.intellij.openapi.diagnostic.Logger
8-
import com.intellij.openapi.project.Project
9-
import com.intellij.openapi.project.guessProjectDir
10-
import com.intellij.openapi.util.io.FileUtilRt
11-
import com.intellij.openapi.vfs.VirtualFile
12-
import com.intellij.openapi.vfs.findFile
138
import com.intellij.psi.PsiFile
149
import xyz.block.kotlinformatter.Ktfmt
15-
import java.io.InputStreamReader
10+
import xyz.block.kotlinformatter.idea.KotlinReformatService.Companion.FORMATTING_IGNORE_FILE
1611

1712
/**
1813
* A service that overrides the default IntelliJ formatting behavior for Kotlin files.
@@ -29,6 +24,11 @@ class KotlinReformatService : AsyncDocumentFormattingService() {
2924
* called.
3025
*/
3126
override fun canFormat(file: PsiFile): Boolean {
27+
if (!file.project.getService(FormatConfigurationService::class.java).formattingEnabled) {
28+
LOG.info("Formatting is not enabled")
29+
return false
30+
}
31+
3232
if (!file.name.endsWith(".kt")) return false
3333

3434
return !isFormattingIgnored(file)
@@ -107,25 +107,6 @@ class KotlinReformatService : AsyncDocumentFormattingService() {
107107
return false
108108
}
109109

110-
private fun Project.getFileContent(filePath: String): String? {
111-
val rootDir = this.guessProjectDir()
112-
if (rootDir == null) {
113-
LOG.info("The project root directory is null - skipping")
114-
return null
115-
}
116-
val file = rootDir.findFile(filePath)
117-
if (file == null) {
118-
LOG.info("The file at $filePath is missing")
119-
return null
120-
}
121-
return file.loadText()
122-
}
123-
124-
private fun VirtualFile.loadText(): String =
125-
InputStreamReader(this.inputStream).use { reader ->
126-
return String(FileUtilRt.loadText(reader, this.length.toInt()))
127-
}
128-
129110
companion object {
130111
private val LOG = Logger.getInstance(KotlinReformatService::class.java)
131112
private const val FORMATTING_IGNORE_FILE = ".kotlin-formatter-ignore"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package xyz.block.kotlinformatter.idea
2+
3+
import com.intellij.openapi.diagnostic.Logger
4+
import com.intellij.openapi.project.Project
5+
import com.intellij.openapi.project.guessProjectDir
6+
import com.intellij.openapi.util.io.FileUtilRt
7+
import com.intellij.openapi.vfs.VirtualFile
8+
import com.intellij.openapi.vfs.findFile
9+
import java.io.InputStreamReader
10+
11+
private val logger = Logger.getInstance(KotlinReformatService::class.java)
12+
13+
fun Project.getFileContent(filePath: String): String? = getFile(filePath)?.loadText()
14+
15+
fun Project.getFile(filePath: String): VirtualFile? {
16+
val rootDir = this.guessProjectDir()
17+
if (rootDir == null) {
18+
logger.info("The project root directory is null - skipping")
19+
return null
20+
}
21+
val file = rootDir.findFile(filePath)
22+
if (file == null) {
23+
logger.info("The file at $filePath is missing")
24+
}
25+
return file
26+
}
27+
28+
private fun VirtualFile.loadText(): String =
29+
InputStreamReader(this.inputStream).use { reader ->
30+
return String(FileUtilRt.loadText(reader, this.length.toInt()))
31+
}

0 commit comments

Comments
 (0)