Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper function 'fromSnippetWithPath' to create a Code instance #2359

Merged
merged 1 commit into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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