diff --git a/.github/workflows/msrv-rust-toolchain.toml b/.github/workflows/msrv-rust-toolchain.toml index ece2fa76..7a3ec50b 100644 --- a/.github/workflows/msrv-rust-toolchain.toml +++ b/.github/workflows/msrv-rust-toolchain.toml @@ -1,3 +1,3 @@ [toolchain] -channel = "1.57.0" +channel = "1.59.0" components = ["rustfmt", "rust-src"] diff --git a/Cargo.lock b/Cargo.lock index d8d82d28..c8bf13bf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1469,5 +1469,6 @@ dependencies = [ "helix-core", "helix-loader", "helix-term", + "helix-view", "toml", ] diff --git a/book/src/generated/lang-support.md b/book/src/generated/lang-support.md index 78362384..86c26042 100644 --- a/book/src/generated/lang-support.md +++ b/book/src/generated/lang-support.md @@ -3,6 +3,7 @@ | astro | ✓ | | | | | awk | ✓ | ✓ | | `awk-language-server` | | bash | ✓ | | | `bash-language-server` | +| bass | ✓ | | | `bass` | | beancount | ✓ | | | | | c | ✓ | ✓ | ✓ | `clangd` | | c-sharp | ✓ | ✓ | | `OmniSharp` | @@ -67,7 +68,7 @@ | llvm-mir-yaml | ✓ | | ✓ | | | lua | ✓ | | ✓ | `lua-language-server` | | make | ✓ | | | | -| markdown | ✓ | | | | +| markdown | ✓ | | | `marksman` | | markdown.inline | ✓ | | | | | meson | ✓ | | ✓ | | | mint | | | | `mint` | diff --git a/book/src/themes.md b/book/src/themes.md index b37ee852..9908456f 100644 --- a/book/src/themes.md +++ b/book/src/themes.md @@ -254,4 +254,10 @@ These scopes are used for theming the editor interface. | `diagnostic.warning` | Diagnostics warning (editing area) | | `diagnostic.error` | Diagnostics error (editing area) | +You can check compliance to spec with + +```shell +cargo xtask themelint onedark # replace onedark with +``` + [editor-section]: ./configuration.md#editor-section diff --git a/languages.toml b/languages.toml index 0b46cc49..949dd372 100644 --- a/languages.toml +++ b/languages.toml @@ -905,7 +905,8 @@ name = "markdown" scope = "source.md" injection-regex = "md|markdown" file-types = ["md", "markdown"] -roots = [] +roots = [".marksman.toml"] +language-server = { command = "marksman", args=["server"] } indent = { tab-width = 2, unit = " " } [[grammar]] @@ -1766,3 +1767,17 @@ indent = { tab-width = 2, unit = " " } [[grammar]] name = "astro" source = { git = "https://github.com/virchau13/tree-sitter-astro", rev = "5f5c3e73c45967df9aa42f861fad2d77cd4e0900" } + +[[language]] +name = "bass" +scope = "source.bass" +injection-regex = "bass" +file-types = ["bass"] +roots = [] +comment-token = ";" +indent = { tab-width = 2, unit = " " } +language-server = { command = "bass", args = ["--lsp"] } + +[[grammar]] +name = "bass" +source = { git = "https://github.com/vito/tree-sitter-bass", rev = "501133e260d768ed4e1fd7374912ed5c86d6fd90" } diff --git a/runtime/queries/bass/highlights.scm b/runtime/queries/bass/highlights.scm new file mode 100644 index 00000000..f8b70fc1 --- /dev/null +++ b/runtime/queries/bass/highlights.scm @@ -0,0 +1,101 @@ +; GENERATED VIA https://github.com/vito/tree-sitter-bass + +;;; comments + +(comment) @comment.line + +;;; punctuation + +["(" ")" "[" "]" "{" "}"] @punctuation.bracket + +;;; constants + +[(ignore) (null)] @constant.builtin + +(bool) @constant.builtin.boolean + +(int) @constant.numeric.integer + +;;; strings + +;; string literals + +(string) @string +(string (string_escape) @constant.character.escape) + +;; keywords (symbol literals) + +(keyword) @string.special.symbol + +;; paths + +(dot) @string.special.path +(dotdot) @string.special.path +(command) @string.special.path +(subpath (symbol) @string.special.path) + +; slashes in a path denote a combiner call +(subpath (slash) @function) + + + +;;; specific highlighting for builtins & special forms + +;; symbol classification based highlighting + +(list . (symbol) @keyword.control.conditional (#match? @keyword.control.conditional "^(if|case|cond|when)$")) +(cons . (symbol) @keyword.control.conditional (#match? @keyword.control.conditional "^(if|case|cond|when)$")) + +(list . (symbol) @keyword.control.repeat (#match? @keyword.control.repeat "^(each)$")) +(cons . (symbol) @keyword.control.repeat (#match? @keyword.control.repeat "^(each)$")) + +(list . (symbol) @label (#match? @label "^(def|defop|defn)$")) +(cons . (symbol) @label (#match? @label "^(def|defop|defn)$")) + +(list . (symbol) @function.builtin (#match? @function.builtin "^(dump|mkfs|json|log|error|now|cons|wrap|unwrap|eval|make-scope|bind|meta|with-meta|null\\?|ignore\\?|boolean\\?|number\\?|string\\?|symbol\\?|scope\\?|sink\\?|source\\?|list\\?|pair\\?|applicative\\?|operative\\?|combiner\\?|path\\?|empty\\?|thunk\\?|\\+|\\*|quot|-|max|min|=|>|>=|<|<=|list->source|across|emit|next|reduce-kv|assoc|symbol->string|string->symbol|str|substring|trim|scope->list|string->fs-path|string->cmd-path|string->dir|subpath|path-name|path-stem|with-image|with-dir|with-args|with-cmd|with-stdin|with-env|with-insecure|with-label|with-port|with-tls|with-mount|thunk-cmd|thunk-args|resolve|start|addr|wait|read|cache-dir|binds\\?|recall-memo|store-memo|mask|list|list\\*|first|rest|length|second|third|map|map-pairs|foldr|foldl|append|filter|conj|list->scope|merge|apply|id|always|vals|keys|memo|succeeds\\?|run|last|take|take-all|insecure!|from|cd|wrap-cmd|mkfile|path-base|not)$")) +(cons . (symbol) @function.builtin (#match? @function.builtin "^(dump|mkfs|json|log|error|now|cons|wrap|unwrap|eval|make-scope|bind|meta|with-meta|null\\?|ignore\\?|boolean\\?|number\\?|string\\?|symbol\\?|scope\\?|sink\\?|source\\?|list\\?|pair\\?|applicative\\?|operative\\?|combiner\\?|path\\?|empty\\?|thunk\\?|\\+|\\*|quot|-|max|min|=|>|>=|<|<=|list->source|across|emit|next|reduce-kv|assoc|symbol->string|string->symbol|str|substring|trim|scope->list|string->fs-path|string->cmd-path|string->dir|subpath|path-name|path-stem|with-image|with-dir|with-args|with-cmd|with-stdin|with-env|with-insecure|with-label|with-port|with-tls|with-mount|thunk-cmd|thunk-args|resolve|start|addr|wait|read|cache-dir|binds\\?|recall-memo|store-memo|mask|list|list\\*|first|rest|length|second|third|map|map-pairs|foldr|foldl|append|filter|conj|list->scope|merge|apply|id|always|vals|keys|memo|succeeds\\?|run|last|take|take-all|insecure!|from|cd|wrap-cmd|mkfile|path-base|not)$")) + +(list . (symbol) @function.macro (#match? @function.macro "^(op|fn|current-scope|quote|let|provide|module|or|and|->|curryfn|for|\\$|linux)$")) +(cons . (symbol) @function.macro (#match? @function.macro "^(op|fn|current-scope|quote|let|provide|module|or|and|->|curryfn|for|\\$|linux)$")) + +(list . (symbol) @keyword.builtin (#match? @keyword.builtin "^(do|doc)$")) +(cons . (symbol) @keyword.builtin (#match? @keyword.builtin "^(do|doc)$")) + +(list . (symbol) @keyword.control.import (#match? @keyword.control.import "^(use|import|load)$")) +(cons . (symbol) @keyword.control.import (#match? @keyword.control.import "^(use|import|load)$")) + + +;; special cases + +; [a & b] highlights & as operator rather than a regular symbol +(list (symbol) @operator (#match? @operator "&")) +(cons (symbol) @operator (#match? @operator "&")) + +; (-> x y z) highlights first x as var, y z as function +(list + . + (symbol) @function.macro + (#eq? @function.macro "->") + . + (symbol) @variable.parameter + (symbol) @function) + +; (-> 42 x y) highlights 42 as regular number +(list + . + (symbol) @function.macro + (#eq? @function.macro "->") + . + (_) + (symbol) @function) + +;;; generic highlighting for all forms + +; first symbol in a list form is a combiner call +(list . (symbol) @function) + +; highlight symbols as vars only when they're clearly vars +(cons (symbol) @variable) +(scope (symbol) @variable) +(path form: (symbol) @variable) +(symbind form: (symbol) @variable) diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 76c38c99..127b931c 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -8,5 +8,6 @@ edition = "2021" [dependencies] helix-term = { version = "0.6", path = "../helix-term" } helix-core = { version = "0.6", path = "../helix-core" } +helix-view = { version = "0.6", path = "../helix-view" } helix-loader = { version = "0.6", path = "../helix-loader" } toml = "0.5" diff --git a/xtask/src/docgen.rs b/xtask/src/docgen.rs new file mode 100644 index 00000000..473882f3 --- /dev/null +++ b/xtask/src/docgen.rs @@ -0,0 +1,117 @@ +use crate::helpers; +use crate::path; +use crate::DynError; + +use helix_term::commands::TYPABLE_COMMAND_LIST; +use helix_term::health::TsFeature; +use std::fs; + +pub const TYPABLE_COMMANDS_MD_OUTPUT: &str = "typable-cmd.md"; +pub const LANG_SUPPORT_MD_OUTPUT: &str = "lang-support.md"; + +fn md_table_heading(cols: &[String]) -> String { + let mut header = String::new(); + header += &md_table_row(cols); + header += &md_table_row(&vec!["---".to_string(); cols.len()]); + header +} + +fn md_table_row(cols: &[String]) -> String { + format!("| {} |\n", cols.join(" | ")) +} + +fn md_mono(s: &str) -> String { + format!("`{}`", s) +} + +pub fn typable_commands() -> Result { + let mut md = String::new(); + md.push_str(&md_table_heading(&[ + "Name".to_owned(), + "Description".to_owned(), + ])); + + let cmdify = |s: &str| format!("`:{}`", s); + + for cmd in TYPABLE_COMMAND_LIST { + let names = std::iter::once(&cmd.name) + .chain(cmd.aliases.iter()) + .map(|a| cmdify(a)) + .collect::>() + .join(", "); + + let doc = cmd.doc.replace('\n', "
"); + + md.push_str(&md_table_row(&[names.to_owned(), doc.to_owned()])); + } + + Ok(md) +} + +pub fn lang_features() -> Result { + let mut md = String::new(); + let ts_features = TsFeature::all(); + + let mut cols = vec!["Language".to_owned()]; + cols.append( + &mut ts_features + .iter() + .map(|t| t.long_title().to_string()) + .collect::>(), + ); + cols.push("Default LSP".to_owned()); + + md.push_str(&md_table_heading(&cols)); + let config = helpers::lang_config(); + + let mut langs = config + .language + .iter() + .map(|l| l.language_id.clone()) + .collect::>(); + langs.sort_unstable(); + + let mut ts_features_to_langs = Vec::new(); + for &feat in ts_features { + ts_features_to_langs.push((feat, helpers::ts_lang_support(feat))); + } + + let mut row = Vec::new(); + for lang in langs { + let lc = config + .language + .iter() + .find(|l| l.language_id == lang) + .unwrap(); // lang comes from config + row.push(lc.language_id.clone()); + + for (_feat, support_list) in &ts_features_to_langs { + row.push( + if support_list.contains(&lang) { + "✓" + } else { + "" + } + .to_owned(), + ); + } + row.push( + lc.language_server + .as_ref() + .map(|s| s.command.clone()) + .map(|c| md_mono(&c)) + .unwrap_or_default(), + ); + + md.push_str(&md_table_row(&row)); + row.clear(); + } + + Ok(md) +} + +pub fn write(filename: &str, data: &str) { + let error = format!("Could not write to {}", filename); + let path = path::book_gen().join(filename); + fs::write(path, data).expect(&error); +} diff --git a/xtask/src/helpers.rs b/xtask/src/helpers.rs new file mode 100644 index 00000000..4f759e74 --- /dev/null +++ b/xtask/src/helpers.rs @@ -0,0 +1,44 @@ +use std::path::{Path, PathBuf}; + +use crate::path; +use helix_core::syntax::Configuration as LangConfig; +use helix_term::health::TsFeature; + +/// Get the list of languages that support a particular tree-sitter +/// based feature. +pub fn ts_lang_support(feat: TsFeature) -> Vec { + let queries_dir = path::ts_queries(); + + find_files(&queries_dir, feat.runtime_filename()) + .iter() + .map(|f| { + // .../helix/runtime/queries/python/highlights.scm + let tail = f.strip_prefix(&queries_dir).unwrap(); // python/highlights.scm + let lang = tail.components().next().unwrap(); // python + lang.as_os_str().to_string_lossy().to_string() + }) + .collect() +} + +// naive implementation, but suffices for our needs +pub fn find_files(dir: &Path, filename: &str) -> Vec { + std::fs::read_dir(dir) + .unwrap() + .filter_map(|entry| { + let path = entry.ok()?.path(); + if path.is_dir() { + Some(find_files(&path, filename)) + } else if path.file_name()?.to_string_lossy() == filename { + Some(vec![path]) + } else { + None + } + }) + .flatten() + .collect() +} + +pub fn lang_config() -> LangConfig { + let bytes = std::fs::read(path::lang_config()).unwrap(); + toml::from_slice(&bytes).unwrap() +} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index c7640cd1..1421fd1a 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -1,255 +1,35 @@ +mod docgen; +mod helpers; +mod path; +mod querycheck; +mod themelint; + use std::{env, error::Error}; type DynError = Box; -pub mod helpers { - use std::path::{Path, PathBuf}; - - use crate::path; - use helix_core::syntax::Configuration as LangConfig; - use helix_term::health::TsFeature; - - /// Get the list of languages that support a particular tree-sitter - /// based feature. - pub fn ts_lang_support(feat: TsFeature) -> Vec { - let queries_dir = path::ts_queries(); - - find_files(&queries_dir, feat.runtime_filename()) - .iter() - .map(|f| { - // .../helix/runtime/queries/python/highlights.scm - let tail = f.strip_prefix(&queries_dir).unwrap(); // python/highlights.scm - let lang = tail.components().next().unwrap(); // python - lang.as_os_str().to_string_lossy().to_string() - }) - .collect() - } - - /// Get the list of languages that have any form of tree-sitter - /// queries defined in the runtime directory. - pub fn langs_with_ts_queries() -> Vec { - std::fs::read_dir(path::ts_queries()) - .unwrap() - .filter_map(|entry| { - let entry = entry.ok()?; - entry - .file_type() - .ok()? - .is_dir() - .then(|| entry.file_name().to_string_lossy().to_string()) - }) - .collect() - } - - // naive implementation, but suffices for our needs - pub fn find_files(dir: &Path, filename: &str) -> Vec { - std::fs::read_dir(dir) - .unwrap() - .filter_map(|entry| { - let path = entry.ok()?.path(); - if path.is_dir() { - Some(find_files(&path, filename)) - } else { - (path.file_name()?.to_string_lossy() == filename).then(|| vec![path]) - } - }) - .flatten() - .collect() - } - - pub fn lang_config() -> LangConfig { - let bytes = std::fs::read(path::lang_config()).unwrap(); - toml::from_slice(&bytes).unwrap() - } -} - -pub mod md_gen { - use crate::DynError; - - use crate::helpers; - use crate::path; - use helix_term::commands::TYPABLE_COMMAND_LIST; - use helix_term::health::TsFeature; - use std::fs; - - pub const TYPABLE_COMMANDS_MD_OUTPUT: &str = "typable-cmd.md"; - pub const LANG_SUPPORT_MD_OUTPUT: &str = "lang-support.md"; - - fn md_table_heading(cols: &[String]) -> String { - let mut header = String::new(); - header += &md_table_row(cols); - header += &md_table_row(&vec!["---".to_string(); cols.len()]); - header - } - - fn md_table_row(cols: &[String]) -> String { - format!("| {} |\n", cols.join(" | ")) - } - - fn md_mono(s: &str) -> String { - format!("`{}`", s) - } - - pub fn typable_commands() -> Result { - let mut md = String::new(); - md.push_str(&md_table_heading(&[ - "Name".to_owned(), - "Description".to_owned(), - ])); - - let cmdify = |s: &str| format!("`:{}`", s); - - for cmd in TYPABLE_COMMAND_LIST { - let names = std::iter::once(&cmd.name) - .chain(cmd.aliases.iter()) - .map(|a| cmdify(a)) - .collect::>() - .join(", "); - - let doc = cmd.doc.replace('\n', "
"); - - md.push_str(&md_table_row(&[names.to_owned(), doc.to_owned()])); - } - - Ok(md) - } - - pub fn lang_features() -> Result { - let mut md = String::new(); - let ts_features = TsFeature::all(); - - let mut cols = vec!["Language".to_owned()]; - cols.append( - &mut ts_features - .iter() - .map(|t| t.long_title().to_string()) - .collect::>(), - ); - cols.push("Default LSP".to_owned()); - - md.push_str(&md_table_heading(&cols)); - let config = helpers::lang_config(); - - let mut langs = config - .language - .iter() - .map(|l| l.language_id.clone()) - .collect::>(); - langs.sort_unstable(); - - let mut ts_features_to_langs = Vec::new(); - for &feat in ts_features { - ts_features_to_langs.push((feat, helpers::ts_lang_support(feat))); - } - - let mut row = Vec::new(); - for lang in langs { - let lc = config - .language - .iter() - .find(|l| l.language_id == lang) - .unwrap(); // lang comes from config - row.push(lc.language_id.clone()); - - for (_feat, support_list) in &ts_features_to_langs { - row.push( - if support_list.contains(&lang) { - "✓" - } else { - "" - } - .to_owned(), - ); - } - row.push( - lc.language_server - .as_ref() - .map(|s| s.command.clone()) - .map(|c| md_mono(&c)) - .unwrap_or_default(), - ); - - md.push_str(&md_table_row(&row)); - row.clear(); - } - - Ok(md) - } - - pub fn write(filename: &str, data: &str) { - let error = format!("Could not write to {}", filename); - let path = path::book_gen().join(filename); - fs::write(path, data).expect(&error); - } -} - -pub mod path { - use std::path::{Path, PathBuf}; - - pub fn project_root() -> PathBuf { - Path::new(env!("CARGO_MANIFEST_DIR")) - .parent() - .unwrap() - .to_path_buf() - } - - pub fn book_gen() -> PathBuf { - project_root().join("book/src/generated/") - } - - pub fn ts_queries() -> PathBuf { - project_root().join("runtime/queries") - } - - pub fn lang_config() -> PathBuf { - project_root().join("languages.toml") - } -} - pub mod tasks { - use crate::md_gen; + use crate::docgen::{lang_features, typable_commands, write}; + use crate::docgen::{LANG_SUPPORT_MD_OUTPUT, TYPABLE_COMMANDS_MD_OUTPUT}; + use crate::querycheck::query_check; + use crate::themelint::{lint, lint_all}; use crate::DynError; pub fn docgen() -> Result<(), DynError> { - use md_gen::*; write(TYPABLE_COMMANDS_MD_OUTPUT, &typable_commands()?); write(LANG_SUPPORT_MD_OUTPUT, &lang_features()?); Ok(()) } - pub fn query_check() -> Result<(), String> { - use crate::helpers::lang_config; - use helix_core::{syntax::read_query, tree_sitter::Query}; - use helix_loader::grammar::get_language; - - let query_files = [ - "highlights.scm", - "locals.scm", - "injections.scm", - "textobjects.scm", - "indents.scm", - ]; - - for language in lang_config().language { - let language_name = language.language_id.to_ascii_lowercase(); - let grammar_name = language.grammar.unwrap_or(language.language_id); - for query_file in query_files { - let language = get_language(&grammar_name); - let query_text = read_query(&language_name, query_file); - if !query_text.is_empty() && language.is_ok() { - if let Err(reason) = Query::new(language.unwrap(), &query_text) { - return Err(format!( - "Failed to parse {} queries for {}: {}", - query_file, language_name, reason - )); - } - } - } + pub fn themelint(file: Option) -> Result<(), DynError> { + match file { + Some(file) => lint(file), + None => lint_all(), } + } - println!("Query check succeeded"); - - Ok(()) + pub fn querycheck() -> Result<(), DynError> { + query_check() } pub fn print_help() { @@ -259,6 +39,7 @@ Usage: Run with `cargo xtask `, eg. `cargo xtask docgen`. Tasks: docgen: Generate files to be included in the mdbook output. + themelint : Report errors for , or all themes if no theme is specified. query-check: Check that tree-sitter queries are valid. " ); @@ -271,7 +52,8 @@ fn main() -> Result<(), DynError> { None => tasks::print_help(), Some(t) => match t.as_str() { "docgen" => tasks::docgen()?, - "query-check" => tasks::query_check()?, + "themelint" => tasks::themelint(env::args().nth(2))?, + "query-check" => tasks::querycheck()?, invalid => return Err(format!("Invalid task name: {}", invalid).into()), }, }; diff --git a/xtask/src/path.rs b/xtask/src/path.rs new file mode 100644 index 00000000..6f4545c2 --- /dev/null +++ b/xtask/src/path.rs @@ -0,0 +1,24 @@ +use std::path::{Path, PathBuf}; + +pub fn project_root() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")) + .parent() + .unwrap() + .to_path_buf() +} + +pub fn book_gen() -> PathBuf { + project_root().join("book/src/generated/") +} + +pub fn ts_queries() -> PathBuf { + project_root().join("runtime/queries") +} + +pub fn lang_config() -> PathBuf { + project_root().join("languages.toml") +} + +pub fn themes() -> PathBuf { + project_root().join("runtime/themes") +} diff --git a/xtask/src/querycheck.rs b/xtask/src/querycheck.rs new file mode 100644 index 00000000..5595b8ec --- /dev/null +++ b/xtask/src/querycheck.rs @@ -0,0 +1,39 @@ +use crate::DynError; + +pub fn query_check() -> Result<(), DynError> { + use crate::helpers::lang_config; + use helix_core::{syntax::read_query, tree_sitter::Query}; + use helix_loader::grammar::get_language; + + let query_files = [ + "highlights.scm", + "locals.scm", + "injections.scm", + "textobjects.scm", + "indents.scm", + ]; + + for language in lang_config().language { + let language_name = language.language_id.to_ascii_lowercase(); + let grammar_name = language.grammar.unwrap_or(language.language_id); + for query_file in query_files { + let language = get_language(&grammar_name); + let query_text = read_query(&language_name, query_file); + if let Ok(lang) = language { + if !query_text.is_empty() { + if let Err(reason) = Query::new(lang, &query_text) { + return Err(format!( + "Failed to parse {} queries for {}: {}", + query_file, language_name, reason + ) + .into()); + } + } + } + } + } + + println!("Query check succeeded"); + + Ok(()) +} diff --git a/xtask/src/themelint.rs b/xtask/src/themelint.rs new file mode 100644 index 00000000..26d1cb04 --- /dev/null +++ b/xtask/src/themelint.rs @@ -0,0 +1,199 @@ +use crate::path; +use crate::DynError; +use helix_view::theme::Loader; +use helix_view::theme::Modifier; +use helix_view::Theme; + +struct Rule { + fg: Option<&'static str>, + bg: Option<&'static str>, + check_both: bool, +} + +enum Require { + Existence(Rule), + Difference(&'static str, &'static str), +} + +// Placed in an fn here, so it's the first thing you see +fn get_rules() -> Vec { + vec![ + // Check for ui.selection, which is required + Require::Existence(Rule::has_either("ui.selection")), + Require::Existence(Rule::has_either("ui.selection.primary")), + Require::Difference("ui.selection", "ui.selection.primary"), + // Check for planned readable text + Require::Existence(Rule::has_fg("ui.text")), + Require::Existence(Rule::has_bg("ui.background")), + // Check for complete editor.statusline bare minimum + Require::Existence(Rule::has_both("ui.statusline")), + Require::Existence(Rule::has_both("ui.statusline.inactive")), + // Check for editor.color-modes + Require::Existence(Rule::has_either("ui.statusline.normal")), + Require::Existence(Rule::has_either("ui.statusline.insert")), + Require::Existence(Rule::has_either("ui.statusline.select")), + Require::Difference("ui.statusline.normal", "ui.statusline.insert"), + Require::Difference("ui.statusline.normal", "ui.statusline.select"), + // Check for editor.cursorline + Require::Existence(Rule::has_bg("ui.cursorline.primary")), + // Check for editor.whitespace + Require::Existence(Rule::has_fg("ui.virtual.whitespace")), + // Check fir rulers + Require::Existence(Rule::has_either("ui.virtual.indent-guide")), + // Check for editor.rulers + Require::Existence(Rule::has_either("ui.virtual.ruler")), + // Check for menus and prompts + Require::Existence(Rule::has_both("ui.menu")), + Require::Existence(Rule::has_both("ui.help")), + Require::Existence(Rule::has_bg("ui.popup")), + Require::Existence(Rule::has_either("ui.window")), + // Check for visible cursor + Require::Existence(Rule::has_bg("ui.cursor.primary")), + Require::Existence(Rule::has_either("ui.cursor.match")), + ] +} + +impl Rule { + fn has_bg(bg: &'static str) -> Rule { + Rule { + fg: None, + bg: Some(bg), + check_both: true, + } + } + fn has_fg(fg: &'static str) -> Rule { + Rule { + fg: Some(fg), + bg: None, + check_both: true, + } + } + fn has_either(item: &'static str) -> Rule { + Rule { + fg: Some(item), + bg: Some(item), + check_both: false, + } + } + fn has_both(item: &'static str) -> Rule { + Rule { + fg: Some(item), + bg: Some(item), + check_both: true, + } + } + fn found_fg(&self, theme: &Theme) -> bool { + if let Some(fg) = &self.fg { + if theme.get(fg).fg.is_none() && theme.get(fg).add_modifier == Modifier::empty() { + return false; + } + } + true + } + fn found_bg(&self, theme: &Theme) -> bool { + if let Some(bg) = &self.bg { + if theme.get(bg).bg.is_none() && theme.get(bg).add_modifier == Modifier::empty() { + return false; + } + } + true + } + fn rule_name(&self) -> &'static str { + if self.fg.is_some() { + self.fg.unwrap() + } else if self.bg.is_some() { + self.bg.unwrap() + } else { + "LINTER_ERROR_NO_RULE" + } + } + + fn check_difference( + theme: &Theme, + a: &'static str, + b: &'static str, + messages: &mut Vec, + ) { + let theme_a = theme.get(a); + let theme_b = theme.get(b); + if theme_a == theme_b { + messages.push(format!("$THEME: `{}` and `{}` cannot be equal", a, b)); + } + } + + fn check_existence(rule: &Rule, theme: &Theme, messages: &mut Vec) { + let found_fg = rule.found_fg(theme); + let found_bg = rule.found_bg(theme); + + if !rule.check_both && (found_fg || found_bg) { + return; + } + if !found_fg || !found_bg { + let mut missing = vec![]; + if !found_fg { + missing.push("`fg`"); + } + if !found_bg { + missing.push("`bg`"); + } + let entry = if !rule.check_both && !found_fg && !found_bg { + missing.join(" or ") + } else { + missing.join(" and ") + }; + messages.push(format!( + "$THEME: missing {} for `{}`", + entry, + rule.rule_name() + )) + } + } +} + +pub fn lint(file: String) -> Result<(), DynError> { + if file.contains("base16") { + println!("Skipping base16: {}", file); + return Ok(()); + } + let path = path::themes().join(file.clone() + ".toml"); + let theme = std::fs::read(&path).unwrap(); + let theme: Theme = toml::from_slice(&theme).expect("Failed to parse theme"); + + let mut messages: Vec = vec![]; + get_rules().iter().for_each(|lint| match lint { + Require::Existence(rule) => Rule::check_existence(rule, &theme, &mut messages), + Require::Difference(a, b) => Rule::check_difference(&theme, a, b, &mut messages), + }); + + if !messages.is_empty() { + messages.iter().for_each(|m| { + let theme = file.clone(); + let message = m.replace("$THEME", theme.as_str()); + println!("{}", message); + }); + Err(format!("{} has issues", file.clone().as_str()).into()) + } else { + Ok(()) + } +} + +pub fn lint_all() -> Result<(), DynError> { + let files = Loader::read_names(path::themes().as_path()); + let files_count = files.len(); + let ok_files_count = files + .into_iter() + .filter_map(|path| lint(path.replace(".toml", "")).ok()) + .collect::>() + .len(); + + if files_count != ok_files_count { + Err(format!( + "{} of {} themes had issues", + files_count - ok_files_count, + files_count + ) + .into()) + } else { + Ok(()) + } +}