mirror of
https://github.com/helix-editor/helix.git
synced 2024-11-22 09:26:19 +04:00
refactor: add_surrounding_impl extract main logic into an fn
This commit is contained in:
parent
3882bf9e9d
commit
c586e410d4
@ -205,6 +205,50 @@ pub fn find_matching_bracket_plaintext(doc: RopeSlice, cursor_pos: usize) -> Opt
|
||||
None
|
||||
}
|
||||
|
||||
// fn create_rename_prompt(
|
||||
// editor: &Editor,
|
||||
// prefill: String,
|
||||
// history_register: Option<char>,
|
||||
// language_server_id: Option<LanguageServerId>,
|
||||
// ) -> Box<ui::Prompt> {
|
||||
// let prompt = ui::Prompt::new(
|
||||
// "rename-to:".into(),
|
||||
// history_register,
|
||||
// ui::completers::none,
|
||||
// move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {
|
||||
// if event != PromptEvent::Validate {
|
||||
// return;
|
||||
// }
|
||||
// let (view, doc) = current!(cx.editor);
|
||||
|
||||
// let Some(language_server) = doc
|
||||
// .language_servers_with_feature(LanguageServerFeature::RenameSymbol)
|
||||
// .find(|ls| language_server_id.map_or(true, |id| id == ls.id()))
|
||||
// else {
|
||||
// cx.editor
|
||||
// .set_error("No configured language server supports symbol renaming");
|
||||
// return;
|
||||
// };
|
||||
|
||||
// let offset_encoding = language_server.offset_encoding();
|
||||
// let pos = doc.position(view.id, offset_encoding);
|
||||
// let future = language_server
|
||||
// .rename_symbol(doc.identifier(), pos, input.to_string())
|
||||
// .unwrap();
|
||||
|
||||
// match block_on(future) {
|
||||
// Ok(edits) => {
|
||||
// let _ = cx.editor.apply_workspace_edit(offset_encoding, &edits);
|
||||
// }
|
||||
// Err(err) => cx.editor.set_error(err.to_string()),
|
||||
// }
|
||||
// },
|
||||
// )
|
||||
// .with_line(prefill, editor);
|
||||
|
||||
// Box::new(prompt)
|
||||
// }
|
||||
|
||||
/// Returns the open and closing chars pair. If not found in
|
||||
/// [`BRACKETS`] returns (ch, ch).
|
||||
///
|
||||
|
@ -36,7 +36,7 @@
|
||||
textobject,
|
||||
unicode::width::UnicodeWidthChar,
|
||||
visual_offset_from_block, Deletion, LineEnding, Position, Range, Rope, RopeGraphemes,
|
||||
RopeReader, RopeSlice, Selection, SmallVec, Syntax, Tendril, Transaction,
|
||||
RopeReader, RopeSlice, Selection, SmallVec, SmartString, Syntax, Tendril, Transaction,
|
||||
};
|
||||
use helix_view::{
|
||||
document::{FormatterError, Mode, SCRATCH_BUFFER_NAME},
|
||||
@ -5616,6 +5616,7 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
|
||||
("a", "Argument/parameter (tree-sitter)"),
|
||||
("c", "Comment (tree-sitter)"),
|
||||
("T", "Test (tree-sitter)"),
|
||||
("x", "Tag (XML/HTML/JSX)"),
|
||||
("e", "Data structure entry (tree-sitter)"),
|
||||
("m", "Closest surrounding pair (tree-sitter)"),
|
||||
("g", "Change"),
|
||||
@ -5625,6 +5626,54 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
|
||||
cx.editor.autoinfo = Some(Info::new(title, &help_text));
|
||||
}
|
||||
|
||||
fn create_surround_prompt(
|
||||
editor: &Editor,
|
||||
prefill: String,
|
||||
history_register: Option<char>,
|
||||
) -> Box<Prompt> {
|
||||
let prompt = Prompt::new(
|
||||
"tag name:".into(),
|
||||
// TODO: change this to actually be history_register
|
||||
Some('z'),
|
||||
ui::completers::none,
|
||||
move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {},
|
||||
)
|
||||
.with_line("temp".into(), editor);
|
||||
|
||||
Box::new(prompt)
|
||||
}
|
||||
|
||||
fn surround_add_impl(
|
||||
doc: &mut Document,
|
||||
// cx: &mut Context<'_>,
|
||||
view: &mut View,
|
||||
surround_len: usize,
|
||||
open: Tendril,
|
||||
close: Tendril,
|
||||
) {
|
||||
let selection = doc.selection(view.id);
|
||||
let mut changes = Vec::with_capacity(selection.len() * 2);
|
||||
let mut ranges = SmallVec::with_capacity(selection.len());
|
||||
let mut offs = 0;
|
||||
|
||||
for range in selection.iter() {
|
||||
changes.push((range.from(), range.from(), Some(open.clone())));
|
||||
changes.push((range.to(), range.to(), Some(close.clone())));
|
||||
|
||||
ranges.push(
|
||||
Range::new(offs + range.from(), offs + range.to() + surround_len)
|
||||
.with_direction(range.direction()),
|
||||
);
|
||||
|
||||
offs += surround_len;
|
||||
}
|
||||
|
||||
let transaction = Transaction::change(doc.text(), changes.into_iter())
|
||||
.with_selection(Selection::new(ranges, selection.primary_index()));
|
||||
doc.apply(&transaction, view.id);
|
||||
// exit_select_mode(cx);
|
||||
}
|
||||
|
||||
fn surround_add(cx: &mut Context) {
|
||||
cx.on_next_key(move |cx, event| {
|
||||
let (view, doc) = current!(cx.editor);
|
||||
@ -5633,20 +5682,23 @@ fn surround_add(cx: &mut Context) {
|
||||
Some(ch) => {
|
||||
let mut open = Tendril::new();
|
||||
let mut close = Tendril::new();
|
||||
let length = if ch == 'x' {
|
||||
let (o, c) = match_brackets::get_pair(ch);
|
||||
open.push(o);
|
||||
close.push(c);
|
||||
// Any character other than "x" will cause 2 chars to get added
|
||||
2
|
||||
} else {
|
||||
let (o, c) = match_brackets::get_pair(ch);
|
||||
open.push(o);
|
||||
close.push(c);
|
||||
// Any character other than "x" will cause 2 chars to get added
|
||||
2
|
||||
};
|
||||
(open, close, length)
|
||||
// let length = if ch == 'x' {
|
||||
// let prompt = create_surround_prompt(cx.editor, "none".into(), Some('z'));
|
||||
// cx.push_layer(prompt);
|
||||
|
||||
// let (o, c) = match_brackets::get_pair(ch);
|
||||
// open.push(o);
|
||||
// close.push(c);
|
||||
// // Any character other than "x" will cause 2 chars to get added
|
||||
// 2
|
||||
// } else {
|
||||
let (o, c) = match_brackets::get_pair(ch);
|
||||
open.push(o);
|
||||
close.push(c);
|
||||
// Any character other than "x" will cause 2 chars to get added
|
||||
// 2
|
||||
// };
|
||||
(open, close, 2)
|
||||
}
|
||||
None if event.code == KeyCode::Enter => (
|
||||
doc.line_ending.as_str().into(),
|
||||
@ -5656,27 +5708,30 @@ fn surround_add(cx: &mut Context) {
|
||||
None => return,
|
||||
};
|
||||
|
||||
let selection = doc.selection(view.id);
|
||||
let mut changes = Vec::with_capacity(selection.len() * 2);
|
||||
let mut ranges = SmallVec::with_capacity(selection.len());
|
||||
let mut offs = 0;
|
||||
|
||||
for range in selection.iter() {
|
||||
changes.push((range.from(), range.from(), Some(open.clone())));
|
||||
changes.push((range.to(), range.to(), Some(close.clone())));
|
||||
|
||||
ranges.push(
|
||||
Range::new(offs + range.from(), offs + range.to() + surround_len)
|
||||
.with_direction(range.direction()),
|
||||
);
|
||||
|
||||
offs += surround_len;
|
||||
}
|
||||
|
||||
let transaction = Transaction::change(doc.text(), changes.into_iter())
|
||||
.with_selection(Selection::new(ranges, selection.primary_index()));
|
||||
doc.apply(&transaction, view.id);
|
||||
surround_add_impl(doc, view, surround_len, open, close);
|
||||
exit_select_mode(cx);
|
||||
|
||||
// let selection = doc.selection(view.id);
|
||||
// let mut changes = Vec::with_capacity(selection.len() * 2);
|
||||
// let mut ranges = SmallVec::with_capacity(selection.len());
|
||||
// let mut offs = 0;
|
||||
|
||||
// for range in selection.iter() {
|
||||
// changes.push((range.from(), range.from(), Some(open.clone())));
|
||||
// changes.push((range.to(), range.to(), Some(close.clone())));
|
||||
|
||||
// ranges.push(
|
||||
// Range::new(offs + range.from(), offs + range.to() + surround_len)
|
||||
// .with_direction(range.direction()),
|
||||
// );
|
||||
|
||||
// offs += surround_len;
|
||||
// }
|
||||
|
||||
// let transaction = Transaction::change(doc.text(), changes.into_iter())
|
||||
// .with_selection(Selection::new(ranges, selection.primary_index()));
|
||||
// doc.apply(&transaction, view.id);
|
||||
// exit_select_mode(cx);
|
||||
})
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user