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

Jump to entry key cli (WIP) #659

Closed
wants to merge 12 commits into from
16 changes: 16 additions & 0 deletions src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ public void processArguments() {
doAuxImport(loaded);
}

if (!cli.isBlank() && cli.isJumpToEntryKey()) {
jumpToEntryKey(loaded, cli.getJumpToEntryKey());
}

this.parserResults = loaded;
}

Expand Down Expand Up @@ -773,6 +777,18 @@ private Optional<ParserResult> fetch(String fetchCommand) {
}
}

private void jumpToEntryKey(List<ParserResult> loaded, String citationKey) {
// search for this key in imported files
Copy link
Member

Choose a reason for hiding this comment

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

Remove this line, because the for each loop makes it clear somehow.

for (ParserResult parserResult : loaded) {
Optional<BibEntry> entry = parserResult.getDatabase().getEntryByCitationKey(citationKey);
if (entry.isPresent()) {
parserResult.setEntryToFocus(entry.get());
return;
}
}
LOGGER.error("Could not find given citation key to jump to");
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
LOGGER.error("Could not find given citation key to jump to");
LOGGER.error("Could not find citation key {} to jump to", citationKey);

Here, System.out.println is OK, because it was a CLI which outputs to the console. Please also add the output to the console. -- Maybe, even replace logger completely:

System.out.println("Could not find citation key %0 in any library", citationKey);

See https://devdocs.jabref.org/code-howtos/localization.html for steps for the localiaztion.

}

public boolean isBlank() {
return cli.isBlank();
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/jabref/cli/JabRefCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ public String getWriteMetadatatoPdf() {
cl.hasOption("embeddBibfileInPdf") ? cl.getOptionValue("embeddBibfileInPdf") : null;
}

public String getJumpToEntryKey() {
return cl.getOptionValue("jumpToEntryKey");
}

public boolean isJumpToEntryKey() {
return cl.hasOption("jumpToEntryKey");
}

private static Options getOptions() {
Options options = new Options();

Expand Down Expand Up @@ -288,6 +296,14 @@ private static Options getOptions() {
.argName("CITEKEY1[,CITEKEY2][,CITEKEYn] | PDF1[,PDF2][,PDFn] | all")
.build());

options.addOption(Option
.builder("j")
.longOpt("jumpToEntryKey")
Copy link
Member

Choose a reason for hiding this comment

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

Rename to jumpToKey. (We use CitationKey throughout JabRef. Sometimes also BibtexKey. Never EntryKey. With key you are on the safe side ^^)

.desc(String.format("%s: '%s'", Localization.lang("Jump to the BibEntry of the given key."), "-j key"))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
.desc(String.format("%s: '%s'", Localization.lang("Jump to the BibEntry of the given key."), "-j key"))
.desc(String.format("%s: '%s'", Localization.lang("Jump to the entry of the given citation key."), "-j key"))

.hasArg()
.argName("ENTRYKEY")
.build());

return options;
}

Expand Down
23 changes: 22 additions & 1 deletion src/main/java/org/jabref/gui/JabRefGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.jabref.logic.shared.exception.InvalidDBMSConnectionPropertiesException;
import org.jabref.logic.shared.exception.NotASharedDatabaseException;
import org.jabref.logic.util.WebViewStore;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.strings.StringUtil;
import org.jabref.model.util.FileUpdateMonitor;
import org.jabref.preferences.GuiPreferences;
Expand Down Expand Up @@ -201,14 +202,29 @@ private void openDatabases() {
.orElse(preferencesService.getGuiPreferences()
.getLastFocusedFile())
.toAbsolutePath();
// check whether there is a jump to entry and need to focus different file
boolean focusDifferent = false;
Optional<BibEntry> focusedEntry = Optional.empty();
for (ParserResult parserResult : parserResults) {
// Make sure this parser result is its own library instead of imported BibTex entries
Copy link
Member

Choose a reason for hiding this comment

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

Write "BibTeX" always with capital X

if (parserResult.getEntryToFocus().isPresent() && !parserResult.toOpenTab()) {
focusDifferent = true;
focusedEntry = parserResult.getEntryToFocus();
break;
}
}

// Add all bibDatabases databases to the frame:
boolean first = false;
for (ParserResult parserResult : parserResults) {
// Define focused tab
if (parserResult.getPath().filter(path -> path.toAbsolutePath().equals(focusedFile)).isPresent()) {
if (parserResult.getPath().filter(path -> path.toAbsolutePath().equals(focusedFile)).isPresent() && !focusDifferent) {
first = true;
}
if (parserResult.getEntryToFocus().isPresent() && focusDifferent) {
Copy link
Member

Choose a reason for hiding this comment

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

Should that be } else if (...?

first = true;
focusDifferent = false;
}

if (parserResult.getDatabase().isShared()) {
try {
Expand Down Expand Up @@ -279,6 +295,11 @@ private void openDatabases() {

OpenDatabaseAction.performPostOpenActions(libraryTab, pr);
}
// focus a particular entry if CLI has received a jump to entry key command
if (focusedEntry.isPresent()) {
mainFrame.getCurrentLibraryTab().clearAndSelect(focusedEntry.get());
mainFrame.showLibraryTab(mainFrame.getCurrentLibraryTab());
}

LOGGER.debug("Finished adding panels");
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/jabref/logic/importer/ParserResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class ParserResult {
private boolean invalid;
private boolean toOpenTab;
private boolean changedOnMigration = false;
private BibEntry entryToFocus;

public ParserResult() {
this(Collections.emptyList());
Expand Down Expand Up @@ -99,6 +100,14 @@ public void setPath(Path path) {
file = path;
}

public Optional<BibEntry> getEntryToFocus() {
return Optional.ofNullable(entryToFocus);
}

public void setEntryToFocus(BibEntry bibEntry) {
entryToFocus = bibEntry;
}

/**
* Add a parser warning.
*
Expand Down
Loading