on master help sign in
Add helix-syntax as a wrapper around tree-sitter parsers.
7eac12aBlaž Hrastnik
.gitmodulesnew file+80
@@ -0,0 +1,80 @@
1+[submodule "helix-syntax/languages/tree-sitter-cpp"]
2+ path = helix-syntax/languages/tree-sitter-cpp
3+ url = https://github.com/tree-sitter/tree-sitter-cpp
4+ shallow = true
5+[submodule "helix-syntax/languages/tree-sitter-javascript"]
6+ path = helix-syntax/languages/tree-sitter-javascript
7+ url = https://github.com/tree-sitter/tree-sitter-javascript
8+ shallow = true
9+[submodule "helix-syntax/languages/tree-sitter-julia"]
10+ path = helix-syntax/languages/tree-sitter-julia
11+ url = https://github.com/tree-sitter/tree-sitter-julia
12+ shallow = true
13+[submodule "helix-syntax/languages/tree-sitter-python"]
14+ path = helix-syntax/languages/tree-sitter-python
15+ url = https://github.com/tree-sitter/tree-sitter-python
16+ shallow = true
17+[submodule "helix-syntax/languages/tree-sitter-typescript"]
18+ path = helix-syntax/languages/tree-sitter-typescript
19+ url = https://github.com/tree-sitter/tree-sitter-typescript
20+ shallow = true
21+[submodule "helix-syntax/languages/tree-sitter-agda"]
22+ path = helix-syntax/languages/tree-sitter-agda
23+ url = https://github.com/tree-sitter/tree-sitter-agda
24+ shallow = true
25+[submodule "helix-syntax/languages/tree-sitter-go"]
26+ path = helix-syntax/languages/tree-sitter-go
27+ url = https://github.com/tree-sitter/tree-sitter-go
28+ shallow = true
29+[submodule "helix-syntax/languages/tree-sitter-ruby"]
30+ path = helix-syntax/languages/tree-sitter-ruby
31+ url = https://github.com/tree-sitter/tree-sitter-ruby
32+ shallow = true
33+[submodule "helix-syntax/languages/tree-sitter-java"]
34+ path = helix-syntax/languages/tree-sitter-java
35+ url = https://github.com/tree-sitter/tree-sitter-java
36+ shallow = true
37+[submodule "helix-syntax/languages/tree-sitter-php"]
38+ path = helix-syntax/languages/tree-sitter-php
39+ url = https://github.com/tree-sitter/tree-sitter-php
40+ shallow = true
41+[submodule "helix-syntax/languages/tree-sitter-html"]
42+ path = helix-syntax/languages/tree-sitter-html
43+ url = https://github.com/tree-sitter/tree-sitter-html
44+ shallow = true
45+[submodule "helix-syntax/languages/tree-sitter-scala"]
46+ path = helix-syntax/languages/tree-sitter-scala
47+ url = https://github.com/tree-sitter/tree-sitter-scala
48+ shallow = true
49+[submodule "helix-syntax/languages/tree-sitter-bash"]
50+ path = helix-syntax/languages/tree-sitter-bash
51+ url = https://github.com/tree-sitter/tree-sitter-bash
52+ shallow = true
53+[submodule "helix-syntax/languages/tree-sitter-rust"]
54+ path = helix-syntax/languages/tree-sitter-rust
55+ url = https://github.com/tree-sitter/tree-sitter-rust
56+ shallow = true
57+[submodule "helix-syntax/languages/tree-sitter-json"]
58+ path = helix-syntax/languages/tree-sitter-json
59+ url = https://github.com/tree-sitter/tree-sitter-json
60+ shallow = true
61+[submodule "helix-syntax/languages/tree-sitter-css"]
62+ path = helix-syntax/languages/tree-sitter-css
63+ url = https://github.com/tree-sitter/tree-sitter-css
64+ shallow = true
65+[submodule "helix-syntax/languages/tree-sitter-c-sharp"]
66+ path = helix-syntax/languages/tree-sitter-c-sharp
67+ url = https://github.com/tree-sitter/tree-sitter-c-sharp
68+ shallow = true
69+[submodule "helix-syntax/languages/tree-sitter-c"]
70+ path = helix-syntax/languages/tree-sitter-c
71+ url = https://github.com/tree-sitter/tree-sitter-c
72+ shallow = true
73+[submodule "helix-syntax/languages/tree-sitter-haskell"]
74+ path = helix-syntax/languages/tree-sitter-haskell
75+ url = https://github.com/tree-sitter/tree-sitter-haskell
76+ shallow = true
77+[submodule "helix-syntax/languages/tree-sitter-swift"]
78+ path = helix-syntax/languages/tree-sitter-swift
79+ url = https://github.com/tree-sitter/tree-sitter-swift
80+ shallow = true
Cargo.lock+87
@@ -1,5 +1,14 @@
1# This file is automatically @generated by Cargo.
1# This file is automatically @generated by Cargo.
2# It is not intended for manual editing.
2# It is not intended for manual editing.
3+[[package]]
4+name = "aho-corasick"
5+version = "0.7.13"
6+source = "registry+https://github.com/rust-lang/crates.io-index"
7+checksum = "043164d8ba5c4c3035fec9bbee8647c0261d788f3474306f93bb65901cae0e86"
8+dependencies = [
9+ "memchr",
10+]
11+
3[[package]]
12[[package]]
4name = "anyhow"
13name = "anyhow"
5version = "1.0.32"
14version = "1.0.32"
@@ -223,6 +232,9 @@ name = "cc"
223version = "1.0.59"
232version = "1.0.59"
224source = "registry+https://github.com/rust-lang/crates.io-index"
233source = "registry+https://github.com/rust-lang/crates.io-index"
225checksum = "66120af515773fb005778dc07c261bd201ec8ce50bd6e7144c927753fe013381"
234checksum = "66120af515773fb005778dc07c261bd201ec8ce50bd6e7144c927753fe013381"
235+dependencies = [
236+ "jobserver",
237+]
226
238
227[[package]]
239[[package]]
228name = "cfg-if"
240name = "cfg-if"
@@ -274,6 +286,26 @@ dependencies = [
274 "winapi",
286 "winapi",
275]
287]
276
288
289+[[package]]
290+name = "enum-iterator"
291+version = "0.6.0"
292+source = "registry+https://github.com/rust-lang/crates.io-index"
293+checksum = "c79a6321a1197d7730510c7e3f6cb80432dfefecb32426de8cea0aa19b4bb8d7"
294+dependencies = [
295+ "enum-iterator-derive",
296+]
297+
298+[[package]]
299+name = "enum-iterator-derive"
300+version = "0.6.0"
301+source = "registry+https://github.com/rust-lang/crates.io-index"
302+checksum = "1e94aa31f7c0dc764f57896dc615ddd76fc13b0d5dca7eb6cc5e018a5a09ec06"
303+dependencies = [
304+ "proc-macro2",
305+ "quote",
306+ "syn",
307+]
308+
277[[package]]
309[[package]]
278name = "event-listener"
310name = "event-listener"
279version = "2.4.0"
311version = "2.4.0"
@@ -344,6 +376,15 @@ dependencies = [
344 "unicode-width",
376 "unicode-width",
345]
377]
346
378
379+[[package]]
380+name = "helix-syntax"
381+version = "0.1.0"
382+dependencies = [
383+ "cc",
384+ "enum-iterator",
385+ "tree-sitter",
386+]
387+
347[[package]]
388[[package]]
348name = "helix-term"
389name = "helix-term"
349version = "0.1.0"
390version = "0.1.0"
@@ -366,6 +407,15 @@ dependencies = [
366 "libc",
407 "libc",
367]
408]
368
409
410+[[package]]
411+name = "jobserver"
412+version = "0.1.21"
413+source = "registry+https://github.com/rust-lang/crates.io-index"
414+checksum = "5c71313ebb9439f74b00d9d2dcec36440beaf57a6aa0623068441dd7cd81a7f2"
415+dependencies = [
416+ "libc",
417+]
418+
369[[package]]
419[[package]]
370name = "lazy_static"
420name = "lazy_static"
371version = "1.4.0"
421version = "1.4.0"
@@ -536,6 +586,24 @@ version = "0.1.57"
536source = "registry+https://github.com/rust-lang/crates.io-index"
586source = "registry+https://github.com/rust-lang/crates.io-index"
537checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
587checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
538
588
589+[[package]]
590+name = "regex"
591+version = "1.3.9"
592+source = "registry+https://github.com/rust-lang/crates.io-index"
593+checksum = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6"
594+dependencies = [
595+ "aho-corasick",
596+ "memchr",
597+ "regex-syntax",
598+ "thread_local",
599+]
600+
601+[[package]]
602+name = "regex-syntax"
603+version = "0.6.18"
604+source = "registry+https://github.com/rust-lang/crates.io-index"
605+checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8"
606+
539[[package]]
607[[package]]
540name = "ropey"
608name = "ropey"
541version = "1.2.0"
609version = "1.2.0"
@@ -629,6 +697,25 @@ dependencies = [
629 "utf-8",
697 "utf-8",
630]
698]
631
699
700+[[package]]
701+name = "thread_local"
702+version = "1.0.1"
703+source = "registry+https://github.com/rust-lang/crates.io-index"
704+checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"
705+dependencies = [
706+ "lazy_static",
707+]
708+
709+[[package]]
710+name = "tree-sitter"
711+version = "0.16.1"
712+source = "registry+https://github.com/rust-lang/crates.io-index"
713+checksum = "1df33680edb07e4fb76edcbdd9c7b849b96709fb878afcf0ada678d6bda167af"
714+dependencies = [
715+ "cc",
716+ "regex",
717+]
718+
632[[package]]
719[[package]]
633name = "tui"
720name = "tui"
634version = "0.10.0"
721version = "0.10.0"
Cargo.toml+1
@@ -2,4 +2,5 @@
2members = [
2members = [
3 "helix-core",
3 "helix-core",
4 "helix-term",
4 "helix-term",
5+ "helix-syntax",
5]
6]
helix-syntax/Cargo.tomlnew file+14
@@ -0,0 +1,14 @@
1+[package]
2+name = "helix-syntax"
3+version = "0.1.0"
4+authors = ["Blaž Hrastnik <blaz@mxxn.io>"]
5+edition = "2018"
6+
7+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+[dependencies]
10+tree-sitter = "0.16"
11+enum-iterator = "0.6"
12+
13+[build-dependencies]
14+cc = { version = "1", features = ["parallel"] }
helix-syntax/build.rsnew file+126
@@ -0,0 +1,126 @@
1+use cc::Build;
2+
3+use std::io::{BufWriter, Read, Write};
4+use std::path::{Path, PathBuf};
5+use std::{env, fs};
6+
7+fn get_opt_level() -> u32 {
8+ env::var("OPT_LEVEL").unwrap().parse::<u32>().unwrap()
9+}
10+
11+fn get_debug() -> bool {
12+ env::var("DEBUG").unwrap() == "true"
13+}
14+
15+fn collect_tree_sitter_dirs(ignore: Vec<String>) -> Vec<String> {
16+ let mut dirs = Vec::new();
17+ for entry in fs::read_dir("languages").unwrap() {
18+ if let Ok(entry) = entry {
19+ let path = entry.path();
20+ let dir = path.file_name().unwrap().to_str().unwrap().to_string();
21+ if !ignore.contains(&dir) {
22+ dirs.push(dir);
23+ }
24+ }
25+ }
26+ dirs
27+}
28+
29+fn collect_src_files(dir: &str) -> (Vec<String>, Vec<String>) {
30+ eprintln!("Collect files for {}", dir);
31+
32+ let mut c_files = Vec::new();
33+ let mut cpp_files = Vec::new();
34+ let path = PathBuf::from("languages").join(&dir).join("src");
35+ for entry in fs::read_dir(path).unwrap() {
36+ if let Ok(entry) = entry {
37+ let path = entry.path();
38+ if path
39+ .file_stem()
40+ .unwrap()
41+ .to_str()
42+ .unwrap()
43+ .starts_with("binding")
44+ {
45+ continue;
46+ }
47+ if let Some(ext) = path.extension() {
48+ if ext == "c" {
49+ c_files.push(path.to_str().unwrap().to_string());
50+ } else if ext == "cc" || ext == "cpp" || ext == "cxx" {
51+ cpp_files.push(path.to_str().unwrap().to_string());
52+ }
53+ }
54+ }
55+ }
56+ (c_files, cpp_files)
57+}
58+
59+fn build_c(files: Vec<String>, language: &str) {
60+ let mut build = cc::Build::new();
61+ for file in files {
62+ build
63+ .file(&file)
64+ .include(PathBuf::from(file).parent().unwrap())
65+ .pic(true)
66+ .opt_level(get_opt_level())
67+ .debug(get_debug())
68+ .warnings(false)
69+ .flag_if_supported("-std=c99");
70+ }
71+ build.compile(&format!("tree-sitter-{}-c", language));
72+}
73+
74+fn build_cpp(files: Vec<String>, language: &str) {
75+ let mut build = cc::Build::new();
76+ for file in files {
77+ build
78+ .file(&file)
79+ .include(PathBuf::from(file).parent().unwrap())
80+ .pic(true)
81+ .opt_level(get_opt_level())
82+ .debug(get_debug())
83+ .warnings(false)
84+ .cpp(true);
85+ }
86+ build.compile(&format!("tree-sitter-{}-cpp", language));
87+}
88+
89+fn build_dir(dir: &str, language: &str) {
90+ println!("Build language {}", language);
91+ if PathBuf::from("languages")
92+ .join(dir)
93+ .read_dir()
94+ .unwrap()
95+ .next()
96+ .is_none()
97+ {
98+ eprintln!(
99+ "The directory {} is empty, did you use 'git clone --recursive'?",
100+ dir
101+ );
102+ eprintln!("You can fix in using 'git submodule init && git submodule update --recursive'.");
103+ std::process::exit(1);
104+ }
105+ let (c, cpp) = collect_src_files(&dir);
106+ if !c.is_empty() {
107+ build_c(c, &language);
108+ }
109+ if !cpp.is_empty() {
110+ build_cpp(cpp, &language);
111+ }
112+}
113+
114+fn main() {
115+ let ignore = vec![
116+ "tree-sitter-typescript".to_string(),
117+ "tree-sitter-cpp".to_string(),
118+ ];
119+ let dirs = collect_tree_sitter_dirs(ignore);
120+ for dir in dirs {
121+ let language = &dir[12..]; // skip tree-sitter- prefix
122+ build_dir(&dir, &language);
123+ }
124+ build_dir("tree-sitter-typescript/tsx", "tsx");
125+ build_dir("tree-sitter-typescript/typescript", "typescript");
126+}
helix-syntax/languages/tree-sitter-agdanew file+1

