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

Fix #3394: Ktlint Custom RuleSet #5558

Closed
wants to merge 9 commits into from
Closed
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
6 changes: 3 additions & 3 deletions scripts/ktlint_lint_check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ else
jar_file_path="$github_actions_path/oppia-android-tools/ktlint"
fi

java -jar $jar_file_path --android app/src/**/*.kt data/src/**/*.kt domain/src/**/*.kt testing/src/**/*.kt utility/src/**/*.kt scripts/src/**/*.kt instrumentation/src/**/*.kt
java -jar $jar_file_path -R ../oppia-android-tools/custom-ktlint-rules.jar --android app/src/**/*.kt data/src/**/*.kt domain/src/**/*.kt testing/src/**/*.kt utility/src/**/*.kt scripts/src/**/*.kt instrumentation/src/**/*.kt

status=$?

Expand All @@ -27,10 +27,10 @@ else
echo "********************************"
echo_error "Ktlint issue found."
echo "Please fix the above issues.
You can also use the java -jar $jar_file_path -F --android domain/src/**/*.kt utility/src/**/*.kt data/src/**/*.kt app/src/**/*.kt testing/src/**/*.kt scripts/src/**/*.kt instrumentation/src/**/*.kt
You can also use the java -jar $jar_file_path -F -R ../oppia-android-tools/custom-ktlint-rules.jar --android domain/src/**/*.kt utility/src/**/*.kt data/src/**/*.kt app/src/**/*.kt testing/src/**/*.kt scripts/src/**/*.kt instrumentation/src/**/*.kt
command to fix the most common issues."
echo_warning "Please note, there might be a possibility where the above command will not fix the issue.
In that case, you will have to fix it yourself."
echo "********************************"
exit 1
fi
fi
21 changes: 20 additions & 1 deletion utility/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ android {
}
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
Expand Down Expand Up @@ -98,6 +97,7 @@ dependencies {
'jakarta.xml.bind:jakarta.xml.bind-api:2.3.2',
'javax.annotation:javax.annotation-api:1.3.2',
'org.glassfish.jaxb:jaxb-runtime:2.3.2',
'com.pinterest.ktlint:ktlint-core:0.37.1',
)
testImplementation(
'androidx.test.ext:junit:1.1.1',
Expand Down Expand Up @@ -125,6 +125,25 @@ dependencies {
// controlled more directly than in Gradle.
implementation project(':model')
}
// Custom task to create a JAR file for ktlint rules.
tasks.register('customRulesJar', Jar) {
archiveFileName = 'custom-ktlint-rules.jar'
from("$buildDir/tmp/kotlin-classes/debug") {
include '**/*.class'
}
from('src/main/res') {
include 'META-INF/services/**'
}
manifest {
attributes(
'Implementation-Title': 'Oppia Android Custom KtLint Rules',
'Implementation-Version': '1.0.0'
)
}
// Ensure that Kotlin and Java classes are compiled before creating the JAR.
dependsOn 'compileDebugKotlin', 'compileDebugJavaWithJavac'
destinationDirectory.set(file("../../oppia-android-tools"))
}
// The GeneratedMessageLite implementations of protobufs are depending on protobuf-java
// instead of protobuf-lite after Android Studio 3.5,
// The below command stops that from happening: https://github.com/google/tink/issues/282
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.oppia.android.util.ktlint

import com.pinterest.ktlint.core.RuleSet
import com.pinterest.ktlint.core.RuleSetProvider
import org.oppia.android.util.ktlint.rules.KDocFormatRule

class CustomRuleSetProvider : RuleSetProvider {

override fun get(): RuleSet = RuleSet("custom", KDocFormatRule())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.oppia.android.util.ktlint.rules

import com.pinterest.ktlint.core.Rule
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.kdoc.psi.impl.KDocImpl
import org.jetbrains.kotlin.psi.KtFile

class KDocFormatRule : Rule("kdoc-format-closing") {
override fun visit(
node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
) {
if (node.psi is KtFile) {
val kdocs = node.psi.children.filterIsInstance<KDocImpl>()
kdocs.forEach { kdoc ->
val lines = kdoc.text.lines()
if (lines.last().contains("* */")) {
emit(
kdoc.startOffset + kdoc.text.lastIndexOf("* *"),
"KDoc closing should be ' */' not '* */'",
true,
)
if (autoCorrect) {
val kdocNode = kdoc.node
val oldText = kdocNode.text
val newText = oldText.replace("* */", " */")
val correctedNode = LeafPsiElement(kdocNode.elementType, newText)
kdocNode.treeParent.replaceChild(kdocNode, correctedNode)
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.oppia.android.util.ktlint.CustomRuleSetProvider
Loading