Avoid suggesting search query if cursor is not on a word

This commit is contained in:
Max Brunsfeld 2023-11-09 12:33:30 -08:00
parent c6b76d908f
commit dba41e99dd
2 changed files with 22 additions and 18 deletions

View file

@ -1,7 +1,7 @@
use crate::{ use crate::{
display_map::ToDisplayPoint, link_go_to_definition::hide_link_definition, link_go_to_definition::hide_link_definition, persistence::DB, scroll::ScrollAnchor, Anchor,
movement::surrounding_word, persistence::DB, scroll::ScrollAnchor, Anchor, Autoscroll, Editor, Autoscroll, Editor, Event, ExcerptId, ExcerptRange, MultiBuffer, MultiBufferSnapshot,
Event, ExcerptId, ExcerptRange, MultiBuffer, MultiBufferSnapshot, NavigationData, ToPoint as _, NavigationData, ToPoint as _,
}; };
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use collections::HashSet; use collections::HashSet;
@ -13,8 +13,8 @@ use gpui::{
ViewHandle, WeakViewHandle, ViewHandle, WeakViewHandle,
}; };
use language::{ use language::{
proto::serialize_anchor as serialize_text_anchor, Bias, Buffer, OffsetRangeExt, Point, proto::serialize_anchor as serialize_text_anchor, Bias, Buffer, CharKind, OffsetRangeExt,
SelectionGoal, Point, SelectionGoal,
}; };
use project::{search::SearchQuery, FormatTrigger, Item as _, Project, ProjectPath}; use project::{search::SearchQuery, FormatTrigger, Item as _, Project, ProjectPath};
use rpc::proto::{self, update_view, PeerId}; use rpc::proto::{self, update_view, PeerId};
@ -940,10 +940,12 @@ impl SearchableItem for Editor {
let display_map = self.snapshot(cx).display_snapshot; let display_map = self.snapshot(cx).display_snapshot;
let selection = self.selections.newest::<usize>(cx); let selection = self.selections.newest::<usize>(cx);
if selection.start == selection.end { if selection.start == selection.end {
let point = selection.start.to_display_point(&display_map); let (range, kind) = display_map
let range = surrounding_word(&display_map, point); .buffer_snapshot
let range = range.start.to_offset(&display_map, Bias::Left) .surrounding_word(selection.start);
..range.end.to_offset(&display_map, Bias::Right); if kind != Some(CharKind::Word) {
return String::new();
}
let text: String = display_map.buffer_snapshot.text_for_range(range).collect(); let text: String = display_map.buffer_snapshot.text_for_range(range).collect();
if text.trim().is_empty() { if text.trim().is_empty() {
String::new() String::new()

View file

@ -1,7 +1,7 @@
use crate::{ use crate::{
display_map::ToDisplayPoint, link_go_to_definition::hide_link_definition, link_go_to_definition::hide_link_definition, movement::surrounding_word, persistence::DB,
movement::surrounding_word, persistence::DB, scroll::ScrollAnchor, Anchor, Autoscroll, Editor, scroll::ScrollAnchor, Anchor, Autoscroll, Editor, Event, ExcerptId, ExcerptRange, MultiBuffer,
Event, ExcerptId, ExcerptRange, MultiBuffer, MultiBufferSnapshot, NavigationData, ToPoint as _, MultiBufferSnapshot, NavigationData, ToPoint as _,
}; };
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
use collections::HashSet; use collections::HashSet;
@ -12,8 +12,8 @@ use gpui::{
ViewContext, VisualContext, WeakView, ViewContext, VisualContext, WeakView,
}; };
use language::{ use language::{
proto::serialize_anchor as serialize_text_anchor, Bias, Buffer, OffsetRangeExt, Point, proto::serialize_anchor as serialize_text_anchor, Bias, Buffer, CharKind, OffsetRangeExt,
SelectionGoal, Point, SelectionGoal,
}; };
use project::{search::SearchQuery, FormatTrigger, Item as _, Project, ProjectPath}; use project::{search::SearchQuery, FormatTrigger, Item as _, Project, ProjectPath};
use rpc::proto::{self, update_view, PeerId}; use rpc::proto::{self, update_view, PeerId};
@ -921,10 +921,12 @@ impl SearchableItem for Editor {
let display_map = self.snapshot(cx).display_snapshot; let display_map = self.snapshot(cx).display_snapshot;
let selection = self.selections.newest::<usize>(cx); let selection = self.selections.newest::<usize>(cx);
if selection.start == selection.end { if selection.start == selection.end {
let point = selection.start.to_display_point(&display_map); let (range, kind) = display_map
let range = surrounding_word(&display_map, point); .buffer_snapshot
let range = range.start.to_offset(&display_map, Bias::Left) .surrounding_word(selection.start);
..range.end.to_offset(&display_map, Bias::Right); if kind != Some(CharKind::Word) {
return String::new();
}
let text: String = display_map.buffer_snapshot.text_for_range(range).collect(); let text: String = display_map.buffer_snapshot.text_for_range(range).collect();
if text.trim().is_empty() { if text.trim().is_empty() {
String::new() String::new()