Compare commits

...

2 Commits

Author SHA1 Message Date
Conrad Irwin
404a268412 Add docs 2025-01-06 20:19:06 -07:00
0x2CA
7aede4a348 add subword 2024-12-24 11:47:02 +08:00
3 changed files with 133 additions and 1 deletions

View File

@@ -389,6 +389,9 @@
"bindings": {
"w": "vim::Word",
"shift-w": ["vim::Word", { "ignorePunctuation": true }],
// Subword TextObject
// "w": "vim::Subword",
// "shift-w": ["vim::Subword", { "ignorePunctuation": true }],
"t": "vim::Tag",
"s": "vim::Sentence",
"p": "vim::Paragraph",

View File

@@ -21,6 +21,7 @@ use serde::Deserialize;
#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize)]
pub enum Object {
Word { ignore_punctuation: bool },
Subword { ignore_punctuation: bool },
Sentence,
Paragraph,
Quotes,
@@ -47,12 +48,18 @@ struct Word {
}
#[derive(Clone, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
struct Subword {
#[serde(default)]
ignore_punctuation: bool,
}
#[derive(Clone, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
struct IndentObj {
#[serde(default)]
include_below: bool,
}
impl_actions!(vim, [Word, IndentObj]);
impl_actions!(vim, [Word, Subword, IndentObj]);
actions!(
vim,
@@ -83,6 +90,13 @@ pub fn register(editor: &mut Editor, cx: &mut ViewContext<Vim>) {
vim.object(Object::Word { ignore_punctuation }, cx)
},
);
Vim::action(
editor,
cx,
|vim, &Subword { ignore_punctuation }: &Subword, cx| {
vim.object(Object::Subword { 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)
@@ -154,6 +168,7 @@ impl Object {
pub fn is_multiline(self) -> bool {
match self {
Object::Word { .. }
| Object::Subword { .. }
| Object::Quotes
| Object::BackQuotes
| Object::VerticalBars
@@ -176,6 +191,7 @@ impl Object {
pub fn always_expands_both_ways(self) -> bool {
match self {
Object::Word { .. }
| Object::Subword { .. }
| Object::Sentence
| Object::Paragraph
| Object::Argument
@@ -198,6 +214,7 @@ impl Object {
pub fn target_visual_mode(self, current_mode: Mode, around: bool) -> Mode {
match self {
Object::Word { .. }
| Object::Subword { .. }
| Object::Sentence
| Object::Quotes
| Object::BackQuotes
@@ -243,6 +260,13 @@ impl Object {
in_word(map, relative_to, ignore_punctuation)
}
}
Object::Subword { ignore_punctuation } => {
if around {
around_subword(map, relative_to, ignore_punctuation)
} else {
in_subword(map, relative_to, ignore_punctuation)
}
}
Object::Sentence => sentence(map, relative_to, around),
Object::Paragraph => paragraph(map, relative_to, around),
Object::Quotes => {
@@ -350,6 +374,63 @@ fn in_word(
Some(start..end)
}
fn in_subword(
map: &DisplaySnapshot,
relative_to: DisplayPoint,
ignore_punctuation: bool,
) -> Option<Range<DisplayPoint>> {
let offset = relative_to.to_offset(map, Bias::Left);
// 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 in_subword = map
.buffer_chars_at(offset)
.next()
.map(|(c, _)| {
if classifier.is_word('-') {
!classifier.is_whitespace(c) && c != '_' && c != '-'
} else {
!classifier.is_whitespace(c) && c != '_'
}
})
.unwrap_or(false);
let start = if in_subword {
movement::find_preceding_boundary_display_point(
map,
right(map, relative_to, 1),
movement::FindRange::SingleLine,
|left, right| {
let is_word_start = classifier.kind(left) != classifier.kind(right);
let is_subword_start = classifier.is_word('-') && left == '-' && right != '-'
|| left == '_' && right != '_'
|| left.is_lowercase() && right.is_uppercase();
is_word_start || is_subword_start
},
)
} else {
movement::find_boundary(map, relative_to, FindRange::SingleLine, |left, right| {
let is_word_start = classifier.kind(left) != classifier.kind(right);
let is_subword_start = classifier.is_word('-') && left == '-' && right != '-'
|| left == '_' && right != '_'
|| left.is_lowercase() && right.is_uppercase();
is_word_start || is_subword_start
})
};
let end = movement::find_boundary(map, relative_to, FindRange::SingleLine, |left, right| {
let is_word_end = classifier.kind(left) != classifier.kind(right);
let is_subword_end = classifier.is_word('-') && left != '-' && right == '-'
|| left != '_' && right == '_'
|| left.is_lowercase() && right.is_uppercase();
is_word_end || is_subword_end
});
Some(start..end)
}
pub fn surrounding_html_tag(
map: &DisplaySnapshot,
head: DisplayPoint,
@@ -461,6 +542,40 @@ fn around_word(
}
}
fn around_subword(
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| {
let is_word_start = classifier.kind(left) != classifier.kind(right);
let is_subword_start = classifier.is_word('-') && left != '-' && right == '-'
|| left != '_' && right == '_'
|| left.is_lowercase() && right.is_uppercase();
is_word_start || is_subword_start
},
);
let end = movement::find_boundary(map, relative_to, FindRange::SingleLine, |left, right| {
let is_word_end = classifier.kind(left) != classifier.kind(right);
let is_subword_end = classifier.is_word('-') && left != '-' && right == '-'
|| left != '_' && right == '_'
|| left.is_lowercase() && right.is_uppercase();
is_word_end || is_subword_end
});
Some(start..end)
}
fn around_containing_word(
map: &DisplaySnapshot,
relative_to: DisplayPoint,

View File

@@ -392,6 +392,20 @@ Subword motion, which allows you to navigate and select individual words in came
]
```
Similarly subword text objects are available, and can be bound with:
```json
[
{
"context": "vim_operator == a || vim_operator == i || vim_operator == cs",
"bindings": {
"w": "vim::Subword",
"shift-w": ["vim::Subword", { "ignorePunctuation": true }],
}
}
]
```
Vim mode comes with shortcuts to surround the selection in normal mode (`ys`), but it doesn't have a shortcut to add surrounds in visual mode. By default, `shift-s` substitutes the selection (erases the text and enters insert mode). To use `shift-s` to add surrounds in visual mode, you can add the following object to your keymap.
```json