Submodule changed.

helix-syntax/languages/tree-sitter-bashnew file+1

Submodule changed.

helix-syntax/languages/tree-sitter-cnew file+1

Submodule changed.

helix-syntax/languages/tree-sitter-c-sharpnew file+1

Submodule changed.

helix-syntax/languages/tree-sitter-cppnew file+1

Submodule changed.

helix-syntax/languages/tree-sitter-cssnew file+1

Submodule changed.

helix-syntax/languages/tree-sitter-gonew file+1

Submodule changed.

helix-syntax/languages/tree-sitter-haskellnew file+1

Submodule changed.

helix-syntax/languages/tree-sitter-htmlnew file+1

Submodule changed.

helix-syntax/languages/tree-sitter-javanew file+1

Submodule changed.

helix-syntax/languages/tree-sitter-javascriptnew file+1

Submodule changed.

helix-syntax/languages/tree-sitter-jsonnew file+1

Submodule changed.

helix-syntax/languages/tree-sitter-julianew file+1

Submodule changed.

helix-syntax/languages/tree-sitter-phpnew file+1

Submodule changed.

helix-syntax/languages/tree-sitter-pythonnew file+1

Submodule changed.

helix-syntax/languages/tree-sitter-rubynew file+1

Submodule changed.

helix-syntax/languages/tree-sitter-rustnew file+1

