Skip to content
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
54 changes: 54 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

resolver = "2"

members = ["crates/plotnik-cli", "crates/plotnik-lib", "crates/plotnik-langs"]
members = ["crates/plotnik-cli", "crates/plotnik-lib", "crates/plotnik-langs", "crates/plotnik-macros"]
10 changes: 5 additions & 5 deletions crates/plotnik-cli/src/commands/debug/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ pub fn resolve_lang(
lang: &Option<String>,
_source_text: &Option<String>,
source_file: &Option<PathBuf>,
) -> Lang {
) -> &'static Lang {
if let Some(name) = lang {
return Lang::from_name(name).unwrap_or_else(|| {
return plotnik_langs::from_name(name).unwrap_or_else(|| {
eprintln!("error: unknown language: {}", name);
std::process::exit(1);
});
Expand All @@ -37,7 +37,7 @@ pub fn resolve_lang(
&& path.as_os_str() != "-"
&& let Some(ext) = path.extension().and_then(|e| e.to_str())
{
return Lang::from_extension(ext).unwrap_or_else(|| {
return plotnik_langs::from_ext(ext).unwrap_or_else(|| {
eprintln!(
"error: cannot infer language from extension '.{}', use --lang",
ext
Expand All @@ -50,10 +50,10 @@ pub fn resolve_lang(
std::process::exit(1);
}

pub fn parse_tree(source: &str, lang: Lang) -> tree_sitter::Tree {
pub fn parse_tree(source: &str, lang: &Lang) -> tree_sitter::Tree {
let mut parser = tree_sitter::Parser::new();
parser
.set_language(&lang.language())
.set_language(&lang.ts_lang)
.expect("failed to set language");
parser.parse(source, None).expect("failed to parse source")
}
Expand Down
97 changes: 58 additions & 39 deletions crates/plotnik-cli/src/commands/langs.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
use plotnik_langs::Lang;

pub fn run() {
let langs = Lang::all();
let langs = plotnik_langs::all();
println!("Supported languages ({}):", langs.len());
for lang in langs {
println!(" {}", lang.name());
println!(" {}", lang.name);
}
}

#[cfg(test)]
mod tests {
use plotnik_langs::Lang;

fn smoke_test(lang: Lang, source: &str, expected_root: &str) {
fn smoke_test(lang: &plotnik_langs::Lang, source: &str, expected_root: &str) {
let mut parser = tree_sitter::Parser::new();
parser.set_language(&lang.language()).unwrap();
parser.set_language(&lang.ts_lang).unwrap();
let tree = parser.parse(source, None).unwrap();
let root = tree.root_node();
assert_eq!(root.kind(), expected_root);
Expand All @@ -24,56 +20,68 @@ mod tests {
#[test]
#[cfg(feature = "bash")]
fn smoke_parse_bash() {
smoke_test(Lang::Bash, "echo hello", "program");
smoke_test(plotnik_langs::bash(), "echo hello", "program");
}

#[test]
#[cfg(feature = "c")]
fn smoke_parse_c() {
smoke_test(Lang::C, "int main() { return 0; }", "translation_unit");
smoke_test(
plotnik_langs::c(),
"int main() { return 0; }",
"translation_unit",
);
}

#[test]
#[cfg(feature = "cpp")]
fn smoke_parse_cpp() {
smoke_test(Lang::Cpp, "int main() { return 0; }", "translation_unit");
smoke_test(
plotnik_langs::cpp(),
"int main() { return 0; }",
"translation_unit",
);
}

#[test]
#[cfg(feature = "csharp")]
fn smoke_parse_csharp() {
smoke_test(Lang::CSharp, "class Foo { }", "compilation_unit");
smoke_test(plotnik_langs::csharp(), "class Foo { }", "compilation_unit");
}

#[test]
#[cfg(feature = "css")]
fn smoke_parse_css() {
smoke_test(Lang::Css, "body { color: red; }", "stylesheet");
smoke_test(plotnik_langs::css(), "body { color: red; }", "stylesheet");
}

#[test]
#[cfg(feature = "elixir")]
fn smoke_parse_elixir() {
smoke_test(Lang::Elixir, "defmodule Foo do end", "source");
smoke_test(plotnik_langs::elixir(), "defmodule Foo do end", "source");
}

#[test]
#[cfg(feature = "go")]
fn smoke_parse_go() {
smoke_test(Lang::Go, "package main", "source_file");
smoke_test(plotnik_langs::go(), "package main", "source_file");
}

#[test]
#[cfg(feature = "haskell")]
fn smoke_parse_haskell() {
smoke_test(Lang::Haskell, "main = putStrLn \"hello\"", "haskell");
smoke_test(
plotnik_langs::haskell(),
"main = putStrLn \"hello\"",
"haskell",
);
}

#[test]
#[cfg(feature = "hcl")]
fn smoke_parse_hcl() {
smoke_test(
Lang::Hcl,
plotnik_langs::hcl(),
"resource \"aws_instance\" \"x\" {}",
"config_file",
);
Expand All @@ -82,20 +90,20 @@ mod tests {
#[test]
#[cfg(feature = "html")]
fn smoke_parse_html() {
smoke_test(Lang::Html, "<html></html>", "document");
smoke_test(plotnik_langs::html(), "<html></html>", "document");
}

#[test]
#[cfg(feature = "java")]
fn smoke_parse_java() {
smoke_test(Lang::Java, "class Foo {}", "program");
smoke_test(plotnik_langs::java(), "class Foo {}", "program");
}

#[test]
#[cfg(feature = "javascript")]
fn smoke_parse_javascript() {
smoke_test(
Lang::JavaScript,
plotnik_langs::javascript(),
"function hello() { return 42; }",
"program",
);
Expand All @@ -104,99 +112,110 @@ mod tests {
#[test]
#[cfg(feature = "json")]
fn smoke_parse_json() {
smoke_test(Lang::Json, r#"{"key": "value"}"#, "document");
smoke_test(plotnik_langs::json(), r#"{"key": "value"}"#, "document");
}

#[test]
#[cfg(feature = "kotlin")]
fn smoke_parse_kotlin() {
smoke_test(Lang::Kotlin, "fun main() {}", "source_file");
smoke_test(plotnik_langs::kotlin(), "fun main() {}", "source_file");
}

#[test]
#[cfg(feature = "lua")]
fn smoke_parse_lua() {
smoke_test(Lang::Lua, "print('hello')", "chunk");
smoke_test(plotnik_langs::lua(), "print('hello')", "chunk");
}

#[test]
#[cfg(feature = "nix")]
fn smoke_parse_nix() {
smoke_test(Lang::Nix, "{ x = 1; }", "source_code");
smoke_test(plotnik_langs::nix(), "{ x = 1; }", "source_code");
}

#[test]
#[cfg(feature = "php")]
fn smoke_parse_php() {
smoke_test(Lang::Php, "<?php echo 1;", "program");
smoke_test(plotnik_langs::php(), "<?php echo 1;", "program");
}

#[test]
#[cfg(feature = "python")]
fn smoke_parse_python() {
smoke_test(Lang::Python, "def hello():\n return 42", "module");
smoke_test(
plotnik_langs::python(),
"def hello():\n return 42",
"module",
);
}

#[test]
#[cfg(feature = "ruby")]
fn smoke_parse_ruby() {
smoke_test(Lang::Ruby, "def hello; end", "program");
smoke_test(plotnik_langs::ruby(), "def hello; end", "program");
}

#[test]
#[cfg(feature = "rust")]
fn smoke_parse_rust() {
smoke_test(Lang::Rust, "fn main() {}", "source_file");
smoke_test(plotnik_langs::rust(), "fn main() {}", "source_file");
}

#[test]
#[cfg(feature = "scala")]
fn smoke_parse_scala() {
smoke_test(Lang::Scala, "object Main {}", "compilation_unit");
smoke_test(plotnik_langs::scala(), "object Main {}", "compilation_unit");
}

#[test]
#[cfg(feature = "solidity")]
fn smoke_parse_solidity() {
smoke_test(Lang::Solidity, "contract Foo {}", "source_file");
smoke_test(plotnik_langs::solidity(), "contract Foo {}", "source_file");
}

#[test]
#[cfg(feature = "swift")]
fn smoke_parse_swift() {
smoke_test(Lang::Swift, "func main() {}", "source_file");
smoke_test(plotnik_langs::swift(), "func main() {}", "source_file");
}

#[test]
#[cfg(feature = "typescript")]
fn smoke_parse_typescript() {
smoke_test(Lang::TypeScript, "const x: number = 42;", "program");
smoke_test(
plotnik_langs::typescript(),
"const x: number = 42;",
"program",
);
}

#[test]
#[cfg(feature = "typescript")]
fn smoke_parse_tsx() {
smoke_test(Lang::Tsx, "const x = <div />;", "program");
smoke_test(plotnik_langs::tsx(), "const x = <div />;", "program");
}

#[test]
#[cfg(feature = "yaml")]
fn smoke_parse_yaml() {
smoke_test(Lang::Yaml, "key: value", "stream");
smoke_test(plotnik_langs::yaml(), "key: value", "stream");
}

#[test]
#[cfg(feature = "javascript")]
fn lang_from_name() {
assert_eq!(Lang::from_name("js"), Some(Lang::JavaScript));
assert_eq!(Lang::from_name("JavaScript"), Some(Lang::JavaScript));
assert_eq!(Lang::from_name("unknown"), None);
assert_eq!(plotnik_langs::from_name("js").unwrap().name, "javascript");
assert_eq!(
plotnik_langs::from_name("JavaScript").unwrap().name,
"javascript"
);
assert!(plotnik_langs::from_name("unknown").is_none());
}

#[test]
#[cfg(feature = "javascript")]
fn lang_from_extension() {
assert_eq!(Lang::from_extension("js"), Some(Lang::JavaScript));
assert_eq!(Lang::from_extension("mjs"), Some(Lang::JavaScript));
assert_eq!(plotnik_langs::from_ext("js").unwrap().name, "javascript");
assert_eq!(plotnik_langs::from_ext("mjs").unwrap().name, "javascript");
}
}
Loading