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.
This commit is contained in:
Michael Davis 2022-02-13 10:42:18 -06:00 committed by Blaž Hrastnik
parent c1f90a127b
commit eeb3f8e963
12 changed files with 72 additions and 141 deletions

16
Cargo.lock generated
View file

@ -358,11 +358,12 @@ dependencies = [
name = "helix-core" name = "helix-core"
version = "0.6.0" version = "0.6.0"
dependencies = [ dependencies = [
"anyhow",
"arc-swap", "arc-swap",
"chrono", "chrono",
"encoding_rs", "encoding_rs",
"etcetera", "etcetera",
"helix-syntax", "libloading",
"log", "log",
"once_cell", "once_cell",
"quickcheck", "quickcheck",
@ -415,22 +416,12 @@ dependencies = [
"which", "which",
] ]
[[package]]
name = "helix-syntax"
version = "0.6.0"
dependencies = [
"anyhow",
"cc",
"libloading",
"threadpool",
"tree-sitter",
]
[[package]] [[package]]
name = "helix-term" name = "helix-term"
version = "0.6.0" version = "0.6.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"cc",
"chrono", "chrono",
"content_inspector", "content_inspector",
"crossterm", "crossterm",
@ -454,6 +445,7 @@ dependencies = [
"serde_json", "serde_json",
"signal-hook", "signal-hook",
"signal-hook-tokio", "signal-hook-tokio",
"threadpool",
"tokio", "tokio",
"tokio-stream", "tokio-stream",
"toml", "toml",

View file

@ -4,7 +4,6 @@ members = [
"helix-view", "helix-view",
"helix-term", "helix-term",
"helix-tui", "helix-tui",
"helix-syntax",
"helix-lsp", "helix-lsp",
"helix-dap", "helix-dap",
"xtask", "xtask",
@ -14,10 +13,6 @@ default-members = [
"helix-term" "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] [profile.dev]
split-debuginfo = "unpacked" split-debuginfo = "unpacked"

View file

@ -1,11 +1,10 @@
| Crate | Description | | Crate | Description |
| ----------- | ----------- | | ----------- | ----------- |
| helix-core | Core editing primitives, functional. | | helix-core | Core editing primitives, functional. |
| helix-syntax | Tree-sitter grammars | | helix-lsp | Language server client |
| helix-lsp | Language server client | | helix-view | UI abstractions for use in backends, imperative shell. |
| helix-view | UI abstractions for use in backends, imperative shell. | | helix-term | Terminal UI |
| helix-term | Terminal UI |
| helix-tui | TUI primitives, forked from tui-rs, inspired by Cursive | | helix-tui | TUI primitives, forked from tui-rs, inspired by Cursive |

View file

@ -13,8 +13,6 @@ include = ["src/**/*", "README.md"]
[features] [features]
[dependencies] [dependencies]
helix-syntax = { version = "0.6", path = "../helix-syntax" }
ropey = "1.3" ropey = "1.3"
smallvec = "1.8" smallvec = "1.8"
smartstring = "1.0.0" smartstring = "1.0.0"
@ -40,5 +38,8 @@ encoding_rs = "0.8"
chrono = { version = "0.4", default-features = false, features = ["alloc", "std"] } chrono = { version = "0.4", default-features = false, features = ["alloc", "std"] }
libloading = "0.7"
anyhow = "1"
[dev-dependencies] [dev-dependencies]
quickcheck = { version = "1", default-features = false } quickcheck = { version = "1", default-features = false }

View file

@ -7,7 +7,9 @@ use crate::{
Rope, RopeSlice, Tendril, 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 arc_swap::{ArcSwap, Guard};
use slotmap::{DefaultKey as LayerId, HopSlotMap}; use slotmap::{DefaultKey as LayerId, HopSlotMap};
@ -25,6 +27,34 @@ use std::{
use once_cell::sync::{Lazy, OnceCell}; use once_cell::sync::{Lazy, OnceCell};
use serde::{Deserialize, Serialize}; 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<Language> {
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<unsafe extern "C" fn() -> 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<Option<Regex>, D::Error> fn deserialize_regex<'de, D>(deserializer: D) -> Result<Option<Regex>, D::Error>
where where
D: serde::Deserializer<'de>, D: serde::Deserializer<'de>,
@ -426,7 +456,7 @@ impl LanguageConfiguration {
&injections_query, &injections_query,
&locals_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); config.configure(scopes);
Some(Arc::new(config)) Some(Arc::new(config))
@ -2023,7 +2053,7 @@ mod test {
); );
let loader = Loader::new(Configuration { language: vec![] }); 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 query = Query::new(language, query_str).unwrap();
let textobject = TextObjectQuery { query }; let textobject = TextObjectQuery { query };

View file

@ -1,21 +0,0 @@
[package]
name = "helix-syntax"
version = "0.6.0"
authors = ["Blaž Hrastnik <blaz@mxxn.io>"]
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"

View file

@ -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
<https://github.com/nvim-treesitter/nvim-treesitter>.
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.

View file

@ -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<Language> {
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<unsafe extern "C" fn() -> 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)
}

View file

@ -66,5 +66,9 @@ grep-searcher = "0.1.8"
# Remove once retain_mut lands in stable rust # Remove once retain_mut lands in stable rust
retain_mut = "0.1.7" 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 [target.'cfg(not(windows))'.dependencies] # https://github.com/vorner/signal-hook/issues/100
signal-hook-tokio = { version = "0.3", features = ["futures-v0_3"] } signal-hook-tokio = { version = "0.3", features = ["futures-v0_3"] }

View file

@ -14,5 +14,10 @@ fn main() {
None => env!("CARGO_PKG_VERSION").into(), 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); println!("cargo:rustc-env=VERSION_AND_GIT_HASH={}", version);
} }

View file

@ -6,11 +6,13 @@ use std::{
process::Command, process::Command,
}; };
use std::sync::mpsc::channel; use helix_core::syntax::DYLIB_EXTENSION;
fn collect_tree_sitter_dirs(ignore: &[String]) -> Result<Vec<String>> { const BUILD_TARGET: &str = env!("BUILD_TARGET");
pub fn collect_tree_sitter_dirs(ignore: &[String]) -> Result<Vec<String>> {
let mut dirs = Vec::new(); let mut dirs = Vec::new();
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("languages"); let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../helix-syntax/languages");
for entry in fs::read_dir(path)? { for entry in fs::read_dir(path)? {
let entry = entry?; let entry = entry?;
@ -32,12 +34,6 @@ fn collect_tree_sitter_dirs(ignore: &[String]) -> Result<Vec<String>> {
Ok(dirs) 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<()> { fn build_library(src_path: &Path, language: &str) -> Result<()> {
let header_path = src_path; let header_path = src_path;
// let grammar_path = src_path.join("grammar.json"); // let grammar_path = src_path.join("grammar.json");
@ -65,7 +61,12 @@ fn build_library(src_path: &Path, language: &str) -> Result<()> {
return Ok(()); return Ok(());
} }
let mut config = cc::Build::new(); let mut config = cc::Build::new();
config.cpp(true).opt_level(2).cargo_metadata(false); config
.cpp(true)
.opt_level(2)
.cargo_metadata(false)
.host(BUILD_TARGET)
.target(BUILD_TARGET);
let compiler = config.get_compiler(); let compiler = config.get_compiler();
let mut command = Command::new(compiler.path()); let mut command = Command::new(compiler.path());
command.current_dir(src_path); command.current_dir(src_path);
@ -148,9 +149,10 @@ fn mtime(path: &Path) -> Result<SystemTime> {
Ok(fs::metadata(path)?.modified()?) Ok(fs::metadata(path)?.modified()?)
} }
fn build_dir(dir: &str, language: &str) { pub fn build_dir(dir: &str, language: &str) {
println!("Build language {}", language); println!("Build language {}", language);
if PathBuf::from("languages") if PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../helix-syntax/languages")
.join(dir) .join(dir)
.read_dir() .read_dir()
.unwrap() .unwrap()
@ -158,49 +160,16 @@ fn build_dir(dir: &str, language: &str) {
.is_none() .is_none()
{ {
eprintln!( eprintln!(
"The directory {} is empty, you probably need to use 'git submodule update --init --recursive'?", "The directory {} is empty, you probably need to use './scripts/grammars sync'?",
dir dir
); );
std::process::exit(1); std::process::exit(1);
} }
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("languages") .join("../helix-syntax/languages")
.join(dir) .join(dir)
.join("src"); .join("src");
build_library(&path, language).unwrap(); 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::<usize>(), 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")
}

View file

@ -7,6 +7,7 @@ pub mod commands;
pub mod compositor; pub mod compositor;
pub mod config; pub mod config;
pub mod health; pub mod health;
pub mod grammars;
pub mod job; pub mod job;
pub mod keymap; pub mod keymap;
pub mod ui; pub mod ui;