-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
helix-core/surround: Syntax
aware
#6893
Open
cole-h
wants to merge
2
commits into
helix-editor:master
Choose a base branch
from
cole-h:syntax-aware-surround
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
use std::fmt::Display; | ||
|
||
use crate::{movement::Direction, search, Range, Selection}; | ||
use crate::{ | ||
match_brackets::find_matching_bracket, movement::Direction, search, Range, Selection, Syntax, | ||
}; | ||
use ropey::RopeSlice; | ||
|
||
pub const PAIRS: &[(char, char)] = &[ | ||
|
@@ -124,6 +126,7 @@ pub fn find_nth_closest_pairs_pos( | |
/// or opening pair. `n` will skip n - 1 pairs (eg. n=2 will discard (only) | ||
/// the first pair found and keep looking) | ||
pub fn find_nth_pairs_pos( | ||
syntax: Option<&Syntax>, | ||
text: RopeSlice, | ||
ch: char, | ||
range: Range, | ||
|
@@ -141,15 +144,26 @@ pub fn find_nth_pairs_pos( | |
|
||
let (open, close) = if open == close { | ||
if Some(open) == text.get_char(pos) { | ||
// Cursor is directly on match char. We return no match | ||
// because there's no way to know which side of the char | ||
// we should be searching on. | ||
return Err(Error::CursorOnAmbiguousPair); | ||
// Cursor is directly on match char and we don't have any tree-sitter syntax available | ||
// to help identify the matching pair. We return no match because there's no way to know | ||
// which side of the char we should be searching on. | ||
let syntax = syntax.ok_or(Error::CursorOnAmbiguousPair)?; | ||
|
||
let match_pos = find_matching_bracket(syntax, &text.into(), pos) | ||
.ok_or(Error::CursorOnAmbiguousPair)?; | ||
let close_char = text.char(match_pos); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should be reading the close_char again here. If we run into some edgecase where |
||
let search_pos = pos.max(match_pos); | ||
|
||
( | ||
search::find_nth_prev(text, open, search_pos, n), | ||
search::find_nth_next(text, close_char, search_pos, n), | ||
) | ||
} else { | ||
( | ||
search::find_nth_prev(text, open, pos, n), | ||
search::find_nth_next(text, close, pos, n), | ||
) | ||
} | ||
( | ||
search::find_nth_prev(text, open, pos, n), | ||
search::find_nth_next(text, close, pos, n), | ||
) | ||
} else { | ||
( | ||
find_nth_open_pair(text, open, close, pos, n), | ||
|
@@ -245,6 +259,7 @@ fn find_nth_close_pair( | |
/// are automatically detected around each cursor (note that this may result | ||
/// in them selecting different surround characters for each selection). | ||
pub fn get_surround_pos( | ||
syntax: Option<&Syntax>, | ||
text: RopeSlice, | ||
selection: &Selection, | ||
ch: Option<char>, | ||
|
@@ -254,7 +269,7 @@ pub fn get_surround_pos( | |
|
||
for &range in selection { | ||
let (open_pos, close_pos) = match ch { | ||
Some(ch) => find_nth_pairs_pos(text, ch, range, skip)?, | ||
Some(ch) => find_nth_pairs_pos(syntax, text, ch, range, skip)?, | ||
None => find_nth_closest_pairs_pos(text, range, skip)?, | ||
}; | ||
if change_pos.contains(&open_pos) || change_pos.contains(&close_pos) { | ||
|
@@ -283,7 +298,7 @@ mod test { | |
); | ||
|
||
assert_eq!( | ||
get_surround_pos(doc.slice(..), &selection, Some('('), 1).unwrap(), | ||
get_surround_pos(None, doc.slice(..), &selection, Some('('), 1).unwrap(), | ||
expectations | ||
); | ||
} | ||
|
@@ -298,7 +313,7 @@ mod test { | |
); | ||
|
||
assert_eq!( | ||
get_surround_pos(doc.slice(..), &selection, Some('('), 1), | ||
get_surround_pos(None, doc.slice(..), &selection, Some('('), 1), | ||
Err(Error::PairNotFound) | ||
); | ||
} | ||
|
@@ -313,7 +328,7 @@ mod test { | |
); | ||
|
||
assert_eq!( | ||
get_surround_pos(doc.slice(..), &selection, Some('('), 1), | ||
get_surround_pos(None, doc.slice(..), &selection, Some('('), 1), | ||
Err(Error::PairNotFound) // overlapping surround chars | ||
); | ||
} | ||
|
@@ -328,7 +343,7 @@ mod test { | |
); | ||
|
||
assert_eq!( | ||
get_surround_pos(doc.slice(..), &selection, Some('['), 1), | ||
get_surround_pos(None, doc.slice(..), &selection, Some('['), 1), | ||
Err(Error::CursorOverlap) | ||
); | ||
} | ||
|
@@ -344,7 +359,7 @@ mod test { | |
|
||
assert_eq!(2, expectations.len()); | ||
assert_eq!( | ||
find_nth_pairs_pos(doc.slice(..), '\'', selection.primary(), 1) | ||
find_nth_pairs_pos(None, doc.slice(..), '\'', selection.primary(), 1) | ||
.expect("find should succeed"), | ||
(expectations[0], expectations[1]) | ||
) | ||
|
@@ -361,7 +376,7 @@ mod test { | |
|
||
assert_eq!(2, expectations.len()); | ||
assert_eq!( | ||
find_nth_pairs_pos(doc.slice(..), '\'', selection.primary(), 2) | ||
find_nth_pairs_pos(None, doc.slice(..), '\'', selection.primary(), 2) | ||
.expect("find should succeed"), | ||
(expectations[0], expectations[1]) | ||
) | ||
|
@@ -377,7 +392,7 @@ mod test { | |
); | ||
|
||
assert_eq!( | ||
find_nth_pairs_pos(doc.slice(..), '\'', selection.primary(), 1), | ||
find_nth_pairs_pos(None, doc.slice(..), '\'', selection.primary(), 1), | ||
Err(Error::CursorOnAmbiguousPair) | ||
) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,4 +21,5 @@ mod test { | |
mod movement; | ||
mod prompt; | ||
mod splits; | ||
mod surround; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use super::*; | ||
|
||
#[tokio::test(flavor = "multi_thread")] | ||
async fn replace_matching_double_quotes_with_treesitter() -> anyhow::Result<()> { | ||
test_with_config( | ||
AppBuilder::new().with_file("foo.rs", None), | ||
( | ||
helpers::platform_line(r#"#["|]#hello world""#), | ||
r#"mr"{"#, | ||
helpers::platform_line(r#"#[{|]#hello world}"#), | ||
), | ||
) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::test(flavor = "multi_thread")] | ||
async fn delete_matching_double_quotes_with_treesitter() -> anyhow::Result<()> { | ||
test_with_config( | ||
AppBuilder::new().with_file("foo.rs", None), | ||
( | ||
helpers::platform_line(r#"#["|]#hello world""#), | ||
r#"md""#, | ||
helpers::platform_line(r#"#[h|]#ello world"#), | ||
), | ||
) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::test(flavor = "multi_thread")] | ||
async fn replace_matching_single_quotes_with_treesitter() -> anyhow::Result<()> { | ||
test_with_config( | ||
AppBuilder::new().with_file("foo.rs", None), | ||
( | ||
helpers::platform_line(r#"#['|]#h'"#), | ||
r#"mr'""#, | ||
helpers::platform_line(r#"#["|]#h""#), | ||
), | ||
) | ||
.await?; | ||
|
||
Ok(()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is the source of the current merge conflict. The RopeSlice can be passed on through now.