Skip to content

Commit

Permalink
Add readonly register handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pickfire committed Jan 24, 2023
1 parent e023dde commit 27be41c
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 140 deletions.
1 change: 0 additions & 1 deletion helix-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub mod movement;
pub mod object;
pub mod path;
mod position;
pub mod register;
pub mod search;
pub mod selection;
pub mod shellwords;
Expand Down
85 changes: 0 additions & 85 deletions helix-core/src/register.rs

This file was deleted.

51 changes: 30 additions & 21 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1683,12 +1683,18 @@ fn search_impl(
}

fn search_completions(cx: &mut Context, reg: Option<char>) -> Vec<String> {
let doc = doc!(cx.editor);
let mut items = reg
.and_then(|reg| cx.editor.registers.get(reg))
.map_or(Vec::new(), |reg| reg.read().iter().take(200).collect());
.and_then(|reg| cx.editor.registers.read(doc, reg))
.map_or(Vec::new(), |mut content| {
for s in &mut content {
s.drain(200..);
}
content
});
items.sort_unstable();
items.dedup();
items.into_iter().cloned().collect()
items.into_iter().map(String::from).collect()
}

fn search(cx: &mut Context) {
Expand Down Expand Up @@ -1749,7 +1755,7 @@ fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Dir
let scrolloff = config.scrolloff;
let (_, doc) = current!(cx.editor);
let registers = &cx.editor.registers;
if let Some(query) = registers.read('/').and_then(|query| query.last()) {
if let Some(query) = registers.last(doc, '/') {
let contents = doc.text().slice(..).to_string();
let search_config = &config.search;
let case_insensitive = if search_config.smart_case {
Expand All @@ -1758,7 +1764,7 @@ fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Dir
false
};
let wrap_around = search_config.wrap_around;
if let Ok(regex) = RegexBuilder::new(query)
if let Ok(regex) = RegexBuilder::new(&query)
.case_insensitive(case_insensitive)
.multi_line(true)
.build()
Expand Down Expand Up @@ -1816,7 +1822,8 @@ fn search_selection(cx: &mut Context) {
}

fn make_search_word_bounded(cx: &mut Context) {
let regex = match cx.editor.registers.last('/') {
let doc = doc!(cx.editor);
let regex = match cx.editor.registers.last(doc, '/') {
Some(regex) => regex,
None => return,
};
Expand All @@ -1834,7 +1841,7 @@ fn make_search_word_bounded(cx: &mut Context) {
if !start_anchored {
new_regex.push_str("\\b");
}
new_regex.push_str(regex);
new_regex.push_str(&regex);
if !end_anchored {
new_regex.push_str("\\b");
}
Expand Down Expand Up @@ -3696,7 +3703,7 @@ fn replace_with_yanked(cx: &mut Context) {
let (view, doc) = current!(cx.editor);
let registers = &mut cx.editor.registers;

if let Some(values) = registers.read(reg_name) {
if let Some(values) = registers.read(doc, reg_name) {
if !values.is_empty() {
let repeat = std::iter::repeat(
values
Expand Down Expand Up @@ -3765,8 +3772,8 @@ fn paste(cx: &mut Context, pos: Paste) {
let (view, doc) = current!(cx.editor);
let registers = &mut cx.editor.registers;

if let Some(values) = registers.read(reg_name) {
paste_impl(values, doc, view, pos, count, cx.editor.mode);
if let Some(values) = registers.read(doc, reg_name) {
paste_impl(&values, doc, view, pos, count, cx.editor.mode);
}
}

Expand Down Expand Up @@ -5144,6 +5151,7 @@ fn record_macro(cx: &mut Context) {
}

fn replay_macro(cx: &mut Context) {
let doc = doc!(cx.editor);
let reg = cx.register.unwrap_or('@');

if cx.editor.macro_replaying.contains(&reg) {
Expand All @@ -5154,18 +5162,19 @@ fn replay_macro(cx: &mut Context) {
return;
}

let keys: Vec<KeyEvent> = if let Some([keys_str]) = cx.editor.registers.read(reg) {
match helix_view::input::parse_macro(keys_str) {
Ok(keys) => keys,
Err(err) => {
cx.editor.set_error(format!("Invalid macro: {}", err));
return;
let keys: Vec<KeyEvent> =
if let Some([keys_str]) = cx.editor.registers.read(doc, reg).as_deref() {
match helix_view::input::parse_macro(keys_str) {
Ok(keys) => keys,
Err(err) => {
cx.editor.set_error(format!("Invalid macro: {}", err));
return;
}
}
}
} else {
cx.editor.set_error(format!("Register [{}] empty", reg));
return;
};
} else {
cx.editor.set_error(format!("Register [{}] empty", reg));
return;
};

// Once the macro has been fully validated, it's marked as being under replay
// to ensure we don't fall into infinite recursion.
Expand Down
33 changes: 12 additions & 21 deletions helix-term/src/ui/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ impl Prompt {
direction: CompletionDirection,
) {
(self.callback_fn)(cx, &self.line, PromptEvent::Abort);
let values = match cx.editor.registers.read(register) {
let doc = doc!(cx.editor);
let values = match cx.editor.registers.read(doc, register) {
Some(values) if !values.is_empty() => values,
_ => return,
};
Expand Down Expand Up @@ -451,11 +452,12 @@ impl Prompt {
// render buffer text
surface.set_string(area.x, area.y + line, &self.prompt, prompt_color);

let doc = doc!(cx.editor);
let (input, is_suggestion): (Cow<str>, bool) = if self.line.is_empty() {
// latest value in the register list
match self
.history_register
.and_then(|reg| cx.editor.registers.last(reg))
.and_then(|reg| cx.editor.registers.last(doc, reg))
.map(|entry| entry.into())
{
Some(value) => (value, true),
Expand Down Expand Up @@ -543,9 +545,10 @@ impl Component for Prompt {
if self.selection.is_some() && self.line.ends_with(std::path::MAIN_SEPARATOR) {
self.recalculate_completion(cx.editor);
} else {
let doc = doc!(cx.editor);
let last_item = self
.history_register
.and_then(|reg| cx.editor.registers.last(reg).cloned())
.and_then(|reg| cx.editor.registers.last(doc, reg))
.map(|entry| entry.into())
.unwrap_or_else(|| Cow::from(""));

Expand Down Expand Up @@ -596,26 +599,14 @@ impl Component for Prompt {
self.completion = cx
.editor
.registers
.inner()
.iter()
.map(|(ch, reg)| {
let content = reg
.read()
.get(0)
.and_then(|s| s.lines().next().to_owned())
.unwrap_or_default();
(0.., format!("{} {}", ch, &content).into())
})
.iter_preview()
.map(|(ch, content)| (0.., format!("{} {}", ch, &content).into()))
.collect();
self.next_char_handler = Some(Box::new(|prompt, c, context| {
self.next_char_handler = Some(Box::new(|prompt, c, cx| {
let doc = doc!(cx.editor);
prompt.insert_str(
context
.editor
.registers
.read(c)
.and_then(|r| r.first())
.map_or("", |r| r.as_str()),
context.editor,
&cx.editor.registers.first(doc, c).unwrap_or_default(),
cx.editor,
);
}));
(self.callback_fn)(cx, &self.line, PromptEvent::Update);
Expand Down
2 changes: 1 addition & 1 deletion helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
graphics::{CursorKind, Rect},
info::Info,
input::KeyEvent,
register::Registers,
theme::{self, Theme},
tree::{self, Tree},
Align, Document, DocumentId, View, ViewId,
Expand Down Expand Up @@ -37,7 +38,6 @@ use tokio::{
use anyhow::{anyhow, bail, Error};

pub use helix_core::diagnostic::Severity;
pub use helix_core::register::Registers;
use helix_core::Position;
use helix_core::{
auto_pairs::AutoPairs,
Expand Down
15 changes: 4 additions & 11 deletions helix-view/src/info.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::input::KeyEvent;
use helix_core::{register::Registers, unicode::width::UnicodeWidthStr};
use crate::register::Registers;
use helix_core::unicode::width::UnicodeWidthStr;
use std::{collections::BTreeSet, fmt::Write};

#[derive(Debug)]
Expand Down Expand Up @@ -69,16 +70,8 @@ impl Info {

pub fn from_registers(registers: &Registers) -> Self {
let body: Vec<_> = registers
.inner()
.iter()
.map(|(ch, reg)| {
let content = reg
.read()
.get(0)
.and_then(|s| s.lines().next())
.unwrap_or_default();
(ch.to_string(), content)
})
.iter_preview()
.map(|(ch, content)| (ch.to_string(), content))
.collect();

let mut infobox = Self::new("Registers", &body);
Expand Down
1 change: 1 addition & 0 deletions helix-view/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod base64;
pub mod info;
pub mod input;
pub mod keyboard;
pub mod register;
pub mod theme;
pub mod tree;
pub mod view;
Expand Down
Loading

0 comments on commit 27be41c

Please sign in to comment.