Skip to content

LO/OO: Retain in-text nature when re-rendering CSL citations#15369

Merged
calixtus merged 3 commits into
JabRef:mainfrom
subhramit:oo-hotfix
Mar 19, 2026
Merged

LO/OO: Retain in-text nature when re-rendering CSL citations#15369
calixtus merged 3 commits into
JabRef:mainfrom
subhramit:oo-hotfix

Conversation

@subhramit

Copy link
Copy Markdown
Member

PR Description

We now support re-rendering existing CSL citations not just based on the change of style selected, but also change its in-text nature (normal vs in-text citations).

This also fixes a bug introduced in #15352 wherein generating bibliography would always refresh citations in the document with their non-in-text form, even if the citations used were in-text. Now they are refreshed, but their nature is retained.

Steps to test

Before:

Cite with in-text IEEE
image

Generate bibliography (citations refreshed to non-in-text)
image

Now:

image

Note

A more robust solution would be to embed the in-text nature in the CSL reference mark itself (like we do in JStyles), and when parsing the existing reference marks from the doc, use that piece of data to set whether inTextUsed is true, as otherwise there would be no way to retain the correct information if JabRef is restarted after using in-text citations - and thus generating bibliography would still refresh and overwrite them to normal citations.
Current workaround to that (if in-text citations were used and JabRef was restarted and now bibliography is to be generated without inserting another citation) is to insert another in-text citation and remove it to refresh the internal state. Similarly, if in-text citations were used and JabRef is restarted, and now the user wants to change the citation type to normal citations, they have to manually refresh the nature (to in-text and back to normal by inserting citations of respective styles once, and removing them).

Checklist

  • I own the copyright of the code submitted and I license it under the MIT license
  • I manually tested my changes in running JabRef (always required)
  • [/] I added JUnit tests for changes (if applicable)
  • I added screenshots in the PR description (if change is visible to the user)
  • [/] I added a screenshot in the PR description showing a library with a single entry with me as author and as title the issue number
  • I described the change in CHANGELOG.md in a way that can be understood by the average user (if change is visible to the user)
  • [/] I checked the user documentation for up to dateness and submitted a pull request to our user documentation repository

Signed-off-by: subhramit <subhramit.bb@live.in>
@qodo-free-for-open-source-projects

Copy link
Copy Markdown
Contributor

Review Summary by Qodo

Retain in-text citation nature during re-rendering

🐞 Bug fix ✨ Enhancement

Grey Divider

Walkthroughs

Description
• Retain in-text citation nature when re-rendering citations
• Track in-text usage state with inTextUsed and inTextNatureChanged flags
• Detect transitions between in-text and normal citations
• Pass correct in-text flag to bibliography generation
Diagram
flowchart LR
  A["Citation Insertion"] --> B["Track In-Text State"]
  B --> C["Detect Nature Change"]
  C --> D["Update All Citations"]
  D --> E["Generate Bibliography"]
  E --> F["Preserve In-Text Nature"]
Loading

Grey Divider

File Changes

1. jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLCitationOOAdapter.java 🐞 Bug fix +21/-3

Track and preserve in-text citation nature

• Added inTextUsed and inTextNatureChanged boolean fields to track citation nature state
• Modified insertCitation() to detect transitions from in-text to normal citations
• Modified insertInTextCitation() to detect transitions from normal to in-text citations
• Updated insertBibliography() to pass inTextUsed flag instead of hardcoded false
• Trigger citation re-rendering when in-text nature changes, not just style changes

jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLCitationOOAdapter.java


Grey Divider

Qodo Logo

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

Code Review by Qodo

🐞 Bugs (2) 📘 Rule violations (1) 📎 Requirement gaps (0) 📐 Spec deviations (0)

Grey Divider


Action required

1. In-text commas dropped 🐞 Bug ✓ Correctness
Description
When bibliography generation refreshes citations in in-text mode, regenerated mark text does not
preserve the trailing comma that was originally inserted inside each single-entry in-text reference
mark. This can remove separators between adjacent in-text citations after refresh, changing the
document text.
Code

jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLCitationOOAdapter.java[200]

+        updateAllCitationsWithNewStyle(selectedStyle, inTextUsed);
Evidence
The PR change makes bibliography refresh call updateAllCitationsWithNewStyle with the current
inTextUsed flag, so the in-text update path now runs during bibliography generation. In-text
citations for multiple selected entries are inserted as one reference mark per entry and the comma
separator is appended to the mark text itself; however, the in-text refresh code rebuilds each
mark’s text without re-appending any existing trailing comma and then overwrites the mark content,
causing commas to be lost.

jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLCitationOOAdapter.java[188-235]
jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLCitationOOAdapter.java[126-178]
jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLCitationOOAdapter.java[283-379]
jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLReferenceMarkManager.java[75-124]
jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLReferenceMarkManager.java[257-296]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
Refreshing CSL citations in *in-text* mode during bibliography generation can drop the comma separators between multiple in-text citations inserted in one action. This happens because commas are currently embedded inside each single-entry reference mark’s text during insertion, but `updateAllCitationsWithNewStyle(..., true)` regenerates each mark’s text without preserving any existing trailing punctuation, and then overwrites the mark content.

### Issue Context
- `insertInTextCitation` inserts one reference mark per entry and appends `,` to the text for each entry except the last.
- `insertBibliography` now triggers `updateAllCitationsWithNewStyle(selectedStyle, inTextUsed)`, so in-text refresh is executed during bibliography generation.
- `updateAllCitationsWithNewStyle` (in-text branch) rebuilds mark text purely from regenerated citation strings and overwrites the existing mark text.

### Fix Focus Areas
- jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLCitationOOAdapter.java[188-235]
- jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLCitationOOAdapter.java[126-178]
- jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLCitationOOAdapter.java[283-379]
- jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLReferenceMarkManager.java[257-296]

### Implementation direction
In the in-text refresh loop, read the existing mark text (e.g., `mark.getTextContent().getAnchor().getString()`), detect/preserve a trailing separator (at minimum a trailing comma, ideally a small set like `,`/`;`), and append it to the regenerated citation text when updating that mark. Alternatively (more robust), change insertion so separators are outside the reference mark ranges, but that is a larger behavioral change.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Remediation recommended

2. Duplicate in-text state logic 📘 Rule violation ⚙ Maintainability
Description
The PR adds near-identical inTextUsed/inTextNatureChanged state-update logic in both
insertCitation and insertInTextCitation, increasing maintenance risk and chances of inconsistent
future fixes. This should be consolidated into a shared helper/workflow step to avoid duplication.
Code

jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLCitationOOAdapter.java[R85-96]

+        if (inTextUsed) {
+            inTextNatureChanged = true;
+            inTextUsed = false;
+        } else {
+            inTextNatureChanged = false;
+        }
+
        // Placing this at the beginning reduces the number of updates needed by 1 (in the positive case)
