Skip to content

Commit

Permalink
Change codebase formatting from ktlint to ktfmt
Browse files Browse the repository at this point in the history
(But not the comments. There isn't a way to turn that
off with ktfmt, so manually run kdocformat afterwards,
and remove any damaged comments such as extra ``` due
to facebook/ktfmt#235

I couldn't find a way to have the ktfmt plugin applied
(so I can run the format task) without it also on its
own adding itself into the check task, which means
check now fails (because the sources don't match ktfmts
attempt to format comments too), so for now I've
commented out the tasks from the build file.

To format, put these back, then run
   ./gradlew ktfmtFormat
  • Loading branch information
pulling-star committed Jul 8, 2022
1 parent 753eb61 commit d5db1f3
Show file tree
Hide file tree
Showing 26 changed files with 747 additions and 650 deletions.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ buildscript {
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.7.0'
//id 'com.ncorti.ktfmt.gradle' version '0.8.0'
}

group 'kdocformatter'
Expand Down Expand Up @@ -54,6 +55,10 @@ task format(type: JavaExec, group: "verification") {
args "-F", "--disabled_rules=import-ordering", "**/*.kt"
}

//ktfmt {
// kotlinLangStyle()
//}

task lint {
dependsOn ':library:lint', ':cli:lint', ':ide-plugin:lint'
}
Expand Down
5 changes: 5 additions & 0 deletions cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
id 'org.jetbrains.kotlin.jvm'
id 'application'
id 'com.android.lint'
//id 'com.ncorti.ktfmt.gradle'
}

group = "kdoc-formatter"
Expand Down Expand Up @@ -31,6 +32,10 @@ lint {
textReport true
}

//ktfmt {
// kotlinLangStyle()
//}

test {
useJUnitPlatform()
}
Expand Down
17 changes: 12 additions & 5 deletions cli/src/main/kotlin/kdocformatter/cli/GitRangeFilter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import java.nio.file.Files
*/
class GitRangeFilter private constructor(rangeMap: RangeMap) : LineRangeFilter(rangeMap) {
companion object {
fun create(gitPath: String, fileInRepository: File, staged: Boolean = false): GitRangeFilter? {
fun create(
gitPath: String,
fileInRepository: File,
staged: Boolean = false
): GitRangeFilter? {
val git = findGit(gitPath) ?: return null
val args = mutableListOf<String>()
val gitRepo = findGitRepo(fileInRepository) ?: return null
Expand Down Expand Up @@ -56,10 +60,14 @@ class GitRangeFilter private constructor(rangeMap: RangeMap) : LineRangeFilter(r
// KDocFileFormattingOptions.parse (which is necessary such that we don't
// accidentally handle relative paths like "./" etc as "foo/./bar" which
// isn't treated as equal to "foo/bar").
currentPath = (if (root != null) File(root, relative) else File(relative)).canonicalFile
currentPath =
(if (root != null) File(root, relative) else File(relative)).canonicalFile
} else if (line.startsWith("@@ ")) {
//noinspection FileComparisons
if (currentPath === root || currentPath == null || !currentPath.path.endsWith(".kt")) {
if (currentPath === root ||
currentPath == null ||
!currentPath.path.endsWith(".kt")
) {
continue
}
val rangeStart = line.indexOf('+') + 1
Expand Down Expand Up @@ -146,8 +154,7 @@ class GitRangeFilter private constructor(rangeMap: RangeMap) : LineRangeFilter(r
}
}

val git = findOnPath("git")
?: findOnPath("git.exe")
val git = findOnPath("git") ?: findOnPath("git.exe")
if (git != null) {
val gitFile = File(git)
if (!gitFile.canExecute()) {
Expand Down
7 changes: 4 additions & 3 deletions cli/src/main/kotlin/kdocformatter/cli/KDocFileFormatter.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package kdocformatter.cli

import java.io.File
import kdocformatter.EditorConfigs
import kdocformatter.KDocFormatter
import java.io.File

/**
* This class attempts to iterate over an entire Kotlin source file
Expand Down Expand Up @@ -31,7 +31,7 @@ class KDocFileFormatter(private val options: KDocFileFormattingOptions) {
}

return if (file.path.endsWith(".kt") && options.filter.includes(file) ||
options.includeMd && file.path.endsWith(".md") && options.filter.includes(file)
options.includeMd && file.path.endsWith(".md") && options.filter.includes(file)
) {
val original = file.readText()
val reformatted = reformatFile(file, original)
Expand Down Expand Up @@ -59,7 +59,8 @@ class KDocFileFormatter(private val options: KDocFileFormattingOptions) {

val sb = StringBuilder()
val tokens = tokenizeKotlin(source)
val formattingOptions = file?.let { EditorConfigs.getOptions(it) } ?: options.formattingOptions
val formattingOptions =
file?.let { EditorConfigs.getOptions(it) } ?: options.formattingOptions
val formatter = KDocFormatter(formattingOptions)
val filter = options.filter
var nextIsComment = false
Expand Down
38 changes: 24 additions & 14 deletions cli/src/main/kotlin/kdocformatter/cli/KDocFileFormattingOptions.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package kdocformatter.cli

import java.io.File
import kdocformatter.KDocFormattingOptions
import kdocformatter.Version
import java.io.File

/**
* Options for configuring whole files or directories. The
Expand Down Expand Up @@ -39,22 +39,28 @@ class KDocFileFormattingOptions {
arg == "--max-line-width" || arg == "--line-width" || arg == "--right-margin" ->
options.formattingOptions.maxLineWidth = parseInt(args[i++])
arg.startsWith("--max-line-width=") ->
options.formattingOptions.maxLineWidth = parseInt(arg.substring("--max-line-width=".length))
options.formattingOptions.maxLineWidth =
parseInt(arg.substring("--max-line-width=".length))
arg == "--max-comment-width" ->
options.formattingOptions.maxCommentWidth = parseInt(args[i++])
arg.startsWith("--max-comment-width=") ->
options.formattingOptions.maxCommentWidth = parseInt(arg.substring("--max-comment-width=".length))
arg == "--hanging-indent" -> options.formattingOptions.hangingIndent = parseInt(args[i++])
options.formattingOptions.maxCommentWidth =
parseInt(arg.substring("--max-comment-width=".length))
arg == "--hanging-indent" ->
options.formattingOptions.hangingIndent = parseInt(args[i++])
arg.startsWith("--hanging-indent=") ->
options.formattingOptions.hangingIndent = parseInt(arg.substring("--hanging-indent=".length))
options.formattingOptions.hangingIndent =
parseInt(arg.substring("--hanging-indent=".length))
arg == "--convert-markup" -> options.formattingOptions.convertMarkup = true
arg == "--add-punctuation" -> options.formattingOptions.addPunctuation = true
arg.startsWith("--single-line-comments=collapse") ->
options.formattingOptions.collapseSingleLine = true
arg.startsWith("--single-line-comments=expand") ->
options.formattingOptions.collapseSingleLine = false
arg.startsWith("--single-line-comments=") ->
error("Only `collapse` and `expand` are supported for --single-line-comments")
error(
"Only `collapse` and `expand` are supported for --single-line-comments"
)
arg == "--overlaps-git-changes=HEAD" -> options.gitHead = true
arg == "--overlaps-git-changes=staged" -> options.gitStaged = true
arg.startsWith("--overlaps-git-changes=") ->
Expand All @@ -64,7 +70,8 @@ class KDocFileFormattingOptions {
arg == "--dry-run" || arg == "-n" -> options.dryRun = true
arg == "--quiet" || arg == "-q" -> options.quiet = true
arg == "--git-path" -> options.gitPath = args[i++]
arg.startsWith("--git-path=") -> options.gitPath = arg.substring("--git-path=".length)
arg.startsWith("--git-path=") ->
options.gitPath = arg.substring("--git-path=".length)
arg == "--include-md-files" -> options.includeMd = true
arg == "--greedy" -> options.formattingOptions.optimal = false
else -> {
Expand Down Expand Up @@ -99,18 +106,21 @@ class KDocFileFormattingOptions {
if (options.gitHead) {
GitRangeFilter.create(options.gitPath, files.first(), false)?.let {
filters.add(it)
} ?: error("Could not create git range filter for the staged files")
}
?: error("Could not create git range filter for the staged files")
}
if (options.gitStaged) {
GitRangeFilter.create(options.gitPath, files.first(), true)?.let {
filters.add(it)
} ?: error("Could not create git range filter for the files in HEAD")
}
options.filter = if (filters.size == 2) {
UnionFilter(filters)
} else {
filters.first()
}
?: error("Could not create git range filter for the files in HEAD")
}
options.filter =
if (filters.size == 2) {
UnionFilter(filters)
} else {
filters.first()
}
} else if (rangeLines.isNotEmpty()) {
if (files.size != 1 || !files[0].isFile) {
error("The --lines option can only be used with a single file")
Expand Down
16 changes: 8 additions & 8 deletions cli/src/main/kotlin/kdocformatter/cli/LineRangeFilter.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package kdocformatter.cli

import kdocformatter.getLineNumber
import java.io.File
import kdocformatter.getLineNumber

open class LineRangeFilter protected constructor(private val rangeMap: RangeMap) : RangeFilter() {
var valid: Boolean = false
Expand Down Expand Up @@ -61,17 +61,17 @@ open class LineRangeFilter protected constructor(private val rangeMap: RangeMap)
for (s in rangeString.split(",")) {
val endSeparator = s.indexOf(':')
if (endSeparator == -1) {
val line = s.toIntOrNull()
?: error("Line $s is not a number")
val line = s.toIntOrNull() ?: error("Line $s is not a number")
rangeMap.addRange(file, line, line + 1)
} else {
val startString = s.substring(0, endSeparator)
val endString = s.substring(endSeparator + 1)
val start = startString.toIntOrNull()
?: error("Line $startString is not a number")
val end = endString.toIntOrNull()
?: error("Line $endString is not a number")
// ranges operate with endLine is exclusive, but command line option is inclusive
val start =
startString.toIntOrNull() ?: error("Line $startString is not a number")
val end =
endString.toIntOrNull() ?: error("Line $endString is not a number")
// ranges operate with endLine is exclusive, but command line option is
// inclusive
rangeMap.addRange(file, start, end + 1)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package kdocformatter.cli

import java.io.File
import kdocformatter.KDocFormatter
import kdocformatter.KDocFormattingOptions
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import java.io.File

// The formatter is mostly tested by KDocFormatterTest. This tests the part
// where we (1) find the comments and format them, and (2) process the code
Expand Down
5 changes: 5 additions & 0 deletions gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ plugins {
id 'java-gradle-plugin'
id 'maven-publish'
id 'com.gradle.plugin-publish' version '0.9.10'
//id 'com.ncorti.ktfmt.gradle'
}

// https://issues.sonatype.org/browse/OSSRH-63191
Expand Down Expand Up @@ -59,6 +60,10 @@ compileKotlin {
}
}

//ktfmt {
// kotlinLangStyle()
//}

gradlePlugin {
plugins {
kdocformatter {
Expand Down
5 changes: 5 additions & 0 deletions ide-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ plugins {
id("org.jetbrains.changelog") version "1.3.1"
id("org.jetbrains.qodana") version "0.1.13"
id("com.android.lint")
//id("com.ncorti.ktfmt.gradle")
}

val pluginVersion: String = Properties().apply {
Expand Down Expand Up @@ -110,6 +111,10 @@ lint {
baseline = file("lint-baseline.xml")
}

//ktfmt {
// kotlinLangStyle()
//}

dependencies {
implementation(project(":library"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,53 @@ import com.intellij.ui.layout.panel
import org.jetbrains.annotations.Nls

class KDocOptionsConfigurable : SearchableConfigurable, Configurable.NoScroll {
@Nls
override fun getDisplayName() = "KDoc Formatting"

@Suppress("SpellCheckingInspection")
override fun getId() = "kdocformatter.options"
private val formatProcessorCheckBox = JBCheckBox("Participate in IDE formatting operations")
private val alternateCheckBox = JBCheckBox("Alternate line breaking algorithms when invoked repeatedly")
private val collapseLinesCheckBox = JBCheckBox("Collapse short comments that fit on a single line")
private val convertMarkupCheckBox = JBCheckBox("Convert markup like <b>bold</b> into **bold**")
private val addPunctuationCheckBox = JBCheckBox("Add missing punctuation")
private val lineCommentsCheckBox = JBCheckBox("Allow formatting line comments interactively")

private val state = KDocPluginOptions.instance.globalState

override fun createComponent() = panel {
row { formatProcessorCheckBox() }
row { alternateCheckBox() }
row { collapseLinesCheckBox() }
row { convertMarkupCheckBox() }
row { addPunctuationCheckBox() }
row { lineCommentsCheckBox() }
}

override fun isModified() =
alternateCheckBox.isSelected != state.alternateActions ||
collapseLinesCheckBox.isSelected != state.collapseSingleLines ||
convertMarkupCheckBox.isSelected != state.convertMarkup ||
lineCommentsCheckBox.isSelected != state.lineComments ||
addPunctuationCheckBox.isSelected != state.addPunctuation ||
formatProcessorCheckBox.isSelected != state.formatProcessor

@Throws(ConfigurationException::class)
override fun apply() {
state.alternateActions = alternateCheckBox.isSelected
state.collapseSingleLines = collapseLinesCheckBox.isSelected
state.convertMarkup = convertMarkupCheckBox.isSelected
state.lineComments = lineCommentsCheckBox.isSelected
state.addPunctuation = addPunctuationCheckBox.isSelected
state.formatProcessor = formatProcessorCheckBox.isSelected
}

override fun reset() {
alternateCheckBox.isSelected = state.alternateActions
collapseLinesCheckBox.isSelected = state.collapseSingleLines
convertMarkupCheckBox.isSelected = state.convertMarkup
lineCommentsCheckBox.isSelected = state.lineComments
addPunctuationCheckBox.isSelected = state.addPunctuation
formatProcessorCheckBox.isSelected = state.formatProcessor
}
@Nls override fun getDisplayName() = "KDoc Formatting"

@Suppress("SpellCheckingInspection") override fun getId() = "kdocformatter.options"
private val formatProcessorCheckBox = JBCheckBox("Participate in IDE formatting operations")
private val alternateCheckBox =
JBCheckBox("Alternate line breaking algorithms when invoked repeatedly")
private val collapseLinesCheckBox =
JBCheckBox("Collapse short comments that fit on a single line")
private val convertMarkupCheckBox = JBCheckBox("Convert markup like <b>bold</b> into **bold**")
private val addPunctuationCheckBox = JBCheckBox("Add missing punctuation")
private val lineCommentsCheckBox = JBCheckBox("Allow formatting line comments interactively")

private val state = KDocPluginOptions.instance.globalState

override fun createComponent() = panel {
row { formatProcessorCheckBox() }
row { alternateCheckBox() }
row { collapseLinesCheckBox() }
row { convertMarkupCheckBox() }
row { addPunctuationCheckBox() }
row { lineCommentsCheckBox() }
}

override fun isModified() =
alternateCheckBox.isSelected != state.alternateActions ||
collapseLinesCheckBox.isSelected != state.collapseSingleLines ||
convertMarkupCheckBox.isSelected != state.convertMarkup ||
lineCommentsCheckBox.isSelected != state.lineComments ||
addPunctuationCheckBox.isSelected != state.addPunctuation ||
formatProcessorCheckBox.isSelected != state.formatProcessor

@Throws(ConfigurationException::class)
override fun apply() {
state.alternateActions = alternateCheckBox.isSelected
state.collapseSingleLines = collapseLinesCheckBox.isSelected
state.convertMarkup = convertMarkupCheckBox.isSelected
state.lineComments = lineCommentsCheckBox.isSelected
state.addPunctuation = addPunctuationCheckBox.isSelected
state.formatProcessor = formatProcessorCheckBox.isSelected
}

override fun reset() {
alternateCheckBox.isSelected = state.alternateActions
collapseLinesCheckBox.isSelected = state.collapseSingleLines
convertMarkupCheckBox.isSelected = state.convertMarkup
lineCommentsCheckBox.isSelected = state.lineComments
addPunctuationCheckBox.isSelected = state.addPunctuation
formatProcessorCheckBox.isSelected = state.formatProcessor
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.intellij.openapi.options.Configurable
import com.intellij.openapi.options.ConfigurableProvider

class KDocOptionsConfigurableProvider : ConfigurableProvider() {
override fun createConfigurable(): Configurable {
return KDocOptionsConfigurable()
}
override fun createConfigurable(): Configurable {
return KDocOptionsConfigurable()
}
}
Loading

0 comments on commit d5db1f3

Please sign in to comment.