PY-7711 PY-36912 Don't spell check strings containing injections

rather delegating to the spell checker of the injected language.

Otherwise, we either just report typos twice (PY-36912) or, worse, report
numerous false positives since we try to apply tokenization rules for Python
to a different language as we notoriously have been doing all this time for
injected regular expressions.

Implementations of spell checkers for other languages such as Java or
JavaScript have been using this policy for ages.

GitOrigin-RevId: d1a2a3d48b7a5efff0dbb32ec2847fba1482f3a8
This commit is contained in:
Mikhail Golubev
2019-12-04 12:07:42 +00:00
committed by intellij-monorepo-bot
parent c0f584bb6e
commit 13b6bac7af
5 changed files with 26 additions and 0 deletions
@@ -15,6 +15,7 @@
*/
package com.jetbrains.python.spellchecker;
import com.intellij.lang.injection.InjectedLanguageManager;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.spellchecker.inspections.PlainTextSplitter;
@@ -94,6 +95,10 @@ public class PythonSpellcheckerStrategy extends SpellcheckingStrategy {
@Override
public Tokenizer getTokenizer(PsiElement element) {
if (element instanceof PyStringLiteralExpression) {
final InjectedLanguageManager injectionManager = InjectedLanguageManager.getInstance(element.getProject());
if (element.getTextLength() >= 2 && injectionManager.getInjectedPsiFiles(element) != null) {
return EMPTY_TOKENIZER;
}
PsiElement parent = element.getParent();
if (parent instanceof PyBinaryExpression) {
PyBinaryExpression binaryExpression = (PyBinaryExpression)parent;
@@ -0,0 +1,3 @@
"""
>>> 'There are also always two inspection <TYPO descr="Typo: In word 'messags'">messags</TYPO>.'
"""
@@ -0,0 +1,2 @@
# language=Python
s = '"<TYPO descr="Typo: In word 'mstyped'">mstyped</TYPO>"'
@@ -0,0 +1,2 @@
import re
re_sth = re.compile(r'(\w+)\stells\syou')
@@ -66,6 +66,20 @@ public class PySpellCheckerTest extends PyTestCase {
doTest();
}
public void testTyposInInjectedPythonStringsReportedOnce() {
doTest();
}
// PY-36912
public void testTyposInDoctestsReportedOnce() {
doTest();
}
// PY-7711
public void testTyposInRegexIgnored() {
doTest();
}
private void doTest() {
myFixture.enableInspections(SpellCheckingInspection.class);
myFixture.configureByFile("inspections/spelling/" + getTestName(true) + ".py");