mirror of
https://github.com/zed-industries/zed.git
synced 2024-11-08 07:35:01 +03:00
Add "Select Enclosing Symbol" command (#13435)
I use this for a much faster workflow with inline assist when using fast models. Release Notes: - Added "Select Enclosing Symbol" command based on tree-sitter outline. Useful in combination with inline assist to rewrite a function.
This commit is contained in:
parent
7be1ffb9ec
commit
f96e4ba84f
@ -152,7 +152,8 @@
|
|||||||
// "focus": false
|
// "focus": false
|
||||||
// }
|
// }
|
||||||
// ],
|
// ],
|
||||||
"ctrl->": "assistant::QuoteSelection"
|
"ctrl->": "assistant::QuoteSelection",
|
||||||
|
"ctrl-alt-e": "editor::SelectEnclosingSymbol"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -188,7 +188,8 @@
|
|||||||
"focus": false
|
"focus": false
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"cmd->": "assistant::QuoteSelection"
|
"cmd->": "assistant::QuoteSelection",
|
||||||
|
"cmd-alt-e": "editor::SelectEnclosingSymbol"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -268,6 +268,7 @@ gpui::actions!(
|
|||||||
SelectAllMatches,
|
SelectAllMatches,
|
||||||
SelectDown,
|
SelectDown,
|
||||||
SelectLargerSyntaxNode,
|
SelectLargerSyntaxNode,
|
||||||
|
SelectEnclosingSymbol,
|
||||||
SelectLeft,
|
SelectLeft,
|
||||||
SelectLine,
|
SelectLine,
|
||||||
SelectRight,
|
SelectRight,
|
||||||
|
@ -8226,6 +8226,58 @@ impl Editor {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn select_enclosing_symbol(
|
||||||
|
&mut self,
|
||||||
|
_: &SelectEnclosingSymbol,
|
||||||
|
cx: &mut ViewContext<Self>,
|
||||||
|
) {
|
||||||
|
let buffer = self.buffer.read(cx).snapshot(cx);
|
||||||
|
let old_selections = self.selections.all::<usize>(cx).into_boxed_slice();
|
||||||
|
|
||||||
|
fn update_selection(
|
||||||
|
selection: &Selection<usize>,
|
||||||
|
buffer_snap: &MultiBufferSnapshot,
|
||||||
|
) -> Option<Selection<usize>> {
|
||||||
|
let cursor = selection.head();
|
||||||
|
let (_buffer_id, symbols) = buffer_snap.symbols_containing(cursor, None)?;
|
||||||
|
for symbol in symbols.iter().rev() {
|
||||||
|
let start = symbol.range.start.to_offset(&buffer_snap);
|
||||||
|
let end = symbol.range.end.to_offset(&buffer_snap);
|
||||||
|
let new_range = start..end;
|
||||||
|
if start < selection.start || end > selection.end {
|
||||||
|
return Some(Selection {
|
||||||
|
id: selection.id,
|
||||||
|
start: new_range.start,
|
||||||
|
end: new_range.end,
|
||||||
|
goal: SelectionGoal::None,
|
||||||
|
reversed: selection.reversed,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut selected_larger_symbol = false;
|
||||||
|
let new_selections = old_selections
|
||||||
|
.iter()
|
||||||
|
.map(|selection| match update_selection(selection, &buffer) {
|
||||||
|
Some(new_selection) => {
|
||||||
|
if new_selection.range() != selection.range() {
|
||||||
|
selected_larger_symbol = true;
|
||||||
|
}
|
||||||
|
new_selection
|
||||||
|
}
|
||||||
|
None => selection.clone(),
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
if selected_larger_symbol {
|
||||||
|
self.change_selections(Some(Autoscroll::fit()), cx, |s| {
|
||||||
|
s.select(new_selections);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn select_larger_syntax_node(
|
pub fn select_larger_syntax_node(
|
||||||
&mut self,
|
&mut self,
|
||||||
_: &SelectLargerSyntaxNode,
|
_: &SelectLargerSyntaxNode,
|
||||||
|
@ -276,6 +276,7 @@ impl EditorElement {
|
|||||||
register_action(view, cx, Editor::toggle_comments);
|
register_action(view, cx, Editor::toggle_comments);
|
||||||
register_action(view, cx, Editor::select_larger_syntax_node);
|
register_action(view, cx, Editor::select_larger_syntax_node);
|
||||||
register_action(view, cx, Editor::select_smaller_syntax_node);
|
register_action(view, cx, Editor::select_smaller_syntax_node);
|
||||||
|
register_action(view, cx, Editor::select_enclosing_symbol);
|
||||||
register_action(view, cx, Editor::move_to_enclosing_bracket);
|
register_action(view, cx, Editor::move_to_enclosing_bracket);
|
||||||
register_action(view, cx, Editor::undo_selection);
|
register_action(view, cx, Editor::undo_selection);
|
||||||
register_action(view, cx, Editor::redo_selection);
|
register_action(view, cx, Editor::redo_selection);
|
||||||
|
Loading…
Reference in New Issue
Block a user