Skip to content

Commit

Permalink
Refactor DefaultLatexParser, LatexParserResult classes (JabRef#11158)
Browse files Browse the repository at this point in the history
* Convert Citation class to record

* Add citation key to the Citation record

We need to represent the citations in a map of `Path` and  `Set<Citations>` to update each file individually.

* Remove fileList and nestedFiles list from LatexParserResult

`fileList` and `nestedFiles` are not used anywhere.

* Modified DefaultTexParser to parse file by file

* Adapt test classes

* Minor changes

* Fix MainArchitectureTest

* Update DefaultLatexParser.java

* Normalize Path to avoid duplications

* Add CitationFinder class

* new CitationFinder instance if the directory changed

* Update parse method to return Optional

* Fix refresh button

* Fix refresh button
  • Loading branch information
LoayGhreeb authored Apr 11, 2024
1 parent 8329b82 commit 3ddb9e0
Show file tree
Hide file tree
Showing 15 changed files with 419 additions and 424 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package org.jabref.gui.entryeditor;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Future;

Expand All @@ -22,12 +18,11 @@
import org.jabref.gui.util.DirectoryDialogConfiguration;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.texparser.DefaultLatexParser;
import org.jabref.logic.texparser.CitationFinder;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.texparser.Citation;
import org.jabref.model.texparser.LatexParserResult;
import org.jabref.preferences.PreferencesService;

import org.slf4j.Logger;
Expand All @@ -49,11 +44,11 @@ enum Status {
private final TaskExecutor taskExecutor;
private final DialogService dialogService;
private final ObjectProperty<Path> directory;
private CitationFinder citationFinder;
private final ObservableList<Citation> citationList;
private final ObjectProperty<Status> status;
private final StringProperty searchError;
private Future<?> searchTask;
private LatexParserResult latexParserResult;
private BibEntry currentEntry;

public LatexCitationsTabViewModel(BibDatabaseContext databaseContext,
Expand All @@ -66,6 +61,8 @@ public LatexCitationsTabViewModel(BibDatabaseContext databaseContext,
this.dialogService = dialogService;
this.directory = new SimpleObjectProperty<>(databaseContext.getMetaData().getLatexFileDirectory(preferencesService.getFilePreferences().getUserAndHost())
.orElse(FileUtil.getInitialDirectory(databaseContext, preferencesService.getFilePreferences().getWorkingDirectory())));

this.citationFinder = new CitationFinder(directory.get());
this.citationList = FXCollections.observableArrayList();
this.status = new SimpleObjectProperty<>(Status.IN_PROGRESS);
this.searchError = new SimpleStringProperty("");
Expand Down Expand Up @@ -102,7 +99,10 @@ public StringProperty searchErrorProperty() {
}

private void startSearch(String citeKey) {
searchTask = BackgroundTask.wrap(() -> searchAndParse(citeKey))
// we need to check whether the user meanwhile set the LaTeX file directory or the database changed locations
checkAndUpdateDirectory();

searchTask = BackgroundTask.wrap(() -> citationFinder.searchAndParse(citeKey))
.onRunning(() -> status.set(Status.IN_PROGRESS))
.onSuccess(result -> {
citationList.setAll(result);
Expand All @@ -124,39 +124,13 @@ private void cancelSearch() {
searchTask.cancel(true);
}

private Collection<Citation> searchAndParse(String citeKey) throws IOException {
// we need to check whether the user meanwhile set the LaTeX file directory or the database changed locations
public void checkAndUpdateDirectory() {
Path newDirectory = databaseContext.getMetaData().getLatexFileDirectory(preferencesService.getFilePreferences().getUserAndHost())
.orElse(FileUtil.getInitialDirectory(databaseContext, preferencesService.getFilePreferences().getWorkingDirectory()));

if (latexParserResult == null || !newDirectory.equals(directory.get())) {
if (!newDirectory.equals(directory.get())) {
directory.set(newDirectory);

if (!newDirectory.toFile().exists()) {
throw new IOException("Current search directory does not exist: %s".formatted(newDirectory));
}

List<Path> texFiles = searchDirectory(newDirectory);
LOGGER.debug("Found tex files: {}", texFiles);
latexParserResult = new DefaultLatexParser().parse(texFiles);
}

return latexParserResult.getCitationsByKey(citeKey);
}

/**
* @param directory the directory to search for. It is recursively searched.
*/
private List<Path> searchDirectory(Path directory) {
LOGGER.debug("Searching directory {}", directory);
try {
return Files.walk(directory)
.filter(Files::isRegularFile)
.filter(path -> path.toString().endsWith(TEX_EXT))
.toList();
} catch (IOException e) {
LOGGER.error("Error while searching files", e);
return List.of();
citationFinder = new CitationFinder(newDirectory);
}
}

Expand All @@ -171,7 +145,7 @@ public void setLatexDirectory() {
}

public void refreshLatexDirectory() {
latexParserResult = null;
citationFinder = new CitationFinder(directory.get());
init(currentEntry);
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/jabref/gui/texparser/CitationsDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public ObjectProperty<Path> basePathProperty() {

private Node getDisplayGraphic(Citation item) {
if (basePath.get() == null) {
basePath.set(item.getPath().getRoot());
basePath.set(item.path().getRoot());
}

Node citationIcon = IconTheme.JabRefIcons.LATEX_COMMENT.getGraphicNode();
Expand All @@ -49,19 +49,19 @@ private Node getDisplayGraphic(Citation item) {
HBox contextBox = new HBox(8, citationIcon, contextText);
contextBox.getStyleClass().add("contextBox");

Label fileNameLabel = new Label(String.format("%s", basePath.get().relativize(item.getPath())));
Label fileNameLabel = new Label(String.format("%s", basePath.get().relativize(item.path())));
fileNameLabel.setGraphic(IconTheme.JabRefIcons.LATEX_FILE.getGraphicNode());
Label positionLabel = new Label("(%s:%s-%s)".formatted(item.getLine(), item.getColStart(), item.getColEnd()));
Label positionLabel = new Label("(%s:%s-%s)".formatted(item.line(), item.colStart(), item.colEnd()));
positionLabel.setGraphic(IconTheme.JabRefIcons.LATEX_LINE.getGraphicNode());
HBox dataBox = new HBox(5, fileNameLabel, positionLabel);

return new VBox(contextBox, dataBox);
}

private Tooltip getDisplayTooltip(Citation item) {
String line = item.getLineText();
int start = item.getColStart();
int end = item.getColEnd();
String line = item.lineText();
int start = item.colStart();
int end = item.colEnd();

List<Text> texts = new ArrayList<>(3);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private FileNodeViewModel searchDirectory(Path directory) throws IOException {
List<Path> files = fileListPartition.get(false)
.stream()
.filter(path -> path.toString().endsWith(TEX_EXT))
.collect(Collectors.toList());
.toList();
int fileCount = 0;

for (Path subDirectory : subDirectories) {
Expand All @@ -185,7 +185,7 @@ private FileNodeViewModel searchDirectory(Path directory) throws IOException {
parent.setFileCount(files.size() + fileCount);
parent.getChildren().addAll(files.stream()
.map(FileNodeViewModel::new)
.collect(Collectors.toList()));
.toList());
return parent;
}

Expand All @@ -196,15 +196,14 @@ public void parseButtonClicked() {
List<Path> fileList = checkedFileList.stream()
.map(item -> item.getValue().getPath())
.filter(path -> path.toFile().isFile())
.collect(Collectors.toList());
.toList();
if (fileList.isEmpty()) {
LOGGER.warn("There are no valid files checked");
return;
}

TexBibEntriesResolver entriesResolver = new TexBibEntriesResolver(
databaseContext.getDatabase(),
preferencesService.getLibraryPreferences(),
preferencesService.getImportFormatPreferences(),
fileMonitor);

Expand Down
56 changes: 56 additions & 0 deletions src/main/java/org/jabref/logic/texparser/CitationFinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.jabref.logic.texparser;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;

import org.jabref.model.texparser.Citation;
import org.jabref.model.texparser.LatexParserResults;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CitationFinder {

private static final Logger LOGGER = LoggerFactory.getLogger(CitationFinder.class);
private static final String TEX_EXT = ".tex";
private final Path directory;

private LatexParserResults latexParserResults;

public CitationFinder(Path directory) {
this.directory = directory;
}

public Collection<Citation> searchAndParse(String citeKey) throws IOException {
if (latexParserResults == null) {
if (!Files.exists(directory)) {
throw new IOException("Current search directory does not exist: %s".formatted(directory));
}

List<Path> texFiles = searchDirectory(directory);
LOGGER.debug("Found tex files: {}", texFiles);
latexParserResults = new DefaultLatexParser().parse(texFiles);
}

return latexParserResults.getCitationsByKey(citeKey);
}

/**
* @param directory the directory to search for. It is recursively searched.
*/
private List<Path> searchDirectory(Path directory) {
LOGGER.debug("Searching directory {}", directory);
try (Stream<Path> paths = Files.walk(directory)) {
return paths.filter(Files::isRegularFile)
.filter(path -> path.toString().endsWith(TEX_EXT))
.toList();
} catch (IOException e) {
LOGGER.error("Error while searching files", e);
return List.of();
}
}
}
Loading

0 comments on commit 3ddb9e0

Please sign in to comment.