There's still a bit more work to do on this, but this PR is compiling (with warnings) after eliminating the key types. When the tasks below are complete, this will be the new narrative for GPUI: - `Entity<T>` - This replaces `View<T>`/`Model<T>`. It represents a unit of state, and if `T` implements `Render`, then `Entity<T>` implements `Element`. - `&mut App` This replaces `AppContext` and represents the app. - `&mut Context<T>` This replaces `ModelContext` and derefs to `App`. It is provided by the framework when updating an entity. - `&mut Window` Broken out of `&mut WindowContext` which no longer exists. Every method that once took `&mut WindowContext` now takes `&mut Window, &mut App` and every method that took `&mut ViewContext<T>` now takes `&mut Window, &mut Context<T>` Not pictured here are the two other failed attempts. It's been quite a month! Tasks: - [x] Remove `View`, `ViewContext`, `WindowContext` and thread through `Window` - [x] [@cole-miller @mikayla-maki] Redraw window when entities change - [x] [@cole-miller @mikayla-maki] Get examples and Zed running - [x] [@cole-miller @mikayla-maki] Fix Zed rendering - [x] [@mikayla-maki] Fix todo! macros and comments - [x] Fix a bug where the editor would not be redrawn because of view caching - [x] remove publicness window.notify() and replace with `AppContext::notify` - [x] remove `observe_new_window_models`, replace with `observe_new_models` with an optional window - [x] Fix a bug where the project panel would not be redrawn because of the wrong refresh() call being used - [x] Fix the tests - [x] Fix warnings by eliminating `Window` params or using `_` - [x] Fix conflicts - [x] Simplify generic code where possible - [x] Rename types - [ ] Update docs ### issues post merge - [x] Issues switching between normal and insert mode - [x] Assistant re-rendering failure - [x] Vim test failures - [x] Mac build issue Release Notes: - N/A --------- Co-authored-by: Antonio Scandurra <me@as-cii.com> Co-authored-by: Cole Miller <cole@zed.dev> Co-authored-by: Mikayla <mikayla@zed.dev> Co-authored-by: Joseph <joseph@zed.dev> Co-authored-by: max <max@zed.dev> Co-authored-by: Michael Sloan <michael@zed.dev> Co-authored-by: Mikayla Maki <mikaylamaki@Mikaylas-MacBook-Pro.local> Co-authored-by: Mikayla <mikayla.c.maki@gmail.com> Co-authored-by: joão <joao@zed.dev>
235 lines
9.0 KiB
Rust
235 lines
9.0 KiB
Rust
mod popover;
|
||
mod state;
|
||
|
||
use crate::actions::ShowSignatureHelp;
|
||
use crate::{Editor, EditorSettings, ToggleAutoSignatureHelp};
|
||
use gpui::{App, Context, Window};
|
||
use language::markdown::parse_markdown;
|
||
use language::BufferSnapshot;
|
||
use multi_buffer::{Anchor, ToOffset};
|
||
use settings::Settings;
|
||
use std::ops::Range;
|
||
|
||
pub use popover::SignatureHelpPopover;
|
||
pub use state::SignatureHelpState;
|
||
|
||
// Language-specific settings may define quotes as "brackets", so filter them out separately.
|
||
const QUOTE_PAIRS: [(&str, &str); 3] = [("'", "'"), ("\"", "\""), ("`", "`")];
|
||
|
||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||
pub enum SignatureHelpHiddenBy {
|
||
AutoClose,
|
||
Escape,
|
||
Selection,
|
||
}
|
||
|
||
impl Editor {
|
||
pub fn toggle_auto_signature_help_menu(
|
||
&mut self,
|
||
_: &ToggleAutoSignatureHelp,
|
||
window: &mut Window,
|
||
cx: &mut Context<Self>,
|
||
) {
|
||
self.auto_signature_help = self
|
||
.auto_signature_help
|
||
.map(|auto_signature_help| !auto_signature_help)
|
||
.or_else(|| Some(!EditorSettings::get_global(cx).auto_signature_help));
|
||
match self.auto_signature_help {
|
||
Some(auto_signature_help) if auto_signature_help => {
|
||
self.show_signature_help(&ShowSignatureHelp, window, cx);
|
||
}
|
||
Some(_) => {
|
||
self.hide_signature_help(cx, SignatureHelpHiddenBy::AutoClose);
|
||
}
|
||
None => {}
|
||
}
|
||
cx.notify();
|
||
}
|
||
|
||
pub(super) fn hide_signature_help(
|
||
&mut self,
|
||
cx: &mut Context<Self>,
|
||
signature_help_hidden_by: SignatureHelpHiddenBy,
|
||
) -> bool {
|
||
if self.signature_help_state.is_shown() {
|
||
self.signature_help_state.kill_task();
|
||
self.signature_help_state.hide(signature_help_hidden_by);
|
||
cx.notify();
|
||
true
|
||
} else {
|
||
false
|
||
}
|
||
}
|
||
|
||
pub fn auto_signature_help_enabled(&self, cx: &App) -> bool {
|
||
if let Some(auto_signature_help) = self.auto_signature_help {
|
||
auto_signature_help
|
||
} else {
|
||
EditorSettings::get_global(cx).auto_signature_help
|
||
}
|
||
}
|
||
|
||
pub(super) fn should_open_signature_help_automatically(
|
||
&mut self,
|
||
old_cursor_position: &Anchor,
|
||
backspace_pressed: bool,
|
||
|
||
cx: &mut Context<Self>,
|
||
) -> bool {
|
||
if !(self.signature_help_state.is_shown() || self.auto_signature_help_enabled(cx)) {
|
||
return false;
|
||
}
|
||
let newest_selection = self.selections.newest::<usize>(cx);
|
||
let head = newest_selection.head();
|
||
|
||
// There are two cases where the head and tail of a selection are different: selecting multiple ranges and using backspace.
|
||
// If we don’t exclude the backspace case, signature_help will blink every time backspace is pressed, so we need to prevent this.
|
||
if !newest_selection.is_empty() && !backspace_pressed && head != newest_selection.tail() {
|
||
self.signature_help_state
|
||
.hide(SignatureHelpHiddenBy::Selection);
|
||
return false;
|
||
}
|
||
|
||
let buffer_snapshot = self.buffer().read(cx).snapshot(cx);
|
||
let bracket_range = |position: usize| match (position, position + 1) {
|
||
(0, b) if b <= buffer_snapshot.len() => 0..b,
|
||
(0, b) => 0..b - 1,
|
||
(a, b) if b <= buffer_snapshot.len() => a - 1..b,
|
||
(a, b) => a - 1..b - 1,
|
||
};
|
||
let not_quote_like_brackets =
|
||
|buffer: &BufferSnapshot, start: Range<usize>, end: Range<usize>| {
|
||
let text_start = buffer.text_for_range(start).collect::<String>();
|
||
let text_end = buffer.text_for_range(end).collect::<String>();
|
||
QUOTE_PAIRS
|
||
.into_iter()
|
||
.all(|(start, end)| text_start != start && text_end != end)
|
||
};
|
||
|
||
let previous_position = old_cursor_position.to_offset(&buffer_snapshot);
|
||
let previous_brackets_range = bracket_range(previous_position);
|
||
let previous_brackets_surround = buffer_snapshot
|
||
.innermost_enclosing_bracket_ranges(
|
||
previous_brackets_range,
|
||
Some(¬_quote_like_brackets),
|
||
)
|
||
.filter(|(start_bracket_range, end_bracket_range)| {
|
||
start_bracket_range.start != previous_position
|
||
&& end_bracket_range.end != previous_position
|
||
});
|
||
let current_brackets_range = bracket_range(head);
|
||
let current_brackets_surround = buffer_snapshot
|
||
.innermost_enclosing_bracket_ranges(
|
||
current_brackets_range,
|
||
Some(¬_quote_like_brackets),
|
||
)
|
||
.filter(|(start_bracket_range, end_bracket_range)| {
|
||
start_bracket_range.start != head && end_bracket_range.end != head
|
||
});
|
||
|
||
match (previous_brackets_surround, current_brackets_surround) {
|
||
(None, None) => {
|
||
self.signature_help_state
|
||
.hide(SignatureHelpHiddenBy::AutoClose);
|
||
false
|
||
}
|
||
(Some(_), None) => {
|
||
self.signature_help_state
|
||
.hide(SignatureHelpHiddenBy::AutoClose);
|
||
false
|
||
}
|
||
(None, Some(_)) => true,
|
||
(Some(previous), Some(current)) => {
|
||
let condition = self.signature_help_state.hidden_by_selection()
|
||
|| previous != current
|
||
|| (previous == current && self.signature_help_state.is_shown());
|
||
if !condition {
|
||
self.signature_help_state
|
||
.hide(SignatureHelpHiddenBy::AutoClose);
|
||
}
|
||
condition
|
||
}
|
||
}
|
||
}
|
||
|
||
pub fn show_signature_help(
|
||
&mut self,
|
||
_: &ShowSignatureHelp,
|
||
window: &mut Window,
|
||
cx: &mut Context<Self>,
|
||
) {
|
||
if self.pending_rename.is_some() || self.has_active_completions_menu() {
|
||
return;
|
||
}
|
||
|
||
let position = self.selections.newest_anchor().head();
|
||
let Some((buffer, buffer_position)) =
|
||
self.buffer.read(cx).text_anchor_for_position(position, cx)
|
||
else {
|
||
return;
|
||
};
|
||
|
||
self.signature_help_state
|
||
.set_task(cx.spawn_in(window, move |editor, mut cx| async move {
|
||
let signature_help = editor
|
||
.update(&mut cx, |editor, cx| {
|
||
let language = editor.language_at(position, cx);
|
||
let project = editor.project.clone()?;
|
||
let (markdown, language_registry) = {
|
||
project.update(cx, |project, cx| {
|
||
let language_registry = project.languages().clone();
|
||
(
|
||
project.signature_help(&buffer, buffer_position, cx),
|
||
language_registry,
|
||
)
|
||
})
|
||
};
|
||
Some((markdown, language_registry, language))
|
||
})
|
||
.ok()
|
||
.flatten();
|
||
let signature_help_popover = if let Some((
|
||
signature_help_task,
|
||
language_registry,
|
||
language,
|
||
)) = signature_help
|
||
{
|
||
// TODO allow multiple signature helps inside the same popover
|
||
if let Some(mut signature_help) = signature_help_task.await.into_iter().next() {
|
||
let mut parsed_content = parse_markdown(
|
||
signature_help.markdown.as_str(),
|
||
Some(&language_registry),
|
||
language,
|
||
)
|
||
.await;
|
||
parsed_content
|
||
.highlights
|
||
.append(&mut signature_help.highlights);
|
||
Some(SignatureHelpPopover { parsed_content })
|
||
} else {
|
||
None
|
||
}
|
||
} else {
|
||
None
|
||
};
|
||
editor
|
||
.update(&mut cx, |editor, cx| {
|
||
let previous_popover = editor.signature_help_state.popover();
|
||
if previous_popover != signature_help_popover.as_ref() {
|
||
if let Some(signature_help_popover) = signature_help_popover {
|
||
editor
|
||
.signature_help_state
|
||
.set_popover(signature_help_popover);
|
||
} else {
|
||
editor
|
||
.signature_help_state
|
||
.hide(SignatureHelpHiddenBy::AutoClose);
|
||
}
|
||
cx.notify();
|
||
}
|
||
})
|
||
.ok();
|
||
}));
|
||
}
|
||
}
|