aboutsummaryrefslogtreecommitdiff
path: root/helix-syntax
diff options
context:
space:
mode:
authorMichael Davis2022-02-13 16:42:18 +0000
committerBlaž Hrastnik2022-03-10 08:31:57 +0000
commiteeb3f8e9639e68ea2d54a6be26d45543d01057f6 (patch)
treeb6847b9e8edb0422f6d88bf84f8a6dddb036f373 /helix-syntax
parentc1f90a127b972c8b41b0d24dcacb3bdd480be9c5 (diff)
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.
Diffstat (limited to 'helix-syntax')
-rw-r--r--helix-syntax/Cargo.toml21
-rw-r--r--helix-syntax/README.md13
-rw-r--r--helix-syntax/build.rs206
-rw-r--r--helix-syntax/src/lib.rs31
4 files changed, 0 insertions, 271 deletions
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 <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"
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
-<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.
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<Vec<String>> {
- 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<PathBuf>,
-) -> Result<bool> {
- 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<SystemTime> {
- 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::<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")
-}
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<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)
-}