Skip to content

Commit

Permalink
feat: add support for Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnychevalier committed Aug 2, 2024
1 parent c3f5589 commit a497750
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 7 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ lang-all = [
"lang-rust",
"lang-toml",
"lang-yaml",

"lang-kotlin",
]
lang-c = ["dep:tree-sitter-c"]
lang-cpp = ["dep:tree-sitter-cpp"]
Expand All @@ -32,6 +32,7 @@ lang-python = ["dep:tree-sitter-python"]
lang-rust = ["dep:tree-sitter-rust"]
lang-toml = ["dep:tree-sitter-toml-ng"]
lang-yaml = ["dep:tree-sitter-yaml"]
lang-kotlin = ["dep:tree-sitter-kotlin"]

[dependencies]
anyhow = "1.0.86"
Expand All @@ -49,6 +50,7 @@ tree-sitter-c = { version = "0.21.4", optional = true }
tree-sitter-cpp = { version = "0.22.2", optional = true }
tree-sitter-go = { version = "0.21.0", optional = true }
tree-sitter-json = { version = "0.21.0", optional = true }
tree-sitter-kotlin = { version = "0.3.7", optional = true }
tree-sitter-md = { version = "0.2.3", optional = true }
tree-sitter-python = { version = "0.21.0", optional = true }
tree-sitter-rust = { version = "0.21.2", optional = true }
Expand Down
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@ typographical errors, and other mistakes that are not covered by tools
like [`typos`][typos] to ensure that your source code is
free from typographical heresy.

**This tool is still experimental.**

The *goal* is to make the number of false positives low so that
it can be integrated into a CI, like [`typos`][typos].

## Rules

The tool only has one rule at the moment:
`typope` has only one rule at the moment:

- [No space before a punctuation mark](./src/lint/punctuation.rs)

Expand All @@ -24,6 +19,7 @@ The tool only has one rule at the moment:

- Rust
- Go
- Kotlin
- Python
- C++
- C
Expand Down
3 changes: 3 additions & 0 deletions src/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ mod cpp;
mod go;
#[cfg(feature = "lang-json")]
mod json;
#[cfg(feature = "lang-kotlin")]
mod kotlin;
#[cfg(feature = "lang-markdown")]
mod markdown;
#[cfg(feature = "lang-python")]
Expand Down Expand Up @@ -52,6 +54,7 @@ impl Mapping {
lang!(rust, "lang-rust");
lang!(c, "lang-c");
lang!(cpp, "lang-cpp");
lang!(kotlin, "lang-kotlin");
lang!(go, "lang-go");
lang!(python, "lang-python");
lang!(toml, "lang-toml");
Expand Down
80 changes: 80 additions & 0 deletions src/lang/kotlin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use super::{Language, Mode};

impl Language {
/// Creates a language parser for Kotlin
pub fn kotlin() -> Self {
Self {
name: "kotlin",
language: tree_sitter_kotlin::language(),
extensions: &["kt"],
parser: Mode::Generic {
tree_sitter_types: &["string_content"],
},
}
}
}

#[cfg(test)]
mod tests {
use std::ffi::OsStr;

use crate::lang::LintableString;
use crate::SharedSource;

use super::Language;

#[test]
fn exists_in_iter() {
assert!(Language::iter().any(|lang| lang.name() == "kotlin"));
}

#[test]
fn find_from_extensions() {
for ext in Language::kotlin().extensions() {
assert_eq!(
"kotlin",
Language::from_extension(OsStr::new(ext)).unwrap().name()
);
}
}

#[test]
fn lintable_strings() {
let kotlin = r#"
package org.kotlinlang.play
fun f(): String {
return "abcdef"
}
fun main() {
var s = "foobar"
println("Hello, World! ($s) ghijkl")
}
"#;
let kotlin = SharedSource::new("file.kt", kotlin.as_bytes().to_vec());
let mut parsed = Language::kotlin().parse(&kotlin).unwrap();
let strings = parsed.strings(kotlin.as_ref()).collect::<Vec<_>>();
assert_eq!(
strings,
[
LintableString {
offset: 60,
value: "abcdef".into()
},
LintableString {
offset: 97,
value: "foobar".into()
},
LintableString {
offset: 118,
value: "Hello, World! (".into()
},
LintableString {
offset: 135,
value: ") ghijkl".into()
},
]
);
}
}

0 comments on commit a497750

Please sign in to comment.