Skip to content

Commit

Permalink
Add helper function 'fromSnippetWithPath' to create a Code instance t…
Browse files Browse the repository at this point in the history
…hat is to be linted/formatted. (#2359)

Closes #2340
  • Loading branch information
paul-dingemans authored Nov 19, 2023
1 parent 44e7354 commit 03f044a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ktlint-rule-engine/api/ktlint-rule-engine.api
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 03f044a

Please sign in to comment.