mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-06-01 21:21:15 -03:00
Make parse_util_token_extent return its output instead of mutating input.
This commit is contained in:
committed by
Johannes Altmanninger
parent
4994000a27
commit
b5c869d5e7
@@ -216,7 +216,6 @@ pub fn commandline(parser: &Parser, streams: &mut IoStreams, args: &mut [&wstr])
|
|||||||
let mut is_valid = false;
|
let mut is_valid = false;
|
||||||
let mut showing_suggestion = false;
|
let mut showing_suggestion = false;
|
||||||
|
|
||||||
let mut range = 0..0;
|
|
||||||
let mut override_buffer = None;
|
let mut override_buffer = None;
|
||||||
|
|
||||||
const short_options: &wstr = L!(":abijpctfxorhI:CBELSsP");
|
const short_options: &wstr = L!(":abijpctfxorhI:CBELSsP");
|
||||||
@@ -602,6 +601,7 @@ pub fn commandline(parser: &Parser, streams: &mut IoStreams, args: &mut [&wstr])
|
|||||||
return Some(1);
|
return Some(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let range;
|
||||||
if search_field_mode {
|
if search_field_mode {
|
||||||
range = 0..current_buffer.len();
|
range = 0..current_buffer.len();
|
||||||
} else {
|
} else {
|
||||||
@@ -616,7 +616,7 @@ pub fn commandline(parser: &Parser, streams: &mut IoStreams, args: &mut [&wstr])
|
|||||||
range = parse_util_process_extent(current_buffer, current_cursor_pos, None);
|
range = parse_util_process_extent(current_buffer, current_cursor_pos, None);
|
||||||
}
|
}
|
||||||
TextScope::Token => {
|
TextScope::Token => {
|
||||||
parse_util_token_extent(current_buffer, current_cursor_pos, &mut range, None);
|
(range, _) = parse_util_token_extent(current_buffer, current_cursor_pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -475,13 +475,7 @@ pub fn complete(parser: &Parser, streams: &mut IoStreams, argv: &mut [&wstr]) ->
|
|||||||
Some(param) => param,
|
Some(param) => param,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut token = 0..0;
|
let (token, _) = parse_util_token_extent(&do_complete_param, do_complete_param.len());
|
||||||
parse_util_token_extent(
|
|
||||||
&do_complete_param,
|
|
||||||
do_complete_param.len(),
|
|
||||||
&mut token,
|
|
||||||
None,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Create a scoped transient command line, so that builtin_commandline will see our
|
// Create a scoped transient command line, so that builtin_commandline will see our
|
||||||
// argument, not the reader buffer.
|
// argument, not the reader buffer.
|
||||||
|
|||||||
@@ -437,30 +437,20 @@ fn job_or_process_extent(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Find the beginning and end of the token under the cursor and the token before the current token.
|
/// Find the beginning and end of the token under the cursor and the token before the current token.
|
||||||
/// Any combination of tok_begin, tok_end, prev_begin and prev_end may be null.
|
|
||||||
///
|
///
|
||||||
/// \param buff the string to search for subshells
|
/// \param buff the string to search for subshells
|
||||||
/// \param cursor_pos the position of the cursor
|
/// \param cursor_pos the position of the cursor
|
||||||
/// \param tok_begin the start of the current token
|
pub fn parse_util_token_extent(buff: &wstr, cursor_pos: usize) -> (Range<usize>, Range<usize>) {
|
||||||
/// \param tok_end the end of the current token
|
|
||||||
/// \param prev_begin the start o the token before the current token
|
|
||||||
/// \param prev_end the end of the token before the current token
|
|
||||||
pub fn parse_util_token_extent(
|
|
||||||
buff: &wstr,
|
|
||||||
cursor_pos: usize,
|
|
||||||
out_tok: &mut ops::Range<usize>,
|
|
||||||
mut out_prev: Option<&mut ops::Range<usize>>,
|
|
||||||
) {
|
|
||||||
let cmdsubst_range = parse_util_cmdsubst_extent(buff, cursor_pos);
|
let cmdsubst_range = parse_util_cmdsubst_extent(buff, cursor_pos);
|
||||||
let cmdsubst_begin = cmdsubst_range.start;
|
let cmdsubst_begin = cmdsubst_range.start;
|
||||||
|
|
||||||
// pos is equivalent to cursor_pos within the range of the command substitution {begin, end}.
|
// pos is equivalent to cursor_pos within the range of the command substitution {begin, end}.
|
||||||
let offset_within_cmdsubst = cursor_pos - cmdsubst_range.start;
|
let offset_within_cmdsubst = cursor_pos - cmdsubst_range.start;
|
||||||
|
|
||||||
let mut a = cmdsubst_begin + offset_within_cmdsubst;
|
let mut cur_begin = cmdsubst_begin + offset_within_cmdsubst;
|
||||||
let mut b = a;
|
let mut cur_end = cur_begin;
|
||||||
let mut pa = a;
|
let mut prev_begin = cur_begin;
|
||||||
let mut pb = pa;
|
let mut prev_end = cur_begin;
|
||||||
|
|
||||||
assert!(cmdsubst_begin <= buff.len());
|
assert!(cmdsubst_begin <= buff.len());
|
||||||
assert!(cmdsubst_range.end <= buff.len());
|
assert!(cmdsubst_range.end <= buff.len());
|
||||||
@@ -477,31 +467,30 @@ pub fn parse_util_token_extent(
|
|||||||
// Cursor was before beginning of this token, means that the cursor is between two tokens,
|
// Cursor was before beginning of this token, means that the cursor is between two tokens,
|
||||||
// so we set it to a zero element string and break.
|
// so we set it to a zero element string and break.
|
||||||
if tok_begin > offset_within_cmdsubst {
|
if tok_begin > offset_within_cmdsubst {
|
||||||
a = cmdsubst_begin + offset_within_cmdsubst;
|
cur_begin = cmdsubst_begin + offset_within_cmdsubst;
|
||||||
b = a;
|
cur_end = cur_begin;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If cursor is inside the token, this is the token we are looking for. If so, set a and b
|
// If cursor is inside the token, this is the token we are looking for. If so, set
|
||||||
// and break.
|
// cur_begin and cur_end and break.
|
||||||
if token.type_ == TokenType::string && tok_end >= offset_within_cmdsubst {
|
if token.type_ == TokenType::string && tok_end >= offset_within_cmdsubst {
|
||||||
a = cmdsubst_begin + token.offset();
|
cur_begin = cmdsubst_begin + token.offset();
|
||||||
b = a + token.length();
|
cur_end = cur_begin + token.length();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remember previous string token.
|
// Remember previous string token.
|
||||||
if token.type_ == TokenType::string {
|
if token.type_ == TokenType::string {
|
||||||
pa = cmdsubst_begin + token.offset();
|
prev_begin = cmdsubst_begin + token.offset();
|
||||||
pb = pa + token.length();
|
prev_end = prev_begin + token.length();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*out_tok = a..b;
|
assert!(prev_begin <= buff.len());
|
||||||
out_prev.as_mut().map(|prev| **prev = pa..pb);
|
assert!(prev_end >= prev_begin);
|
||||||
assert!(pa <= buff.len());
|
assert!(prev_end <= buff.len());
|
||||||
assert!(pb >= pa);
|
(cur_begin..cur_end, prev_begin..prev_end)
|
||||||
assert!(pb <= buff.len());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the line number at the specified character offset.
|
/// Get the line number at the specified character offset.
|
||||||
|
|||||||
@@ -1457,8 +1457,7 @@ pub fn combine_command_and_autosuggestion(
|
|||||||
// Here we do something funny: if the last token of the command line contains any uppercase
|
// Here we do something funny: if the last token of the command line contains any uppercase
|
||||||
// characters, we use its case. Otherwise we use the case of the autosuggestion. This
|
// characters, we use its case. Otherwise we use the case of the autosuggestion. This
|
||||||
// is an idea from issue #335.
|
// is an idea from issue #335.
|
||||||
let mut tok = 0..0;
|
let (tok, _) = parse_util_token_extent(cmdline, cmdline.len() - 1);
|
||||||
parse_util_token_extent(cmdline, cmdline.len() - 1, &mut tok, None);
|
|
||||||
let last_token_contains_uppercase = cmdline[tok].chars().any(|c| c.is_uppercase());
|
let last_token_contains_uppercase = cmdline[tok].chars().any(|c| c.is_uppercase());
|
||||||
if !last_token_contains_uppercase {
|
if !last_token_contains_uppercase {
|
||||||
// Use the autosuggestion's case.
|
// Use the autosuggestion's case.
|
||||||
@@ -1856,8 +1855,7 @@ fn clear_transient_edit(&mut self) {
|
|||||||
fn replace_current_token(&mut self, new_token: WString) {
|
fn replace_current_token(&mut self, new_token: WString) {
|
||||||
// Find current token.
|
// Find current token.
|
||||||
let (elt, el) = self.active_edit_line();
|
let (elt, el) = self.active_edit_line();
|
||||||
let mut token_range = 0..0;
|
let (token_range, _) = parse_util_token_extent(el.text(), el.position());
|
||||||
parse_util_token_extent(el.text(), el.position(), &mut token_range, None);
|
|
||||||
|
|
||||||
self.replace_substring(elt, token_range, new_token);
|
self.replace_substring(elt, token_range, new_token);
|
||||||
}
|
}
|
||||||
@@ -2934,8 +2932,7 @@ fn handle_readline_command(&mut self, c: ReadlineCmd) {
|
|||||||
let el = &self.data.command_line;
|
let el = &self.data.command_line;
|
||||||
if mode == SearchMode::Token {
|
if mode == SearchMode::Token {
|
||||||
// Searching by token.
|
// Searching by token.
|
||||||
let mut token_range = 0..0;
|
let (token_range, _) = parse_util_token_extent(el.text(), el.position());
|
||||||
parse_util_token_extent(el.text(), el.position(), &mut token_range, None);
|
|
||||||
self.data.history_search.reset_to_mode(
|
self.data.history_search.reset_to_mode(
|
||||||
el.text()[token_range.clone()].to_owned(),
|
el.text()[token_range.clone()].to_owned(),
|
||||||
self.history.clone(),
|
self.history.clone(),
|
||||||
@@ -3402,15 +3399,13 @@ fn handle_readline_command(&mut self, c: ReadlineCmd) {
|
|||||||
let (elt, el) = self.active_edit_line();
|
let (elt, el) = self.active_edit_line();
|
||||||
let text = el.text();
|
let text = el.text();
|
||||||
|
|
||||||
let mut tok = 0..0;
|
let (mut tok, mut prev_tok) = parse_util_token_extent(text, el.position());
|
||||||
let mut prev_tok = 0..0;
|
|
||||||
parse_util_token_extent(text, el.position(), &mut tok, Some(&mut prev_tok));
|
|
||||||
|
|
||||||
// In case we didn't find a token at or after the cursor...
|
// In case we didn't find a token at or after the cursor...
|
||||||
if tok.start == el.len() {
|
if tok.start == el.len() {
|
||||||
// ...retry beginning from the previous token.
|
// ...retry beginning from the previous token.
|
||||||
let pos = prev_tok.end;
|
let pos = prev_tok.end;
|
||||||
parse_util_token_extent(text, pos, &mut tok, Some(&mut prev_tok));
|
(tok, prev_tok) = parse_util_token_extent(text, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure we have two tokens.
|
// Make sure we have two tokens.
|
||||||
@@ -3792,9 +3787,7 @@ fn backward_token(&mut self) -> Option<usize> {
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut tok = 0..0;
|
let (tok, prev_tok) = parse_util_token_extent(el.text(), el.position());
|
||||||
let mut prev_tok = 0..0;
|
|
||||||
parse_util_token_extent(el.text(), el.position(), &mut tok, Some(&mut prev_tok));
|
|
||||||
|
|
||||||
// if we are at the start of a token, go back one
|
// if we are at the start of a token, go back one
|
||||||
let new_position = if tok.start == pos {
|
let new_position = if tok.start == pos {
|
||||||
@@ -6012,8 +6005,7 @@ pub fn completion_apply_to_command_line(
|
|||||||
|
|
||||||
if do_replace_token {
|
if do_replace_token {
|
||||||
let mut move_cursor = 0;
|
let mut move_cursor = 0;
|
||||||
let mut range = 0..0;
|
let (range, _) = parse_util_token_extent(command_line, cursor_pos);
|
||||||
parse_util_token_extent(command_line, cursor_pos, &mut range, None);
|
|
||||||
|
|
||||||
let mut sb = command_line[..range.start].to_owned();
|
let mut sb = command_line[..range.start].to_owned();
|
||||||
|
|
||||||
@@ -6049,8 +6041,7 @@ pub fn completion_apply_to_command_line(
|
|||||||
|
|
||||||
let mut quote = None;
|
let mut quote = None;
|
||||||
let replaced = if do_escape {
|
let replaced = if do_escape {
|
||||||
let mut tok = 0..0;
|
let (tok, _) = parse_util_token_extent(command_line, cursor_pos);
|
||||||
parse_util_token_extent(command_line, cursor_pos, &mut tok, None);
|
|
||||||
// Find the last quote in the token to complete.
|
// Find the last quote in the token to complete.
|
||||||
let mut have_token = false;
|
let mut have_token = false;
|
||||||
if tok.contains(&cursor_pos) || cursor_pos == tok.end {
|
if tok.contains(&cursor_pos) || cursor_pos == tok.end {
|
||||||
@@ -6155,13 +6146,8 @@ fn compute_and_apply_completions(&mut self, c: ReadlineCmd) {
|
|||||||
|
|
||||||
// Figure out the extent of the token within the command substitution. Note we
|
// Figure out the extent of the token within the command substitution. Note we
|
||||||
// pass cmdsub_begin here, not buff.
|
// pass cmdsub_begin here, not buff.
|
||||||
let mut token_range = 0..0;
|
let (mut token_range, _) =
|
||||||
parse_util_token_extent(
|
parse_util_token_extent(&el.text()[cmdsub_range.clone()], position_in_cmdsub);
|
||||||
&el.text()[cmdsub_range.clone()],
|
|
||||||
position_in_cmdsub,
|
|
||||||
&mut token_range,
|
|
||||||
None,
|
|
||||||
);
|
|
||||||
let position_in_token = position_in_cmdsub - token_range.start;
|
let position_in_token = position_in_cmdsub - token_range.start;
|
||||||
|
|
||||||
// Hack: the token may extend past the end of the command substitution, e.g. in
|
// Hack: the token may extend past the end of the command substitution, e.g. in
|
||||||
|
|||||||
Reference in New Issue
Block a user