From c87ed9a7b7595f23aad6e827ab8ad71f2c044925 Mon Sep 17 00:00:00 2001 From: Ekaterina Berezhko Date: Fri, 29 Mar 2024 13:58:52 +0200 Subject: [PATCH] [Grazie][IDEA-326315] Do not exclude short reference link label in MarkdownTextExtractor GitOrigin-RevId: fa98cb71ef188735269aeb4666e374e147123d68 --- .../grazie/ide/language/markdown/MarkdownTextExtractor.kt | 7 ++++++- .../com/intellij/grazie/text/TextExtractionTest.java | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/plugins/grazie/markdown/src/main/kotlin/com/intellij/grazie/ide/language/markdown/MarkdownTextExtractor.kt b/plugins/grazie/markdown/src/main/kotlin/com/intellij/grazie/ide/language/markdown/MarkdownTextExtractor.kt index 6b3a0cf3a1a3..41886fbcbcfe 100644 --- a/plugins/grazie/markdown/src/main/kotlin/com/intellij/grazie/ide/language/markdown/MarkdownTextExtractor.kt +++ b/plugins/grazie/markdown/src/main/kotlin/com/intellij/grazie/ide/language/markdown/MarkdownTextExtractor.kt @@ -22,7 +22,7 @@ class MarkdownTextExtractor : TextExtractor() { .withUnknown { it.node.isMarkdownCodeType() } .withMarkup { e -> e.elementType in markup || - e.firstChild == null && e.parent.node.isMarkdownLinkType() && !isLinkText(e) + e.firstChild == null && e.parent.node.isMarkdownLinkType() && !isLinkText(e) && !isShortRefLinkLabel(e) } .excluding { it.elementType == MarkdownElementTypes.IMAGE } .removingIndents(" \t").removingLineSuffixes(" \t") @@ -34,4 +34,9 @@ class MarkdownTextExtractor : TextExtractor() { private fun isLinkText(e: PsiElement) = (e.elementType == MarkdownTokenTypes.TEXT || e.elementType == MarkdownTokenTypes.GFM_AUTOLINK || e is PsiWhiteSpace) && e.parent.elementType == MarkdownElementTypes.LINK_TEXT + + private fun isShortRefLinkLabel(e: PsiElement) = + e.parent.parent.elementType == MarkdownElementTypes.SHORT_REFERENCE_LINK && + e.parent.elementType == MarkdownElementTypes.LINK_LABEL && + (e.elementType == MarkdownTokenTypes.TEXT || e is PsiWhiteSpace) } \ No newline at end of file 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 6543fda8a27b..dd50ddd23ba4 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 @@ -58,6 +58,12 @@ public class TextExtractionTest extends BasePlatformTestCase { Assertions.assertArrayEquals(new int[]{0, extracted.length()}, extracted.markupOffsets()); } + public void testMarkdownShortRefLink() { + TextContent extracted = extractText("a.md", "go to [link] and validate\n[link] : http://localhost", 3); + assertEquals("go to link and validate\nlink : http://localhost", unknownOffsets(extracted)); + Assertions.assertArrayEquals(new int[]{6, 10, 24, 28}, extracted.markupOffsets()); + } + public void testMarkdownIndent() { TextContent extracted = extractText("a.md", "* first line \n second line", 3); assertEquals("first line\nsecond line", unknownOffsets(extracted));