Submodule changed.

helix-syntax/languages/tree-sitter-scalanew file+1

Submodule changed.

helix-syntax/languages/tree-sitter-swiftnew file+1

Submodule changed.

helix-syntax/languages/tree-sitter-typescriptnew file+1

Submodule changed.

helix-syntax/src/lib.rsnew file+87
@@ -0,0 +1,87 @@
1+use enum_iterator::IntoEnumIterator;
2+use tree_sitter::Language;
3+
4+#[macro_export]
5+macro_rules! mk_extern {
6+ ( $( $name:ident ),* ) => {
7+ $(
8+ extern "C" { pub fn $name() -> Language; }
9+ )*
10+ };
11+}
12+
13+#[macro_export]
14+macro_rules! mk_enum {
15+ ( $( $camel:ident ),* ) => {
16+ #[derive(Clone, Debug, IntoEnumIterator, PartialEq)]
17+ pub enum LANG {
18+ $(
19+ $camel,
20+ )*
21+ }
22+ };
23+}
24+
25+#[macro_export]
26+macro_rules! mk_get_language {
27+ ( $( ($camel:ident, $name:ident) ),* ) => {
28+ pub fn get_language(lang: &LANG) -> Language {
29+ unsafe {
30+ match lang {
31+ $(
32+ LANG::$camel => $name(),
33+ )*
34+ }
35+ }
36+ }
37+ };
38+}
39+
40+#[macro_export]
41+macro_rules! mk_get_language_name {
42+ ( $( $camel:ident ),* ) => {
43+ pub fn get_language_name(lang: &LANG) -> &'static str {
44+ match lang {
45+ $(
46+ LANG::$camel => stringify!($camel),
47+ )*
48+ }
49+ }
50+ };
51+}
52+
53+#[macro_export]
54+macro_rules! mk_langs {
55+ ( $( ($camel:ident, $name:ident) ),* ) => {
56+ mk_extern!($( $name ),*);
57+ mk_enum!($( $camel ),*);
58+ mk_get_language!($( ($camel, $name) ),*);
59+ mk_get_language_name!($( $camel ),*);
60+ };
61+}
62+
63+mk_langs!(
64+ // 1) Name for enum
65+ // 2) tree-sitter function to call to get a Language
66+ (Agda, tree_sitter_agda),
67+ (Bash, tree_sitter_bash),
68+ (C, tree_sitter_c),
69+ (CSharp, tree_sitter_c_sharp),
70+ // (Cpp, tree_sitter_cpp),
71+ (Css, tree_sitter_css),
72+ (Go, tree_sitter_go),
73+ (Haskell, tree_sitter_haskell),
74+ (Html, tree_sitter_html),
75+ (Java, tree_sitter_java),
76+ (Javascript, tree_sitter_javascript),
77+ (Json, tree_sitter_json),
78+ (Julia, tree_sitter_julia),
79+ (Php, tree_sitter_php),
80+ (Python, tree_sitter_python),
81+ (Ruby, tree_sitter_ruby),
82+ (Rust, tree_sitter_rust),
83+ (Scala, tree_sitter_scala),
84+ (Swift, tree_sitter_swift),
85+ (Tsx, tree_sitter_tsx),
86+ (Typescript, tree_sitter_typescript)
87+);
helix-term/Cargo.toml+2
@@ -14,6 +14,8 @@ path = "src/main.rs"
14# termwiz = { git = "https://github.com/wez/wezterm", features = ["widgets"] }
14# termwiz = { git = "https://github.com/wez/wezterm", features = ["widgets"] }
15# termwiz = { path = "../../wezterm/termwiz", default-features = false, features = ["widgets"] }
15# termwiz = { path = "../../wezterm/termwiz", default-features = false, features = ["widgets"] }
16
16
17+helix-syntax = { path = "../helix-syntax" }
18+
17anyhow = "1"
19anyhow = "1"
18argh = "0.1.3"
20argh = "0.1.3"
19helix-core = { path = "../helix-core" }
21helix-core = { path = "../helix-core" }