Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SuperEditorSpellCheck] Add API to ignore text during spellcheck (Resolves #2564) #2567

Merged
merged 2 commits into from
Feb 11, 2025

Conversation

angelosilvestre
Copy link
Collaborator

@angelosilvestre angelosilvestre commented Feb 7, 2025

[SuperEditorSpellCheck] Add API to ignore text during spellcheck (Resolves #2564)

This PR adds an optional ignoreRules to the plugin constructor, so apps can choose to ignore certain texts, like links, tags, etc.

Each rule is a function that returns a list of TextRanges that should be ignored. All ranges from all rules are ignored.

Since on mobile we pass the whole text at once to the spellchecker, we need a workaround to do this. This PR replaces all the ignored ranges with whitespaces, so the spellchecker doesn't report anything on those ranges.

This PR implements built-in rules for ignore by pattern, attribution and attribution filters.

Without the ignored ranges:

image

With ignored ranges:

image

@angelosilvestre
Copy link
Collaborator Author

@matthew-carroll Do you have any ideas on what is the ideal approach to add tests for this? I think we could make the plugin take a SpellCheckService(Flutter's interface for spellchecking) as an optional parameter and implement a SpellCheckService that returns spelling errors for everything that isn't a whitespace.

Also, which other built-in rules should we add?

///
/// This method replaces the ignored ranges with whitespaces so that the spellchecker
/// doesn't see them.
String _filterIgnoredRanges(TextNode node, Document document) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove the document from this? This seems to spread the document around to a bunch of callsites, and in theory the broader document shouldn't be necessary to evaluate rules for patterns, attributions, or explicit text spans.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

typedef SpellingIgnoreRule = List<TextRange> Function(TextNode node, Document document);

/// A collection of built-in rules for ignoring spans of text from spellchecking.
class SpellingIgnoreRules {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a rule for explicit text spans, just in case an app already knows the range of text to ignore.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like we talked before, the knowledge about ignored ranges lives only in the SpellingAndGrammarReaction. So, a rule that takes a TextRange would apply that range for all nodes in the document. A rule to ignore a portion of the document would need to take a DocumentRange and a Document, to generate different TextRanges for each node, as the rules return the ignored range for a single TextNode.

Regarding adding option directly on our native spellchecker plugin, I'm not sure it's a good idea, because the plugin basically just exposes the macOS API's, so I think we shouldn't be adding arbitrary parameters.

Copy link
Contributor

@matthew-carroll matthew-carroll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - We should add some kind of tests for this. Maybe send a fake spell checker and verify that the text that's sent to the checker has blanked out the various ignore span. But I'll merge this as-is so that ClickUp can use it.

@angelosilvestre
Copy link
Collaborator Author

@matthew-carroll Added a PR with tests: #2574

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE][Spellcheck] - Add APIs to ignore text for spellcheck
2 participants