Use noSpellCheckProp NodeProp to exclude node types from spell check (#21335)

GitOrigin-RevId: 91ba72285b0f01c3c00fdb1a64c30e9ff67f72f5
This commit is contained in:
Alf Eaton 2024-10-25 12:32:06 +01:00 committed by Copybot
parent 516a7abc2a
commit c0d6a9c5f0
5 changed files with 40 additions and 13 deletions

View file

@ -10,6 +10,7 @@ import {
getFoldRange, getFoldRange,
} from '../../utils/tree-query' } from '../../utils/tree-query'
import { closeBracketConfig } from './close-bracket-config' import { closeBracketConfig } from './close-bracket-config'
import { noSpellCheckProp } from '@/features/source-editor/utils/node-props'
const styleOverrides: Record<string, any> = { const styleOverrides: Record<string, any> = {
DocumentClassCtrlSeq: t.keyword, DocumentClassCtrlSeq: t.keyword,
@ -118,6 +119,24 @@ export const LaTeXLanguage = LRLanguage.define({
return content return content
}, },
}), }),
// disable spell check in these node types when they're inside these parents (empty string = any parent)
noSpellCheckProp.add({
BibKeyArgument: [['']],
BibliographyArgument: [['']],
BibliographyStyleArgument: [['']],
DocumentClassArgument: [['']],
LabelArgument: [['']],
PackageArgument: [['']],
RefArgument: [['']],
OptionalArgument: [
['DocumentClass'],
['IncludeGraphics'],
['LineBreak'],
['UsePackage'],
],
ShortTextArgument: [['Date']],
TextArgument: [['TabularEnvironment', 'BeginEnv']],
}),
// TODO: does this override groups defined in the grammar? // TODO: does this override groups defined in the grammar?
NodeProp.group.add(type => { NodeProp.group.add(type => {
const types = [] const types = []

View file

@ -87,6 +87,7 @@
AuthorCtrlSeq, AuthorCtrlSeq,
AffilCtrlSeq, AffilCtrlSeq,
AffiliationCtrlSeq, AffiliationCtrlSeq,
DateCtrlSeq,
MaketitleCtrlSeq, MaketitleCtrlSeq,
TextColorCtrlSeq, TextColorCtrlSeq,
ColorBoxCtrlSeq, ColorBoxCtrlSeq,
@ -245,6 +246,9 @@ KnownCommand<ArgumentType> {
Affiliation { Affiliation {
AffiliationCtrlSeq optionalWhitespace? OptionalArgument? optionalWhitespace? TextArgument AffiliationCtrlSeq optionalWhitespace? OptionalArgument? optionalWhitespace? TextArgument
} | } |
Date {
DateCtrlSeq optionalWhitespace? OptionalArgument? optionalWhitespace? ShortTextArgument
} |
DocumentClass { DocumentClass {
DocumentClassCtrlSeq optionalWhitespace? OptionalArgument? DocumentClassCtrlSeq optionalWhitespace? OptionalArgument?
DocumentClassArgument { ShortTextArgument } DocumentClassArgument { ShortTextArgument }

View file

@ -25,6 +25,7 @@ import {
AuthorCtrlSeq, AuthorCtrlSeq,
AffilCtrlSeq, AffilCtrlSeq,
AffiliationCtrlSeq, AffiliationCtrlSeq,
DateCtrlSeq,
DocumentClassCtrlSeq, DocumentClassCtrlSeq,
UsePackageCtrlSeq, UsePackageCtrlSeq,
HrefCtrlSeq, HrefCtrlSeq,
@ -546,6 +547,7 @@ const otherKnowncommands = {
'\\author': AuthorCtrlSeq, '\\author': AuthorCtrlSeq,
'\\affil': AffilCtrlSeq, '\\affil': AffilCtrlSeq,
'\\affiliation': AffiliationCtrlSeq, '\\affiliation': AffiliationCtrlSeq,
'\\date': DateCtrlSeq,
'\\documentclass': DocumentClassCtrlSeq, '\\documentclass': DocumentClassCtrlSeq,
'\\usepackage': UsePackageCtrlSeq, '\\usepackage': UsePackageCtrlSeq,
'\\href': HrefCtrlSeq, '\\href': HrefCtrlSeq,

View file

@ -0,0 +1,9 @@
import { NodeProp } from '@lezer/common'
/**
* A node prop that contains an array, each item of which is an array of parent node types that's
* passed to [matchContext](https://lezer.codemirror.net/docs/ref/#common.SyntaxNodeRef.matchContext)
* to test whether the node matches the given context. If so, the text in the node is excluded from spell checking.
* An empty string is treated as a wildcard, so `[['']]` indicates that the node type should always be excluded.
*/
export const noSpellCheckProp = new NodeProp<string[][]>()

View file

@ -3,6 +3,7 @@ import { Line } from '@codemirror/state'
import { EditorView } from '@codemirror/view' import { EditorView } from '@codemirror/view'
import { SyntaxNodeRef } from '@lezer/common' import { SyntaxNodeRef } from '@lezer/common'
import OError from '@overleaf/o-error' import OError from '@overleaf/o-error'
import { noSpellCheckProp } from '@/features/source-editor/utils/node-props'
/* A convenient wrapper around 'Normal' tokens */ /* A convenient wrapper around 'Normal' tokens */
export class NormalTextSpan { export class NormalTextSpan {
@ -39,18 +40,6 @@ export class NormalTextSpan {
} }
} }
const NotNormalNodeAncestors = [
'Include',
'Input',
'IncludeGraphics',
'Cite',
'LabelArgument',
'UsePackage',
'PackageArgument',
'EnvNameGroup',
'RefArgument',
]
export const getNormalTextSpansFromLine = ( export const getNormalTextSpansFromLine = (
view: EditorView, view: EditorView,
line: Line line: Line
@ -64,7 +53,11 @@ export const getNormalTextSpansFromLine = (
from: lineStart, from: lineStart,
to: lineEnd, to: lineEnd,
enter: (node: SyntaxNodeRef) => { enter: (node: SyntaxNodeRef) => {
if (NotNormalNodeAncestors.includes(node.type.name)) { if (
node.type.prop(noSpellCheckProp)?.some(context => {
return node.matchContext(context)
})
) {
return false return false
} }
if (node.type.name === 'Normal') { if (node.type.name === 'Normal') {