Fix - being a word character for selections (#17171)

Co-Authored-By: Mikayla <mikayla@zed.dev>
Co-Authored-By: Nate <nate@zed.dev>

Closes #15606
Closes #13515

Release Notes:

- Fixes `-` being considered a word character for selections in some
languages

Co-authored-by: Mikayla <mikayla@zed.dev>
Co-authored-by: Nate <nate@zed.dev>
This commit is contained in:
Conrad Irwin
2024-08-30 12:34:23 -06:00
committed by GitHub
co-authored by Mikayla Nate
parent c0731bfa28
commit ee6ec50b15
12 changed files with 239 additions and 143 deletions
+13 -11
View File
@@ -89,13 +89,12 @@ pub use inline_completion_provider::*;
pub use items::MAX_TAB_TITLE_LEN;
use itertools::Itertools;
use language::{
char_kind,
language_settings::{self, all_language_settings, InlayHintSettings},
markdown, point_from_lsp, AutoindentMode, BracketPair, Buffer, Capability, CharKind, CodeLabel,
CursorShape, Diagnostic, Documentation, IndentKind, IndentSize, Language, OffsetRangeExt,
Point, Selection, SelectionGoal, TransactionId,
};
use language::{point_to_lsp, BufferRow, Runnable, RunnableRange};
use language::{point_to_lsp, BufferRow, CharClassifier, Runnable, RunnableRange};
use linked_editing_ranges::refresh_linked_ranges;
use task::{ResolvedTask, TaskTemplate, TaskVariables};
@@ -2443,7 +2442,8 @@ impl Editor {
if let Some(completion_menu) = completion_menu {
let cursor_position = new_cursor_position.to_offset(buffer);
let (word_range, kind) = buffer.surrounding_word(completion_menu.initial_position);
let (word_range, kind) =
buffer.surrounding_word(completion_menu.initial_position, true);
if kind == Some(CharKind::Word)
&& word_range.to_inclusive().contains(&cursor_position)
{
@@ -3289,10 +3289,8 @@ impl Editor {
let start_anchor = snapshot.anchor_before(selection.start);
let is_word_char = text.chars().next().map_or(true, |char| {
let scope = snapshot.language_scope_at(start_anchor.to_offset(&snapshot));
let kind = char_kind(&scope, char);
kind == CharKind::Word
let classifier = snapshot.char_classifier_at(start_anchor.to_offset(&snapshot));
classifier.is_word(char)
});
if is_word_char {
@@ -3923,7 +3921,7 @@ impl Editor {
fn completion_query(buffer: &MultiBufferSnapshot, position: impl ToOffset) -> Option<String> {
let offset = position.to_offset(buffer);
let (word_range, kind) = buffer.surrounding_word(offset);
let (word_range, kind) = buffer.surrounding_word(offset, true);
if offset > word_range.start && kind == Some(CharKind::Word) {
Some(
buffer
@@ -12302,10 +12300,11 @@ fn snippet_completions(
};
let scope = language.map(|language| language.default_scope());
let classifier = CharClassifier::new(scope).for_completion(true);
let mut last_word = line_at
.chars()
.rev()
.take_while(|c| char_kind(&scope, *c) == CharKind::Word)
.take_while(|c| classifier.is_word(*c))
.collect::<String>();
last_word = last_word.chars().rev().collect();
let as_offset = text::ToOffset::to_offset(&buffer_position, &snapshot);
@@ -12436,8 +12435,11 @@ impl CompletionProvider for Model<Project> {
}
let buffer = buffer.read(cx);
let scope = buffer.snapshot().language_scope_at(position);
if trigger_in_words && char_kind(&scope, char) == CharKind::Word {
let classifier = buffer
.snapshot()
.char_classifier_at(position)
.for_completion(true);
if trigger_in_words && classifier.is_word(char) {
return true;
}