Implement buffer::move_to_enclosing_bracket
This commit is contained in:
parent
276991fb5f
commit
e9bd872b06
2 changed files with 35 additions and 15 deletions
|
@ -1,6 +1,6 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
editor::{
|
editor::{
|
||||||
buffer::{Anchor, Buffer, Point, ToPoint},
|
buffer::{Anchor, Buffer, Point, ToOffset as _, ToPoint as _},
|
||||||
display_map::DisplayMap,
|
display_map::DisplayMap,
|
||||||
Bias, DisplayPoint,
|
Bias, DisplayPoint,
|
||||||
},
|
},
|
||||||
|
@ -61,7 +61,7 @@ impl Selection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn range(&self, buffer: &Buffer) -> Range<Point> {
|
pub fn point_range(&self, buffer: &Buffer) -> Range<Point> {
|
||||||
let start = self.start.to_point(buffer);
|
let start = self.start.to_point(buffer);
|
||||||
let end = self.end.to_point(buffer);
|
let end = self.end.to_point(buffer);
|
||||||
if self.reversed {
|
if self.reversed {
|
||||||
|
@ -71,6 +71,16 @@ impl Selection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn offset_range(&self, buffer: &Buffer) -> Range<usize> {
|
||||||
|
let start = self.start.to_offset(buffer);
|
||||||
|
let end = self.end.to_offset(buffer);
|
||||||
|
if self.reversed {
|
||||||
|
end..start
|
||||||
|
} else {
|
||||||
|
start..end
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn display_range(&self, map: &DisplayMap, app: &AppContext) -> Range<DisplayPoint> {
|
pub fn display_range(&self, map: &DisplayMap, app: &AppContext) -> Range<DisplayPoint> {
|
||||||
let start = self.start.to_display_point(map, app);
|
let start = self.start.to_display_point(map, app);
|
||||||
let end = self.end.to_display_point(map, app);
|
let end = self.end.to_display_point(map, app);
|
||||||
|
|
|
@ -178,7 +178,7 @@ pub fn init(app: &mut MutableAppContext) {
|
||||||
),
|
),
|
||||||
Binding::new(
|
Binding::new(
|
||||||
"ctrl-m",
|
"ctrl-m",
|
||||||
"buffer:jump_to_enclosing_bracket",
|
"buffer:move_to_enclosing_bracket",
|
||||||
Some("BufferView"),
|
Some("BufferView"),
|
||||||
),
|
),
|
||||||
Binding::new("pageup", "buffer:page_up", Some("BufferView")),
|
Binding::new("pageup", "buffer:page_up", Some("BufferView")),
|
||||||
|
@ -724,7 +724,7 @@ impl BufferView {
|
||||||
{
|
{
|
||||||
let buffer = self.buffer.read(ctx);
|
let buffer = self.buffer.read(ctx);
|
||||||
for selection in &mut selections {
|
for selection in &mut selections {
|
||||||
let range = selection.range(buffer);
|
let range = selection.point_range(buffer);
|
||||||
if range.start == range.end {
|
if range.start == range.end {
|
||||||
let head = selection
|
let head = selection
|
||||||
.head()
|
.head()
|
||||||
|
@ -751,7 +751,7 @@ impl BufferView {
|
||||||
{
|
{
|
||||||
let buffer = self.buffer.read(ctx);
|
let buffer = self.buffer.read(ctx);
|
||||||
for selection in &mut selections {
|
for selection in &mut selections {
|
||||||
let range = selection.range(buffer);
|
let range = selection.point_range(buffer);
|
||||||
if range.start == range.end {
|
if range.start == range.end {
|
||||||
let head = selection
|
let head = selection
|
||||||
.head()
|
.head()
|
||||||
|
@ -930,7 +930,7 @@ impl BufferView {
|
||||||
let mut contiguous_selections = Vec::new();
|
let mut contiguous_selections = Vec::new();
|
||||||
while let Some(selection) = selections.next() {
|
while let Some(selection) = selections.next() {
|
||||||
// Accumulate contiguous regions of rows that we want to move.
|
// Accumulate contiguous regions of rows that we want to move.
|
||||||
contiguous_selections.push(selection.range(buffer));
|
contiguous_selections.push(selection.point_range(buffer));
|
||||||
let (mut buffer_rows, mut display_rows) =
|
let (mut buffer_rows, mut display_rows) =
|
||||||
selection.buffer_rows_for_display_rows(false, &self.display_map, app);
|
selection.buffer_rows_for_display_rows(false, &self.display_map, app);
|
||||||
while let Some(next_selection) = selections.peek() {
|
while let Some(next_selection) = selections.peek() {
|
||||||
|
@ -939,7 +939,7 @@ impl BufferView {
|
||||||
if next_buffer_rows.start <= buffer_rows.end {
|
if next_buffer_rows.start <= buffer_rows.end {
|
||||||
buffer_rows.end = next_buffer_rows.end;
|
buffer_rows.end = next_buffer_rows.end;
|
||||||
display_rows.end = next_display_rows.end;
|
display_rows.end = next_display_rows.end;
|
||||||
contiguous_selections.push(next_selection.range(buffer));
|
contiguous_selections.push(next_selection.point_range(buffer));
|
||||||
selections.next().unwrap();
|
selections.next().unwrap();
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
|
@ -1014,7 +1014,7 @@ impl BufferView {
|
||||||
let mut contiguous_selections = Vec::new();
|
let mut contiguous_selections = Vec::new();
|
||||||
while let Some(selection) = selections.next() {
|
while let Some(selection) = selections.next() {
|
||||||
// Accumulate contiguous regions of rows that we want to move.
|
// Accumulate contiguous regions of rows that we want to move.
|
||||||
contiguous_selections.push(selection.range(buffer));
|
contiguous_selections.push(selection.point_range(buffer));
|
||||||
let (mut buffer_rows, mut display_rows) =
|
let (mut buffer_rows, mut display_rows) =
|
||||||
selection.buffer_rows_for_display_rows(false, &self.display_map, app);
|
selection.buffer_rows_for_display_rows(false, &self.display_map, app);
|
||||||
while let Some(next_selection) = selections.peek() {
|
while let Some(next_selection) = selections.peek() {
|
||||||
|
@ -1023,7 +1023,7 @@ impl BufferView {
|
||||||
if next_buffer_rows.start <= buffer_rows.end {
|
if next_buffer_rows.start <= buffer_rows.end {
|
||||||
buffer_rows.end = next_buffer_rows.end;
|
buffer_rows.end = next_buffer_rows.end;
|
||||||
display_rows.end = next_display_rows.end;
|
display_rows.end = next_display_rows.end;
|
||||||
contiguous_selections.push(next_selection.range(buffer));
|
contiguous_selections.push(next_selection.point_range(buffer));
|
||||||
selections.next().unwrap();
|
selections.next().unwrap();
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
|
@ -1647,7 +1647,7 @@ impl BufferView {
|
||||||
let mut to_unfold = Vec::new();
|
let mut to_unfold = Vec::new();
|
||||||
let mut new_selections = Vec::new();
|
let mut new_selections = Vec::new();
|
||||||
for selection in self.selections(app) {
|
for selection in self.selections(app) {
|
||||||
let range = selection.range(buffer).sorted();
|
let range = selection.point_range(buffer).sorted();
|
||||||
if range.start.row != range.end.row {
|
if range.start.row != range.end.row {
|
||||||
new_selections.push(Selection {
|
new_selections.push(Selection {
|
||||||
id: post_inc(&mut self.next_selection_id),
|
id: post_inc(&mut self.next_selection_id),
|
||||||
|
@ -1826,13 +1826,23 @@ impl BufferView {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn move_to_enclosing_bracket(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
|
pub fn move_to_enclosing_bracket(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
|
||||||
use super::RangeExt as _;
|
|
||||||
|
|
||||||
let buffer = self.buffer.read(ctx.as_ref());
|
let buffer = self.buffer.read(ctx.as_ref());
|
||||||
let mut selections = self.selections(ctx.as_ref()).to_vec();
|
let mut selections = self.selections(ctx.as_ref()).to_vec();
|
||||||
for selection in &mut selections {
|
for selection in &mut selections {
|
||||||
let range = selection.range(buffer).sorted();
|
let selection_range = selection.offset_range(buffer);
|
||||||
if let Some((open_range, close_range)) = buffer.enclosing_bracket_ranges(range) {}
|
if let Some((open_range, close_range)) =
|
||||||
|
buffer.enclosing_bracket_ranges(selection_range.clone())
|
||||||
|
{
|
||||||
|
let destination = if close_range.contains(&selection_range.start)
|
||||||
|
&& close_range.contains(&selection_range.end)
|
||||||
|
{
|
||||||
|
open_range.end
|
||||||
|
} else {
|
||||||
|
close_range.start
|
||||||
|
};
|
||||||
|
selection.start = buffer.anchor_before(destination);
|
||||||
|
selection.end = selection.start.clone();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.update_selections(selections, true, ctx);
|
self.update_selections(selections, true, ctx);
|
||||||
|
@ -2080,7 +2090,7 @@ impl BufferView {
|
||||||
let ranges = self
|
let ranges = self
|
||||||
.selections(ctx.as_ref())
|
.selections(ctx.as_ref())
|
||||||
.iter()
|
.iter()
|
||||||
.map(|s| s.range(buffer).sorted())
|
.map(|s| s.point_range(buffer).sorted())
|
||||||
.collect();
|
.collect();
|
||||||
self.fold_ranges(ranges, ctx);
|
self.fold_ranges(ranges, ctx);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue