diff options
author | Arseniy Pendryak <a.pendryak@yandex.ru> | 2019-11-17 02:47:28 +0300 |
---|---|---|
committer | Arseniy Pendryak <a.pendryak@yandex.ru> | 2019-11-22 11:59:17 +0300 |
commit | 78677541a53b692121f589466d720448faff66e5 (patch) | |
tree | 6a0ba121876d5e1b4bcdbf2199d5c814272fbfac /grazie | |
parent | d36922a029629a96e3760b0f12e0865cf05b6aaa (diff) | |
download | intellij-rust-78677541a53b692121f589466d720448faff66e5.tar.gz |
Add support of `Grazie` plugin
Diffstat (limited to 'grazie')
4 files changed, 85 insertions, 0 deletions
diff --git a/grazie/src/main/kotlin/org/rust/grazie/RsGrammarCheckingStrategy.kt b/grazie/src/main/kotlin/org/rust/grazie/RsGrammarCheckingStrategy.kt new file mode 100644 index 000000000..9eccd555f --- /dev/null +++ b/grazie/src/main/kotlin/org/rust/grazie/RsGrammarCheckingStrategy.kt @@ -0,0 +1,35 @@ +/* + * Use of this source code is governed by the MIT license that can be + * found in the LICENSE file. + */ + +package org.rust.grazie + +import com.intellij.grazie.grammar.strategy.GrammarCheckingStrategy +import com.intellij.grazie.grammar.strategy.StrategyUtils +import com.intellij.grazie.grammar.strategy.impl.RuleGroup +import com.intellij.grazie.utils.LinkedSet +import com.intellij.psi.PsiElement +import org.rust.ide.injected.findDoctestInjectableRanges +import org.rust.lang.core.psi.RsDocCommentImpl +import org.rust.lang.core.psi.RsLitExpr +import org.rust.lang.core.psi.ext.stubKind +import org.rust.lang.core.stubs.RsStubLiteralKind + +class RsGrammarCheckingStrategy : GrammarCheckingStrategy { + override fun isMyContextRoot(element: PsiElement): Boolean = + element is RsDocCommentImpl || element is RsLitExpr && element.stubKind is RsStubLiteralKind.String + + override fun isTypoAccepted(root: PsiElement, typoRange: IntRange, ruleRange: IntRange): Boolean { + if (root !is RsDocCommentImpl) return true + + return findDoctestInjectableRanges(root) + .flatten() + .none { it.intersects(typoRange.first, typoRange.last) } + } + + override fun getIgnoredRuleGroup(root: PsiElement, child: PsiElement): RuleGroup? = RuleGroup.LITERALS + + override fun getStealthyRanges(root: PsiElement, text: CharSequence): LinkedSet<IntRange> = + StrategyUtils.indentIndexes(text, setOf(' ', '/', '!')) +} diff --git a/grazie/src/main/resources/META-INF/grazie-only.xml b/grazie/src/main/resources/META-INF/grazie-only.xml new file mode 100644 index 000000000..b48a4be7e --- /dev/null +++ b/grazie/src/main/resources/META-INF/grazie-only.xml @@ -0,0 +1,5 @@ +<idea-plugin> + <extensions defaultExtensionNs="com.intellij.grazie"> + <grammar.strategy language="Rust" implementationClass="org.rust.grazie.RsGrammarCheckingStrategy"/> + </extensions> +</idea-plugin> diff --git a/grazie/src/test/kotlin/org/rust/grazie/RsGrammarCheckingTest.kt b/grazie/src/test/kotlin/org/rust/grazie/RsGrammarCheckingTest.kt new file mode 100644 index 000000000..ff8503cdd --- /dev/null +++ b/grazie/src/test/kotlin/org/rust/grazie/RsGrammarCheckingTest.kt @@ -0,0 +1,40 @@ +/* + * Use of this source code is governed by the MIT license that can be + * found in the LICENSE file. + */ + +package org.rust.grazie + +import com.intellij.grazie.GrazieConfig +import com.intellij.grazie.ide.GrazieInspection +import com.intellij.grazie.jlanguage.LangDetector +import org.rust.ide.annotator.RsAnnotationTestFixture +import org.rust.ide.inspections.RsInspectionsTestBase + +class RsGrammarCheckingTest : RsInspectionsTestBase(GrazieInspection::class) { + + override fun setUp() { + super.setUp() + LangDetector.init(GrazieConfig.get(), project) + } + + override fun createAnnotationFixture(): RsAnnotationTestFixture = + RsAnnotationTestFixture(myFixture, inspectionClasses = listOf(inspectionClass), baseFileName = "lib.rs") + + fun `test check literals`() = checkByText(""" + fn foo() { + let literal = "<TYPO>There is two apples</TYPO>"; + let raw_literal = r"<TYPO>There is two apples</TYPO>"; + let binary_literal = b"<TYPO>There is two apples</TYPO>"; + } + """) + + fun `test check doc comments`() = checkByText(""" + /// + /// ``` + /// let literal = "<TYPO>There is two apples</TYPO>"; + /// for i in 1..10 {} + /// ``` + pub fn foo() {} + """) +} diff --git a/grazie/src/test/resources/META-INF/plugin.xml b/grazie/src/test/resources/META-INF/plugin.xml new file mode 100644 index 000000000..0863f974b --- /dev/null +++ b/grazie/src/test/resources/META-INF/plugin.xml @@ -0,0 +1,5 @@ +<idea-plugin xmlns:xi="http://www.w3.org/2001/XInclude"> + <id>org.rust.grazie</id> + <xi:include href="/META-INF/core.xml" xpointer="xpointer(/idea-plugin/*)"/> + <depends optional="true" config-file="grazie-only.xml">tanvd.grazi</depends> +</idea-plugin> |