@@ -5,14 +5,9 @@ import com.intellij.formatting.service.AsyncFormattingRequest
5
5
import com.intellij.formatting.service.FormattingService
6
6
import com.intellij.openapi.application.ApplicationManager
7
7
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
13
8
import com.intellij.psi.PsiFile
14
9
import xyz.block.kotlinformatter.Ktfmt
15
- import java.io.InputStreamReader
10
+ import xyz.block.kotlinformatter.idea.KotlinReformatService.Companion.FORMATTING_IGNORE_FILE
16
11
17
12
/* *
18
13
* A service that overrides the default IntelliJ formatting behavior for Kotlin files.
@@ -29,6 +24,11 @@ class KotlinReformatService : AsyncDocumentFormattingService() {
29
24
* called.
30
25
*/
31
26
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
+
32
32
if (! file.name.endsWith(" .kt" )) return false
33
33
34
34
return ! isFormattingIgnored(file)
@@ -107,25 +107,6 @@ class KotlinReformatService : AsyncDocumentFormattingService() {
107
107
return false
108
108
}
109
109
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
-
129
110
companion object {
130
111
private val LOG = Logger .getInstance(KotlinReformatService ::class .java)
131
112
private const val FORMATTING_IGNORE_FILE = " .kotlin-formatter-ignore"
0 commit comments