This PR adds new config option to language config called
`word_boundaries` that controls which characters should be recognised as
word boundary for a given language. This will improve our UX for
languages such as PHP and Tailwind.

Release Notes:

- Improved completions for PHP
[#1820](https://github.com/zed-industries/community/issues/1820)

---------

Co-authored-by: Julia Risley <julia@zed.dev>
This commit is contained in:
Piotr Osiewicz 2023-08-22 10:35:20 +02:00 committed by GitHub
parent a836f9c23d
commit d27cebd977
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 120 additions and 59 deletions

View file

@ -2192,13 +2192,16 @@ impl BufferSnapshot {
let mut end = start;
let mut next_chars = self.chars_at(start).peekable();
let mut prev_chars = self.reversed_chars_at(start).peekable();
let language = self.language_at(start);
let kind = |c| char_kind(language, c);
let word_kind = cmp::max(
prev_chars.peek().copied().map(char_kind),
next_chars.peek().copied().map(char_kind),
prev_chars.peek().copied().map(kind),
next_chars.peek().copied().map(kind),
);
for ch in prev_chars {
if Some(char_kind(ch)) == word_kind && ch != '\n' {
if Some(kind(ch)) == word_kind && ch != '\n' {
start -= ch.len_utf8();
} else {
break;
@ -2206,7 +2209,7 @@ impl BufferSnapshot {
}
for ch in next_chars {
if Some(char_kind(ch)) == word_kind && ch != '\n' {
if Some(kind(ch)) == word_kind && ch != '\n' {
end += ch.len_utf8();
} else {
break;
@ -3003,14 +3006,18 @@ pub fn contiguous_ranges(
})
}
pub fn char_kind(c: char) -> CharKind {
pub fn char_kind(language: Option<&Arc<Language>>, c: char) -> CharKind {
if c.is_whitespace() {
CharKind::Whitespace
return CharKind::Whitespace;
} else if c.is_alphanumeric() || c == '_' {
CharKind::Word
} else {
CharKind::Punctuation
return CharKind::Word;
}
if let Some(language) = language {
if language.config.word_characters.contains(&c) {
return CharKind::Word;
}
}
CharKind::Punctuation
}
/// Find all of the ranges of whitespace that occur at the ends of lines