-        if (styleChanged) {
+        if (styleChanged || inTextNatureChanged) {
            updateAllCitationsWithNewStyle(currentStyle, false);
            styleChanged = false;
        }
Evidence
PR Compliance IDs 3 and 34 require avoiding duplicated logic and consolidating repeated workflow
steps. The added inTextUsed/inTextNatureChanged toggling and update-triggering logic appears in
both insertCitation (lines 85-96) and insertInTextCitation (lines 130-140) with only inverted
conditions/arguments, indicating duplication introduced by this PR.

AGENTS.md
jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLCitationOOAdapter.java[85-96]
jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLCitationOOAdapter.java[130-140]
Best Practice: Learned patterns

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The in-text state switching logic (`inTextUsed`/`inTextNatureChanged` plus the `styleChanged || inTextNatureChanged` gate) is duplicated across `insertCitation` and `insertInTextCitation`.

## Issue Context
This duplication increases maintenance cost and makes it easier to introduce inconsistent behavior in future changes.

## Fix Focus Areas
- jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLCitationOOAdapter.java[85-96]
- jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLCitationOOAdapter.java[130-140]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


3. Citation nature state desync 🐞 Bug ⛯ Reliability
Description
insertCitation/insertInTextCitation mutate the new inTextUsed state before performing UNO operations
that can throw, and there is no rollback on failure. If insertion/update fails, subsequent
bibliography generation can refresh all citations using the wrong in-text flag for the actual
document state.
Code

jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLCitationOOAdapter.java[R130-135]

+        if (!inTextUsed) {
+            inTextUsed = true;
+            inTextNatureChanged = true;
+        } else {
+            inTextNatureChanged = false;
+        }
Evidence
Both insertion methods update inTextUsed/inTextNatureChanged before calling
updateAllCitationsWithNewStyle and before writing reference marks, and they propagate UNO
exceptions. The GUI layer catches these exceptions and continues using the same CSLCitationOOAdapter
instance (stored as a field), so a failed operation can leave the adapter’s inTextUsed flag
inconsistent with what is actually present in the document, affecting later insertBibliography
refresh behavior.

jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLCitationOOAdapter.java[81-120]
jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLCitationOOAdapter.java[126-141]
jabgui/src/main/java/org/jabref/gui/openoffice/OOBibBase.java[74-94]
jabgui/src/main/java/org/jabref/gui/openoffice/OOBibBase.java[579-640]
jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLCitationOOAdapter.java[188-202]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`inTextUsed` / `inTextNatureChanged` are updated before exception-prone UNO operations. If an exception occurs, the adapter instance can keep a citation-nature state that doesn’t match the document, and later refreshes (notably bibliography generation) may use the wrong `isInTextStyle`.

### Issue Context
- `insertCitation` and `insertInTextCitation` both `throws com.sun.star.uno.Exception` / `CreationException`.
- The GUI caller (`OOBibBase`) catches these exceptions and continues; the adapter is stored as a field and reused.

### Fix Focus Areas
- jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLCitationOOAdapter.java[79-141]
- jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLCitationOOAdapter.java[188-202]
- jabgui/src/main/java/org/jabref/gui/openoffice/OOBibBase.java[579-640]

### Implementation direction
Use a local variable for the intended new mode (e.g., `boolean targetInTextUsed`) and only assign `this.inTextUsed = targetInTextUsed` after the update + insertion completes successfully. If you need `inTextNatureChanged` for deciding whether to refresh, compute it from the previous state vs target state without mutating fields up front; or snapshot previous field values and restore them in a `catch`/`finally` on failure.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

ⓘ The new review experience is currently in Beta. Learn more

Grey Divider

Qodo Logo

Signed-off-by: subhramit <subhramit.bb@live.in>
@subhramit subhramit added component: libre-office status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers labels Mar 19, 2026
@github-actions github-actions Bot added status: changes-required Pull requests that are not yet complete and removed status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers labels Mar 19, 2026
markManager.setRealTimeNumberUpdateRequired(isNumericStyle);
markManager.readAndUpdateExistingMarks();
updateAllCitationsWithNewStyle(selectedStyle, false);
updateAllCitationsWithNewStyle(selectedStyle, inTextUsed);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action required

1. In-text commas dropped 🐞 Bug ✓ Correctness

When bibliography generation refreshes citations in in-text mode, regenerated mark text does not
preserve the trailing comma that was originally inserted inside each single-entry in-text reference
mark. This can remove separators between adjacent in-text citations after refresh, changing the
document text.
Agent Prompt
### Issue description
Refreshing CSL citations in *in-text* mode during bibliography generation can drop the comma separators between multiple in-text citations inserted in one action. This happens because commas are currently embedded inside each single-entry reference mark’s text during insertion, but `updateAllCitationsWithNewStyle(..., true)` regenerates each mark’s text without preserving any existing trailing punctuation, and then overwrites the mark content.

### Issue Context
- `insertInTextCitation` inserts one reference mark per entry and appends `,` to the text for each entry except the last.
- `insertBibliography` now triggers `updateAllCitationsWithNewStyle(selectedStyle, inTextUsed)`, so in-text refresh is executed during bibliography generation.
- `updateAllCitationsWithNewStyle` (in-text branch) rebuilds mark text purely from regenerated citation strings and overwrites the existing mark text.

### Fix Focus Areas
- jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLCitationOOAdapter.java[188-235]
- jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLCitationOOAdapter.java[126-178]
- jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLCitationOOAdapter.java[283-379]
- jablib/src/main/java/org/jabref/logic/openoffice/oocsltext/CSLReferenceMarkManager.java[257-296]

### Implementation direction
In the in-text refresh loop, read the existing mark text (e.g., `mark.getTextContent().getAnchor().getString()`), detect/preserve a trailing separator (at minimum a trailing comma, ideally a small set like `,`/`;`), and append it to the regenerated citation text when updating that mark. Alternatively (more robust), change insertion so separators are outside the reference mark ranges, but that is a larger behavioral change.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Signed-off-by: subhramit <subhramit.bb@live.in>
@testlens-app

testlens-app Bot commented Mar 19, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: a54cf78
▶️ Tests: 10195 executed
⚪️ Checks: 52/52 completed


Learn more about TestLens at testlens.app.

@calixtus calixtus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@calixtus calixtus added this pull request to the merge queue Mar 19, 2026
@github-actions github-actions Bot added the status: to-be-merged PRs which are accepted and should go into the merge-queue. label Mar 19, 2026
Merged via the queue into JabRef:main with commit 3cb3bbc Mar 19, 2026
52 checks passed
@calixtus calixtus deleted the oo-hotfix branch March 19, 2026 06:57
FynnianB pushed a commit to FynnianB/jabref that referenced this pull request Mar 19, 2026
…15369)

* Retain in text nature when re-rendering citations

Signed-off-by: subhramit <subhramit.bb@live.in>

* Changelog

Signed-off-by: subhramit <subhramit.bb@live.in>

* Better changelog

Signed-off-by: subhramit <subhramit.bb@live.in>

---------

Signed-off-by: subhramit <subhramit.bb@live.in>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: libre-office status: no-bot-comments status: to-be-merged PRs which are accepted and should go into the merge-queue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants