From 03f044a9a8757e792aa2c2fe3a46bc5b7474a3de Mon Sep 17 00:00:00 2001 From: Paul Dingemans Date: Sun, 19 Nov 2023 12:04:41 +0100 Subject: [PATCH] Add helper function 'fromSnippetWithPath' to create a Code instance that is to be linted/formatted. (#2359) Closes #2340 --- ktlint-rule-engine/api/ktlint-rule-engine.api | 2 ++ .../pinterest/ktlint/rule/engine/api/Code.kt | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/ktlint-rule-engine/api/ktlint-rule-engine.api b/ktlint-rule-engine/api/ktlint-rule-engine.api index 8503d0baff..fd09349548 100644 --- a/ktlint-rule-engine/api/ktlint-rule-engine.api +++ b/ktlint-rule-engine/api/ktlint-rule-engine.api @@ -15,6 +15,8 @@ public final class com/pinterest/ktlint/rule/engine/api/Code$Companion { public final fun fromPath (Ljava/nio/file/Path;)Lcom/pinterest/ktlint/rule/engine/api/Code; public final fun fromSnippet (Ljava/lang/String;Z)Lcom/pinterest/ktlint/rule/engine/api/Code; public static synthetic fun fromSnippet$default (Lcom/pinterest/ktlint/rule/engine/api/Code$Companion;Ljava/lang/String;ZILjava/lang/Object;)Lcom/pinterest/ktlint/rule/engine/api/Code; + public final fun fromSnippetWithPath (Ljava/lang/String;Ljava/nio/file/Path;)Lcom/pinterest/ktlint/rule/engine/api/Code; + public static synthetic fun fromSnippetWithPath$default (Lcom/pinterest/ktlint/rule/engine/api/Code$Companion;Ljava/lang/String;Ljava/nio/file/Path;ILjava/lang/Object;)Lcom/pinterest/ktlint/rule/engine/api/Code; public final fun fromStdin ()Lcom/pinterest/ktlint/rule/engine/api/Code; } diff --git a/ktlint-rule-engine/src/main/kotlin/com/pinterest/ktlint/rule/engine/api/Code.kt b/ktlint-rule-engine/src/main/kotlin/com/pinterest/ktlint/rule/engine/api/Code.kt index c64d0e97f8..c703e6e1dc 100644 --- a/ktlint-rule-engine/src/main/kotlin/com/pinterest/ktlint/rule/engine/api/Code.kt +++ b/ktlint-rule-engine/src/main/kotlin/com/pinterest/ktlint/rule/engine/api/Code.kt @@ -1,5 +1,9 @@ package com.pinterest.ktlint.rule.engine.api +import com.pinterest.ktlint.rule.engine.api.Code.Companion.fromFile +import com.pinterest.ktlint.rule.engine.api.Code.Companion.fromPath +import com.pinterest.ktlint.rule.engine.api.Code.Companion.fromSnippet +import com.pinterest.ktlint.rule.engine.api.Code.Companion.fromStdin import com.pinterest.ktlint.rule.engine.api.KtLintRuleEngine.Companion.STDIN_FILE import org.jetbrains.kotlin.konan.file.file import java.io.File @@ -81,6 +85,31 @@ public class Code private constructor( isStdIn = true, ) + /** + * The [content] represent a valid piece of Kotlin code or Kotlin script. The '.editorconfig' file on the filesystem is based on + * the [virtualPath]. The filesystem is not expected to actually contain a file with this name. Use [Code.fromFile] for scanning a + * file that does exist on the filesystem. + */ + public fun fromSnippetWithPath( + /** + * Code to be linted/formatted. + */ + content: String, + /** + * Virtual path of file. Contents of the file is *not* read. The path is only used to determine the '.editorconfig' file + * containing the configuration to be applied on the code that is to be linted/formatted. When not specified, no '.editorconfig' + * is loaded at all. + */ + virtualPath: Path? = null, + ): Code = + Code( + content = content, + filePath = virtualPath, + fileName = null, + script = virtualPath?.pathString.orEmpty().endsWith(".kts", ignoreCase = true), + isStdIn = true, + ) + /** * Create [Code] by reading the snippet from 'stdin'. No '.editorconfig' are taken into account. The '.editorconfig' files on the * filesystem are ignored as the snippet is not associated with a file path. Use [Code.fromFile] for scanning a file while at the