1
1
package com.pinterest.ktlint.rule.engine.internal
2
2
3
3
import com.pinterest.ktlint.rule.engine.core.api.ElementType
4
+ import com.pinterest.ktlint.rule.engine.core.api.ElementType.ANNOTATED_EXPRESSION
5
+ import com.pinterest.ktlint.rule.engine.core.api.ElementType.ANNOTATION_ENTRY
4
6
import com.pinterest.ktlint.rule.engine.core.api.ElementType.COMMA
5
7
import com.pinterest.ktlint.rule.engine.core.api.ElementType.FILE
8
+ import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUN
9
+ import com.pinterest.ktlint.rule.engine.core.api.ElementType.MODIFIER_LIST
6
10
import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_ARGUMENT_LIST
7
11
import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_PARAMETER
8
12
import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_PARAMETER_LIST
@@ -11,6 +15,7 @@ import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_ARGUMENT
11
15
import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_ARGUMENT_LIST
12
16
import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_PARAMETER
13
17
import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_PARAMETER_LIST
18
+ import com.pinterest.ktlint.rule.engine.core.api.KtlintKotlinCompiler
14
19
import com.pinterest.ktlint.rule.engine.core.api.findChildByTypeRecursively
15
20
import com.pinterest.ktlint.rule.engine.core.api.firstChildLeafOrSelf
16
21
import com.pinterest.ktlint.rule.engine.core.api.indent
@@ -21,33 +26,26 @@ import com.pinterest.ktlint.rule.engine.core.api.nextCodeSibling
21
26
import com.pinterest.ktlint.rule.engine.core.api.replaceWith
22
27
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
23
28
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
24
- import org.jetbrains.kotlin.com.intellij.psi.PsiFileFactory
25
29
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl
26
30
import org.jetbrains.kotlin.com.intellij.psi.tree.TokenSet
27
- import org.jetbrains.kotlin.idea.KotlinLanguage
28
31
import org.jetbrains.kotlin.psi.KtAnnotatedExpression
29
32
import org.jetbrains.kotlin.psi.KtAnnotationEntry
30
33
import org.jetbrains.kotlin.psi.KtBinaryExpression
31
34
import org.jetbrains.kotlin.psi.KtBlockExpression
32
35
import org.jetbrains.kotlin.psi.KtClass
33
36
import org.jetbrains.kotlin.psi.KtClassInitializer
34
37
import org.jetbrains.kotlin.psi.KtDeclaration
35
- import org.jetbrains.kotlin.psi.KtDeclarationModifierList
36
38
import org.jetbrains.kotlin.psi.KtExpression
37
39
import org.jetbrains.kotlin.psi.KtFile
38
40
import org.jetbrains.kotlin.psi.KtFileAnnotationList
39
41
import org.jetbrains.kotlin.psi.KtFunction
40
42
import org.jetbrains.kotlin.psi.KtFunctionLiteral
41
43
import org.jetbrains.kotlin.psi.KtLambdaExpression
42
- import org.jetbrains.kotlin.psi.KtNamedFunction
43
44
import org.jetbrains.kotlin.psi.KtPrimaryConstructor
44
45
import org.jetbrains.kotlin.psi.KtProperty
45
46
import org.jetbrains.kotlin.psi.KtPropertyAccessor
46
- import org.jetbrains.kotlin.psi.KtScript
47
- import org.jetbrains.kotlin.psi.KtScriptInitializer
48
47
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
49
48
import org.jetbrains.kotlin.psi.psiUtil.children
50
- import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
51
49
import org.jetbrains.kotlin.util.prefixIfNot
52
50
53
51
private const val KTLINT_PREFIX = " ktlint"
@@ -273,19 +271,15 @@ private fun ASTNode.createSuppressAnnotation(
273
271
val fileAnnotation = targetNode.createFileAnnotation(suppressType, suppressions)
274
272
this .replaceWith(fileAnnotation.firstChildNode)
275
273
} else {
276
- val modifierListWithAnnotation = targetNode.createModifierListWithAnnotationEntry(suppressType, suppressions)
277
- this .replaceWith(
278
- modifierListWithAnnotation
279
- .getChildOfType<KtAnnotationEntry >()!!
280
- .node,
281
- )
274
+ val modifierListWithAnnotation = createModifierListWithAnnotationEntry(suppressType, suppressions)
275
+ this .replaceWith(modifierListWithAnnotation.findChildByType(ANNOTATION_ENTRY )!! )
282
276
}
283
277
}
284
278
285
279
is KtClass , is KtFunction , is KtProperty , is KtPropertyAccessor -> {
286
280
this .addChild(PsiWhiteSpaceImpl (indent()), this .firstChildNode)
287
- val modifierListWithAnnotation = targetNode. createModifierListWithAnnotationEntry(suppressType, suppressions)
288
- this .addChild(modifierListWithAnnotation.node , this .firstChildNode)
281
+ val modifierListWithAnnotation = createModifierListWithAnnotationEntry(suppressType, suppressions)
282
+ this .addChild(modifierListWithAnnotation, this .firstChildNode)
289
283
}
290
284
291
285
else -> {
@@ -294,15 +288,10 @@ private fun ASTNode.createSuppressAnnotation(
294
288
this .elementType != VALUE_PARAMETER
295
289
) {
296
290
val annotatedExpression = targetNode.createAnnotatedExpression(suppressType, suppressions)
297
- treeParent.replaceChild(targetNode, annotatedExpression.node )
291
+ treeParent.replaceChild(targetNode, annotatedExpression)
298
292
} else {
299
- val modifierListWithAnnotation = targetNode.createModifierListWithAnnotationEntry(suppressType, suppressions)
300
- treeParent.addChild(
301
- modifierListWithAnnotation
302
- .getChildOfType<KtAnnotationEntry >()!!
303
- .node,
304
- this ,
305
- )
293
+ val modifierListWithAnnotation = createModifierListWithAnnotationEntry(suppressType, suppressions)
294
+ treeParent.addChild(modifierListWithAnnotation.findChildByType(ANNOTATION_ENTRY )!! , this )
306
295
treeParent.addChild(PsiWhiteSpaceImpl (indent()), this )
307
296
}
308
297
}
@@ -318,12 +307,12 @@ private fun ASTNode.createFileAnnotation(
318
307
.joinToString()
319
308
.let { sortedSuppressions -> " @file:${suppressType.annotationName} ($sortedSuppressions )" }
320
309
.let { annotation ->
321
- PsiFileFactory
322
- .getInstance(psi.project )
323
- .createFileFromText( KotlinLanguage . INSTANCE , annotation)
324
- ?.firstChild
310
+ KtlintKotlinCompiler
311
+ .createPsiFileFromText( " file.kt " , annotation )
312
+ .firstChild
313
+ ?.node
325
314
? : throw IllegalStateException (" Can not create annotation '$annotation '" )
326
- }.node
315
+ }
327
316
328
317
private fun ASTNode.createFileAnnotationList (annotation : ASTNode ) {
329
318
require(isRoot()) { " File annotation list can only be created for root node" }
@@ -340,52 +329,43 @@ private fun ASTNode.createFileAnnotationList(annotation: ASTNode) {
340
329
}
341
330
}
342
331
343
- private fun ASTNode. createModifierListWithAnnotationEntry (
332
+ private fun createModifierListWithAnnotationEntry (
344
333
suppressType : SuppressAnnotationType ,
345
334
suppressions : Set <String >,
346
- ): PsiElement =
335
+ ): ASTNode =
347
336
suppressions
348
337
.sorted()
349
338
.joinToString()
350
339
.let { sortedSuppressions -> " @${suppressType.annotationName} ($sortedSuppressions )" }
351
340
.let { annotation ->
352
- PsiFileFactory
353
- .getInstance(psi.project)
354
- .createFileFromText(
355
- KotlinLanguage .INSTANCE ,
341
+ KtlintKotlinCompiler
342
+ .createASTNodeFromText(
356
343
// Create the annotation for a dummy declaration as the entire code block should be valid Kotlin code
357
344
"""
358
345
$annotation
359
346
fun foo() {}
360
347
""" .trimIndent(),
361
- ).getChildOfType<KtScript >()
362
- ?.getChildOfType<KtBlockExpression >()
363
- ?.getChildOfType<KtNamedFunction >()
364
- ?.getChildOfType<KtDeclarationModifierList >()
348
+ )?.findChildByType(FUN )
349
+ ?.findChildByType(MODIFIER_LIST )
365
350
? : throw IllegalStateException (" Can not create annotation '$annotation '" )
366
351
}
367
352
368
353
private fun ASTNode.createAnnotatedExpression (
369
354
suppressType : SuppressAnnotationType ,
370
355
suppressions : Set <String >,
371
- ): PsiElement =
356
+ ): ASTNode =
372
357
suppressions
373
358
.sorted()
374
359
.joinToString()
375
360
.let { sortedSuppressions -> " @${suppressType.annotationName} ($sortedSuppressions )" }
376
361
.let { annotation ->
377
- PsiFileFactory
378
- .getInstance(psi.project)
379
- .createFileFromText(
380
- KotlinLanguage .INSTANCE ,
362
+ KtlintKotlinCompiler
363
+ .createASTNodeFromText(
381
364
"""
382
365
|${this .indent(false )}$annotation
383
366
|${this .indent(false )}${this .text}
384
367
""" .trimMargin(),
385
- ).getChildOfType<KtScript >()
386
- ?.getChildOfType<KtBlockExpression >()
387
- ?.getChildOfType<KtScriptInitializer >()
388
- ?.getChildOfType<KtAnnotatedExpression >()
368
+ )?.findChildByType(ANNOTATED_EXPRESSION )
389
369
? : throw IllegalStateException (" Can not create annotation '$annotation '" )
390
370
}
391
371
0 commit comments