Skip to content

Commit

Permalink
Put keymaps behind dyn access, refactor config.load()
Browse files Browse the repository at this point in the history
  • Loading branch information
jharrilim committed Mar 18, 2022
1 parent a41c215 commit 06bad8c
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 88 deletions.
10 changes: 6 additions & 4 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ impl Application {
})),
);

let editor_view = Box::new(ui::EditorView::new(std::mem::take(
&mut config.load().keys.clone(),
)));
let keymaps = Box::new(Map::new(Arc::clone(&config), |config: &Config| {
&config.keys
}));

let editor_view = Box::new(ui::EditorView::new(keymaps));
compositor.push(editor_view);

if args.load_tutor {
Expand Down Expand Up @@ -263,7 +265,7 @@ impl Application {
ConfigEvent::Update(editor_config) => {
let mut app_config = (*self.config.load().clone()).clone();
app_config.editor = editor_config;
self.config.swap(Arc::new(app_config));
self.config.store(Arc::new(app_config));
}
}
}
Expand Down
42 changes: 22 additions & 20 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,7 @@ fn align_selections(cx: &mut Context) {

fn goto_window(cx: &mut Context, align: Align) {
let count = cx.count() - 1;
let config = cx.editor.config();
let (view, doc) = current!(cx.editor);

let height = view.inner_area().height as usize;
Expand All @@ -848,12 +849,7 @@ fn goto_window(cx: &mut Context, align: Align) {
// - 1 so we have at least one gap in the middle.
// a height of 6 with padding of 3 on each side will keep shifting the view back and forth
// as we type
let scrolloff = cx
.editor
.config
.load()
.scrolloff
.min(height.saturating_sub(1) / 2);
let scrolloff = config.scrolloff.min(height.saturating_sub(1) / 2);

let last_line = view.last_line(doc);

Expand Down Expand Up @@ -1277,6 +1273,7 @@ fn switch_to_lowercase(cx: &mut Context) {

pub fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
use Direction::*;
let config = cx.editor.config();
let (view, doc) = current!(cx.editor);

let range = doc.selection(view.id).primary();
Expand All @@ -1295,7 +1292,7 @@ pub fn scroll(cx: &mut Context, offset: usize, direction: Direction) {

let height = view.inner_area().height;

let scrolloff = cx.editor.config.load().scrolloff.min(height as usize / 2);
let scrolloff = config.scrolloff.min(height as usize / 2);

view.offset.row = match direction {
Forward => view.offset.row + offset,
Expand Down Expand Up @@ -1587,9 +1584,10 @@ fn rsearch(cx: &mut Context) {
}

fn searcher(cx: &mut Context, direction: Direction) {
let config = cx.editor.config();
let reg = cx.register.unwrap_or('/');
let scrolloff = cx.editor.config.load().scrolloff;
let wrap_around = cx.editor.config.load().search.wrap_around;
let scrolloff = config.scrolloff;
let wrap_around = config.search.wrap_around;

let doc = doc!(cx.editor);

Expand Down Expand Up @@ -1632,13 +1630,14 @@ fn searcher(cx: &mut Context, direction: Direction) {
}

fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Direction) {
let scrolloff = cx.editor.config.load().scrolloff;
let config = cx.editor.config();
let scrolloff = config.scrolloff;
let (view, doc) = current!(cx.editor);
let registers = &cx.editor.registers;
if let Some(query) = registers.read('/') {
let query = query.last().unwrap();
let contents = doc.text().slice(..).to_string();
let search_config = &cx.editor.config.load().search;
let search_config = &config.search;
let case_insensitive = if search_config.smart_case {
!query.chars().any(char::is_uppercase)
} else {
Expand Down Expand Up @@ -1698,8 +1697,9 @@ fn search_selection(cx: &mut Context) {
fn global_search(cx: &mut Context) {
let (all_matches_sx, all_matches_rx) =
tokio::sync::mpsc::unbounded_channel::<(usize, PathBuf)>();
let smart_case = cx.editor.config.load().search.smart_case;
let file_picker_config = cx.editor.config.load().file_picker.clone();
let config = cx.editor.config();
let smart_case = config.search.smart_case;
let file_picker_config = config.file_picker.clone();

let completions = search_completions(cx, None);
let prompt = ui::regex_prompt(
Expand Down Expand Up @@ -2028,7 +2028,7 @@ fn append_mode(cx: &mut Context) {
fn file_picker(cx: &mut Context) {
// We don't specify language markers, root will be the root of the current git repo
let root = find_root(None, &[]).unwrap_or_else(|| PathBuf::from("./"));
let picker = ui::file_picker(root, &cx.editor.config.load());
let picker = ui::file_picker(root, &cx.editor.config());
cx.push_layer(Box::new(overlayed(picker)));
}

Expand Down Expand Up @@ -2104,8 +2104,8 @@ pub fn command_palette(cx: &mut Context) {
cx.callback = Some(Box::new(
move |compositor: &mut Compositor, cx: &mut compositor::Context| {
let doc = doc_mut!(cx.editor);
let keymap =
compositor.find::<ui::EditorView>().unwrap().keymaps[&doc.mode].reverse_map();
let keymap = compositor.find::<ui::EditorView>().unwrap().keymaps()[&doc.mode]
.reverse_map();

let mut commands: Vec<MappableCommand> = MappableCommand::STATIC_COMMAND_LIST.into();
commands.extend(typed::TYPABLE_COMMAND_LIST.iter().map(|cmd| {
Expand Down Expand Up @@ -2571,14 +2571,15 @@ pub mod insert {
// It trigger completion when idle timer reaches deadline
// Only trigger completion if the word under cursor is longer than n characters
pub fn idle_completion(cx: &mut Context) {
let config = cx.editor.config();
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
let cursor = doc.selection(view.id).primary().cursor(text);

use helix_core::chars::char_is_word;
let mut iter = text.chars_at(cursor);
iter.reverse();
for _ in 0..cx.editor.config.load().completion_trigger_len {
for _ in 0..config.completion_trigger_len {
match iter.next() {
Some(c) if char_is_word(c) => {}
_ => return,
Expand Down Expand Up @@ -4141,7 +4142,7 @@ fn shell_keep_pipe(cx: &mut Context) {
Some('|'),
ui::completers::none,
move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {
let shell = &cx.editor.config.load().shell;
let shell = &cx.editor.config().shell;
if event != PromptEvent::Validate {
return;
}
Expand Down Expand Up @@ -4237,7 +4238,8 @@ fn shell(cx: &mut Context, prompt: Cow<'static, str>, behavior: ShellBehavior) {
Some('|'),
ui::completers::none,
move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {
let shell = &cx.editor.config.load().shell;
let config = cx.editor.config();
let shell = &config.shell;
if event != PromptEvent::Validate {
return;
}
Expand Down Expand Up @@ -4282,7 +4284,7 @@ fn shell(cx: &mut Context, prompt: Cow<'static, str>, behavior: ShellBehavior) {

// after replace cursor may be out of bounds, do this to
// make sure cursor is in view and update scroll as well
view.ensure_cursor_in_view(doc, cx.editor.config.load().scrolloff);
view.ensure_cursor_in_view(doc, config.scrolloff);
},
);

Expand Down
4 changes: 2 additions & 2 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ fn theme(
.theme_loader
.load(theme)
.with_context(|| format!("Failed setting theme {}", theme))?;
let true_color = cx.editor.config.load().true_color || crate::true_color();
let true_color = cx.editor.config().true_color || crate::true_color();
if !(true_color || theme.is_16_color()) {
bail!("Unsupported theme: theme requires true color support");
}
Expand Down Expand Up @@ -862,7 +862,7 @@ fn setting(
}
let (key, arg) = (&args[0].to_lowercase(), &args[1]);

let mut runtime_config = cx.editor.config.load().clone();
let mut runtime_config = cx.editor.config().clone();
match key.as_ref() {
"scrolloff" => runtime_config.scrolloff = arg.parse()?,
"scroll-lines" => runtime_config.scroll_lines = arg.parse()?,
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ pub enum KeymapResultKind {

/// Returned after looking up a key in [`Keymap`]. The `sticky` field has a
/// reference to the sticky node if one is currently active.
#[derive(Debug)]
#[derive(Debug, Clone, PartialEq)]
pub struct KeymapResult<'a> {
pub kind: KeymapResultKind,
pub sticky: Option<&'a KeyTrieNode>,
Expand Down
Loading

0 comments on commit 06bad8c

Please sign in to comment.