use std::ops::Range; use crate::{motion::right, state::Mode, Vim};use editor::{ display_map::{DisplaySnapshot, ToDisplayPoint}, movement::{self, FindRange}, Bias, DisplayPoint, Editor,}; use itertools::Itertools; use gpui::{actions, impl_actions, ViewContext};use language::{BufferSnapshot, CharKind, Point, Selection};use multi_buffer::MultiBufferRow;use serde::Deserialize; #[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize)]pub enum Object { Word { ignore_punctuation: bool }, Sentence, Paragraph, Quotes, BackQuotes, DoubleQuotes, VerticalBars, Parentheses, SquareBrackets, CurlyBrackets, AngleBrackets, Argument, Tag,} #[derive(Clone, Deserialize, PartialEq)]#[serde(rename_all = "camelCase")]struct Word { #[serde(default)] ignore_punctuation: bool,} impl_actions!(vim, [Word]); actions!( vim, [ Sentence, Paragraph, Quotes, BackQuotes, DoubleQuotes, VerticalBars, Parentheses, SquareBrackets, CurlyBrackets, AngleBrackets, Argument, Tag ]); pub fn register(editor: &mut Editor, cx: &mut ViewContext<Vim>) { Vim::action( editor, cx, |vim, &Word { ignore_punctuation }: &Word, cx| { vim.object(Object::Word { ignore_punctuation }, cx) }, ); Vim::action(editor, cx, |vim, _: &Tag, cx| vim.object(Object::Tag, cx)); Vim::action(editor, cx, |vim, _: &Sentence, cx| { vim.object(Object::Sentence, cx) }); Vim::action(editor, cx, |vim, _: &Paragraph, cx| { vim.object(Object::Paragraph, cx) }); Vim::action(editor, cx, |vim, _: &Quotes, cx| { vim.object(Object::Quotes, cx) }); Vim::action(editor, cx, |vim, _: &BackQuotes, cx| { vim.object(Object::BackQuotes, cx) }); Vim::action(editor, cx, |vim, _: &DoubleQuotes, cx| { vim.object(Object::DoubleQuotes, cx) }); Vim::action(editor, cx, |vim, _: &Parentheses, cx| { vim.object(Object::Parentheses, cx) }); Vim::action(editor, cx, |vim, _: &SquareBrackets, cx| { vim.object(Object::SquareBrackets, cx) }); Vim::action(editor, cx, |vim, _: &CurlyBrackets, cx| { vim.object(Object::CurlyBrackets, cx) }); Vim::action(editor, cx, |vim, _: &AngleBrackets, cx| { vim.object(Object::AngleBrackets, cx) }); Vim::action(editor, cx, |vim, _: &VerticalBars, cx| { vim.object(Object::VerticalBars, cx) }); Vim::action(editor, cx, |vim, _: &Argument, cx| { vim.object(Object::Argument, cx) });} impl Vim { fn object(&mut self, object: Object, cx: &mut ViewContext<Self>) { match self.mode { Mode::Normal => self.normal_object(object, cx), Mode::Visual | Mode::VisualLine | Mode::VisualBlock => self.visual_object(object, cx), Mode::Insert | Mode::Replace => { // Shouldn't execute a text object in insert mode. Ignoring } } }} impl Object { pub fn is_multiline(self) -> bool { match self { Object::Word { .. } | Object::Quotes | Object::BackQuotes | Object::VerticalBars | Object::DoubleQuotes => false, Object::Sentence | Object::Paragraph | Object::Parentheses | Object::Tag | Object::AngleBrackets | Object::CurlyBrackets | Object::SquareBrackets | Object::Argument => true, } } pub fn always_expands_both_ways(self) -> bool { match self { Object::Word { .. } | Object::Sentence | Object::Paragraph | Object::Argument => false, Object::Quotes | Object::BackQuotes | Object::DoubleQuotes | Object::VerticalBars | Object::Parentheses | Object::SquareBrackets | Object::Tag | Object::CurlyBrackets | Object::AngleBrackets => true, } } pub fn target_visual_mode(self, current_mode: Mode) -> Mode { match self { Object::Word { .. } | Object::Sentence | Object::Quotes | Object::BackQuotes | Object::DoubleQuotes => { if current_mode == Mode::VisualBlock { Mode::VisualBlock } else { Mode::Visual } } Object::Parentheses | Object::SquareBrackets | Object::CurlyBrackets | Object::AngleBrackets | Object::VerticalBars | Object::Tag | Object::Argument => Mode::Visual, Object::Paragraph => Mode::VisualLine, } } pub fn range( self, map: &DisplaySnapshot, selection: Selection<DisplayPoint>, around: bool, ) -> Option<Range<DisplayPoint>> { let relative_to = selection.head(); match self { Object::Word { ignore_punctuation } => { if around { around_word(map, relative_to, ignore_punctuation) } else { in_word(map, relative_to, ignore_punctuation) } } Object::Sentence => sentence(map, relative_to, around), Object::Paragraph => paragraph(map, relative_to, around), Object::Quotes => { surrounding_markers(map, relative_to, around, self.is_multiline(), '\'', '\'') } Object::BackQuotes => { surrounding_markers(map, relative_to, around, self.is_multiline(), '`', '`') } Object::DoubleQuotes => { surrounding_markers(map, relative_to, around, self.is_multiline(), '"', '"') } Object::VerticalBars => { surrounding_markers(map, relative_to, around, self.is_multiline(), '|', '|') } Object::Parentheses => { surrounding_markers(map, relative_to, around, self.is_multiline(), '(', ')') } Object::Tag => surrounding_html_tag(map, selection, around), Object::SquareBrackets => { surrounding_markers(map, relative_to, around, self.is_multiline(), '[', ']') } Object::CurlyBrackets => { surrounding_markers(map, relative_to, around, self.is_multiline(), '{', '}') } Object::AngleBrackets => { surrounding_markers(map, relative_to, around, self.is_multiline(), '<', '>') } Object::Argument => argument(map, relative_to, around), } } pub fn expand_selection( self, map: &DisplaySnapshot, selection: &mut Selection<DisplayPoint>, around: bool, ) -> bool { if let Some(range) = self.range(map, selection.clone(), around) { selection.start = range.start; selection.end = range.end; true } else { false } }} /// Returns a range that surrounds the word `relative_to` is in.////// If `relative_to` is at the start of a word, return the word./// If `relative_to` is between words, return the space between.fn in_word( map: &DisplaySnapshot, relative_to: DisplayPoint, ignore_punctuation: bool,) -> Option<Range<DisplayPoint>> { // Use motion::right so that we consider the character under the cursor when looking for the start let classifier = map .buffer_snapshot .char_classifier_at(relative_to.to_point(map)) .ignore_punctuation(ignore_punctuation); let start = movement::find_preceding_boundary_display_point( map, right(map, relative_to, 1), movement::FindRange::SingleLine, |left, right| classifier.kind(left) != classifier.kind(right), ); let end = movement::find_boundary(map, relative_to, FindRange::SingleLine, |left, right| { classifier.kind(left) != classifier.kind(right) }); Some(start..end)} fn surrounding_html_tag( map: &DisplaySnapshot, selection: Selection<DisplayPoint>, around: bool,) -> Option<Range<DisplayPoint>> { fn read_tag(chars: impl Iterator<Item = char>) -> String { chars .take_while(|c| c.is_alphanumeric() || *c == ':' || *c == '-' || *c == '_' || *c == '.') .collect() } fn open_tag(mut chars: impl Iterator<Item = char>) -> Option<String> { if Some('<') != chars.next() { return None; } Some(read_tag(chars)) } fn close_tag(mut chars: impl Iterator<Item = char>) -> Option<String> { if (Some('<'), Some('/')) != (chars.next(), chars.next()) { return None; } Some(read_tag(chars)) } let snapshot = &map.buffer_snapshot; let offset = selection.head().to_offset(map, Bias::Left); let excerpt = snapshot.excerpt_containing(offset..offset)?; let buffer = excerpt.buffer(); let offset = excerpt.map_offset_to_buffer(offset); // Find the most closest to current offset let mut cursor = buffer.syntax_layer_at(offset)?.node().walk(); let mut last_child_node = cursor.node(); while cursor.goto_first_child_for_byte(offset).is_some() { last_child_node = cursor.node(); } let mut last_child_node = Some(last_child_node); while let Some(cur_node) = last_child_node { if cur_node.child_count() >= 2 { let first_child = cur_node.child(0); let last_child = cur_node.child(cur_node.child_count() - 1); if let (Some(first_child), Some(last_child)) = (first_child, last_child) { let open_tag = open_tag(buffer.chars_for_range(first_child.byte_range())); let close_tag = close_tag(buffer.chars_for_range(last_child.byte_range())); // It needs to be handled differently according to the selection length let is_valid = if selection.end.to_offset(map, Bias::Left) - selection.start.to_offset(map, Bias::Left) <= 1 { offset <= last_child.end_byte() } else { selection.start.to_offset(map, Bias::Left) >= first_child.start_byte() && selection.end.to_offset(map, Bias::Left) <= last_child.start_byte() + 1 }; if open_tag.is_some() && open_tag == close_tag && is_valid { let range = if around { first_child.byte_range().start..last_child.byte_range().end } else { first_child.byte_range().end..last_child.byte_range().start }; if excerpt.contains_buffer_range(range.clone()) { let result = excerpt.map_range_from_buffer(range); return Some( result.start.to_display_point(map)..result.end.to_display_point(map), ); } } } } last_child_node = cur_node.parent(); } None} /// Returns a range that surrounds the word and following whitespace/// relative_to is in.////// If `relative_to` is at the start of a word, return the word and following whitespace./// If `relative_to` is between words, return the whitespace back and the following word.////// if in word/// delete that word/// if there is whitespace following the word, delete that as well/// otherwise, delete any preceding whitespace/// otherwise/// delete whitespace around cursor/// delete word following the cursorfn around_word( map: &DisplaySnapshot, relative_to: DisplayPoint, ignore_punctuation: bool,) -> Option<Range<DisplayPoint>> { let offset = relative_to.to_offset(map, Bias::Left); let classifier = map .buffer_snapshot .char_classifier_at(offset) .ignore_punctuation(ignore_punctuation); let in_word = map .buffer_chars_at(offset) .next() .map(|(c, _)| !classifier.is_whitespace(c)) .unwrap_or(false); if in_word { around_containing_word(map, relative_to, ignore_punctuation) } else { around_next_word(map, relative_to, ignore_punctuation) }} fn around_containing_word( map: &DisplaySnapshot, relative_to: DisplayPoint, ignore_punctuation: bool,) -> Option<Range<DisplayPoint>> { in_word(map, relative_to, ignore_punctuation) .map(|range| expand_to_include_whitespace(map, range, true))} fn around_next_word( map: &DisplaySnapshot, relative_to: DisplayPoint, ignore_punctuation: bool,) -> Option<Range<DisplayPoint>> { let classifier = map .buffer_snapshot .char_classifier_at(relative_to.to_point(map)) .ignore_punctuation(ignore_punctuation); // Get the start of the word let start = movement::find_preceding_boundary_display_point( map, right(map, relative_to, 1), FindRange::SingleLine, |left, right| classifier.kind(left) != classifier.kind(right), ); let mut word_found = false; let end = movement::find_boundary(map, relative_to, FindRange::MultiLine, |left, right| { let left_kind = classifier.kind(left); let right_kind = classifier.kind(right); let found = (word_found && left_kind != right_kind) || right == '\n' && left == '\n'; if right_kind != CharKind::Whitespace { word_found = true; } found }); Some(start..end)} fn argument( map: &DisplaySnapshot, relative_to: DisplayPoint, around: bool,) -> Option<Range<DisplayPoint>> { let snapshot = &map.buffer_snapshot; let offset = relative_to.to_offset(map, Bias::Left); // The `argument` vim text object uses the syntax tree, so we operate at the buffer level and map back to the display level let excerpt = snapshot.excerpt_containing(offset..offset)?; let buffer = excerpt.buffer(); fn comma_delimited_range_at( buffer: &BufferSnapshot, mut offset: usize, include_comma: bool, ) -> Option<Range<usize>> { // Seek to the first non-whitespace character offset += buffer .chars_at(offset) .take_while(|c| c.is_whitespace()) .map(char::len_utf8) .sum::<usize>(); let bracket_filter = |open: Range<usize>, close: Range<usize>| { // Filter out empty ranges if open.end == close.start { return false; } // If the cursor is outside the brackets, ignore them if open.start == offset || close.end == offset { return false; } // TODO: Is there any better way to filter out string brackets? // Used to filter out string brackets matches!( buffer.chars_at(open.start).next(), Some('(' | '[' | '{' | '<' | '|') ) }; // Find the brackets containing the cursor let (open_bracket, close_bracket) = buffer.innermost_enclosing_bracket_ranges(offset..offset, Some(&bracket_filter))?; let inner_bracket_range = open_bracket.end..close_bracket.start; let layer = buffer.syntax_layer_at(offset)?; let node = layer.node(); let mut cursor = node.walk(); // Loop until we find the smallest node whose parent covers the bracket range. This node is the argument in the parent argument list let mut parent_covers_bracket_range = false; loop { let node = cursor.node(); let range = node.byte_range(); let covers_bracket_range = range.start == open_bracket.start && range.end == close_bracket.end; if parent_covers_bracket_range && !covers_bracket_range { break; } parent_covers_bracket_range = covers_bracket_range; // Unable to find a child node with a parent that covers the bracket range, so no argument to select cursor.goto_first_child_for_byte(offset)?; } let mut argument_node = cursor.node(); // If the child node is the open bracket, move to the next sibling. if argument_node.byte_range() == open_bracket { if !cursor.goto_next_sibling() { return Some(inner_bracket_range); } argument_node = cursor.node(); } // While the child node is the close bracket or a comma, move to the previous sibling while argument_node.byte_range() == close_bracket || argument_node.kind() == "," { if !cursor.goto_previous_sibling() { return Some(inner_bracket_range); }Lines 1–500 of 1559. Load more
use std::ops::Range; use crate::{motion::right, state::Mode, Vim}; use editor::{ display_map::{DisplaySnapshot, ToDisplayPoint}, movement::{self, FindRange}, Bias, DisplayPoint, Editor, }; use itertools::Itertools; use gpui::{actions, impl_actions, ViewContext}; use language::{BufferSnapshot, CharKind, Point, Selection}; use multi_buffer::MultiBufferRow; use serde::Deserialize; #[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize)] pub enum Object { Word { ignore_punctuation: bool }, Sentence, Paragraph, Quotes, BackQuotes, DoubleQuotes, VerticalBars, Parentheses, SquareBrackets, CurlyBrackets, AngleBrackets, Argument, Tag, } #[derive(Clone, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] struct Word { #[serde(default)] ignore_punctuation: bool, } impl_actions!(vim, [Word]); actions!( vim, [ Sentence, Paragraph, Quotes, BackQuotes, DoubleQuotes, VerticalBars, Parentheses, SquareBrackets, CurlyBrackets, AngleBrackets, Argument, Tag ] ); pub fn register(editor: &mut Editor, cx: &mut ViewContext<Vim>) { Vim::action( editor, cx, |vim, &Word { ignore_punctuation }: &Word, cx| { vim.object(Object::Word { ignore_punctuation }, cx) }, ); Vim::action(editor, cx, |vim, _: &Tag, cx| vim.object(Object::Tag, cx)); Vim::action(editor, cx, |vim, _: &Sentence, cx| { vim.object(Object::Sentence, cx) }); Vim::action(editor, cx, |vim, _: &Paragraph, cx| { vim.object(Object::Paragraph, cx) }); Vim::action(editor, cx, |vim, _: &Quotes, cx| { vim.object(Object::Quotes, cx) }); Vim::action(editor, cx, |vim, _: &BackQuotes, cx| { vim.object(Object::BackQuotes, cx) }); Vim::action(editor, cx, |vim, _: &DoubleQuotes, cx| { vim.object(Object::DoubleQuotes, cx) }); Vim::action(editor, cx, |vim, _: &Parentheses, cx| { vim.object(Object::Parentheses, cx) }); Vim::action(editor, cx, |vim, _: &SquareBrackets, cx| { vim.object(Object::SquareBrackets, cx) }); Vim::action(editor, cx, |vim, _: &CurlyBrackets, cx| { vim.object(Object::CurlyBrackets, cx) }); Vim::action(editor, cx, |vim, _: &AngleBrackets, cx| { vim.object(Object::AngleBrackets, cx) }); Vim::action(editor, cx, |vim, _: &VerticalBars, cx| { vim.object(Object::VerticalBars, cx) }); Vim::action(editor, cx, |vim, _: &Argument, cx| { vim.object(Object::Argument, cx) }); } impl Vim { fn object(&mut self, object: Object, cx: &mut ViewContext<Self>) { match self.mode { Mode::Normal => self.normal_object(object, cx), Mode::Visual | Mode::VisualLine | Mode::VisualBlock => self.visual_object(object, cx), Mode::Insert | Mode::Replace => { // Shouldn't execute a text object in insert mode. Ignoring } } } } impl Object { pub fn is_multiline(self) -> bool { match self { Object::Word { .. } | Object::Quotes | Object::BackQuotes | Object::VerticalBars | Object::DoubleQuotes => false, Object::Sentence | Object::Paragraph | Object::Parentheses | Object::Tag | Object::AngleBrackets | Object::CurlyBrackets | Object::SquareBrackets | Object::Argument => true, } } pub fn always_expands_both_ways(self) -> bool { match self { Object::Word { .. } | Object::Sentence | Object::Paragraph | Object::Argument => false, Object::Quotes | Object::BackQuotes | Object::DoubleQuotes | Object::VerticalBars | Object::Parentheses | Object::SquareBrackets | Object::Tag | Object::CurlyBrackets | Object::AngleBrackets => true, } } pub fn target_visual_mode(self, current_mode: Mode) -> Mode { match self { Object::Word { .. } | Object::Sentence | Object::Quotes | Object::BackQuotes | Object::DoubleQuotes => { if current_mode == Mode::VisualBlock { Mode::VisualBlock } else { Mode::Visual } } Object::Parentheses | Object::SquareBrackets | Object::CurlyBrackets | Object::AngleBrackets | Object::VerticalBars | Object::Tag | Object::Argument => Mode::Visual, Object::Paragraph => Mode::VisualLine, } } pub fn range( self, map: &DisplaySnapshot, selection: Selection<DisplayPoint>, around: bool, ) -> Option<Range<DisplayPoint>> { let relative_to = selection.head(); match self { Object::Word { ignore_punctuation } => { if around { around_word(map, relative_to, ignore_punctuation) } else { in_word(map, relative_to, ignore_punctuation) } } Object::Sentence => sentence(map, relative_to, around), Object::Paragraph => paragraph(map, relative_to, around), Object::Quotes => { surrounding_markers(map, relative_to, around, self.is_multiline(), '\'', '\'') } Object::BackQuotes => { surrounding_markers(map, relative_to, around, self.is_multiline(), '`', '`') } Object::DoubleQuotes => { surrounding_markers(map, relative_to, around, self.is_multiline(), '"', '"') } Object::VerticalBars => { surrounding_markers(map, relative_to, around, self.is_multiline(), '|', '|') } Object::Parentheses => { surrounding_markers(map, relative_to, around, self.is_multiline(), '(', ')') } Object::Tag => surrounding_html_tag(map, selection, around), Object::SquareBrackets => { surrounding_markers(map, relative_to, around, self.is_multiline(), '[', ']') } Object::CurlyBrackets => { surrounding_markers(map, relative_to, around, self.is_multiline(), '{', '}') } Object::AngleBrackets => { surrounding_markers(map, relative_to, around, self.is_multiline(), '<', '>') } Object::Argument => argument(map, relative_to, around), } } pub fn expand_selection( self, map: &DisplaySnapshot, selection: &mut Selection<DisplayPoint>, around: bool, ) -> bool { if let Some(range) = self.range(map, selection.clone(), around) { selection.start = range.start; selection.end = range.end; true } else { false } } } /// Returns a range that surrounds the word `relative_to` is in. /// /// If `relative_to` is at the start of a word, return the word. /// If `relative_to` is between words, return the space between. fn in_word( map: &DisplaySnapshot, relative_to: DisplayPoint, ignore_punctuation: bool, ) -> Option<Range<DisplayPoint>> { // Use motion::right so that we consider the character under the cursor when looking for the start let classifier = map .buffer_snapshot .char_classifier_at(relative_to.to_point(map)) .ignore_punctuation(ignore_punctuation); let start = movement::find_preceding_boundary_display_point( map, right(map, relative_to, 1), movement::FindRange::SingleLine, |left, right| classifier.kind(left) != classifier.kind(right), ); let end = movement::find_boundary(map, relative_to, FindRange::SingleLine, |left, right| { classifier.kind(left) != classifier.kind(right) }); Some(start..end) } fn surrounding_html_tag( map: &DisplaySnapshot, selection: Selection<DisplayPoint>, around: bool, ) -> Option<Range<DisplayPoint>> { fn read_tag(chars: impl Iterator<Item = char>) -> String { chars .take_while(|c| c.is_alphanumeric() || *c == ':' || *c == '-' || *c == '_' || *c == '.') .collect() } fn open_tag(mut chars: impl Iterator<Item = char>) -> Option<String> { if Some('<') != chars.next() { return None; } Some(read_tag(chars)) } fn close_tag(mut chars: impl Iterator<Item = char>) -> Option<String> { if (Some('<'), Some('/')) != (chars.next(), chars.next()) { return None; } Some(read_tag(chars)) } let snapshot = &map.buffer_snapshot; let offset = selection.head().to_offset(map, Bias::Left); let excerpt = snapshot.excerpt_containing(offset..offset)?; let buffer = excerpt.buffer(); let offset = excerpt.map_offset_to_buffer(offset); // Find the most closest to current offset let mut cursor = buffer.syntax_layer_at(offset)?.node().walk(); let mut last_child_node = cursor.node(); while cursor.goto_first_child_for_byte(offset).is_some() { last_child_node = cursor.node(); } let mut last_child_node = Some(last_child_node); while let Some(cur_node) = last_child_node { if cur_node.child_count() >= 2 { let first_child = cur_node.child(0); let last_child = cur_node.child(cur_node.child_count() - 1); if let (Some(first_child), Some(last_child)) = (first_child, last_child) { let open_tag = open_tag(buffer.chars_for_range(first_child.byte_range())); let close_tag = close_tag(buffer.chars_for_range(last_child.byte_range())); // It needs to be handled differently according to the selection length let is_valid = if selection.end.to_offset(map, Bias::Left) - selection.start.to_offset(map, Bias::Left) <= 1 { offset <= last_child.end_byte() } else { selection.start.to_offset(map, Bias::Left) >= first_child.start_byte() && selection.end.to_offset(map, Bias::Left) <= last_child.start_byte() + 1 }; if open_tag.is_some() && open_tag == close_tag && is_valid { let range = if around { first_child.byte_range().start..last_child.byte_range().end } else { first_child.byte_range().end..last_child.byte_range().start }; if excerpt.contains_buffer_range(range.clone()) { let result = excerpt.map_range_from_buffer(range); return Some( result.start.to_display_point(map)..result.end.to_display_point(map), ); } } } } last_child_node = cur_node.parent(); } None } /// Returns a range that surrounds the word and following whitespace /// relative_to is in. /// /// If `relative_to` is at the start of a word, return the word and following whitespace. /// If `relative_to` is between words, return the whitespace back and the following word. /// /// if in word /// delete that word /// if there is whitespace following the word, delete that as well /// otherwise, delete any preceding whitespace /// otherwise /// delete whitespace around cursor /// delete word following the cursor fn around_word( map: &DisplaySnapshot, relative_to: DisplayPoint, ignore_punctuation: bool, ) -> Option<Range<DisplayPoint>> { let offset = relative_to.to_offset(map, Bias::Left); let classifier = map .buffer_snapshot .char_classifier_at(offset) .ignore_punctuation(ignore_punctuation); let in_word = map .buffer_chars_at(offset) .next() .map(|(c, _)| !classifier.is_whitespace(c)) .unwrap_or(false); if in_word { around_containing_word(map, relative_to, ignore_punctuation) } else { around_next_word(map, relative_to, ignore_punctuation) } } fn around_containing_word( map: &DisplaySnapshot, relative_to: DisplayPoint, ignore_punctuation: bool, ) -> Option<Range<DisplayPoint>> { in_word(map, relative_to, ignore_punctuation) .map(|range| expand_to_include_whitespace(map, range, true)) } fn around_next_word( map: &DisplaySnapshot, relative_to: DisplayPoint, ignore_punctuation: bool, ) -> Option<Range<DisplayPoint>> { let classifier = map .buffer_snapshot .char_classifier_at(relative_to.to_point(map)) .ignore_punctuation(ignore_punctuation); // Get the start of the word let start = movement::find_preceding_boundary_display_point( map, right(map, relative_to, 1), FindRange::SingleLine, |left, right| classifier.kind(left) != classifier.kind(right), ); let mut word_found = false; let end = movement::find_boundary(map, relative_to, FindRange::MultiLine, |left, right| { let left_kind = classifier.kind(left); let right_kind = classifier.kind(right); let found = (word_found && left_kind != right_kind) || right == '\n' && left == '\n'; if right_kind != CharKind::Whitespace { word_found = true; } found }); Some(start..end) } fn argument( map: &DisplaySnapshot, relative_to: DisplayPoint, around: bool, ) -> Option<Range<DisplayPoint>> { let snapshot = &map.buffer_snapshot; let offset = relative_to.to_offset(map, Bias::Left); // The `argument` vim text object uses the syntax tree, so we operate at the buffer level and map back to the display level let excerpt = snapshot.excerpt_containing(offset..offset)?; let buffer = excerpt.buffer(); fn comma_delimited_range_at( buffer: &BufferSnapshot, mut offset: usize, include_comma: bool, ) -> Option<Range<usize>> { // Seek to the first non-whitespace character offset += buffer .chars_at(offset) .take_while(|c| c.is_whitespace()) .map(char::len_utf8) .sum::<usize>(); let bracket_filter = |open: Range<usize>, close: Range<usize>| { // Filter out empty ranges if open.end == close.start { return false; } // If the cursor is outside the brackets, ignore them if open.start == offset || close.end == offset { return false; } // TODO: Is there any better way to filter out string brackets? // Used to filter out string brackets matches!( buffer.chars_at(open.start).next(), Some('(' | '[' | '{' | '<' | '|') ) }; // Find the brackets containing the cursor let (open_bracket, close_bracket) = buffer.innermost_enclosing_bracket_ranges(offset..offset, Some(&bracket_filter))?; let inner_bracket_range = open_bracket.end..close_bracket.start; let layer = buffer.syntax_layer_at(offset)?; let node = layer.node(); let mut cursor = node.walk(); // Loop until we find the smallest node whose parent covers the bracket range. This node is the argument in the parent argument list let mut parent_covers_bracket_range = false; loop { let node = cursor.node(); let range = node.byte_range(); let covers_bracket_range = range.start == open_bracket.start && range.end == close_bracket.end; if parent_covers_bracket_range && !covers_bracket_range { break; } parent_covers_bracket_range = covers_bracket_range; // Unable to find a child node with a parent that covers the bracket range, so no argument to select cursor.goto_first_child_for_byte(offset)?; } let mut argument_node = cursor.node(); // If the child node is the open bracket, move to the next sibling. if argument_node.byte_range() == open_bracket { if !cursor.goto_next_sibling() { return Some(inner_bracket_range); } argument_node = cursor.node(); } // While the child node is the close bracket or a comma, move to the previous sibling while argument_node.byte_range() == close_bracket || argument_node.kind() == "," { if !cursor.goto_previous_sibling() { return Some(inner_bracket_range); }