Skip to content

Commit

Permalink
Fix line endings for bibtex entry from remote file
Browse files Browse the repository at this point in the history
  • Loading branch information
PHPirates committed Dec 20, 2024
1 parent 8a46f6f commit 7120789
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased]

### Added
* Fix auto import from local bibtex file on Windows
* Add sections to breadcrumbs
* Change order in structure view to match source file and sectioning level
* Add command redefinitions to command definition filter in structure view
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import com.intellij.codeInsight.completion.InsertionContext
import com.intellij.codeInsight.lookup.LookupElement
import nl.hannahsten.texifyidea.file.BibtexFile
import nl.hannahsten.texifyidea.psi.BibtexEntry
import nl.hannahsten.texifyidea.psi.LatexPsiHelper
import nl.hannahsten.texifyidea.settings.TexifySettings
import nl.hannahsten.texifyidea.util.files.bibtexIdsInFileSet
import nl.hannahsten.texifyidea.util.files.referencedFileSet
import nl.hannahsten.texifyidea.util.parser.firstChildOfType

/**
* @author Sten Wessel
Expand All @@ -20,18 +22,21 @@ class LatexReferenceInsertHandler(private val remote: Boolean = false, private v

if (remote and TexifySettings.getInstance().automaticBibtexImport) {
remoteBib ?: return
// remoteBib may come from a file with CRLF line separators, which cannot be accepted into a psi file, so we need to fix that
val newBibEntry = LatexPsiHelper(context.project).createBibtexFromText(remoteBib.text.replace("\r\n", "\n")).firstChildOfType(BibtexEntry::class) ?: return

val bibsInFile = context.file.originalFile.bibtexIdsInFileSet()
// Add the bib item after the last item we found in the file set, and hope that that makes sense...
bibsInFile.lastOrNull()?.let {
it.parent.addAfter(remoteBib, it)
it.parent.addAfter(newBibEntry, it)
}

// If there are no bib items in the fileset yet, see if there is a(n empty) bib file we can add the bib entry to.
if (bibsInFile.isEmpty()) {
context.file.originalFile
.referencedFileSet()
.firstOrNull { it is BibtexFile }
?.add(remoteBib)
?.add(newBibEntry)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/nl/hannahsten/texifyidea/psi/LatexPsiHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.intellij.psi.PsiFile
import com.intellij.psi.PsiFileFactory
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.impl.source.tree.LeafPsiElement
import nl.hannahsten.texifyidea.grammar.BibtexLanguage
import nl.hannahsten.texifyidea.grammar.LatexLanguage
import nl.hannahsten.texifyidea.psi.LatexTypes.*
import nl.hannahsten.texifyidea.util.Log
Expand Down Expand Up @@ -57,6 +58,9 @@ class LatexPsiHelper(private val project: Project) {
fun createFromText(text: String): PsiFile =
PsiFileFactory.getInstance(project).createFileFromText("DUMMY.tex", LatexLanguage, text, false, true)

fun createBibtexFromText(text: String): PsiFile =
PsiFileFactory.getInstance(project).createFileFromText("DUMMY.bib", BibtexLanguage, text, false, true)

/**
* Adds the supplied element to the content of the environment.
* @param environment The environment whose content should be manipulated
Expand Down

0 comments on commit 7120789

Please sign in to comment.