Skip to content

Commit 783b3bb

Browse files
authored
Write customized types in alphabetical order (#5663)
* Write customized types in alphabetical order * Update CHANGELOG.md
1 parent 917defc commit 783b3bb

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
1818
- We added an option to show the preview as an extra tab in the entry editor (instead of in a split view). [#5244](https://github.com/JabRef/jabref/issues/5244)
1919
- A custom Open/LibreOffice jstyle file now requires a layout line for the entry type `default` [#5452](https://github.com/JabRef/jabref/issues/5452)
2020
- The entry editor is now open by default when JabRef starts up. [#5460](https://github.com/JabRef/jabref/issues/5460)
21+
- Customized entry types are now serialized in alphabetical order in the bib file.
2122
- We added a new ADS fetcher to use the new ADS API [#4949](https://github.com/JabRef/jabref/issues/4949)
2223
- We added support of the [X11 primary selection](https://unix.stackexchange.com/a/139193/18033) [#2389](https://github.com/JabRef/jabref/issues/2389)
2324
- We added support to switch between biblatex and bibtex library types. [#5550](https://github.com/JabRef/jabref/issues/5550)

src/main/java/org/jabref/logic/exporter/BibDatabaseWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
import java.util.Collections;
88
import java.util.Comparator;
99
import java.util.HashMap;
10-
import java.util.HashSet;
1110
import java.util.LinkedList;
1211
import java.util.List;
1312
import java.util.Map;
1413
import java.util.Objects;
1514
import java.util.Optional;
1615
import java.util.Set;
16+
import java.util.TreeSet;
1717
import java.util.regex.Matcher;
1818
import java.util.regex.Pattern;
1919
import java.util.stream.Collectors;
@@ -157,7 +157,7 @@ public void savePartOfDatabase(BibDatabaseContext bibDatabaseContext, List<BibEn
157157
}
158158

159159
// Map to collect entry type definitions that we must save along with entries using them.
160-
Set<BibEntryType> typesToWrite = new HashSet<>();
160+
Set<BibEntryType> typesToWrite = new TreeSet<>();
161161

162162
// Some file formats write something at the start of the file (like the encoding)
163163
if (preferences.getSaveType() != SavePreferences.DatabaseSaveType.PLAIN_BIBTEX) {

src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,38 @@ void writeEntryWithCustomizedTypeAlsoWritesTypeDeclaration() throws Exception {
287287
+ "@Comment{jabref-entrytype: customizedtype: req[author;date;title] opt[month;publisher;year]}" + OS.NEWLINE,
288288
stringWriter.toString());
289289
}
290+
291+
@Test
292+
void writeCustomizedTypesInAlphabeticalOrder() throws Exception {
293+
EntryType customizedType = new UnknownEntryType("customizedType");
294+
EntryType otherCustomizedType = new UnknownEntryType("otherCustomizedType");
295+
BibEntryType customizedBibType = new BibEntryType(
296+
customizedType,
297+
Collections.singletonList(new BibField(StandardField.TITLE, FieldPriority.IMPORTANT)),
298+
Collections.singletonList(new OrFields(StandardField.TITLE)));
299+
BibEntryType otherCustomizedBibType = new BibEntryType(
300+
otherCustomizedType,
301+
Collections.singletonList(new BibField(StandardField.TITLE, FieldPriority.IMPORTANT)),
302+
Collections.singletonList(new OrFields(StandardField.TITLE)));
303+
entryTypesManager.addCustomOrModifiedType(otherCustomizedBibType, BibDatabaseMode.BIBTEX);
304+
entryTypesManager.addCustomOrModifiedType(customizedBibType, BibDatabaseMode.BIBTEX);
305+
BibEntry entry = new BibEntry(customizedType);
306+
BibEntry otherEntry = new BibEntry(otherCustomizedType);
307+
database.insertEntry(otherEntry);
308+
database.insertEntry(entry);
309+
310+
databaseWriter.savePartOfDatabase(bibtexContext, Arrays.asList(entry, otherEntry));
311+
312+
assertEquals(
313+
OS.NEWLINE
314+
+ "@Customizedtype{," + OS.NEWLINE + "}" + OS.NEWLINE + OS.NEWLINE
315+
+ "@Othercustomizedtype{," + OS.NEWLINE + "}" + OS.NEWLINE + OS.NEWLINE
316+
+ "@Comment{jabref-meta: databaseType:bibtex;}"
317+
+ OS.NEWLINE + OS.NEWLINE
318+
+ "@Comment{jabref-entrytype: customizedtype: req[title] opt[]}" + OS.NEWLINE + OS.NEWLINE
319+
+ "@Comment{jabref-entrytype: othercustomizedtype: req[title] opt[]}" + OS.NEWLINE,
320+
stringWriter.toString());
321+
}
290322

291323
@Test
292324
void roundtripWithArticleMonths() throws Exception {

0 commit comments

Comments
 (0)