diff --git a/Cargo.lock b/Cargo.lock index f316ed7..6b6a8e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -210,7 +210,7 @@ version = "4.5.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "54b755194d6389280185988721fffba69495eed5ee9feeee9a599b53db80318c" dependencies = [ - "heck 0.5.0", + "heck", "proc-macro2", "quote", "syn", @@ -283,13 +283,6 @@ dependencies = [ "ureq", ] -[[package]] -name = "codebook-zed" -version = "0.1.0" -dependencies = [ - "zed_extension_api", -] - [[package]] name = "codebook_config" version = "0.1.0" @@ -538,15 +531,6 @@ dependencies = [ "foldhash", ] -[[package]] -name = "heck" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" -dependencies = [ - "unicode-segmentation", -] - [[package]] name = "heck" version = "0.5.0" @@ -683,12 +667,6 @@ dependencies = [ "syn", ] -[[package]] -name = "id-arena" -version = "2.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25a2bc672d1148e28034f176e01fffebb08b35768468cc954630da77a1449005" - [[package]] name = "idna" version = "1.0.3" @@ -718,7 +696,6 @@ checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f" dependencies = [ "equivalent", "hashbrown 0.15.2", - "serde", ] [[package]] @@ -739,12 +716,6 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" -[[package]] -name = "leb128" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "884e2677b40cc8c339eaefcb701c32ef1fd2493d71118dc0ca4b6a736c93bd67" - [[package]] name = "libc" version = "0.2.169" @@ -1037,12 +1008,6 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" -[[package]] -name = "semver" -version = "1.0.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3cb6eb87a131f756572d7fb904f6e7b68633f09cca868c5df1c4b8d1a694bbba" - [[package]] name = "serde" version = "1.0.217" @@ -1135,15 +1100,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "spdx" -version = "0.10.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "58b69356da67e2fc1f542c71ea7e654a361a79c938e4424392ecf4fa065d2193" -dependencies = [ - "smallvec", -] - [[package]] name = "spellbook" version = "0.2.0" @@ -1502,18 +1458,6 @@ version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" -[[package]] -name = "unicode-segmentation" -version = "1.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" - -[[package]] -name = "unicode-xid" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" - [[package]] name = "untrusted" version = "0.9.0" @@ -1578,42 +1522,6 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" -[[package]] -name = "wasm-encoder" -version = "0.201.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9c7d2731df60006819b013f64ccc2019691deccf6e11a1804bc850cd6748f1a" -dependencies = [ - "leb128", -] - -[[package]] -name = "wasm-metadata" -version = "0.201.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fd83062c17b9f4985d438603cde0a5e8c5c8198201a6937f778b607924c7da2" -dependencies = [ - "anyhow", - "indexmap", - "serde", - "serde_derive", - "serde_json", - "spdx", - "wasm-encoder", - "wasmparser", -] - -[[package]] -name = "wasmparser" -version = "0.201.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84e5df6dba6c0d7fafc63a450f1738451ed7a0b52295d83e868218fa286bf708" -dependencies = [ - "bitflags 2.8.0", - "indexmap", - "semver", -] - [[package]] name = "webpki-roots" version = "0.26.7" @@ -1736,98 +1644,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "wit-bindgen" -version = "0.22.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "288f992ea30e6b5c531b52cdd5f3be81c148554b09ea416f058d16556ba92c27" -dependencies = [ - "bitflags 2.8.0", - "wit-bindgen-rt", - "wit-bindgen-rust-macro", -] - -[[package]] -name = "wit-bindgen-core" -version = "0.22.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e85e72719ffbccf279359ad071497e47eb0675fe22106dea4ed2d8a7fcb60ba4" -dependencies = [ - "anyhow", - "wit-parser", -] - -[[package]] -name = "wit-bindgen-rt" -version = "0.22.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fcb8738270f32a2d6739973cbbb7c1b6dd8959ce515578a6e19165853272ee64" - -[[package]] -name = "wit-bindgen-rust" -version = "0.22.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8a39a15d1ae2077688213611209849cad40e9e5cccf6e61951a425850677ff3" -dependencies = [ - "anyhow", - "heck 0.4.1", - "indexmap", - "wasm-metadata", - "wit-bindgen-core", - "wit-component", -] - -[[package]] -name = "wit-bindgen-rust-macro" -version = "0.22.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d376d3ae5850526dfd00d937faea0d81a06fa18f7ac1e26f386d760f241a8f4b" -dependencies = [ - "anyhow", - "proc-macro2", - "quote", - "syn", - "wit-bindgen-core", - "wit-bindgen-rust", -] - -[[package]] -name = "wit-component" -version = "0.201.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "421c0c848a0660a8c22e2fd217929a0191f14476b68962afd2af89fd22e39825" -dependencies = [ - "anyhow", - "bitflags 2.8.0", - "indexmap", - "log", - "serde", - "serde_derive", - "serde_json", - "wasm-encoder", - "wasm-metadata", - "wasmparser", - "wit-parser", -] - -[[package]] -name = "wit-parser" -version = "0.201.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "196d3ecfc4b759a8573bf86a9b3f8996b304b3732e4c7de81655f875f6efdca6" -dependencies = [ - "anyhow", - "id-arena", - "indexmap", - "log", - "semver", - "serde", - "serde_derive", - "serde_json", - "unicode-xid", - "wasmparser", -] - [[package]] name = "write16" version = "1.0.0" @@ -1864,17 +1680,6 @@ dependencies = [ "synstructure", ] -[[package]] -name = "zed_extension_api" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9fd16b8b30a9dc920fc1678ff852f696b5bdf5b5843bc745a128be0aac29859e" -dependencies = [ - "serde", - "serde_json", - "wit-bindgen", -] - [[package]] name = "zerocopy" version = "0.7.35" diff --git a/Cargo.toml b/Cargo.toml index c2054c5..004a9b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["codebook", "codebook-config", "codebook-lsp", "codebook-zed"] +members = ["codebook", "codebook-config", "codebook-lsp"] resolver = "2" [profile.test] diff --git a/codebook-config/src/lib.rs b/codebook-config/src/lib.rs index 49de660..6a005c0 100644 --- a/codebook-config/src/lib.rs +++ b/codebook-config/src/lib.rs @@ -95,11 +95,7 @@ impl CodebookConfig { } pub fn new_no_file() -> Self { - Self { - settings: Arc::new(RwLock::new(ConfigSettings::default())), - config_path: None, - cache_dir: env::temp_dir().join(CACHE_DIR), - } + Self::default() } pub fn reload(&self) -> Result<()> { diff --git a/codebook-lsp/src/lsp.rs b/codebook-lsp/src/lsp.rs index 76f64d1..69dc54c 100644 --- a/codebook-lsp/src/lsp.rs +++ b/codebook-lsp/src/lsp.rs @@ -10,7 +10,7 @@ use tower_lsp::{Client, LanguageServer}; use codebook::Codebook; use codebook_config::CodebookConfig; -use log::info; +use log::{debug, info}; use crate::file_cache::{TextDocumentCache, TextDocumentCacheItem}; @@ -153,10 +153,8 @@ impl LanguageServer for Backend { } impl Backend { - pub fn new(client: Client, cache_dir: &PathBuf, workspace_dir: &PathBuf) -> Self { - let mut config = - CodebookConfig::load_from_dir(workspace_dir).expect("Unable to make config."); - config.cache_dir = cache_dir.clone(); + pub fn new(client: Client, workspace_dir: &PathBuf) -> Self { + let config = CodebookConfig::load_from_dir(workspace_dir).expect("Unable to make config."); let config_arc = Arc::new(config); let codebook = Codebook::new(Arc::clone(&config_arc)).expect("Unable to make codebook"); Self { @@ -259,12 +257,12 @@ impl Backend { }) .collect(); - info!("Diagnostics: {:?}", diagnostics); + debug!("Diagnostics: {:?}", diagnostics); // 3) Send the diagnostics to the client. self.client .publish_diagnostics(uri, diagnostics, None) .await; - info!("Published diagnostics for: {:?}", file_path); + debug!("Published diagnostics for: {:?}", file_path); } fn spell_check( diff --git a/codebook-lsp/src/main.rs b/codebook-lsp/src/main.rs index eca5261..3f55b54 100644 --- a/codebook-lsp/src/main.rs +++ b/codebook-lsp/src/main.rs @@ -10,11 +10,8 @@ use tower_lsp::{LspService, Server}; #[derive(Parser)] #[command(version, about, long_about = None, arg_required_else_help = true)] struct Cli { - /// Sets a custom config file - #[arg(short, long, value_name = "FOLDER")] - cache_dir: Option, - - /// Root of the workspace/project being checked + /// Root of the workspace/project being checked. + /// This may or may not have a codebook.toml file. #[arg(short, long, value_name = "FOLDER")] root: Option, @@ -34,11 +31,6 @@ async fn main() { env_logger::init(); let cli = Cli::parse(); - let cache_dir = match cli.cache_dir.as_deref() { - Some(path) => path, - None => Path::new(".cache/dictionaries/"), - }; - let root = match cli.root.as_deref() { Some(path) => path, None => Path::new("."), @@ -46,15 +38,15 @@ async fn main() { match &cli.command { Some(Commands::Serve {}) => { - serve_lsp(&cache_dir.to_path_buf(), &root.to_path_buf()).await; + serve_lsp(&root.to_path_buf()).await; } None => {} } } -async fn serve_lsp(cache_dir: &PathBuf, root: &PathBuf) { - info!("Starting SpellCheck Language Server..."); +async fn serve_lsp(root: &PathBuf) { + info!("Starting Codebook Language Server..."); let (stdin, stdout) = (tokio::io::stdin(), tokio::io::stdout()); - let (service, socket) = LspService::new(|client| Backend::new(client, cache_dir, root)); + let (service, socket) = LspService::new(|client| Backend::new(client, root)); Server::new(stdin, stdout, socket).serve(service).await; } diff --git a/codebook-zed/Cargo.toml b/codebook-zed/Cargo.toml deleted file mode 100644 index 9d7eeee..0000000 --- a/codebook-zed/Cargo.toml +++ /dev/null @@ -1,14 +0,0 @@ -[package] -name = "codebook-zed" -version = "0.1.0" -edition = "2021" -authors = ["Bo"] -description = "Codebook: A Zed extension for spellchecking." - -[dependencies] -zed_extension_api = "<1" - - -[lib] -path = "src/lib.rs" -crate-type = ["cdylib"] diff --git a/codebook-zed/extension.toml b/codebook-zed/extension.toml deleted file mode 100644 index e2f626e..0000000 --- a/codebook-zed/extension.toml +++ /dev/null @@ -1,25 +0,0 @@ -id = "codebook" -name = "Codebook" -version = "0.1.0" -description = "Zed extension for codebook." -authors = ["Bo"] -license = "MIT" -schema_version = 1 -# repository = "https://github.com/your-name/my-zed-extension" - -[language_servers.codebook] -name = "codebook" -languages = [ - "CSS", - "Go", - "HTML", - "JavaScript React", - "JavaScript", - "Markdown", - "Plain Text", - "Python", - "Rust", - "TOML", - "TypeScript React", - "TypeScript", -] diff --git a/codebook-zed/src/lib.rs b/codebook-zed/src/lib.rs deleted file mode 100644 index 1044bc4..0000000 --- a/codebook-zed/src/lib.rs +++ /dev/null @@ -1,183 +0,0 @@ -use std::fs; -use std::path::PathBuf; -use zed_extension_api::{self as zed, Result}; - -const EXTENSION_LSP_NAME: &str = "codebook-lsp"; - -struct CodebookExtension { - binary_cache: Option, -} - -#[derive(Clone)] -struct CodebookBinary { - path: PathBuf, - env: Option>, -} - -impl CodebookExtension { - fn new() -> Self { - Self { binary_cache: None } - } - - fn get_binary( - &mut self, - language_server_id: &zed::LanguageServerId, - worktree: &zed::Worktree, - ) -> Result { - let dev_path = PathBuf::from(EXTENSION_LSP_NAME); - if dev_path.exists() { - return Ok(CodebookBinary { - path: dev_path, - env: Some(vec![("RUST_LOG".to_string(), "debug".to_string())]), - }); - } - - if let Some(path) = worktree.which(EXTENSION_LSP_NAME) { - return Ok(CodebookBinary { - path: PathBuf::from(path), - env: Some(worktree.shell_env()), - }); - } - - if let Some(path) = &self.binary_cache { - if path.exists() { - return Ok(CodebookBinary { - path: path.clone(), - env: None, - }); - } - } - - self.install_binary(language_server_id) - } - - fn install_binary( - &mut self, - language_server_id: &zed::LanguageServerId, - ) -> Result { - zed::set_language_server_installation_status( - language_server_id, - &zed::LanguageServerInstallationStatus::CheckingForUpdate, - ); - - let release = zed::latest_github_release( - "blopker/codebook", - zed::GithubReleaseOptions { - require_assets: true, - pre_release: false, - }, - ) - .map_err(|e| format!("Failed to fetch latest release: {}", e))?; - - let (platform, arch) = zed::current_platform(); - let arch_name = match arch { - zed::Architecture::Aarch64 => "aarch64", - zed::Architecture::X8664 => "x86_64", - zed::Architecture::X86 => return Err("x86 architecture is not supported".into()), - }; - - let (os_str, file_ext) = match platform { - zed::Os::Mac => ("apple-darwin", "tar.gz"), - zed::Os::Linux => ("unknown-linux-gnu", "tar.gz"), - zed::Os::Windows => ("pc-windows-msvc", "zip"), - }; - - let asset_name = format!("{EXTENSION_LSP_NAME}-{arch_name}-{os_str}.{file_ext}"); - let asset = release - .assets - .iter() - .find(|a| a.name == asset_name) - .ok_or_else(|| { - format!("No compatible Codebook binary found for {arch_name}-{os_str}") - })?; - - let version_dir = format!("{EXTENSION_LSP_NAME}-{}", release.version); - let mut binary_path = PathBuf::from(&version_dir).join(EXTENSION_LSP_NAME); - - if platform == zed::Os::Windows { - binary_path.set_extension("exe"); - } - - if !binary_path.exists() { - zed::set_language_server_installation_status( - language_server_id, - &zed::LanguageServerInstallationStatus::Downloading, - ); - - let download_result = (|| -> Result<()> { - zed::download_file( - &asset.download_url, - &version_dir, - if platform == zed::Os::Windows { - zed::DownloadedFileType::Zip - } else { - zed::DownloadedFileType::GzipTar - }, - ) - .map_err(|e| format!("Failed to download Codebook binary: {}", e))?; - - zed::make_file_executable(binary_path.to_str().ok_or("Invalid binary path")?) - .map_err(|e| format!("Failed to make binary executable: {}", e))?; - - Ok(()) - })(); - - if let Err(e) = download_result { - fs::remove_dir_all(&version_dir).ok(); - return Err(e); - } - - if let Ok(entries) = fs::read_dir(".") { - for entry in entries.flatten() { - if let Ok(name) = entry.file_name().into_string() { - if name != version_dir { - fs::remove_dir_all(entry.path()).ok(); - } - } - } - } - } - - self.binary_cache = Some(binary_path.clone()); - Ok(CodebookBinary { - path: binary_path, - env: None, - }) - } -} - -impl zed::Extension for CodebookExtension { - fn new() -> Self { - Self::new() - } - - fn language_server_command( - &mut self, - language_server_id: &zed::LanguageServerId, - worktree: &zed::Worktree, - ) -> Result { - let binary = self.get_binary(language_server_id, worktree)?; - let current_working_dir = std::env::current_dir() - .map_err(|e| format!("Failed to get current working directory: {}", e))?; - let cache_dir = current_working_dir.join(".cache"); - let cache_dir = cache_dir - .to_str() - .ok_or("Failed to convert cache dir to string")?; - let project_path = worktree.root_path(); - Ok(zed::Command { - command: binary - .path - .to_str() - .ok_or("Failed to convert binary path to string")? - .to_string(), - args: vec![ - format!("--cache-dir={cache_dir}"), - format!("--root={project_path}"), - "serve".to_string(), - ], - env: binary.env.unwrap_or_default(), - }) - } -} - -zed::register_extension!(CodebookExtension); diff --git a/codebook/src/dictionary.rs b/codebook/src/dictionary.rs index 6c1e53c..294421c 100644 --- a/codebook/src/dictionary.rs +++ b/codebook/src/dictionary.rs @@ -87,10 +87,10 @@ impl CodeDictionary { } pub fn suggest(&self, word: &str) -> Vec { - info!("Checking Cache: {:?}", word); + debug!("Checking Cache: {:?}", word); // First try to get from cache with write lock since get() needs to modify LRU order if let Some(suggestions) = self.suggestion_cache.write().unwrap().get_mut(word) { - info!("Cache hit for {:?}", word); + debug!("Cache hit for {:?}", word); return suggestions.clone(); } @@ -263,10 +263,10 @@ impl CodeDictionary { let current_line = node_start.row as u32; let current_column = node_start.column as u32; let words = self.get_words_from_text(node_text); - info!("Found Capture:: {node_text:?}"); - info!("Words:: {words:?}"); - info!("Column: {current_column}"); - info!("Line: {current_line}"); + debug!("Found Capture:: {node_text:?}"); + debug!("Words:: {words:?}"); + debug!("Column: {current_column}"); + debug!("Line: {current_line}"); for (word_text, (text_start_char, text_line)) in words { info!("Checking: {:?}", word_text); if !self.check(&word_text) { diff --git a/codebook/src/splitter.rs b/codebook/src/splitter.rs index de23476..c07463d 100644 --- a/codebook/src/splitter.rs +++ b/codebook/src/splitter.rs @@ -112,7 +112,7 @@ pub fn find_url_end(text: &str) -> Option<(usize, usize)> { #[cfg(test)] mod tests { - use log::info; + use log::debug; use super::*; @@ -190,7 +190,7 @@ mod tests { assert!(find_url_end(text).is_none()); let text = "://example.com/path/to/file.html)not a url"; let (start, end) = find_url_end(text).unwrap(); - info!("URL: {}", &text[start..end]); + debug!("URL: {}", &text[start..end]); assert_eq!(&text[start..end], "://example.com/path/to/file.html"); } }