Skip to content

Commit

Permalink
Merge pull request #3370 from Hannah-Sten/3361-bibtex-duplicate-strin…
Browse files Browse the repository at this point in the history
…g-id

Fix #3361: false positive on duplicate identifier on @string entries in bib files
  • Loading branch information
PHPirates authored Dec 30, 2023
2 parents 854dffc + f545115 commit 12bbb26
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/nl/hannahsten/texifyidea/util/parser/BibtexPsiUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ fun BibtexEntry.getYear(): String {

fun BibtexEntry.getIdentifier(): String {
val stub = this.stub
return stub?.identifier ?: (firstChildOfType(BibtexId::class)?.text ?: return "")
return stub?.identifier
?: firstChildOfType(BibtexId::class)?.text
?: this.entryContent?.tagList?.firstOrNull()?.key?.text
?: this.preamble?.text
?: ""
}

fun BibtexEntry.getAbstract(): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
"""
<error descr="Duplicate identifier 'a'">@string{ a = test </error>}
<error descr="Duplicate identifier 'a'">@string{ a = c </error>}
""".trimIndent()
)
myFixture.checkHighlighting()
}
}
20 changes: 17 additions & 3 deletions test/nl/hannahsten/texifyidea/psi/BibtexEntryImplUtilTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down Expand Up @@ -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())
}
}

0 comments on commit 12bbb26

Please sign in to comment.