use std::{
cell::{Cell, RefCell},
fmt,
ops::Range,
rc::Rc,
sync::Arc,
};
use web_time::Duration;
use unicode_segmentation::UnicodeSegmentation;
use crate::{Color, HighlightStyle, Point, PopoverMenuItem};
pub const MAX_SPELLCHECK_BYTES: usize = 16 * 1024;
pub const MAX_SPELL_GUESSES: usize = 16;
pub const MAX_MISSPELLED_RANGES: usize = 512;
pub const MAX_SPELL_WORD_BYTES: usize = 256;
pub const MAX_DEFINITION_LOOKUP_BYTES: usize = 1_024;
pub const SPELL_CHECK_SETTLE_DELAY: Duration = Duration::from_millis(300);
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TextServiceError {
Unsupported,
InvalidRequest,
}
impl fmt::Display for TextServiceError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Unsupported => formatter.write_str("text service is unsupported on this target"),
Self::InvalidRequest => formatter.write_str("text service request was out of bounds"),
}
}
}
impl std::error::Error for TextServiceError {}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum MisspellingKind {
#[default]
Spelling,
Grammar,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Misspelling {
range: Range<usize>,
kind: MisspellingKind,
}
impl Misspelling {
pub const fn new(range: Range<usize>, kind: MisspellingKind) -> Self {
Self { range, kind }
}
pub const fn spelling(range: Range<usize>) -> Self {
Self::new(range, MisspellingKind::Spelling)
}
pub const fn grammar(range: Range<usize>) -> Self {
Self::new(range, MisspellingKind::Grammar)
}
pub fn range(&self) -> Range<usize> {
self.range.clone()
}
pub const fn kind(&self) -> MisspellingKind {
self.kind
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TextSubstitution {
range: Range<usize>,
replacement: Arc<str>,
}
impl TextSubstitution {
pub fn new(range: Range<usize>, replacement: impl Into<Arc<str>>) -> Self {
Self {
range,
replacement: replacement.into(),
}
}
pub fn range(&self) -> Range<usize> {
self.range.clone()
}
pub fn replacement(&self) -> &Arc<str> {
&self.replacement
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Autocorrection {
range: Range<usize>,
original: Arc<str>,
replacement: Arc<str>,
}
impl Autocorrection {
pub(crate) fn new(
range: Range<usize>,
original: impl Into<Arc<str>>,
replacement: impl Into<Arc<str>>,
) -> Self {
Self {
range,
original: original.into(),
replacement: replacement.into(),
}
}
pub fn range(&self) -> Range<usize> {
self.range.clone()
}
pub fn original(&self) -> &Arc<str> {
&self.original
}
pub fn replacement(&self) -> &Arc<str> {
&self.replacement
}
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub struct TextCheckingPolicy {
pub spellcheck: bool,
pub grammar_check: bool,
pub autocorrect: bool,
pub smart_quotes: bool,
pub smart_dashes: bool,
pub text_replacement: bool,
pub lookup_on_force_click: bool,
}
impl TextCheckingPolicy {
pub const NONE: Self = Self {
spellcheck: false,
grammar_check: false,
autocorrect: false,
smart_quotes: false,
smart_dashes: false,
text_replacement: false,
lookup_on_force_click: false,
};
pub const ALL: Self = Self {
spellcheck: true,
grammar_check: true,
autocorrect: true,
smart_quotes: true,
smart_dashes: true,
text_replacement: true,
lookup_on_force_click: true,
};
pub const fn checks_after_settle(self) -> bool {
self.spellcheck || self.grammar_check
}
pub const fn substitutes_on_insert(self) -> bool {
self.smart_quotes || self.smart_dashes || self.text_replacement
}
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub struct TextCheckingOverrides {
pub spellcheck: Option<bool>,
pub grammar_check: Option<bool>,
pub autocorrect: Option<bool>,
pub smart_quotes: Option<bool>,
pub smart_dashes: Option<bool>,
pub text_replacement: Option<bool>,
pub lookup_on_force_click: Option<bool>,
}
impl TextCheckingOverrides {
pub fn resolve(self, default: TextCheckingPolicy) -> TextCheckingPolicy {
TextCheckingPolicy {
spellcheck: self.spellcheck.unwrap_or(default.spellcheck),
grammar_check: self.grammar_check.unwrap_or(default.grammar_check),
autocorrect: self.autocorrect.unwrap_or(default.autocorrect),
smart_quotes: self.smart_quotes.unwrap_or(default.smart_quotes),
smart_dashes: self.smart_dashes.unwrap_or(default.smart_dashes),
text_replacement: self.text_replacement.unwrap_or(default.text_replacement),
lookup_on_force_click: self
.lookup_on_force_click
.unwrap_or(default.lookup_on_force_click),
}
}
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct SpellDocumentTag(pub i64);
impl SpellDocumentTag {
pub const NONE: Self = Self(0);
}
pub trait SpellCheckProvider {
fn check(
&self,
text: &str,
range: Range<usize>,
policy: TextCheckingPolicy,
) -> Vec<Misspelling>;
fn guesses(&self, _word: &str) -> Vec<String> {
Vec::new()
}
fn correction(&self, _text: &str, _range: Range<usize>) -> Option<String> {
None
}
fn check_text_substitutions(
&self,
_text: &str,
_range: Range<usize>,
_policy: TextCheckingPolicy,
) -> Option<TextSubstitution> {
None
}
fn learn(&self, _word: &str) {}
fn ignore(&self, _word: &str, _tag: SpellDocumentTag) {}
fn open_document(&self) -> SpellDocumentTag {
SpellDocumentTag::NONE
}
fn close_document(&self, _tag: SpellDocumentTag) {}
fn show_definition(&self, _text: &str, _position: Point) -> Result<(), TextServiceError> {
Err(TextServiceError::Unsupported)
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct NoSpellCheckProvider;
impl SpellCheckProvider for NoSpellCheckProvider {
fn check(
&self,
_text: &str,
_range: Range<usize>,
_policy: TextCheckingPolicy,
) -> Vec<Misspelling> {
Vec::new()
}
}
#[cfg(any(test, feature = "test-support"))]
#[derive(Debug, Default)]
pub struct TestSpellCheckProvider {
misspelled: RefCell<Vec<Arc<str>>>,
guesses: RefCell<Vec<(Arc<str>, Vec<String>)>>,
corrections: RefCell<Vec<(Arc<str>, Arc<str>)>>,
replacements: RefCell<Vec<(Arc<str>, Arc<str>)>>,
learned: RefCell<Vec<Arc<str>>>,
ignored: RefCell<Vec<Arc<str>>>,
next_tag: Cell<i64>,
open_documents: Cell<usize>,
}
#[cfg(any(test, feature = "test-support"))]
impl TestSpellCheckProvider {
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn misspelling(self, word: &str) -> Self {
self.misspelled.borrow_mut().push(Arc::from(word));
self
}
#[must_use]
pub fn guess(self, word: &str, guesses: &[&str]) -> Self {
self.guesses.borrow_mut().push((
Arc::from(word),
guesses.iter().map(|guess| (*guess).to_owned()).collect(),
));
self
}
#[must_use]
pub fn correction_for(self, word: &str, correction: &str) -> Self {
self.corrections
.borrow_mut()
.push((Arc::from(word), Arc::from(correction)));
self
}
#[must_use]
pub fn replacement_for(self, word: &str, replacement: &str) -> Self {
self.replacements
.borrow_mut()
.push((Arc::from(word), Arc::from(replacement)));
self
}
pub fn learned(&self) -> Vec<Arc<str>> {
self.learned.borrow().clone()
}
pub fn ignored(&self) -> Vec<Arc<str>> {
self.ignored.borrow().clone()
}
pub fn open_documents(&self) -> usize {
self.open_documents.get()
}
fn flags(&self, word: &str) -> bool {
let matches = |list: &RefCell<Vec<Arc<str>>>| {
list.borrow().iter().any(|entry| entry.as_ref() == word)
};
matches(&self.misspelled) && !matches(&self.learned) && !matches(&self.ignored)
}
}
#[cfg(any(test, feature = "test-support"))]
impl SpellCheckProvider for TestSpellCheckProvider {
fn check(
&self,
text: &str,
range: Range<usize>,
policy: TextCheckingPolicy,
) -> Vec<Misspelling> {
if !policy.spellcheck {
return Vec::new();
}
let Some(slice) = text.get(range.clone()) else {
return Vec::new();
};
slice
.split_word_bound_indices()
.filter(|(_, segment)| self.flags(segment))
.map(|(start, segment)| {
Misspelling::spelling(range.start + start..range.start + start + segment.len())
})
.collect()
}
fn guesses(&self, word: &str) -> Vec<String> {
self.guesses
.borrow()
.iter()
.find(|(entry, _)| entry.as_ref() == word)
.map(|(_, guesses)| guesses.clone())
.unwrap_or_default()
}
fn correction(&self, text: &str, range: Range<usize>) -> Option<String> {
let word = text.get(range)?;
self.corrections
.borrow()
.iter()
.find(|(entry, _)| entry.as_ref() == word)
.map(|(_, correction)| correction.to_string())
}
fn check_text_substitutions(
&self,
text: &str,
range: Range<usize>,
policy: TextCheckingPolicy,
) -> Option<TextSubstitution> {
if !policy.text_replacement {
return None;
}
let word = text.get(range.clone())?;
self.replacements
.borrow()
.iter()
.find(|(entry, _)| entry.as_ref() == word)
.map(|(_, replacement)| TextSubstitution::new(range, replacement.clone()))
}
fn learn(&self, word: &str) {
self.learned.borrow_mut().push(Arc::from(word));
}
fn ignore(&self, word: &str, _tag: SpellDocumentTag) {
self.ignored.borrow_mut().push(Arc::from(word));
}
fn open_document(&self) -> SpellDocumentTag {
self.next_tag.set(self.next_tag.get() + 1);
self.open_documents.set(self.open_documents.get() + 1);
SpellDocumentTag(self.next_tag.get())
}
fn close_document(&self, _tag: SpellDocumentTag) {
self.open_documents
.set(self.open_documents.get().saturating_sub(1));
}
}
thread_local! {
static PROVIDER: RefCell<Option<Rc<dyn SpellCheckProvider>>> =
const { RefCell::new(None) };
static DEFAULT_POLICY: Cell<TextCheckingPolicy> =
const { Cell::new(TextCheckingPolicy::NONE) };
static SPELLING_STYLE: RefCell<Option<HighlightStyle>> = const { RefCell::new(None) };
static GRAMMAR_STYLE: RefCell<Option<HighlightStyle>> = const { RefCell::new(None) };
}
pub fn set_spell_check_provider(provider: impl SpellCheckProvider + 'static) {
PROVIDER.with(|slot| *slot.borrow_mut() = Some(Rc::new(provider)));
}
pub fn set_shared_spell_check_provider(provider: Rc<dyn SpellCheckProvider>) {
PROVIDER.with(|slot| *slot.borrow_mut() = Some(provider));
}
pub fn clear_spell_check_provider() {
PROVIDER.with(|slot| *slot.borrow_mut() = None);
}
pub fn has_spell_check_provider() -> bool {
PROVIDER.with(|slot| slot.borrow().is_some())
}
pub fn spell_check_provider() -> Rc<dyn SpellCheckProvider> {
if let Some(provider) = PROVIDER.with(|slot| slot.borrow().clone()) {
return provider;
}
platform_spell_check_provider()
}
#[cfg(target_os = "macos")]
fn platform_spell_check_provider() -> Rc<dyn SpellCheckProvider> {
crate::macos::spell::shared_provider()
}
#[cfg(not(target_os = "macos"))]
fn platform_spell_check_provider() -> Rc<dyn SpellCheckProvider> {
thread_local! {
static FALLBACK: Rc<dyn SpellCheckProvider> = Rc::new(NoSpellCheckProvider);
}
FALLBACK.with(Rc::clone)
}
pub fn set_default_text_checking(policy: TextCheckingPolicy) {
DEFAULT_POLICY.with(|slot| slot.set(policy));
}
pub fn default_text_checking() -> TextCheckingPolicy {
DEFAULT_POLICY.with(Cell::get)
}
pub fn set_misspelling_highlight_style(style: Option<HighlightStyle>) {
SPELLING_STYLE.with(|slot| *slot.borrow_mut() = style);
}
pub fn set_grammar_highlight_style(style: Option<HighlightStyle>) {
GRAMMAR_STYLE.with(|slot| *slot.borrow_mut() = style);
}
pub fn misspelling_highlight_style() -> HighlightStyle {
SPELLING_STYLE.with(|slot| {
slot.borrow().clone().unwrap_or_else(|| {
HighlightStyle::default()
.underline()
.text_decoration_wavy()
.text_decoration_1()
.underline_color(Color::rgb8(248, 113, 113))
})
})
}
pub fn grammar_highlight_style() -> HighlightStyle {
GRAMMAR_STYLE.with(|slot| {
slot.borrow().clone().unwrap_or_else(|| {
HighlightStyle::default()
.underline()
.text_decoration_wavy()
.text_decoration_1()
.underline_color(Color::rgb8(52, 211, 153))
})
})
}
pub fn show_definition_for(text: &str, position: Point) -> Result<(), TextServiceError> {
let text = text.trim();
if text.is_empty() || text.len() > MAX_DEFINITION_LOOKUP_BYTES {
return Err(TextServiceError::InvalidRequest);
}
spell_check_provider().show_definition(text, position)
}
pub(crate) struct SpellDocument {
provider: Rc<dyn SpellCheckProvider>,
tag: SpellDocumentTag,
}
impl SpellDocument {
pub(crate) fn open() -> Rc<Self> {
let provider = spell_check_provider();
let tag = provider.open_document();
Rc::new(Self { provider, tag })
}
pub(crate) const fn tag(&self) -> SpellDocumentTag {
self.tag
}
}
impl fmt::Debug for SpellDocument {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("SpellDocument")
.field("tag", &self.tag)
.finish_non_exhaustive()
}
}
impl Drop for SpellDocument {
fn drop(&mut self) {
if self.tag != SpellDocumentTag::NONE {
self.provider.close_document(self.tag);
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ReplaceWord {
pub range: Range<usize>,
pub replacement: Arc<str>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LearnWord {
pub word: Arc<str>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct IgnoreWord {
pub word: Arc<str>,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct LookUpSelection;
pub const SPELLING_MENU_ID_PREFIX: &str = "quickgui-spelling";
pub fn spelling_menu_items(
range: Range<usize>,
word: &str,
guesses: &[Arc<str>],
labels: SpellingMenuLabels,
) -> Vec<PopoverMenuItem> {
let mut items = Vec::with_capacity(guesses.len() + 3);
if guesses.is_empty() {
items.push(
PopoverMenuItem::action(
format!("{SPELLING_MENU_ID_PREFIX}-empty"),
labels.no_guesses.clone(),
ReplaceWord {
range: range.clone(),
replacement: Arc::from(word),
},
)
.disabled(true),
);
}
for (index, guess) in guesses.iter().take(MAX_SPELL_GUESSES).enumerate() {
items.push(PopoverMenuItem::action(
format!("{SPELLING_MENU_ID_PREFIX}-guess-{index}"),
guess.clone(),
ReplaceWord {
range: range.clone(),
replacement: guess.clone(),
},
));
}
items.push(PopoverMenuItem::separator());
items.push(PopoverMenuItem::action(
format!("{SPELLING_MENU_ID_PREFIX}-learn"),
labels.learn.clone(),
LearnWord {
word: Arc::from(word),
},
));
items.push(PopoverMenuItem::action(
format!("{SPELLING_MENU_ID_PREFIX}-ignore"),
labels.ignore.clone(),
IgnoreWord {
word: Arc::from(word),
},
));
items
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SpellingMenuLabels {
pub no_guesses: Arc<str>,
pub learn: Arc<str>,
pub ignore: Arc<str>,
}
impl Default for SpellingMenuLabels {
fn default() -> Self {
Self {
no_guesses: Arc::from("No Guesses Found"),
learn: Arc::from("Learn Spelling"),
ignore: Arc::from("Ignore Spelling"),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct SmartSubstitution {
pub(crate) remove_before: usize,
pub(crate) replacement: String,
}
pub(crate) fn smart_substitution(
preceding: &str,
value: &str,
policy: TextCheckingPolicy,
) -> Option<SmartSubstitution> {
if value.chars().count() != 1 {
return None;
}
let character = value.chars().next()?;
match character {
'"' | '\'' if policy.smart_quotes => {
let opening = opens_quotation(preceding);
let replacement = match (character, opening) {
('"', true) => '\u{201c}',
('"', false) => '\u{201d}',
(_, true) => '\u{2018}',
(_, false) => '\u{2019}',
};
Some(SmartSubstitution {
remove_before: 0,
replacement: replacement.to_string(),
})
}
'-' if policy.smart_dashes => {
let mut characters = preceding.chars().rev();
if characters.next() != Some('-') || characters.next() == Some('-') {
return None;
}
Some(SmartSubstitution {
remove_before: '-'.len_utf8(),
replacement: '\u{2014}'.to_string(),
})
}
_ => None,
}
}
fn opens_quotation(preceding: &str) -> bool {
preceding.chars().next_back().is_none_or(|character| {
character.is_whitespace()
|| matches!(
character,
'(' | '[' | '{' | '\u{201c}' | '\u{2018}' | '\u{2014}' | '\u{2013}' | '/' | '-'
)
})
}
pub(crate) fn completes_word(character: char) -> bool {
character.is_whitespace() || (!character.is_alphanumeric() && character != '_')
}
pub fn word_range_at(text: &str, offset: usize) -> Option<Range<usize>> {
if offset > text.len() || !text.is_char_boundary(offset) {
return None;
}
let mut preceding = None;
for (start, segment) in text.split_word_bound_indices() {
if !segment.chars().any(char::is_alphanumeric) {
continue;
}
let end = start + segment.len();
if offset >= start && offset < end {
return Some(start..end);
}
if end == offset {
preceding = Some(start..end);
}
if start > offset {
break;
}
}
preceding
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn smart_quotes_follow_the_preceding_character() {
let policy = TextCheckingPolicy {
smart_quotes: true,
..TextCheckingPolicy::NONE
};
assert_eq!(
smart_substitution("", "\"", policy).unwrap().replacement,
"\u{201c}"
);
assert_eq!(
smart_substitution("say ", "\"", policy)
.unwrap()
.replacement,
"\u{201c}"
);
assert_eq!(
smart_substitution("hello", "\"", policy)
.unwrap()
.replacement,
"\u{201d}"
);
assert_eq!(
smart_substitution("it", "'", policy).unwrap().replacement,
"\u{2019}"
);
assert!(smart_substitution("hello", "\"", TextCheckingPolicy::NONE).is_none());
assert!(smart_substitution("hi", "ab", policy).is_none());
}
#[test]
fn smart_dashes_collapse_exactly_two_hyphens() {
let policy = TextCheckingPolicy {
smart_dashes: true,
..TextCheckingPolicy::NONE
};
let substitution = smart_substitution("a-", "-", policy).unwrap();
assert_eq!(substitution.remove_before, 1);
assert_eq!(substitution.replacement, "\u{2014}");
assert!(smart_substitution("a", "-", policy).is_none());
assert!(smart_substitution("a--", "-", policy).is_none());
}
#[test]
fn word_ranges_prefer_the_containing_then_the_completed_word() {
let text = "alpha beta";
assert_eq!(word_range_at(text, 2), Some(0..5));
assert_eq!(word_range_at(text, 5), Some(0..5));
assert_eq!(word_range_at(text, 6), Some(6..10));
assert_eq!(word_range_at(text, 10), Some(6..10));
assert_eq!(word_range_at(" ", 1), None);
assert_eq!(word_range_at(text, 99), None);
}
#[test]
fn overrides_resolve_against_the_application_policy() {
let default = TextCheckingPolicy {
spellcheck: true,
smart_quotes: true,
..TextCheckingPolicy::NONE
};
let overrides = TextCheckingOverrides {
spellcheck: Some(false),
autocorrect: Some(true),
..TextCheckingOverrides::default()
};
let resolved = overrides.resolve(default);
assert!(!resolved.spellcheck);
assert!(resolved.smart_quotes);
assert!(resolved.autocorrect);
assert!(!resolved.grammar_check);
}
#[test]
fn spelling_menu_items_expose_bounded_typed_actions() {
let guesses: Vec<Arc<str>> = vec![Arc::from("hello"), Arc::from("halo")];
let items = spelling_menu_items(0..4, "helo", &guesses, SpellingMenuLabels::default());
assert_eq!(items.len(), 5);
assert_eq!(items[0].label().as_ref(), "hello");
assert_eq!(items[3].label().as_ref(), "Learn Spelling");
assert_eq!(items[4].label().as_ref(), "Ignore Spelling");
let empty = spelling_menu_items(0..4, "helo", &[], SpellingMenuLabels::default());
assert_eq!(empty.len(), 4);
assert!(empty[0].is_disabled());
}
#[test]
fn definition_requests_are_bounded_and_unsupported_without_a_provider() {
clear_spell_check_provider();
set_spell_check_provider(NoSpellCheckProvider);
assert_eq!(
show_definition_for("", Point::new(0.0, 0.0)),
Err(TextServiceError::InvalidRequest)
);
assert_eq!(
show_definition_for(
&"x".repeat(MAX_DEFINITION_LOOKUP_BYTES + 1),
Point::default()
),
Err(TextServiceError::InvalidRequest)
);
assert_eq!(
show_definition_for("word", Point::default()),
Err(TextServiceError::Unsupported)
);
clear_spell_check_provider();
}
}