diff --git a/plugins/grazie/src/test/kotlin/com/intellij/grazie/text/TextExtractionTest.java b/plugins/grazie/src/test/kotlin/com/intellij/grazie/text/TextExtractionTest.java index b5bd848ba239..332fd29aae4c 100644 --- a/plugins/grazie/src/test/kotlin/com/intellij/grazie/text/TextExtractionTest.java +++ b/plugins/grazie/src/test/kotlin/com/intellij/grazie/text/TextExtractionTest.java @@ -511,4 +511,9 @@ public class TextExtractionTest extends BasePlatformTestCase { public static TextContent extractText(String fileName, String fileText, int offset, Project project) { return TextExtractor.findTextAt(createFile(fileName, fileText, project), offset, TextContent.TextDomain.ALL); } + + public static Set extractAllTexts(String fileName, String fileText, Project project) { + PsiFile file = createFile(fileName, fileText, project); + return TextExtractor.findAllTextContents(file.getViewProvider(), TextContent.TextDomain.ALL); + } } diff --git a/plugins/kotlin/grazie/intellij.kotlin.grazie.tests/test/org/jetbrains/kotlin/idea/fir/grazie/KotlinGrazieSupportTest28.kt b/plugins/kotlin/grazie/intellij.kotlin.grazie.tests/test/org/jetbrains/kotlin/idea/fir/grazie/KotlinGrazieSupportTest28.kt index c9e18855fa5f..74960592622a 100644 --- a/plugins/kotlin/grazie/intellij.kotlin.grazie.tests/test/org/jetbrains/kotlin/idea/fir/grazie/KotlinGrazieSupportTest28.kt +++ b/plugins/kotlin/grazie/intellij.kotlin.grazie.tests/test/org/jetbrains/kotlin/idea/fir/grazie/KotlinGrazieSupportTest28.kt @@ -5,16 +5,13 @@ import com.intellij.grazie.GrazieTestBase import com.intellij.grazie.jlanguage.Lang import com.intellij.grazie.text.TextContent import com.intellij.grazie.text.TextContentTest +import com.intellij.grazie.text.TextExtractionTest import com.intellij.grazie.text.TextExtractor import com.intellij.testFramework.LightProjectDescriptor import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor class KotlinGrazieSupportTest28 : GrazieTestBase() { - - - override fun getProjectDescriptor(): LightProjectDescriptor { - return KotlinWithJdkAndRuntimeLightProjectDescriptor.getInstance() - } + override fun getProjectDescriptor(): LightProjectDescriptor = KotlinWithJdkAndRuntimeLightProjectDescriptor.getInstance() override val additionalEnabledRules: Set = setOf("UPPERCASE_SENTENCE_START") @@ -88,4 +85,59 @@ class KotlinGrazieSupportTest28 : GrazieTestBase() { ) myFixture.checkHighlighting() } + + fun `test code-like fragments are not extracted`() { + val texts = TextExtractionTest.extractAllTexts("a.kt", $$""" + /** + * The Markdown lexer folds a nested list item's leading indentation into its `LIST_BULLET`/`LIST_NUMBER` token + * (e.g. `" - "`), so a list / list-item block would otherwise start inside the line's indentation. Trim that + * leading whitespace here so the block starts at its real content; otherwise offset-based consumers such as + * indent auto-detection (`FormatterBasedLineIndentInfoBuilder`) undercount the indent of nested list lines. + * + * With code fence: + * ``` + * fun String.helloWorld() { + * println("Hello World, $this") + * } + * ``` + * With indentation: + * + * fun String.helloWorld() { + * println("Hello World, $this") + * } + * + * With tilde: + * ~~~ + * fun String.helloWorld() { + * println("Hello World, $this") + * } + * ~~~ + * + * With backticks: + * `fun main() { println("Hello, Kotlin") }` + */ + fun main() {} + """.trimIndent(), project) + assertEquals(1, texts.size) + assertEquals(texts.first().toString(), """ + The Markdown lexer folds a nested list item's leading indentation into its / token + (e.g. ), so a list / list-item block would otherwise start inside the line's indentation. Trim that + leading whitespace here so the block starts at its real content; otherwise offset-based consumers such as + indent auto-detection () undercount the indent of nested list lines. + + With code fence: + + With indentation: + + + + + + With tilde: + + + With backticks: + + """.trimIndent()) + } } \ No newline at end of file diff --git a/plugins/kotlin/grazie/src/org/jetbrains/kotlin/idea/grazie/KotlinTextExtractor.kt b/plugins/kotlin/grazie/src/org/jetbrains/kotlin/idea/grazie/KotlinTextExtractor.kt index c03b89374f37..281a74845715 100644 --- a/plugins/kotlin/grazie/src/org/jetbrains/kotlin/idea/grazie/KotlinTextExtractor.kt +++ b/plugins/kotlin/grazie/src/org/jetbrains/kotlin/idea/grazie/KotlinTextExtractor.kt @@ -28,13 +28,15 @@ import org.jetbrains.kotlin.psi.psiUtil.isSingleQuoted import java.util.regex.Pattern internal class KotlinTextExtractor : TextExtractor() { - private val kdocBuilder = TextContentBuilder.FromPsi - .withUnknown { e -> e.elementType == KDocTokens.MARKDOWN_LINK && e.text.startsWith("[") } - .excluding { e -> e.elementType == KDocTokens.MARKDOWN_LINK && !e.text.startsWith("[") } - .excluding { e -> val elementType = e.elementType - elementType == LEADING_ASTERISK || elementType == CODE_BLOCK_TEXT || elementType == CODE_SPAN_TEXT - } - .removingIndents(" \t").removingLineSuffixes(" \t") + private val kdocBuilder = TextContentBuilder.FromPsi + .withUnknown { e -> e.elementType == KDocTokens.MARKDOWN_LINK && e.text.startsWith("[") } + .excluding { e -> e.elementType == KDocTokens.MARKDOWN_LINK && !e.text.startsWith("[") } + .excluding { e -> + val elementType = e.elementType + elementType == LEADING_ASTERISK || elementType == CODE_BLOCK_TEXT + } + .withUnknown { e -> e.elementType == CODE_SPAN_TEXT } + .removingIndents(" \t").removingLineSuffixes(" \t") public override fun buildTextContents(root: PsiElement, allowedDomains: Set): List { if (InjectedLanguageManager.getInstance(root.project).shouldInspectionsBeLenient(root)) { @@ -76,7 +78,7 @@ internal class KotlinTextExtractor : TextExtractor() { return null } - private val codeFragments = Pattern.compile("(?s)```.+?```|`.+?`") + private val codeFragments = Pattern.compile("(?s)```.+?```|~~~.+?~~~|``") private val markdownHeading = Pattern.compile("^[ \\t]*#{1,6}[ \\t]+[^\\n]*?(\\n|$)", Pattern.MULTILINE) private fun TextContent.removeCode(): TextContent? = diff --git a/plugins/kotlin/idea/tests/testData/grazie/Docs.kt b/plugins/kotlin/idea/tests/testData/grazie/Docs.kt index 24df6920e2b7..30c622e54066 100644 --- a/plugins/kotlin/idea/tests/testData/grazie/Docs.kt +++ b/plugins/kotlin/idea/tests/testData/grazie/Docs.kt @@ -66,6 +66,15 @@ class ForMultiLanguageSupport { // Das daert geschätzt fünf Mannstunden. } +/** + * The Markdown lexer folds a nested list item's leading indentation into its `LIST_BULLET`/`LIST_NUMBER` token + * (e.g. `" - "`), so a list / list-item block would otherwise start inside the line's indentation. Trim that + * leading whitespace here so the block starts at its real content; otherwise offset-based consumers such as + * indent auto-detection (`ForMultiLanguageSupport`) undercount the indent of nested list lines. + */ +fun ff() {} + + /** * Returns `an true` if expression is part of when condition expression that looks like * ```