From 9aba2ebaa509691e70e9df1e950b3e71ddf79d33 Mon Sep 17 00:00:00 2001 From: Abby Berkers Date: Tue, 26 Dec 2023 10:50:26 +0100 Subject: [PATCH 1/2] Fix #3361: false positive on duplicate identifier on @string entries in bib files --- .../texifyidea/util/parser/BibtexPsiUtil.kt | 7 ++++- .../bibtex/BibtexDuplicateIdInspectionTest.kt | 31 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 test/nl/hannahsten/texifyidea/inspections/bibtex/BibtexDuplicateIdInspectionTest.kt diff --git a/src/nl/hannahsten/texifyidea/util/parser/BibtexPsiUtil.kt b/src/nl/hannahsten/texifyidea/util/parser/BibtexPsiUtil.kt index e4c98fd40..e3a552dd0 100644 --- a/src/nl/hannahsten/texifyidea/util/parser/BibtexPsiUtil.kt +++ b/src/nl/hannahsten/texifyidea/util/parser/BibtexPsiUtil.kt @@ -23,7 +23,12 @@ fun BibtexEntry.getYear(): String { fun BibtexEntry.getIdentifier(): String { val stub = this.stub - return stub?.identifier ?: (firstChildOfType(BibtexId::class)?.text ?: return "") + return stub?.identifier ?: if (this.type.text.lowercase() == "@string") { + this.entryContent?.tagList?.firstOrNull()?.key?.text ?: "" + } + else { + firstChildOfType(BibtexId::class)?.text ?: "" + } } fun BibtexEntry.getAbstract(): String { diff --git a/test/nl/hannahsten/texifyidea/inspections/bibtex/BibtexDuplicateIdInspectionTest.kt b/test/nl/hannahsten/texifyidea/inspections/bibtex/BibtexDuplicateIdInspectionTest.kt new file mode 100644 index 000000000..b695c0e89 --- /dev/null +++ b/test/nl/hannahsten/texifyidea/inspections/bibtex/BibtexDuplicateIdInspectionTest.kt @@ -0,0 +1,31 @@ +package nl.hannahsten.texifyidea.inspections.bibtex + +import nl.hannahsten.texifyidea.file.BibtexFileType +import nl.hannahsten.texifyidea.inspections.TexifyInspectionTestBase + +class BibtexDuplicateIdInspectionTest : TexifyInspectionTestBase(BibtexDuplicateIdInspection()) { + + fun `test @strings are not always duplicate ids`() { + myFixture.configureByText( + BibtexFileType, + """ + @string{ a = test } + + @string{ b = c } + """.trimIndent() + ) + myFixture.checkHighlighting() + } + + fun `test duplicate @strings`() { + myFixture.configureByText( + BibtexFileType, + """ + @string{ a = test } + + @string{ a = c } + """.trimIndent() + ) + myFixture.checkHighlighting() + } +} \ No newline at end of file From 922020c7cfd8456b798a8a7618fecff0a4bb5835 Mon Sep 17 00:00:00 2001 From: Abby Berkers Date: Tue, 26 Dec 2023 11:30:04 +0100 Subject: [PATCH 2/2] Fix #3360: Get id of 'empty' bib entry correctly --- .../texifyidea/util/parser/BibtexPsiUtil.kt | 11 +++++----- .../texifyidea/psi/BibtexEntryImplUtilTest.kt | 20 ++++++++++++++++--- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/nl/hannahsten/texifyidea/util/parser/BibtexPsiUtil.kt b/src/nl/hannahsten/texifyidea/util/parser/BibtexPsiUtil.kt index e3a552dd0..278a6aa4c 100644 --- a/src/nl/hannahsten/texifyidea/util/parser/BibtexPsiUtil.kt +++ b/src/nl/hannahsten/texifyidea/util/parser/BibtexPsiUtil.kt @@ -23,12 +23,11 @@ fun BibtexEntry.getYear(): String { fun BibtexEntry.getIdentifier(): String { val stub = this.stub - return stub?.identifier ?: if (this.type.text.lowercase() == "@string") { - this.entryContent?.tagList?.firstOrNull()?.key?.text ?: "" - } - else { - firstChildOfType(BibtexId::class)?.text ?: "" - } + return stub?.identifier + ?: firstChildOfType(BibtexId::class)?.text + ?: this.entryContent?.tagList?.firstOrNull()?.key?.text + ?: this.preamble?.text + ?: "" } fun BibtexEntry.getAbstract(): String { diff --git a/test/nl/hannahsten/texifyidea/psi/BibtexEntryImplUtilTest.kt b/test/nl/hannahsten/texifyidea/psi/BibtexEntryImplUtilTest.kt index 62d8a0222..da32e9b03 100644 --- a/test/nl/hannahsten/texifyidea/psi/BibtexEntryImplUtilTest.kt +++ b/test/nl/hannahsten/texifyidea/psi/BibtexEntryImplUtilTest.kt @@ -7,9 +7,9 @@ import com.intellij.testFramework.fixtures.BasePlatformTestCase import junit.framework.TestCase import nl.hannahsten.texifyidea.file.BibtexFileType import nl.hannahsten.texifyidea.util.parser.firstChildOfType +import nl.hannahsten.texifyidea.util.parser.getIdentifier import nl.hannahsten.texifyidea.util.parser.getTagContent import org.intellij.lang.annotations.Language -import org.junit.Test class BibtexEntryImplUtilTest : BasePlatformTestCase() { @@ -37,15 +37,29 @@ class BibtexEntryImplUtilTest : BasePlatformTestCase() { myFixture.configureByText(BibtexFileType, entryText) } - @Test fun testEntryGetReferences() { listOf(WebReference(entryElement, url)).map { it.url }.forEach { UsefulTestCase.assertContainsElements(entryElement.references.map { reference -> (reference as WebReference).url }, it) } } - @Test fun testGetTagContent() { TestCase.assertEquals("TeXiFy IDEA", entryElement.getTagContent("title")) } + + fun `test get id of 'empty' element`() { + myFixture.configureByText( + BibtexFileType, + """ + @misc{identifier, + } + """.trimIndent() + ) + TestCase.assertEquals("identifier", entryElement.getIdentifier()) + } + + fun `test get id of @string element`() { + myFixture.configureByText(BibtexFileType, "@string{a = b}") + TestCase.assertEquals("a", entryElement.getIdentifier()) + } } \ No newline at end of file