From adb89df0d7553110ea5beb9bac425a52d42180db Mon Sep 17 00:00:00 2001 From: Gerry Agbobada Date: Wed, 20 Mar 2024 22:33:05 +0100 Subject: [PATCH] Part 1 of compilation fixes Upgrade to emacs crate 0.19 Fix all conversions to/from Lisp except the Vec <-> Vector equivalence Apparently one reference to env keeps escaping --- Cargo.lock | 275 +------------------------ Cargo.toml | 2 +- src/emacs_wrapper.rs | 115 +++++++---- src/types.rs | 467 +++++++++++++++++++++---------------------- 4 files changed, 318 insertions(+), 541 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cb988fb..19b6ddf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,123 +2,30 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "aho-corasick" -version = "0.7.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5" -dependencies = [ - "memchr", -] - -[[package]] -name = "ansi_term" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" -dependencies = [ - "winapi", -] - [[package]] name = "anyhow" version = "1.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "afddf7f520a80dbf76e6f50a35bca42a2331ef227a28b3b6dc5c2e2338d114b1" -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi", - "libc", - "winapi", -] - [[package]] name = "base-x" version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4521f3e3d031370679b3b140beb36dfe4801b09ac77e30c61941f97df3ef28b" -[[package]] -name = "bindgen" -version = "0.56.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2da379dbebc0b76ef63ca68d8fc6e71c0f13e59432e0987e508c1820e6ab5239" -dependencies = [ - "bitflags", - "cexpr", - "clang-sys", - "clap", - "env_logger", - "lazy_static", - "lazycell", - "log", - "peeking_take_while", - "proc-macro2", - "quote", - "regex", - "rustc-hash", - "shlex", - "which", -] - -[[package]] -name = "bitflags" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" - [[package]] name = "bumpalo" version = "3.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535" -[[package]] -name = "cexpr" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4aedb84272dbe89af497cf81375129abda4fc0a9e7c5d317498c15cc30c0d27" -dependencies = [ - "nom", -] - [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "clang-sys" -version = "1.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f54d78e30b388d4815220c8dd03fea5656b6c6d32adb59e89061552a102f8da1" -dependencies = [ - "glob", - "libc", - "libloading", -] - -[[package]] -name = "clap" -version = "2.33.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" -dependencies = [ - "ansi_term", - "atty", - "bitflags", - "strsim 0.8.0", - "textwrap", - "unicode-width", - "vec_map", -] - [[package]] name = "ctor" version = "0.1.19" @@ -149,7 +56,7 @@ dependencies = [ "ident_case", "proc-macro2", "quote", - "strsim 0.9.3", + "strsim", "syn", ] @@ -172,9 +79,9 @@ checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0" [[package]] name = "emacs" -version = "0.16.2" +version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a5b60e3348d9d0939ee3c33b3d146bf9fa4af87859710d35ad562a9968fc863" +checksum = "9a773f2eeb842fea9f9e52f8360dbf7c0c54af0c904e025c9e08db5053b8718c" dependencies = [ "anyhow", "ctor", @@ -187,9 +94,9 @@ dependencies = [ [[package]] name = "emacs-macros" -version = "0.15.1" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b31160ef111ecc0fcee57fe01740df4c31b7d077c2bb258515aefdc2dd3e477" +checksum = "69656fdfe7c2608b87164964db848b5c3795de7302e3130cce7131552c6be161" dependencies = [ "darling", "proc-macro2", @@ -199,25 +106,9 @@ dependencies = [ [[package]] name = "emacs_module" -version = "0.16.2" +version = "0.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3eebdd29085b1283edb5e83cc56426a4dd2a23d1c7b1f909c8b8caf700cc1869" -dependencies = [ - "bindgen", -] - -[[package]] -name = "env_logger" -version = "0.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17392a012ea30ef05a610aa97dfb49496e71c9f676b27879922ea5bdf60d9d3f" -dependencies = [ - "atty", - "humantime", - "log", - "regex", - "termcolor", -] +checksum = "b3067bc974045ed2c6db333bd4fc30d3bdaafa6421a9a889fa7b2826b6f7f2fa" [[package]] name = "fnv" @@ -234,27 +125,6 @@ dependencies = [ "unicode-width", ] -[[package]] -name = "glob" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" - -[[package]] -name = "hermit-abi" -version = "0.1.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" -dependencies = [ - "libc", -] - -[[package]] -name = "humantime" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" - [[package]] name = "ident_case" version = "1.0.1" @@ -273,28 +143,12 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" -[[package]] -name = "lazycell" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" - [[package]] name = "libc" version = "0.2.87" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "265d751d31d6780a3f956bb5b8022feba2d94eeee5a84ba64f4212eedca42213" -[[package]] -name = "libloading" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f84d96438c15fcd6c3f244c8fce01d1e2b9c6b5623e9c711dc9286d8fc92d6a" -dependencies = [ - "cfg-if", - "winapi", -] - [[package]] name = "log" version = "0.4.14" @@ -304,22 +158,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "memchr" -version = "2.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" - -[[package]] -name = "nom" -version = "5.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffb4262d26ed83a1c0a33a38fe2bb15797329c85770da05e6b828ddb782627af" -dependencies = [ - "memchr", - "version_check", -] - [[package]] name = "once_cell" version = "1.7.2" @@ -342,12 +180,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "peeking_take_while" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" - [[package]] name = "proc-macro2" version = "1.0.24" @@ -366,30 +198,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "regex" -version = "1.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9251239e129e16308e70d853559389de218ac275b515068abc96829d05b948a" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", - "thread_local", -] - -[[package]] -name = "regex-syntax" -version = "0.6.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5eb417147ba9860a96cfe72a0b93bf88fee1744b5636ec99ab20c1aa9376581" - -[[package]] -name = "rustc-hash" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" - [[package]] name = "rustc_version" version = "0.2.3" @@ -454,12 +262,6 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" -[[package]] -name = "shlex" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2" - [[package]] name = "stdweb" version = "0.4.20" @@ -511,12 +313,6 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0" -[[package]] -name = "strsim" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" - [[package]] name = "strsim" version = "0.9.3" @@ -534,24 +330,6 @@ dependencies = [ "unicode-xid", ] -[[package]] -name = "termcolor" -version = "1.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" -dependencies = [ - "winapi-util", -] - -[[package]] -name = "textwrap" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" -dependencies = [ - "unicode-width", -] - [[package]] name = "thiserror" version = "1.0.24" @@ -572,15 +350,6 @@ dependencies = [ "syn", ] -[[package]] -name = "thread_local" -version = "1.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180" -dependencies = [ - "once_cell", -] - [[package]] name = "unicode-segmentation" version = "1.7.1" @@ -599,18 +368,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" -[[package]] -name = "vec_map" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" - -[[package]] -name = "version_check" -version = "0.9.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" - [[package]] name = "wasm-bindgen" version = "0.2.71" @@ -665,15 +422,6 @@ version = "0.2.71" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d6f8ec44822dd71f5f221a5847fb34acd9060535c1211b70a05844c0f6383b1" -[[package]] -name = "which" -version = "3.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d011071ae14a2f6671d0b74080ae0cd8ebf3a6f8c9589a2cd45f23126fe29724" -dependencies = [ - "libc", -] - [[package]] name = "winapi" version = "0.3.9" @@ -690,15 +438,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" -[[package]] -name = "winapi-util" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" -dependencies = [ - "winapi", -] - [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index d4439a1..b10e2ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ path = "src/main.rs" getopts = "0.2" libc = "0.2.39" serde = "1.0" -emacs = {version = "0.16.2", optional = true} +emacs = {version = "0.19", optional = true} serde_json = "1.0" serde_derive = "1.0" unicode-segmentation = "1.1.0" diff --git a/src/emacs_wrapper.rs b/src/emacs_wrapper.rs index 440f060..acacfb5 100644 --- a/src/emacs_wrapper.rs +++ b/src/emacs_wrapper.rs @@ -1,5 +1,5 @@ use super::parinfer::rc_process; -use emacs::{Env, IntoLisp, Result, Value}; +use emacs::{Env, FromLisp, IntoLisp, Result, Value, Vector}; use types::{Change, Error, Options, Request, SharedRequest, WrappedAnswer}; use std::{cell::RefCell, convert::TryFrom, fs::OpenOptions, io::Write, rc::Rc}; @@ -133,12 +133,12 @@ fn new_options( selection_start_line: to_usize(selection_start_line), changes: changes.clone(), prev_text: None, - ..old_options + ..old_options.clone() }) } emacs::define_errors! { - unknown_option_error "This option name is unknown should not be negative" (error) + unknown_option_error "This option name is unknown" (error) } #[defun(user_ptr, mod_in_name = false)] @@ -167,48 +167,78 @@ fn set_option<'a>( new_value: Option>, ) -> Result<()> { let env = option_name.env; - if env.eq(option_name, env.intern("partial-result")?) { - options.partial_result = new_value.into_rust()?; + if option_name.eq(env.intern("partial-result")?) { + options.partial_result = new_value + .map(|val| val.is_not_nil()) + .unwrap_or_else(Options::default_false); return Ok(()); } - if env.eq(option_name, env.intern("force-balance")?) { - options.force_balance = new_value.into_rust()?; + if option_name.eq(env.intern("force-balance")?) { + options.force_balance = new_value + .map(|val| val.is_not_nil()) + .unwrap_or_else(Options::default_false); return Ok(()); } - if env.eq(option_name, env.intern("return-parens")?) { - options.return_parens = new_value.into_rust()?; + if option_name.eq(env.intern("return-parens")?) { + options.return_parens = new_value + .map(|val| val.is_not_nil()) + .unwrap_or_else(Options::default_false); return Ok(()); } - if env.eq(option_name, env.intern("comment-char")?) { - options.comment_char = new_value.into_rust()?; + if option_name.eq(env.intern("comment-char")?) { + options.comment_char = new_value + .map(|val| String::from_lisp(val)) + .transpose()? + .map(|char_as_str| char_as_str.chars().next()) + .flatten() + .unwrap_or_else(Options::default_comment); return Ok(()); } - if env.eq(option_name, env.intern("string-delimiters")?) { - options.string_delimiters = new_value.into_rust()?; + if option_name.eq(env.intern("string-delimiters")?) { + if let Some(new_value) = new_value { + let vector = Vector::from_lisp(new_value)?; + let rust_values = vector + .into_iter() + .map(|inner_value| String::from_lisp(inner_value)) + .collect::>>()?; + options.string_delimiters = rust_values; + } else { + options.string_delimiters = Options::default_string_delimiters(); + } return Ok(()); } - if env.eq(option_name, env.intern("lisp-vline-symbols")?) { - options.lisp_vline_symbols = new_value.into_rust()?; + if option_name.eq(env.intern("lisp-vline-symbols")?) { + options.lisp_vline_symbols = new_value + .map(|val| val.is_not_nil()) + .unwrap_or_else(Options::default_false); return Ok(()); } - if env.eq(option_name, env.intern("lisp-block-comments")?) { - options.lisp_block_comments = new_value.into_rust()?; + if option_name.eq(env.intern("lisp-block-comments")?) { + options.lisp_block_comments = new_value + .map(|val| val.is_not_nil()) + .unwrap_or_else(Options::default_false); return Ok(()); } - if env.eq(option_name, env.intern("guile-block-comments")?) { - options.guile_block_comments = new_value.into_rust()?; + if option_name.eq(env.intern("guile-block-comments")?) { + options.guile_block_comments = new_value + .map(|val| val.is_not_nil()) + .unwrap_or_else(Options::default_false); return Ok(()); } - if env.eq(option_name, env.intern("scheme-sexp-comments")?) { - options.scheme_sexp_comments = new_value.into_rust()?; + if option_name.eq(env.intern("scheme-sexp-comments")?) { + options.scheme_sexp_comments = new_value + .map(|val| val.is_not_nil()) + .unwrap_or_else(Options::default_false); return Ok(()); } - if env.eq(option_name, env.intern("julia-long-strings")?) { - options.julia_long_strings = new_value.into_rust()?; + if option_name.eq(env.intern("julia-long-strings")?) { + options.janet_long_strings = new_value + .map(|val| val.is_not_nil()) + .unwrap_or_else(Options::default_false); return Ok(()); } - env.signal(unknown_option_error, option_name) + env.signal(unknown_option_error, [option_name]) } #[defun(user_ptr, mod_in_name = false)] @@ -235,38 +265,47 @@ fn get_option<'a>(options: &Options, option_name: Value<'a>) -> Result // The function is returning a type-erased Value because it can either be a boolean // or a list let env = option_name.env; - if env.eq(option_name, env.intern("partial-result")?) { + if option_name.eq(env.intern("partial-result")?) { return Ok(options.partial_result.into_lisp(env)?); } - if env.eq(option_name, env.intern("force-balance")?) { + if option_name.eq(env.intern("force-balance")?) { return Ok(options.force_balance.into_lisp(env)?); } - if env.eq(option_name, env.intern("return-parens")?) { + if option_name.eq(env.intern("return-parens")?) { return Ok(options.return_parens.into_lisp(env)?); } - if env.eq(option_name, env.intern("comment-char")?) { - return Ok(options.comment_char.into_lisp(env)?); + if option_name.eq(env.intern("comment-char")?) { + return Ok(options.comment_char.to_string().into_lisp(env)?); } - if env.eq(option_name, env.intern("string-delimiters")?) { - return Ok(options.string_delimiters.into_lisp(env)?); + if option_name.eq(env.intern("string-delimiters")?) { + return Ok(to_lisp_vec(env, options.string_delimiters.clone())?); } - if env.eq(option_name, env.intern("lisp-vline-symbols")?) { + if option_name.eq(env.intern("lisp-vline-symbols")?) { return Ok(options.lisp_vline_symbols.into_lisp(env)?); } - if env.eq(option_name, env.intern("lisp-block-comments")?) { + if option_name.eq(env.intern("lisp-block-comments")?) { return Ok(options.lisp_block_comments.into_lisp(env)?); } - if env.eq(option_name, env.intern("guile-block-comments")?) { + if option_name.eq(env.intern("guile-block-comments")?) { return Ok(options.guile_block_comments.into_lisp(env)?); } - if env.eq(option_name, env.intern("scheme-sexp-comments")?) { + if option_name.eq(env.intern("scheme-sexp-comments")?) { return Ok(options.scheme_sexp_comments.into_lisp(env)?); } - if env.eq(option_name, env.intern("julia-long-strings")?) { - return Ok(options.julia_long_strings.into_lisp(env)?); + if option_name.eq(env.intern("julia-long-strings")?) { + return Ok(options.janet_long_strings.into_lisp(env)?); } - env.signal(unknown_option_error, option_name) + env.signal(unknown_option_error, [option_name]) +} + +fn to_lisp_vec(env: &Env, vec: Vec) -> Result { + env.vector( + &vec + .into_iter() + .map(|s| s.into_lisp(env)) + .collect::>>()?, + ) } #[defun(mod_in_name = false)] diff --git a/src/types.rs b/src/types.rs index e54efd3..bf965db 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,9 +1,7 @@ use serde; use serde_json; use std; -use std::{fmt, - mem, - rc::Rc}; +use std::{fmt, mem, rc::Rc}; pub type LineNumber = usize; pub type Column = usize; @@ -12,290 +10,292 @@ pub type Delta = i64; #[derive(Clone, Debug, Deserialize, PartialEq, Eq)] #[serde(rename_all = "camelCase")] pub struct Change { - pub x: Column, - pub line_no: LineNumber, - pub old_text: String, - pub new_text: String, + pub x: Column, + pub line_no: LineNumber, + pub old_text: String, + pub new_text: String, } #[derive(Clone, Deserialize, Debug)] #[serde(rename_all = "camelCase")] pub struct Options { - pub cursor_x: Option, - pub cursor_line: Option, - pub prev_cursor_x: Option, - pub prev_cursor_line: Option, - pub prev_text: Option, - pub selection_start_line: Option, - #[serde(default = "Options::default_changes")] - pub changes: Vec, - #[serde(default = "Options::default_false")] - pub partial_result: bool, - #[serde(default = "Options::default_false")] - pub force_balance: bool, - #[serde(default = "Options::default_false")] - pub return_parens: bool, - #[serde(default = "Options::default_comment")] - pub comment_char: char, - #[serde(default = "Options::default_string_delimiters")] - pub string_delimiters: Vec, - #[serde(default = "Options::default_false")] - pub lisp_vline_symbols: bool, - #[serde(default = "Options::default_false")] - pub lisp_block_comments: bool, - #[serde(default = "Options::default_false")] - pub guile_block_comments: bool, - #[serde(default = "Options::default_false")] - pub scheme_sexp_comments: bool, - #[serde(default = "Options::default_false")] - pub janet_long_strings: bool, + pub cursor_x: Option, + pub cursor_line: Option, + pub prev_cursor_x: Option, + pub prev_cursor_line: Option, + pub prev_text: Option, + pub selection_start_line: Option, + #[serde(default = "Options::default_changes")] + pub changes: Vec, + #[serde(default = "Options::default_false")] + pub partial_result: bool, + #[serde(default = "Options::default_false")] + pub force_balance: bool, + #[serde(default = "Options::default_false")] + pub return_parens: bool, + #[serde(default = "Options::default_comment")] + pub comment_char: char, + #[serde(default = "Options::default_string_delimiters")] + pub string_delimiters: Vec, + #[serde(default = "Options::default_false")] + pub lisp_vline_symbols: bool, + #[serde(default = "Options::default_false")] + pub lisp_block_comments: bool, + #[serde(default = "Options::default_false")] + pub guile_block_comments: bool, + #[serde(default = "Options::default_false")] + pub scheme_sexp_comments: bool, + #[serde(default = "Options::default_false")] + pub janet_long_strings: bool, } impl Default for Options { - fn default() -> Self { - Self { - cursor_x: None, - cursor_line: None, - prev_cursor_x: None, - prev_cursor_line: None, - prev_text: None, - selection_start_line: None, - changes: Self::default_changes(), - partial_result: false, - force_balance: false, - return_parens: false, - comment_char: Self::default_comment(), - string_delimiters: Self::default_string_delimiters(), - lisp_vline_symbols: false, - lisp_block_comments: false, - guile_block_comments: false, - scheme_sexp_comments: false, - janet_long_strings: false, - } + fn default() -> Self { + Self { + cursor_x: None, + cursor_line: None, + prev_cursor_x: None, + prev_cursor_line: None, + prev_text: None, + selection_start_line: None, + changes: Self::default_changes(), + partial_result: false, + force_balance: false, + return_parens: false, + comment_char: Self::default_comment(), + string_delimiters: Self::default_string_delimiters(), + lisp_vline_symbols: false, + lisp_block_comments: false, + guile_block_comments: false, + scheme_sexp_comments: false, + janet_long_strings: false, } + } } - impl Options { - fn default_changes() -> Vec { - vec![] - } - - fn default_false() -> bool { - false - } - - fn default_comment() -> char { - ';' - } - fn default_string_delimiters() -> Vec { - vec!["\"".to_string()] - } + pub(crate) fn default_changes() -> Vec { + vec![] + } + + pub(crate) fn default_false() -> bool { + false + } + + pub(crate) fn default_comment() -> char { + ';' + } + pub(crate) fn default_string_delimiters() -> Vec { + vec!["\"".to_string()] + } } #[derive(Deserialize, Debug)] #[serde(rename_all = "camelCase")] pub struct Request { - pub mode: String, - pub text: String, - pub options: Options, + pub mode: String, + pub text: String, + pub options: Options, } -#[derive(Clone,Serialize,Debug)] +#[derive(Clone, Serialize, Debug)] #[serde(rename_all = "camelCase")] pub struct TabStop<'a> { - pub ch: &'a str, - pub x: Column, - pub line_no: LineNumber, - pub arg_x: Option, + pub ch: &'a str, + pub x: Column, + pub line_no: LineNumber, + pub arg_x: Option, } -#[derive(Clone,Debug,Serialize)] +#[derive(Clone, Debug, Serialize)] #[serde(rename_all = "camelCase")] pub struct ParenTrail { - pub line_no: LineNumber, - pub start_x: Column, - pub end_x: Column, + pub line_no: LineNumber, + pub start_x: Column, + pub end_x: Column, } #[derive(Clone, Debug)] pub struct Closer<'a> { - pub line_no: LineNumber, - pub x: Column, - pub ch: &'a str, - pub trail: Option + pub line_no: LineNumber, + pub x: Column, + pub ch: &'a str, + pub trail: Option, } -#[derive(Clone,Debug,Serialize)] +#[derive(Clone, Debug, Serialize)] #[serde(rename_all = "camelCase")] pub struct Paren<'a> { - pub line_no: LineNumber, - pub ch: &'a str, - pub x: Column, - pub indent_delta: Delta, - pub max_child_indent: Option, - pub arg_x: Option, - pub input_line_no: LineNumber, - pub input_x: Column, - - #[serde(skip)] - pub closer: Option>, - #[serde(skip)] - pub children: Vec> + pub line_no: LineNumber, + pub ch: &'a str, + pub x: Column, + pub indent_delta: Delta, + pub max_child_indent: Option, + pub arg_x: Option, + pub input_line_no: LineNumber, + pub input_x: Column, + + #[serde(skip)] + pub closer: Option>, + #[serde(skip)] + pub children: Vec>, } #[derive(Serialize, Debug, Clone)] #[serde(rename_all = "camelCase")] pub struct Answer<'a> { - pub text: std::borrow::Cow<'a, str>, - pub success: bool, - pub error: Option, - pub cursor_x: Option, - pub cursor_line: Option, - pub tab_stops: Vec>, - pub paren_trails: Vec, - pub parens: Vec>, + pub text: std::borrow::Cow<'a, str>, + pub success: bool, + pub error: Option, + pub cursor_x: Option, + pub cursor_line: Option, + pub tab_stops: Vec>, + pub paren_trails: Vec, + pub parens: Vec>, } impl<'a> From for Answer<'a> { - fn from(error: Error) -> Answer<'a> { - Answer { - text: std::borrow::Cow::from(""), - success: false, - error: Some(error), - cursor_x: None, - cursor_line: None, - tab_stops: vec![], - paren_trails: vec![], - parens: vec![], - } + fn from(error: Error) -> Answer<'a> { + Answer { + text: std::borrow::Cow::from(""), + success: false, + error: Some(error), + cursor_x: None, + cursor_line: None, + tab_stops: vec![], + paren_trails: vec![], + parens: vec![], } + } } #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)] pub enum ErrorName { - QuoteDanger, - EolBackslash, - UnclosedQuote, - UnclosedParen, - UnmatchedCloseParen, - UnmatchedOpenParen, - LeadingCloseParen, - - Utf8EncodingError, - JsonEncodingError, - Panic, - - Restart, + QuoteDanger, + EolBackslash, + UnclosedQuote, + UnclosedParen, + UnmatchedCloseParen, + UnmatchedOpenParen, + LeadingCloseParen, + + Utf8EncodingError, + JsonEncodingError, + Panic, + + Restart, } impl Default for ErrorName { - fn default() -> ErrorName { - ErrorName::Restart - } + fn default() -> ErrorName { + ErrorName::Restart + } } impl ToString for ErrorName { - fn to_string(&self) -> String { - String::from(match self { - &ErrorName::QuoteDanger => "quote-danger", - &ErrorName::EolBackslash => "eol-backslash", - &ErrorName::UnclosedQuote => "unclosed-quote", - &ErrorName::UnclosedParen => "unclosed-paren", - &ErrorName::UnmatchedCloseParen => "unmatched-close-paren", - &ErrorName::UnmatchedOpenParen => "unmatched-open-paren", - &ErrorName::LeadingCloseParen => "leading-close-paren", - &ErrorName::Utf8EncodingError => "utf8-error", - &ErrorName::JsonEncodingError => "json-error", - &ErrorName::Panic => "panic", - _ => "??", - }) - } + fn to_string(&self) -> String { + String::from(match self { + &ErrorName::QuoteDanger => "quote-danger", + &ErrorName::EolBackslash => "eol-backslash", + &ErrorName::UnclosedQuote => "unclosed-quote", + &ErrorName::UnclosedParen => "unclosed-paren", + &ErrorName::UnmatchedCloseParen => "unmatched-close-paren", + &ErrorName::UnmatchedOpenParen => "unmatched-open-paren", + &ErrorName::LeadingCloseParen => "leading-close-paren", + &ErrorName::Utf8EncodingError => "utf8-error", + &ErrorName::JsonEncodingError => "json-error", + &ErrorName::Panic => "panic", + _ => "??", + }) + } } impl serde::Serialize for ErrorName { - fn serialize(&self, serializer: S) -> Result - where S: serde::Serializer - { - serializer.serialize_str(&self.to_string()) - } + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serializer.serialize_str(&self.to_string()) + } } impl<'a> serde::Deserialize<'a> for ErrorName { - fn deserialize(deserializer: D) -> Result - where D: serde::Deserializer<'a> - { - struct Visitor; - - impl<'de> serde::de::Visitor<'de> for Visitor { - type Value = ErrorName; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("error name") - } - - fn visit_str(self, value: &str) -> Result - where E: serde::de::Error - { - match value { - "quote-danger" => Ok(ErrorName::QuoteDanger), - "eol-backslash" => Ok(ErrorName::EolBackslash), - "unclosed-quote" => Ok(ErrorName::UnclosedQuote), - "unclosed-paren" => Ok(ErrorName::UnclosedParen), - "unmatched-close-paren" => Ok(ErrorName::UnmatchedCloseParen), - "unmatched-open-paren" => Ok(ErrorName::UnmatchedOpenParen), - "leading-close-paren" => Ok(ErrorName::LeadingCloseParen), - "utf8-error" => Ok(ErrorName::Utf8EncodingError), - "json-error" => Ok(ErrorName::JsonEncodingError), - "panic" => Ok(ErrorName::Panic), - _ => Err(E::custom(format!("unknown error name: {}", value))) - } - } + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'a>, + { + struct Visitor; + + impl<'de> serde::de::Visitor<'de> for Visitor { + type Value = ErrorName; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("error name") + } + + fn visit_str(self, value: &str) -> Result + where + E: serde::de::Error, + { + match value { + "quote-danger" => Ok(ErrorName::QuoteDanger), + "eol-backslash" => Ok(ErrorName::EolBackslash), + "unclosed-quote" => Ok(ErrorName::UnclosedQuote), + "unclosed-paren" => Ok(ErrorName::UnclosedParen), + "unmatched-close-paren" => Ok(ErrorName::UnmatchedCloseParen), + "unmatched-open-paren" => Ok(ErrorName::UnmatchedOpenParen), + "leading-close-paren" => Ok(ErrorName::LeadingCloseParen), + "utf8-error" => Ok(ErrorName::Utf8EncodingError), + "json-error" => Ok(ErrorName::JsonEncodingError), + "panic" => Ok(ErrorName::Panic), + _ => Err(E::custom(format!("unknown error name: {}", value))), } - - deserializer.deserialize_string(Visitor) + } } + + deserializer.deserialize_string(Visitor) + } } #[derive(Debug, Default, Serialize, Clone)] #[serde(rename_all = "camelCase")] pub struct Error { - pub name: ErrorName, - pub message: String, - pub x: Column, - pub line_no: LineNumber, - pub input_x: Column, - pub input_line_no: LineNumber, + pub name: ErrorName, + pub message: String, + pub x: Column, + pub line_no: LineNumber, + pub input_x: Column, + pub input_line_no: LineNumber, } impl From for Error { - fn from(error: std::str::Utf8Error) -> Error { - Error { - name: ErrorName::Utf8EncodingError, - message: format!("Error decoding UTF8: {}", error), - ..Error::default() - } + fn from(error: std::str::Utf8Error) -> Error { + Error { + name: ErrorName::Utf8EncodingError, + message: format!("Error decoding UTF8: {}", error), + ..Error::default() } + } } impl From for Error { - fn from(error: std::ffi::NulError) -> Error { - Error { - name: ErrorName::Panic, - message: format!("{}", error), - ..Error::default() - } + fn from(error: std::ffi::NulError) -> Error { + Error { + name: ErrorName::Panic, + message: format!("{}", error), + ..Error::default() } + } } impl From for Error { - fn from(error: serde_json::Error) -> Error { - Error { - name: ErrorName::JsonEncodingError, - message: format!("Error parsing JSON: {}", error), - ..Error::default() - } + fn from(error: serde_json::Error) -> Error { + Error { + name: ErrorName::JsonEncodingError, + message: format!("Error parsing JSON: {}", error), + ..Error::default() } + } } // Introduce the concept of Reference Counting of requests to work with emacs memory module @@ -307,35 +307,34 @@ const ANSWER_LEN: usize = mem::size_of::() / 8; pub type RawAnswer = [u64; ANSWER_LEN]; #[allow(dead_code)] -pub struct WrappedAnswer{ +pub struct WrappedAnswer { request: SharedRequest, - raw: RawAnswer + raw: RawAnswer, } - impl WrappedAnswer { - #[inline] - #[allow(dead_code)] - pub unsafe fn new(request: SharedRequest, inner: Answer) -> Self { - let ptr = (&inner as *const Answer) as *const RawAnswer; - // Delay inner cursor's cleanup (until wrapper is dropped). - mem::forget(inner); - let raw = ptr.read(); - Self { request, raw } - } - - #[inline] - #[allow(dead_code)] - pub fn inner(&self) -> &Answer { - let ptr = (&self.raw as *const RawAnswer) as *const Answer; - unsafe { &*ptr } - } + #[inline] + #[allow(dead_code)] + pub unsafe fn new(request: SharedRequest, inner: Answer) -> Self { + let ptr = (&inner as *const Answer) as *const RawAnswer; + // Delay inner cursor's cleanup (until wrapper is dropped). + mem::forget(inner); + let raw = ptr.read(); + Self { request, raw } + } + + #[inline] + #[allow(dead_code)] + pub fn inner(&self) -> &Answer { + let ptr = (&self.raw as *const RawAnswer) as *const Answer; + unsafe { &*ptr } + } } impl Drop for WrappedAnswer { - #[inline] - fn drop(&mut self) { - let ptr = (&mut self.raw as *mut RawAnswer) as *mut Answer; - unsafe { ptr.read() }; - } + #[inline] + fn drop(&mut self) { + let ptr = (&mut self.raw as *mut RawAnswer) as *mut Answer; + unsafe { ptr.read() }; + } }