Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3361: false positive on duplicate identifier on @string entries in bib files #3370

Merged
merged 3 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
}
}
Loading