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

Ignore non-displayable fields for indent during writing #851

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 9 additions & 2 deletions src/main/java/net/sf/jabref/bibtex/BibEntryWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,16 @@ private boolean writeField(BibEntry entry, Writer out, String name, boolean prep
}
}

/**
* Determine length of longest field key.
* Only displayable fields are regarded as writable-only fields are not shown in the UI, but the user expects the entry to be rendered as shown in the UI
* This is a quick fix for https://github.com/JabRef/jabref/issues/834
* TODO: Long term fix: Fix https://github.com/JabRef/jabref/issues/574
*/
private int getLengthOfLongestFieldName(BibEntry entry) {
Predicate<String> isNotBibtexKey = field -> !"bibtexkey".equals(field);
return entry.getFieldNames().stream().filter(isNotBibtexKey).mapToInt(field -> field.length()).max().orElse(0);
Predicate<String> isDisplayableField = field -> InternalBibtexFields.isDisplayableField(field);
return entry.getFieldNames().stream().filter(isDisplayableField).mapToInt(field -> field.length()).max()
.orElse(0);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/sf/jabref/gui/InternalBibtexFields.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ private InternalBibtexFields() {
// some semi-standard fields
dummy = new BibtexSingleField(BibEntry.KEY_FIELD, true);
dummy.setPrivate();
// the field is displayed and written with a special handling. Therefore, we set displayable and writable to false
dummy.setWriteable(false);
dummy.setDisplayable(false);
add(dummy);

dummy = new BibtexSingleField("doi", true, GUIGlobals.SMALL_W);
Expand Down
42 changes: 37 additions & 5 deletions src/test/java/net/sf/jabref/bibtex/BibEntryWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public void setUpWriter() {
writer = new BibEntryWriter(new LatexFieldFormatter(), true);
}

@Test
public void testSerialization() throws IOException {
StringWriter stringWriter = new StringWriter();

/**
* Generates a BibEntry with type "article", key "1234", and some test data
*/
private static BibEntry generateArticle1234() {
BibEntry entry = new BibEntry("1234", "article");
//set a required field
entry.setField("author", "Foo Bar");
Expand All @@ -54,7 +54,14 @@ public void testSerialization() throws IOException {
entry.setField("number", "1");
entry.setField("note", "some note");

writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX);
return entry;
}

@Test
public void testSerialization() throws IOException {
StringWriter stringWriter = new StringWriter();

writer.write(generateArticle1234(), stringWriter, BibDatabaseMode.BIBTEX);

String actual = stringWriter.toString();

Expand All @@ -70,6 +77,31 @@ public void testSerialization() throws IOException {
assertEquals(expected, actual);
}

@Test
public void testSerializationOfNonDisplayableFields() throws IOException {
StringWriter stringWriter = new StringWriter();

BibEntry entry = generateArticle1234();
// mark entry; __markedentry is a writable field, but not a displayable
// we cannot use `EntryMarker.markEntry(entry, 1, false, nc);`, because even though Globals.DEFAEULT_OWNER is "jabref", the entry is marked by "travis" on TravisCI
entry.setField("__markedentry", "[[jabref]:1]");

writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX);

String actual = stringWriter.toString();

// @formatter:off
String expected = Globals.NEWLINE + "@Article{," + Globals.NEWLINE +
" author = {Foo Bar}," + Globals.NEWLINE +
" journal = {International Journal of Something}," + Globals.NEWLINE +
" number = {1}," + Globals.NEWLINE +
" note = {some note}," + Globals.NEWLINE +
" __markedentry = {[[jabref]:1]}" + Globals.NEWLINE +
"}" + Globals.NEWLINE;
// @formatter:on
assertEquals(expected, actual);
}

@Test
public void roundTripTest() throws IOException {
// @formatter:off
Expand Down