From eeb3f8e9639e68ea2d54a6be26d45543d01057f6 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Sun, 13 Feb 2022 10:42:18 -0600 Subject: migrate helix-syntax crate into helix-core and helix-term helix-syntax mostly existed for the sake of the build task which checks and compiles the submodules. Since we won't be relying on that process anymore, it doesn't end up making much sense to have a very thin crate just for some functions that we could port to helix-core. The remaining build-related code is moved to helix-term which will be able to provide grammar builds through the --build-grammars CLI flag. --- Cargo.lock | 16 +--- Cargo.toml | 5 -- docs/architecture.md | 13 ++- helix-core/Cargo.toml | 5 +- helix-core/src/syntax.rs | 36 +++++++- helix-syntax/Cargo.toml | 21 ----- helix-syntax/README.md | 13 --- helix-syntax/build.rs | 206 --------------------------------------------- helix-syntax/src/lib.rs | 31 ------- helix-term/Cargo.toml | 4 + helix-term/build.rs | 5 ++ helix-term/src/grammars.rs | 175 ++++++++++++++++++++++++++++++++++++++ helix-term/src/lib.rs | 1 + 13 files changed, 231 insertions(+), 300 deletions(-) delete mode 100644 helix-syntax/Cargo.toml delete mode 100644 helix-syntax/README.md delete mode 100644 helix-syntax/build.rs delete mode 100644 helix-syntax/src/lib.rs create mode 100644 helix-term/src/grammars.rs diff --git a/Cargo.lock b/Cargo.lock index ac49da51..469a5862 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -358,11 +358,12 @@ dependencies = [ name = "helix-core" version = "0.6.0" dependencies = [ + "anyhow", "arc-swap", "chrono", "encoding_rs", "etcetera", - "helix-syntax", + "libloading", "log", "once_cell", "quickcheck", @@ -415,22 +416,12 @@ dependencies = [ "which", ] -[[package]] -name = "helix-syntax" -version = "0.6.0" -dependencies = [ - "anyhow", - "cc", - "libloading", - "threadpool", - "tree-sitter", -] - [[package]] name = "helix-term" version = "0.6.0" dependencies = [ "anyhow", + "cc", "chrono", "content_inspector", "crossterm", @@ -454,6 +445,7 @@ dependencies = [ "serde_json", "signal-hook", "signal-hook-tokio", + "threadpool", "tokio", "tokio-stream", "toml", diff --git a/Cargo.toml b/Cargo.toml index 31088f98..0847e6ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,6 @@ members = [ "helix-view", "helix-term", "helix-tui", - "helix-syntax", "helix-lsp", "helix-dap", "xtask", @@ -14,10 +13,6 @@ default-members = [ "helix-term" ] -# Build helix-syntax in release mode to make the code path faster in development. -# [profile.dev.package."helix-syntax"] -# opt-level = 3 - [profile.dev] split-debuginfo = "unpacked" diff --git a/docs/architecture.md b/docs/architecture.md index 40e01745..3c743eca 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -1,11 +1,10 @@ -| Crate | Description | -| ----------- | ----------- | -| helix-core | Core editing primitives, functional. | -| helix-syntax | Tree-sitter grammars | -| helix-lsp | Language server client | -| helix-view | UI abstractions for use in backends, imperative shell. | -| helix-term | Terminal UI | +| Crate | Description | +| ----------- | ----------- | +| helix-core | Core editing primitives, functional. | +| helix-lsp | Language server client | +| helix-view | UI abstractions for use in backends, imperative shell. | +| helix-term | Terminal UI | | helix-tui | TUI primitives, forked from tui-rs, inspired by Cursive | diff --git a/helix-core/Cargo.toml b/helix-core/Cargo.toml index 6682c37f..5582d38b 100644 --- a/helix-core/Cargo.toml +++ b/helix-core/Cargo.toml @@ -13,8 +13,6 @@ include = ["src/**/*", "README.md"] [features] [dependencies] -helix-syntax = { version = "0.6", path = "../helix-syntax" } - ropey = "1.3" smallvec = "1.8" smartstring = "1.0.0" @@ -40,5 +38,8 @@ encoding_rs = "0.8" chrono = { version = "0.4", default-features = false, features = ["alloc", "std"] } +libloading = "0.7" +anyhow = "1" + [dev-dependencies] quickcheck = { version = "1", default-features = false } diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index 53d20da3..0f7d224d 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -7,7 +7,9 @@ use crate::{ Rope, RopeSlice, Tendril, }; -pub use helix_syntax::get_language; +use anyhow::{Context, Result}; +use libloading::{Library, Symbol}; +use tree_sitter::Language; use arc_swap::{ArcSwap, Guard}; use slotmap::{DefaultKey as LayerId, HopSlotMap}; @@ -25,6 +27,34 @@ use std::{ use once_cell::sync::{Lazy, OnceCell}; use serde::{Deserialize, Serialize}; +#[cfg(unix)] +pub const DYLIB_EXTENSION: &str = "so"; + +#[cfg(windows)] +pub const DYLIB_EXTENSION: &str = "dll"; + +fn replace_dashes_with_underscores(name: &str) -> String { + name.replace('-', "_") +} + +pub fn get_language(runtime_path: &std::path::Path, name: &str) -> Result { + let name = name.to_ascii_lowercase(); + let mut library_path = runtime_path.join("grammars").join(&name); + library_path.set_extension(DYLIB_EXTENSION); + + let library = unsafe { Library::new(&library_path) } + .with_context(|| format!("Error opening dynamic library {:?}", &library_path))?; + let language_fn_name = format!("tree_sitter_{}", replace_dashes_with_underscores(&name)); + let language = unsafe { + let language_fn: Symbol Language> = library + .get(language_fn_name.as_bytes()) + .with_context(|| format!("Failed to load symbol {}", language_fn_name))?; + language_fn() + }; + std::mem::forget(library); + Ok(language) +} + fn deserialize_regex<'de, D>(deserializer: D) -> Result, D::Error> where D: serde::Deserializer<'de>, @@ -426,7 +456,7 @@ impl LanguageConfiguration { &injections_query, &locals_query, ) - .unwrap(); // TODO: avoid panic + .unwrap_or_else(|query_error| panic!("Could not parse queries for language {:?}. Are your grammars out of sync? Try running 'hx --fetch-grammars' and 'hx --build-grammars'. This query could not be parsed: {:?}", self.language_id, query_error)); config.configure(scopes); Some(Arc::new(config)) @@ -2023,7 +2053,7 @@ mod test { ); let loader = Loader::new(Configuration { language: vec![] }); - let language = get_language(&crate::RUNTIME_DIR, "Rust").unwrap(); + let language = get_language("Rust").unwrap(); let query = Query::new(language, query_str).unwrap(); let textobject = TextObjectQuery { query }; diff --git a/helix-syntax/Cargo.toml b/helix-syntax/Cargo.toml deleted file mode 100644 index 855839be..00000000 --- a/helix-syntax/Cargo.toml +++ /dev/null @@ -1,21 +0,0 @@ -[package] -name = "helix-syntax" -version = "0.6.0" -authors = ["Blaž Hrastnik "] -edition = "2021" -license = "MPL-2.0" -description = "Tree-sitter grammars support" -categories = ["editor"] -repository = "https://github.com/helix-editor/helix" -homepage = "https://helix-editor.com" -include = ["src/**/*", "languages/**/*", "build.rs", "!**/docs/**/*", "!**/test/**/*", "!**/examples/**/*", "!**/build/**/*"] - -[dependencies] -tree-sitter = "0.20" -libloading = "0.7" -anyhow = "1" - -[build-dependencies] -cc = { version = "1" } -threadpool = { version = "1.0" } -anyhow = "1" diff --git a/helix-syntax/README.md b/helix-syntax/README.md deleted file mode 100644 index bba2197a..00000000 --- a/helix-syntax/README.md +++ /dev/null @@ -1,13 +0,0 @@ -helix-syntax -============ - -Syntax highlighting for helix, (shallow) submodules resides here. - -Differences from nvim-treesitter --------------------------------- - -As the syntax are commonly ported from -. - -Note that we do not support the custom `#any-of` predicate which is -supported by neovim so one needs to change it to `#match` with regex. diff --git a/helix-syntax/build.rs b/helix-syntax/build.rs deleted file mode 100644 index fa8be8b3..00000000 --- a/helix-syntax/build.rs +++ /dev/null @@ -1,206 +0,0 @@ -use anyhow::{anyhow, Context, Result}; -use std::fs; -use std::time::SystemTime; -use std::{ - path::{Path, PathBuf}, - process::Command, -}; - -use std::sync::mpsc::channel; - -fn collect_tree_sitter_dirs(ignore: &[String]) -> Result> { - let mut dirs = Vec::new(); - let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("languages"); - - for entry in fs::read_dir(path)? { - let entry = entry?; - let path = entry.path(); - - if !entry.file_type()?.is_dir() { - continue; - } - - let dir = path.file_name().unwrap().to_str().unwrap().to_string(); - - // filter ignores - if ignore.contains(&dir) { - continue; - } - dirs.push(dir) - } - - Ok(dirs) -} - -#[cfg(unix)] -const DYLIB_EXTENSION: &str = "so"; - -#[cfg(windows)] -const DYLIB_EXTENSION: &str = "dll"; - -fn build_library(src_path: &Path, language: &str) -> Result<()> { - let header_path = src_path; - // let grammar_path = src_path.join("grammar.json"); - let parser_path = src_path.join("parser.c"); - let mut scanner_path = src_path.join("scanner.c"); - - let scanner_path = if scanner_path.exists() { - Some(scanner_path) - } else { - scanner_path.set_extension("cc"); - if scanner_path.exists() { - Some(scanner_path) - } else { - None - } - }; - let parser_lib_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../runtime/grammars"); - let mut library_path = parser_lib_path.join(language); - library_path.set_extension(DYLIB_EXTENSION); - - let recompile = needs_recompile(&library_path, &parser_path, &scanner_path) - .with_context(|| "Failed to compare source and binary timestamps")?; - - if !recompile { - return Ok(()); - } - let mut config = cc::Build::new(); - config.cpp(true).opt_level(2).cargo_metadata(false); - let compiler = config.get_compiler(); - let mut command = Command::new(compiler.path()); - command.current_dir(src_path); - for (key, value) in compiler.env() { - command.env(key, value); - } - - if cfg!(windows) { - command - .args(&["/nologo", "/LD", "/I"]) - .arg(header_path) - .arg("/Od") - .arg("/utf-8"); - if let Some(scanner_path) = scanner_path.as_ref() { - command.arg(scanner_path); - } - - command - .arg(parser_path) - .arg("/link") - .arg(format!("/out:{}", library_path.to_str().unwrap())); - } else { - command - .arg("-shared") - .arg("-fPIC") - .arg("-fno-exceptions") - .arg("-g") - .arg("-I") - .arg(header_path) - .arg("-o") - .arg(&library_path) - .arg("-O2"); - if let Some(scanner_path) = scanner_path.as_ref() { - if scanner_path.extension() == Some("c".as_ref()) { - command.arg("-xc").arg("-std=c99").arg(scanner_path); - } else { - command.arg(scanner_path); - } - } - command.arg("-xc").arg(parser_path); - if cfg!(all(unix, not(target_os = "macos"))) { - command.arg("-Wl,-z,relro,-z,now"); - } - } - - let output = command - .output() - .with_context(|| "Failed to execute C compiler")?; - if !output.status.success() { - return Err(anyhow!( - "Parser compilation failed.\nStdout: {}\nStderr: {}", - String::from_utf8_lossy(&output.stdout), - String::from_utf8_lossy(&output.stderr) - )); - } - - Ok(()) -} -fn needs_recompile( - lib_path: &Path, - parser_c_path: &Path, - scanner_path: &Option, -) -> Result { - if !lib_path.exists() { - return Ok(true); - } - let lib_mtime = mtime(lib_path)?; - if mtime(parser_c_path)? > lib_mtime { - return Ok(true); - } - if let Some(scanner_path) = scanner_path { - if mtime(scanner_path)? > lib_mtime { - return Ok(true); - } - } - Ok(false) -} - -fn mtime(path: &Path) -> Result { - Ok(fs::metadata(path)?.modified()?) -} - -fn build_dir(dir: &str, language: &str) { - println!("Build language {}", language); - if PathBuf::from("languages") - .join(dir) - .read_dir() - .unwrap() - .next() - .is_none() - { - eprintln!( - "The directory {} is empty, you probably need to use 'git submodule update --init --recursive'?", - dir - ); - std::process::exit(1); - } - - let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("languages") - .join(dir) - .join("src"); - - build_library(&path, language).unwrap(); -} - -fn main() { - let ignore = vec![ - "tree-sitter-typescript".to_string(), - "tree-sitter-ocaml".to_string(), - ]; - let dirs = collect_tree_sitter_dirs(&ignore).unwrap(); - - let mut n_jobs = 0; - let pool = threadpool::Builder::new().build(); // by going through the builder, it'll use num_cpus - let (tx, rx) = channel(); - - for dir in dirs { - let tx = tx.clone(); - n_jobs += 1; - - pool.execute(move || { - let language = &dir.strip_prefix("tree-sitter-").unwrap(); - build_dir(&dir, language); - - // report progress - tx.send(1).unwrap(); - }); - } - pool.join(); - // drop(tx); - assert_eq!(rx.try_iter().sum::(), n_jobs); - - build_dir("tree-sitter-typescript/tsx", "tsx"); - build_dir("tree-sitter-typescript/typescript", "typescript"); - build_dir("tree-sitter-ocaml/ocaml", "ocaml"); - build_dir("tree-sitter-ocaml/interface", "ocaml-interface") -} diff --git a/helix-syntax/src/lib.rs b/helix-syntax/src/lib.rs deleted file mode 100644 index b0ec48d8..00000000 --- a/helix-syntax/src/lib.rs +++ /dev/null @@ -1,31 +0,0 @@ -use anyhow::{Context, Result}; -use libloading::{Library, Symbol}; -use tree_sitter::Language; - -fn replace_dashes_with_underscores(name: &str) -> String { - name.replace('-', "_") -} -#[cfg(unix)] -const DYLIB_EXTENSION: &str = "so"; - -#[cfg(windows)] -const DYLIB_EXTENSION: &str = "dll"; - -pub fn get_language(runtime_path: &std::path::Path, name: &str) -> Result { - let name = name.to_ascii_lowercase(); - let mut library_path = runtime_path.join("grammars").join(&name); - // TODO: duplicated under build - library_path.set_extension(DYLIB_EXTENSION); - - let library = unsafe { Library::new(&library_path) } - .with_context(|| format!("Error opening dynamic library {:?}", &library_path))?; - let language_fn_name = format!("tree_sitter_{}", replace_dashes_with_underscores(&name)); - let language = unsafe { - let language_fn: Symbol Language> = library - .get(language_fn_name.as_bytes()) - .with_context(|| format!("Failed to load symbol {}", language_fn_name))?; - language_fn() - }; - std::mem::forget(library); - Ok(language) -} diff --git a/helix-term/Cargo.toml b/helix-term/Cargo.toml index 9f7821f6..93d50d7e 100644 --- a/helix-term/Cargo.toml +++ b/helix-term/Cargo.toml @@ -66,5 +66,9 @@ grep-searcher = "0.1.8" # Remove once retain_mut lands in stable rust retain_mut = "0.1.7" +# compiling grammars +cc = { version = "1" } +threadpool = { version = "1.0" } + [target.'cfg(not(windows))'.dependencies] # https://github.com/vorner/signal-hook/issues/100 signal-hook-tokio = { version = "0.3", features = ["futures-v0_3"] } diff --git a/helix-term/build.rs b/helix-term/build.rs index b5d62b28..7303041c 100644 --- a/helix-term/build.rs +++ b/helix-term/build.rs @@ -14,5 +14,10 @@ fn main() { None => env!("CARGO_PKG_VERSION").into(), }; + println!( + "cargo:rustc-env=BUILD_TARGET={}", + std::env::var("TARGET").unwrap() + ); + println!("cargo:rustc-env=VERSION_AND_GIT_HASH={}", version); } diff --git a/helix-term/src/grammars.rs b/helix-term/src/grammars.rs new file mode 100644 index 00000000..6a4910a3 --- /dev/null +++ b/helix-term/src/grammars.rs @@ -0,0 +1,175 @@ +use anyhow::{anyhow, Context, Result}; +use std::fs; +use std::time::SystemTime; +use std::{ + path::{Path, PathBuf}, + process::Command, +}; + +use helix_core::syntax::DYLIB_EXTENSION; + +const BUILD_TARGET: &str = env!("BUILD_TARGET"); + +pub fn collect_tree_sitter_dirs(ignore: &[String]) -> Result> { + let mut dirs = Vec::new(); + let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../helix-syntax/languages"); + + for entry in fs::read_dir(path)? { + let entry = entry?; + let path = entry.path(); + + if !entry.file_type()?.is_dir() { + continue; + } + + let dir = path.file_name().unwrap().to_str().unwrap().to_string(); + + // filter ignores + if ignore.contains(&dir) { + continue; + } + dirs.push(dir) + } + + Ok(dirs) +} + +fn build_library(src_path: &Path, language: &str) -> Result<()> { + let header_path = src_path; + // let grammar_path = src_path.join("grammar.json"); + let parser_path = src_path.join("parser.c"); + let mut scanner_path = src_path.join("scanner.c"); + + let scanner_path = if scanner_path.exists() { + Some(scanner_path) + } else { + scanner_path.set_extension("cc"); + if scanner_path.exists() { + Some(scanner_path) + } else { + None + } + }; + let parser_lib_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../runtime/grammars"); + let mut library_path = parser_lib_path.join(language); + library_path.set_extension(DYLIB_EXTENSION); + + let recompile = needs_recompile(&library_path, &parser_path, &scanner_path) + .with_context(|| "Failed to compare source and binary timestamps")?; + + if !recompile { + return Ok(()); + } + let mut config = cc::Build::new(); + config + .cpp(true) + .opt_level(2) + .cargo_metadata(false) + .host(BUILD_TARGET) + .target(BUILD_TARGET); + let compiler = config.get_compiler(); + let mut command = Command::new(compiler.path()); + command.current_dir(src_path); + for (key, value) in compiler.env() { + command.env(key, value); + } + + if cfg!(windows) { + command + .args(&["/nologo", "/LD", "/I"]) + .arg(header_path) + .arg("/Od") + .arg("/utf-8"); + if let Some(scanner_path) = scanner_path.as_ref() { + command.arg(scanner_path); + } + + command + .arg(parser_path) + .arg("/link") + .arg(format!("/out:{}", library_path.to_str().unwrap())); + } else { + command + .arg("-shared") + .arg("-fPIC") + .arg("-fno-exceptions") + .arg("-g") + .arg("-I") + .arg(header_path) + .arg("-o") + .arg(&library_path) + .arg("-O2"); + if let Some(scanner_path) = scanner_path.as_ref() { + if scanner_path.extension() == Some("c".as_ref()) { + command.arg("-xc").arg("-std=c99").arg(scanner_path); + } else { + command.arg(scanner_path); + } + } + command.arg("-xc").arg(parser_path); + if cfg!(all(unix, not(target_os = "macos"))) { + command.arg("-Wl,-z,relro,-z,now"); + } + } + + let output = command + .output() + .with_context(|| "Failed to execute C compiler")?; + if !output.status.success() { + return Err(anyhow!( + "Parser compilation failed.\nStdout: {}\nStderr: {}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + )); + } + + Ok(()) +} +fn needs_recompile( + lib_path: &Path, + parser_c_path: &Path, + scanner_path: &Option, +) -> Result { + if !lib_path.exists() { + return Ok(true); + } + let lib_mtime = mtime(lib_path)?; + if mtime(parser_c_path)? > lib_mtime { + return Ok(true); + } + if let Some(scanner_path) = scanner_path { + if mtime(scanner_path)? > lib_mtime { + return Ok(true); + } + } + Ok(false) +} + +fn mtime(path: &Path) -> Result { + Ok(fs::metadata(path)?.modified()?) +} + +pub fn build_dir(dir: &str, language: &str) { + println!("Build language {}", language); + if PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("../helix-syntax/languages") + .join(dir) + .read_dir() + .unwrap() + .next() + .is_none() + { + eprintln!( + "The directory {} is empty, you probably need to use './scripts/grammars sync'?", + dir + ); + std::process::exit(1); + } + + let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("../helix-syntax/languages") + .join(dir) + .join("src"); + + build_library(&path, language).unwrap(); +} diff --git a/helix-term/src/lib.rs b/helix-term/src/lib.rs index fc8e934e..22747998 100644 --- a/helix-term/src/lib.rs +++ b/helix-term/src/lib.rs @@ -7,6 +7,7 @@ pub mod commands; pub mod compositor; pub mod config; pub mod health; +pub mod grammars; pub mod job; pub mod keymap; pub mod ui; -- cgit v1.2.3-70-g09d2