-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Preserve user comments in bib file #1471
Merged
Merged
Changes from 34 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
d6d6721
Add failing test with preceding comment
lenhard cb85443
Add failing parser test
lenhard b50c3d6
Improve test naming
lenhard 2c39c89
Reuse Globals.ENCODING_PREFIX in test
lenhard b547703
Check explicitly for encoding line and purge it
lenhard ce56e23
Add changelog entry
lenhard fe79b41
Write out user comments also for modified entries
lenhard ec589e8
Add test to check preservation of ENCODING_PREFIX inside an entry
lenhard c7f4694
Make BibEntryWriter more robust when dealing with faulty parsed seria…
lenhard 3957680
Add test with user comment in file
lenhard 399bcb9
Add test that changes and reformats an entry with a user comment
lenhard 14db4bf
Add test with user comment before String
lenhard 392864e
Preserve newlines also when used with bibtex strings
lenhard 9c23e94
Add test for serialization of epilog
lenhard 21eac95
Fix string detection in test
lenhard 3206b29
In case of change, only remove trailing whitespaces between user comm…
lenhard a1c98c5
Merge branch 'master' into preserve-comments
lenhard 678c83e
Remove unused variable
lenhard 7a3522a
Remove unused import
lenhard 51d82a2
Reformat changelog entry
lenhard 1dca1a7
Move comment detection logic to BibtexString
lenhard cee181f
Compute user comment in string only once
lenhard 6496190
Move user comment computation to BibEntry
lenhard afea9b0
Add test for comment in the same line as BibEntry
lenhard dc4ae0f
Merge branch 'master' into preserve-comments
lenhard ccddef0
Remove unnecessary epilog test
lenhard 6d1ac65
Remove redundant test bib file
lenhard 0972b51
Remove redundant asserts
lenhard b4b288a
Elevate registry actions
stefan-kolb 67a4885
Delete stuff
stefan-kolb 9000f1c
Revert "Elevate registry actions"
stefan-kolb d970a4c
Revert "Delete stuff"
stefan-kolb c1db48f
Merge branch 'master' into preserve-comments
lenhard 75d6262
Merge branch 'preserve-comments' of github.com:JabRef/jabref into pre…
lenhard 98cbe98
Use optional in assert
lenhard 72de4ce
Remove duplicate test
lenhard e5e5c44
Remove unnecessary asserts
lenhard a8a7b20
Remove unused import of Optional
lenhard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -291,6 +291,82 @@ public void roundtrip() throws IOException { | |||
} | ||||
} | ||||
|
||||
@Test | ||||
public void roundtripWithUserComment() throws IOException { | ||||
Path testBibtexFile = Paths.get("src/test/resources/testbib/bibWithUserComments.bib"); | ||||
Charset encoding = StandardCharsets.UTF_8; | ||||
ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); | ||||
|
||||
SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true); | ||||
BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), | ||||
new Defaults(BibDatabaseMode.BIBTEX)); | ||||
|
||||
databaseWriter.writePartOfDatabase(stringWriter, context, result.getDatabase().getEntries(), preferences); | ||||
try (Scanner scanner = new Scanner(testBibtexFile,encoding.name())) { | ||||
assertEquals(scanner.useDelimiter("\\A").next(), stringWriter.toString()); | ||||
} | ||||
} | ||||
|
||||
@Test | ||||
public void roundtripWithUserCommentAndEntryChange() throws IOException { | ||||
Path testBibtexFile = Paths.get("src/test/resources/testbib/bibWithUserComments.bib"); | ||||
Charset encoding = StandardCharsets.UTF_8; | ||||
ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); | ||||
|
||||
BibEntry entry = result.getDatabase().getEntryByKey("1137631").get(); | ||||
entry.setField("author", "Mr. Author"); | ||||
|
||||
SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true); | ||||
BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), | ||||
new Defaults(BibDatabaseMode.BIBTEX)); | ||||
|
||||
databaseWriter.writePartOfDatabase(stringWriter, context, result.getDatabase().getEntries(), preferences); | ||||
|
||||
try (Scanner scanner = new Scanner(Paths.get("src/test/resources/testbib/bibWithUserCommentAndEntryChange.bib"),encoding.name())) { | ||||
assertEquals(scanner.useDelimiter("\\A").next(), stringWriter.toString()); | ||||
} | ||||
} | ||||
|
||||
@Test | ||||
public void roundtripWithUserCommentBeforeString() throws IOException { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like an exact copy of
|
||||
Path testBibtexFile = Paths.get("src/test/resources/testbib/complex.bib"); | ||||
Charset encoding = StandardCharsets.UTF_8; | ||||
ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); | ||||
|
||||
SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true); | ||||
BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), | ||||
new Defaults(BibDatabaseMode.BIBTEX)); | ||||
|
||||
databaseWriter.writePartOfDatabase(stringWriter, context, result.getDatabase().getEntries(), preferences); | ||||
try (Scanner scanner = new Scanner(testBibtexFile,encoding.name())) { | ||||
assertEquals(scanner.useDelimiter("\\A").next(), stringWriter.toString()); | ||||
} | ||||
} | ||||
|
||||
@Test | ||||
public void roundtripWithUserCommentBeforeStringAndChange() throws IOException { | ||||
Path testBibtexFile = Paths.get("src/test/resources/testbib/complex.bib"); | ||||
Charset encoding = StandardCharsets.UTF_8; | ||||
ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); | ||||
|
||||
BibtexString string = result.getDatabase().getStringValues().iterator().next(); | ||||
if(string.getContent().isEmpty()) { | ||||
// do nothing | ||||
} else { | ||||
string.setContent("my first string"); | ||||
} | ||||
|
||||
SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true); | ||||
BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), | ||||
new Defaults(BibDatabaseMode.BIBTEX)); | ||||
|
||||
databaseWriter.writePartOfDatabase(stringWriter, context, result.getDatabase().getEntries(), preferences); | ||||
|
||||
try (Scanner scanner = new Scanner(testBibtexFile,encoding.name())) { | ||||
assertEquals(scanner.useDelimiter("\\A").next(), stringWriter.toString()); | ||||
} | ||||
} | ||||
|
||||
@Test | ||||
public void writeSavedSerializationOfEntryIfUnchanged() throws IOException { | ||||
BibEntry entry = new BibEntry(); | ||||
|
@@ -543,4 +619,5 @@ public void writeEntriesInOriginalOrderWhenNoSaveOrderConfigIsSetInMetadata() th | |||
+ Globals.NEWLINE | ||||
, stringWriter.toString()); | ||||
} | ||||
|
||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now an exact copy of BibEntry.getUserComments right? Maybe add a superclass BibItem containing this method and then BibEntry and BibString derive from this superclass (maybe there is even more common code which could be extracted to the super class)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry to always slip into the role of your antagonist, but I am against creating a class hierarchy. In most cases, it makes the code harder to understand, and the code duplication savings are just not worth the effort. In addition, one has to adhere to the liskov substitution principle to create a good class hierarchy, but this is hard to do right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem, having an experienced antagonist is probably the best way to learn something 😄
So now ignoring everything about the actual implementation and only speaking about "business objects": There are different items which can be contained in a bib file. For example
@Comments
,@Strings
and normal bib entries. They all have a similar structure (e.g begin with@
, followed by some identifier and then braces which surround the actual content) and all of them could have some text comments in front of them. Thus there is also a common behavior when it comes to parsing and writing.So how would my deer antagonist reflect this common structure and behavior in java code?
My idea would be: BibString, BibEntry, BibComment all derive from BibItem. BibItem has methods to parse and write at least the form
@something { abstract method to parse / write value }
. Then the writer just gets a list of BibItems and invokes the write method on them. Similarly the parser returns a list of BibItems while the parsing of every item was done in the subclass. But again...this idea somehow corresponds from aservice
implementation to a full-blown domain model....so yeah, maybe we should discuss this in the next dev call ;-)