Update some editor methods to instead take immutable references (#23578)

Makes the signatures more informative and can be more convenient as
multiple immutable borrows are allowed.

Release Notes:

- N/A
This commit is contained in:
Michael Sloan 2025-01-23 14:52:10 -07:00 committed by GitHub
parent 966533624a
commit 52494f3fdf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 19 additions and 21 deletions

View file

@ -40,7 +40,8 @@ pub use crease_map::*;
pub use fold_map::{Fold, FoldId, FoldPlaceholder, FoldPoint}; pub use fold_map::{Fold, FoldId, FoldPlaceholder, FoldPoint};
use fold_map::{FoldMap, FoldSnapshot}; use fold_map::{FoldMap, FoldSnapshot};
use gpui::{ use gpui::{
AnyElement, Font, HighlightStyle, LineLayout, Model, ModelContext, Pixels, UnderlineStyle, AnyElement, AppContext, Font, HighlightStyle, LineLayout, Model, ModelContext, Pixels,
UnderlineStyle,
}; };
pub use inlay_map::Inlay; pub use inlay_map::Inlay;
use inlay_map::{InlayMap, InlaySnapshot}; use inlay_map::{InlayMap, InlaySnapshot};
@ -543,7 +544,7 @@ impl DisplayMap {
self.block_map.read(snapshot, edits); self.block_map.read(snapshot, edits);
} }
fn tab_size(buffer: &Model<MultiBuffer>, cx: &mut ModelContext<Self>) -> NonZeroU32 { fn tab_size(buffer: &Model<MultiBuffer>, cx: &AppContext) -> NonZeroU32 {
let buffer = buffer.read(cx).as_singleton().map(|buffer| buffer.read(cx)); let buffer = buffer.read(cx).as_singleton().map(|buffer| buffer.read(cx));
let language = buffer let language = buffer
.and_then(|buffer| buffer.language()) .and_then(|buffer| buffer.language())

View file

@ -1579,7 +1579,7 @@ impl Editor {
self.buffer().read(cx).title(cx) self.buffer().read(cx).title(cx)
} }
pub fn snapshot(&mut self, cx: &mut WindowContext) -> EditorSnapshot { pub fn snapshot(&self, cx: &mut WindowContext) -> EditorSnapshot {
let git_blame_gutter_max_author_length = self let git_blame_gutter_max_author_length = self
.render_git_blame_gutter(cx) .render_git_blame_gutter(cx)
.then(|| { .then(|| {
@ -5004,7 +5004,7 @@ impl Editor {
} }
fn inline_completion_menu_hint( fn inline_completion_menu_hint(
&mut self, &self,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Option<InlineCompletionMenuHint> { ) -> Option<InlineCompletionMenuHint> {
let provider = self.inline_completion_provider()?; let provider = self.inline_completion_provider()?;
@ -6185,7 +6185,7 @@ impl Editor {
} }
fn gather_revert_changes( fn gather_revert_changes(
&mut self, &self,
selections: &[Selection<Point>], selections: &[Selection<Point>],
cx: &mut ViewContext<Editor>, cx: &mut ViewContext<Editor>,
) -> HashMap<BufferId, Vec<(Range<text::Anchor>, Rope)>> { ) -> HashMap<BufferId, Vec<(Range<text::Anchor>, Rope)>> {
@ -6198,7 +6198,7 @@ impl Editor {
} }
pub fn prepare_revert_change( pub fn prepare_revert_change(
&mut self, &self,
revert_changes: &mut HashMap<BufferId, Vec<(Range<text::Anchor>, Rope)>>, revert_changes: &mut HashMap<BufferId, Vec<(Range<text::Anchor>, Rope)>>,
hunk: &MultiBufferDiffHunk, hunk: &MultiBufferDiffHunk,
cx: &AppContext, cx: &AppContext,
@ -11721,23 +11721,23 @@ impl Editor {
self.show_git_blame_gutter self.show_git_blame_gutter
} }
pub fn render_git_blame_gutter(&mut self, cx: &mut WindowContext) -> bool { pub fn render_git_blame_gutter(&self, cx: &WindowContext) -> bool {
self.show_git_blame_gutter && self.has_blame_entries(cx) self.show_git_blame_gutter && self.has_blame_entries(cx)
} }
pub fn render_git_blame_inline(&mut self, cx: &mut WindowContext) -> bool { pub fn render_git_blame_inline(&self, cx: &WindowContext) -> bool {
self.show_git_blame_inline self.show_git_blame_inline
&& self.focus_handle.is_focused(cx) && self.focus_handle.is_focused(cx)
&& !self.newest_selection_head_on_empty_line(cx) && !self.newest_selection_head_on_empty_line(cx)
&& self.has_blame_entries(cx) && self.has_blame_entries(cx)
} }
fn has_blame_entries(&self, cx: &mut WindowContext) -> bool { fn has_blame_entries(&self, cx: &WindowContext) -> bool {
self.blame() self.blame()
.map_or(false, |blame| blame.read(cx).has_generated_entries()) .map_or(false, |blame| blame.read(cx).has_generated_entries())
} }
fn newest_selection_head_on_empty_line(&mut self, cx: &mut WindowContext) -> bool { fn newest_selection_head_on_empty_line(&self, cx: &WindowContext) -> bool {
let cursor_anchor = self.selections.newest_anchor().head(); let cursor_anchor = self.selections.newest_anchor().head();
let snapshot = self.buffer.read(cx).snapshot(cx); let snapshot = self.buffer.read(cx).snapshot(cx);
@ -11746,7 +11746,7 @@ impl Editor {
snapshot.line_len(buffer_row) == 0 snapshot.line_len(buffer_row) == 0
} }
fn get_permalink_to_line(&mut self, cx: &mut ViewContext<Self>) -> Task<Result<url::Url>> { fn get_permalink_to_line(&self, cx: &mut ViewContext<Self>) -> Task<Result<url::Url>> {
let buffer_and_selection = maybe!({ let buffer_and_selection = maybe!({
let selection = self.selections.newest::<Point>(cx); let selection = self.selections.newest::<Point>(cx);
let selection_range = selection.range(); let selection_range = selection.range();
@ -12030,10 +12030,7 @@ impl Editor {
/// Merges all anchor ranges for all context types ever set, picking the last highlight added in case of a row conflict. /// Merges all anchor ranges for all context types ever set, picking the last highlight added in case of a row conflict.
/// Returns a map of display rows that are highlighted and their corresponding highlight color. /// Returns a map of display rows that are highlighted and their corresponding highlight color.
/// Allows to ignore certain kinds of highlights. /// Allows to ignore certain kinds of highlights.
pub fn highlighted_display_rows( pub fn highlighted_display_rows(&self, cx: &mut WindowContext) -> BTreeMap<DisplayRow, Hsla> {
&mut self,
cx: &mut WindowContext,
) -> BTreeMap<DisplayRow, Hsla> {
let snapshot = self.snapshot(cx); let snapshot = self.snapshot(cx);
let mut used_highlight_orders = HashMap::default(); let mut used_highlight_orders = HashMap::default();
self.highlighted_rows self.highlighted_rows
@ -12147,7 +12144,7 @@ impl Editor {
#[cfg(feature = "test-support")] #[cfg(feature = "test-support")]
pub fn all_text_background_highlights( pub fn all_text_background_highlights(
&mut self, &self,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Vec<(Range<DisplayPoint>, Hsla)> { ) -> Vec<(Range<DisplayPoint>, Hsla)> {
let snapshot = self.snapshot(cx); let snapshot = self.snapshot(cx);
@ -13204,7 +13201,7 @@ impl Editor {
} }
pub fn to_pixel_point( pub fn to_pixel_point(
&mut self, &self,
source: multi_buffer::Anchor, source: multi_buffer::Anchor,
editor_snapshot: &EditorSnapshot, editor_snapshot: &EditorSnapshot,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,

View file

@ -6,7 +6,7 @@ use git::{
blame::{Blame, BlameEntry}, blame::{Blame, BlameEntry},
parse_git_remote_url, GitHostingProvider, GitHostingProviderRegistry, Oid, PullRequest, parse_git_remote_url, GitHostingProvider, GitHostingProviderRegistry, Oid, PullRequest,
}; };
use gpui::{Model, ModelContext, Subscription, Task}; use gpui::{AppContext, Model, ModelContext, Subscription, Task};
use http_client::HttpClient; use http_client::HttpClient;
use language::{markdown, Bias, Buffer, BufferSnapshot, Edit, LanguageRegistry, ParsedMarkdown}; use language::{markdown, Bias, Buffer, BufferSnapshot, Edit, LanguageRegistry, ParsedMarkdown};
use multi_buffer::MultiBufferRow; use multi_buffer::MultiBufferRow;
@ -195,7 +195,7 @@ impl GitBlame {
pub fn blame_for_rows<'a>( pub fn blame_for_rows<'a>(
&'a mut self, &'a mut self,
rows: impl 'a + IntoIterator<Item = Option<MultiBufferRow>>, rows: impl 'a + IntoIterator<Item = Option<MultiBufferRow>>,
cx: &mut ModelContext<Self>, cx: &AppContext,
) -> impl 'a + Iterator<Item = Option<BlameEntry>> { ) -> impl 'a + Iterator<Item = Option<BlameEntry>> {
self.sync(cx); self.sync(cx);
@ -207,7 +207,7 @@ impl GitBlame {
}) })
} }
pub fn max_author_length(&mut self, cx: &mut ModelContext<Self>) -> usize { pub fn max_author_length(&mut self, cx: &AppContext) -> usize {
self.sync(cx); self.sync(cx);
let mut max_author_length = 0; let mut max_author_length = 0;
@ -240,7 +240,7 @@ impl GitBlame {
} }
} }
fn sync(&mut self, cx: &mut ModelContext<Self>) { fn sync(&mut self, cx: &AppContext) {
let edits = self.buffer_edits.consume(); let edits = self.buffer_edits.consume();
let new_snapshot = self.buffer.read(cx).snapshot(); let new_snapshot = self.buffer.read(cx).snapshot();