Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jtroo committed Oct 20, 2024
1 parent 4e53544 commit 1dc8e4a
Show file tree
Hide file tree
Showing 19 changed files with 979 additions and 38 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ indoc = { version = "2.0.4", optional = true }
regex = { version = "1.10.4", optional = true }

[features]
default = ["tcp_server","win_sendinput_send_scancodes"]
default = ["tcp_server","win_sendinput_send_scancodes", "zippychord"]
perf_logging = []
tcp_server = ["serde_json"]
win_sendinput_send_scancodes = ["kanata-parser/win_sendinput_send_scancodes"]
Expand All @@ -136,6 +136,7 @@ gui = ["win_manifest","kanata-parser/gui",
"winapi/processthreadsapi",
"native-windows-gui/tray-notification","native-windows-gui/message-window","native-windows-gui/menu","native-windows-gui/cursor","native-windows-gui/high-dpi","native-windows-gui/embed-resource","native-windows-gui/image-decoder","native-windows-gui/notice","native-windows-gui/animation-timer",
]
zippychord = ["kanata-parser/zippychord"]

[profile.release]
opt-level = "z"
Expand Down
1 change: 1 addition & 0 deletions parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ gui = []
lsp = []
win_llhook_read_scancodes = []
win_sendinput_send_scancodes = []
zippychord = []
42 changes: 39 additions & 3 deletions parser/src/cfg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ pub use key_outputs::*;
mod permutations;
use permutations::*;

mod zippychord;
pub use zippychord::*;

use crate::lsp_hints::{self, LspHints};

mod str_ext;
Expand Down Expand Up @@ -221,7 +224,7 @@ type TapHoldCustomFunc =
) -> &'static (dyn Fn(QueuedIter) -> (Option<WaitingAction>, bool) + Send + Sync);

pub type BorrowedKLayout<'a> = Layout<'a, KEYS_IN_ROW, 2, &'a &'a [&'a CustomAction]>;
pub type KeySeqsToFKeys = Trie;
pub type KeySeqsToFKeys = Trie<(u8, u16)>;

pub struct KanataLayout {
layout: KLayout,
Expand Down Expand Up @@ -271,21 +274,26 @@ pub struct Cfg {
pub fake_keys: HashMap<String, usize>,
/// The maximum value of switch's key-timing item in the configuration.
pub switch_max_key_timing: u16,
/// Zipchord-like configuration.
pub zippy: Option<ZchPossibleChords>,
}

/// Parse a new configuration from a file.
pub fn new_from_file(p: &Path) -> MResult<Cfg> {
parse_cfg(p)
}

pub fn new_from_str(cfg_text: &str) -> MResult<Cfg> {
pub fn new_from_str(cfg_text: &str, file_content: Option<String>) -> MResult<Cfg> {
let mut s = ParserState::default();
let icfg = parse_cfg_raw_string(
cfg_text,
&mut s,
&PathBuf::from("configuration"),
&mut FileContentProvider {
get_file_content_fn: &mut |_| Err("include is not supported".into()),
get_file_content_fn: &mut move |_| match &file_content {
Some(s) => Ok(s.clone()),
None => Err("include is not supported".into()),
},
},
DEF_LOCAL_KEYS,
Err("environment variables are not supported".into()),
Expand Down Expand Up @@ -322,6 +330,7 @@ pub fn new_from_str(cfg_text: &str) -> MResult<Cfg> {
overrides: icfg.overrides,
fake_keys,
switch_max_key_timing,
zippy: icfg.zippy,
})
}

Expand Down Expand Up @@ -373,6 +382,7 @@ fn parse_cfg(p: &Path) -> MResult<Cfg> {
overrides: icfg.overrides,
fake_keys,
switch_max_key_timing,
zippy: icfg.zippy,
})
}

Expand Down Expand Up @@ -409,6 +419,7 @@ pub struct IntermediateCfg {
pub overrides: Overrides,
pub chords_v2: Option<ChordsV2<'static, KanataCustom>>,
pub start_action: Option<&'static KanataAction>,
pub zippy: Option<ZchPossibleChords>,
}

// A snapshot of enviroment variables, or an error message with an explanation
Expand Down Expand Up @@ -875,6 +886,29 @@ pub fn parse_cfg_raw_string(
.into());
}

let zippy_exprs = root_exprs
.iter()
.filter(gen_first_atom_filter("defzippy-experimental"))
.collect::<Vec<_>>();
let zippy = match zippy_exprs.len() {
0 => None,
1 => {
let zippy = parse_zippy(zippy_exprs[0], s, file_content_provider)?;
Some(zippy)
}
_ => {
let spanned = spanned_root_exprs
.iter()
.filter(gen_first_atom_filter_spanned("defzippy-experimental"))
.nth(1)
.expect("> 2 overrides");
bail_span!(
spanned,
"Only one defzippy allowed, found more.\nDelete the extras."
)
}
};

#[cfg(feature = "lsp")]
LSP_VARIABLE_REFERENCES.with_borrow_mut(|refs| {
s.lsp_hints
Expand All @@ -895,6 +929,7 @@ pub fn parse_cfg_raw_string(
overrides,
chords_v2,
start_action,
zippy,
})
}

Expand Down Expand Up @@ -928,6 +963,7 @@ fn error_on_unknown_top_level_atoms(exprs: &[Spanned<Vec<SExpr>>]) -> Result<()>
| "defvar"
| "deftemplate"
| "defchordsv2-experimental"
| "defzippy-experimental"
| "defseq" => Ok(()),
_ => err_span!(expr, "Found unknown configuration item"),
})
Expand Down
7 changes: 7 additions & 0 deletions parser/src/cfg/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,13 @@ fn parse_file_with_utf8_bom() {
new_from_file(&std::path::PathBuf::from("./test_cfgs/utf8bom.kbd")).unwrap();
}

#[test]
#[cfg(feature = "zippychord")]
fn parse_zippychord_file() {
let _lk = lock(&CFG_PARSE_LOCK);
new_from_file(&std::path::PathBuf::from("./test_cfgs/testzch.kbd")).unwrap();
}

#[test]
fn disallow_nested_tap_hold() {
let _lk = lock(&CFG_PARSE_LOCK);
Expand Down
Loading

0 comments on commit 1dc8e4a

Please sign in to comment.