From 003ec3bf87730fee51db9d4e2ab941a3f7fc74cb Mon Sep 17 00:00:00 2001 From: AndreasTu Date: Sat, 5 Mar 2016 18:26:50 +0100 Subject: [PATCH 1/2] Added the first version of the SynctexParserAdapter --- de.vonloesch.pdf4eclipse/META-INF/MANIFEST.MF | 1 + de.vonloesch.pdf4eclipse/build.properties | 3 +- de.vonloesch.pdf4eclipse/plugin.xml | 1 + ...esch.pdf4Eclipse.synctexParserAdapter.exsd | 112 ++++++++++++++++++ .../pdf4eclipse/editors/PDFEditor.java | 25 ++-- .../synctex/DefaultSynctexParserFactory.java | 67 +++++++++++ .../de/vonloesch/synctex/ISynctexParser.java | 40 +++++++ .../synctex/ISynctexParserAdapterFactory.java | 33 ++++++ .../synctex/SimpleSynctexParser.java | 17 ++- 9 files changed, 284 insertions(+), 15 deletions(-) create mode 100644 de.vonloesch.pdf4eclipse/schema/de.vonloesch.pdf4Eclipse.synctexParserAdapter.exsd create mode 100644 de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/DefaultSynctexParserFactory.java create mode 100644 de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/ISynctexParser.java create mode 100644 de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/ISynctexParserAdapterFactory.java diff --git a/de.vonloesch.pdf4eclipse/META-INF/MANIFEST.MF b/de.vonloesch.pdf4eclipse/META-INF/MANIFEST.MF index 3c73f82..5c6b2e0 100644 --- a/de.vonloesch.pdf4eclipse/META-INF/MANIFEST.MF +++ b/de.vonloesch.pdf4eclipse/META-INF/MANIFEST.MF @@ -20,3 +20,4 @@ Bundle-ClassPath: ., lib/jbig2.jar, lib/PDFrenderer.jar Bundle-Vendor: %Bundle-Vendor +Export-Package: de.vonloesch.synctex diff --git a/de.vonloesch.pdf4eclipse/build.properties b/de.vonloesch.pdf4eclipse/build.properties index 16e1348..ea806f7 100644 --- a/de.vonloesch.pdf4eclipse/build.properties +++ b/de.vonloesch.pdf4eclipse/build.properties @@ -5,5 +5,6 @@ bin.includes = plugin.xml,\ .,\ icons/,\ lib/,\ - OSGI-INF/l10n/bundle.properties + OSGI-INF/l10n/bundle.properties,\ + schema/ diff --git a/de.vonloesch.pdf4eclipse/plugin.xml b/de.vonloesch.pdf4eclipse/plugin.xml index fad2752..b8948e2 100644 --- a/de.vonloesch.pdf4eclipse/plugin.xml +++ b/de.vonloesch.pdf4eclipse/plugin.xml @@ -1,6 +1,7 @@ + diff --git a/de.vonloesch.pdf4eclipse/schema/de.vonloesch.pdf4Eclipse.synctexParserAdapter.exsd b/de.vonloesch.pdf4eclipse/schema/de.vonloesch.pdf4Eclipse.synctexParserAdapter.exsd new file mode 100644 index 0000000..45cae24 --- /dev/null +++ b/de.vonloesch.pdf4eclipse/schema/de.vonloesch.pdf4Eclipse.synctexParserAdapter.exsd @@ -0,0 +1,112 @@ + + + + + + + + + SynctexParserAdapter provides an extension to register an adapter to manipulate the SyncTex file resolution. +This could be used the change the file which will open/is open by the PDF-Viewer. + +This is especially helpful, when the used .tex files are generated from other files and the editor shall display the source files. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + [Enter the first release in which this extension point appears.] + + + + + + + + + [Enter extension point usage example here.] + + + + + + + + + [Enter API information here.] + + + + + + + + + [Enter information about supplied implementation of this extension point.] + + + + + diff --git a/de.vonloesch.pdf4eclipse/src/de/vonloesch/pdf4eclipse/editors/PDFEditor.java b/de.vonloesch.pdf4eclipse/src/de/vonloesch/pdf4eclipse/editors/PDFEditor.java index 12e3459..a053beb 100644 --- a/de.vonloesch.pdf4eclipse/src/de/vonloesch/pdf4eclipse/editors/PDFEditor.java +++ b/de.vonloesch.pdf4eclipse/src/de/vonloesch/pdf4eclipse/editors/PDFEditor.java @@ -88,7 +88,8 @@ import de.vonloesch.pdf4eclipse.model.PDFFactory; import de.vonloesch.pdf4eclipse.outline.PDFFileOutline; import de.vonloesch.pdf4eclipse.preferences.PreferenceConstants; -import de.vonloesch.synctex.SimpleSynctexParser; +import de.vonloesch.synctex.DefaultSynctexParserFactory; +import de.vonloesch.synctex.ISynctexParser; /** * @@ -523,7 +524,7 @@ private File getSyncTeXFile() { return null; } - private SimpleSynctexParser createSimpleSynctexParser(File f) + private ISynctexParser createSimpleSynctexParser(File f) throws IOException { InputStream in; if (f.getName().toLowerCase().endsWith(".gz")) { @@ -533,7 +534,7 @@ private SimpleSynctexParser createSimpleSynctexParser(File f) in = new FileInputStream(f); } BufferedReader r = new BufferedReader(new InputStreamReader(in)); - return new SimpleSynctexParser(r); + return DefaultSynctexParserFactory.create(r); } /** @@ -551,7 +552,7 @@ public int forwardSearch(String file, int lineNr) { if (syncTeXFile == null) return FORWARD_SEARCH_NO_SYNCTEX; try { //FIXME: Create a job for this - SimpleSynctexParser p = createSimpleSynctexParser(syncTeXFile); + ISynctexParser p = createSimpleSynctexParser(syncTeXFile); //System.out.println("Start Forward search"); p.setForwardSearchInformation(file, lineNr); p.startForward(); @@ -595,23 +596,25 @@ public void reverseSearch(double pdfX, double pdfY) { //File f = new File (((IFileEditorInput) getEditorInput()).getFile().getRawLocation().removeFileExtension().addFileExtension("synctex.gz").toOSString()); try { //FIXME: Create a job for this - SimpleSynctexParser p = createSimpleSynctexParser(f); + ISynctexParser p = createSimpleSynctexParser(f); p.setReverseSearchInformation(currentPage, pdfX, pdfY); p.startReverse(); p.close(); - if (p.sourceFilePath == null) { + String sourceFilePath = p.getSourceFilePath(); + int sourceLineNr = p.getSourceLineNr(); + if (sourceFilePath == null) { //Could not find a source file writeStatusLineError(Messages.PDFEditor_SynctexMsg2); return; } - File sourceFile = new File(p.sourceFilePath); - String path = p.sourceFilePath; + File sourceFile = new File(sourceFilePath); + String path = sourceFilePath; if (!sourceFile.isAbsolute()) { //Append it to the path of the pdf path = f.getCanonicalPath(); - path = path.substring(0, path.lastIndexOf(File.separatorChar)+1) + p.sourceFilePath; + path = path.substring(0, path.lastIndexOf(File.separatorChar)+1) + sourceFilePath; } IFileStore fileStore = EFS.getLocalFileSystem().fromLocalFile(new File(path)); if (!fileStore.fetchInfo().isDirectory() && fileStore.fetchInfo().exists()) { @@ -621,12 +624,12 @@ public void reverseSearch(double pdfX, double pdfY) { if (part instanceof AbstractTextEditor) { AbstractTextEditor t = (AbstractTextEditor) part; IDocument doc = t.getDocumentProvider().getDocument(t.getEditorInput()); - t.selectAndReveal(doc.getLineOffset(p.sourceLineNr - 1), doc.getLineLength(p.sourceLineNr - 1)); + t.selectAndReveal(doc.getLineOffset(sourceLineNr - 1), doc.getLineLength(sourceLineNr - 1)); } } catch (PartInitException e) { e.printStackTrace(); } catch (BadLocationException e) { - writeStatusLineError(NLS.bind(Messages.PDFEditor_SynctexMsg3, p.sourceLineNr - 1)); + writeStatusLineError(NLS.bind(Messages.PDFEditor_SynctexMsg3, sourceLineNr - 1)); } } else { writeStatusLineError(NLS.bind(Messages.PDFEditor_SynctexMsg4, path)); diff --git a/de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/DefaultSynctexParserFactory.java b/de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/DefaultSynctexParserFactory.java new file mode 100644 index 0000000..2e69ee0 --- /dev/null +++ b/de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/DefaultSynctexParserFactory.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * Copyright (c) 2016 Boris von Loesch. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Andreas Turban - initial API and implementation + ******************************************************************************/ +package de.vonloesch.synctex; + +import java.io.BufferedReader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IConfigurationElement; +import org.eclipse.core.runtime.Platform; + +/** + * Factory to create {@link ISynctexParser} instances and wrap them with + * contributed adapters of the {@link ISynctexParser}, with + * {@link ISynctexParserAdapterFactory}. + * + *

+ * The {@link ISynctexParserAdapterFactory} instances are contributed via an + * eclipse extension point. + *

+ * + * @author Andreas Turban + * + */ +public class DefaultSynctexParserFactory { + + private static final String EXT_ID = "de.vonloesch.pdf4Eclipse.synctexParserAdapter"; + + private static final List adapters; + + static { + ArrayList adaptersLoc = new ArrayList(); + IConfigurationElement[] configurationElements = Platform.getExtensionRegistry() + .getConfigurationElementsFor(EXT_ID); + for (IConfigurationElement elem : configurationElements) { + try { + Object obj = elem.createExecutableExtension("class"); + if (obj instanceof ISynctexParserAdapterFactory) { + adaptersLoc.add((ISynctexParserAdapterFactory) obj); + } + } catch (CoreException e) { + // Ignore wrong contributed adapter + } + } + adaptersLoc.trimToSize(); + adapters = Collections.unmodifiableList(adaptersLoc); + } + + public static ISynctexParser create(BufferedReader r) { + ISynctexParser parser = new SimpleSynctexParser(r); + for (ISynctexParserAdapterFactory factory : adapters) { + parser = factory.createAdapter(parser); + } + return parser; + } + +} diff --git a/de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/ISynctexParser.java b/de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/ISynctexParser.java new file mode 100644 index 0000000..9e30164 --- /dev/null +++ b/de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/ISynctexParser.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * Copyright (c) 2016 Boris von Loesch. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Andreas Turban - initial API + ******************************************************************************/ +package de.vonloesch.synctex; + +import java.io.IOException; + +/** + * {@link ISynctexParser} defines the API for the Synctex search feature. + * + * @author Andreas Turban + * @author Boris von Loesch + * @see DefaultSynctexParserFactory + */ +public interface ISynctexParser { + + void setReverseSearchInformation(int currentPage, double pdfX, double pdfY); + + void startReverse() throws IOException; + + void startForward() throws IOException; + + void close() throws IOException; + + String getSourceFilePath(); + + int getSourceLineNr(); + + void setForwardSearchInformation(String file, int lineNr); + + double[] getForwardSearchResult(); + +} diff --git a/de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/ISynctexParserAdapterFactory.java b/de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/ISynctexParserAdapterFactory.java new file mode 100644 index 0000000..4722fe4 --- /dev/null +++ b/de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/ISynctexParserAdapterFactory.java @@ -0,0 +1,33 @@ +/******************************************************************************* + * Copyright (c) 2016 Boris von Loesch. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Andreas Turban - initial API and implementation + ******************************************************************************/ +package de.vonloesch.synctex; + + +/** + * {@link ISynctexParserAdapterFactory} wraps an passed {@link ISynctexParser} with an adapter to change the forward and reverse search behavior. + * contributed adapters of the {@link ISynctexParser}, with + * {@link ISynctexParserAdapterFactory}. + * + *

+ * The {@link ISynctexParserAdapterFactory} instances are contributed via the + * eclipse extension point "de.vonloesch.pdf4Eclipse.synctexParserAdapter". + *

+ * + * @author Andreas Turban + * + */ +public interface ISynctexParserAdapterFactory { + + /** + * Shall wrap the passed {@link ISynctexParser} to adapt the forward and reverse search behavior. + */ + ISynctexParser createAdapter(ISynctexParser parser); +} diff --git a/de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/SimpleSynctexParser.java b/de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/SimpleSynctexParser.java index 8229b2e..74f6963 100644 --- a/de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/SimpleSynctexParser.java +++ b/de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/SimpleSynctexParser.java @@ -7,6 +7,7 @@ * * Contributors: * Boris von Loesch - initial API and implementation + * Andreas Turban - Added interface ISynctexParser ******************************************************************************/ package de.vonloesch.synctex; @@ -28,7 +29,7 @@ * @author Boris von Loesch * */ -public class SimpleSynctexParser { +class SimpleSynctexParser implements ISynctexParser { public static final int SEARCH_FORWARD = 1; public static final int SEARCH_REVERSE = 2; @@ -43,8 +44,8 @@ public class SimpleSynctexParser { Map fileMap; //Forward search information - public String sourceFilePath; - public int sourceLineNr; + private String sourceFilePath; + private int sourceLineNr; //helper (forward search) private int smallerLineNr; @@ -465,4 +466,14 @@ String getColonArg(String line, int pos) { nextPos = endPos; return line.substring(pos + 1, endPos); } + + @Override + public String getSourceFilePath() { + return sourceFilePath; + } + + @Override + public int getSourceLineNr() { + return sourceLineNr; + } } From 5c7a968250563bea5a5d67a24249e1128445bb33 Mon Sep 17 00:00:00 2001 From: Andreas Turban Date: Tue, 8 Mar 2016 08:06:33 +0100 Subject: [PATCH 2/2] IProject reference added in ISynctexParser to allow lookup of mulitple PDF. Fixed wrong schema link inf plugin.xml --- de.vonloesch.pdf4eclipse/plugin.xml | 2 +- .../pdf4eclipse/editors/PDFEditor.java | 8 ++++++-- .../de/vonloesch/synctex/ISynctexParser.java | 20 +++++++++++-------- .../synctex/SimpleSynctexParser.java | 12 +++++++++++ 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/de.vonloesch.pdf4eclipse/plugin.xml b/de.vonloesch.pdf4eclipse/plugin.xml index b8948e2..74b1ba1 100644 --- a/de.vonloesch.pdf4eclipse/plugin.xml +++ b/de.vonloesch.pdf4eclipse/plugin.xml @@ -1,7 +1,7 @@ - + diff --git a/de.vonloesch.pdf4eclipse/src/de/vonloesch/pdf4eclipse/editors/PDFEditor.java b/de.vonloesch.pdf4eclipse/src/de/vonloesch/pdf4eclipse/editors/PDFEditor.java index a053beb..c4277ee 100644 --- a/de.vonloesch.pdf4eclipse/src/de/vonloesch/pdf4eclipse/editors/PDFEditor.java +++ b/de.vonloesch.pdf4eclipse/src/de/vonloesch/pdf4eclipse/editors/PDFEditor.java @@ -27,6 +27,7 @@ import org.eclipse.core.filesystem.EFS; import org.eclipse.core.filesystem.IFileStore; import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResourceChangeEvent; import org.eclipse.core.resources.IResourceChangeListener; import org.eclipse.core.resources.IResourceDelta; @@ -128,6 +129,7 @@ public class PDFEditor extends EditorPart implements IResourceChangeListener, private Cursor cursorHand; private Cursor cursorArrow; + private IProject eclipseProject; public PDFEditor() { super(); @@ -177,7 +179,9 @@ public void readPdfFile() throws PartInitException{ file = new File(((FileStoreEditorInput)input).getURI()); } else if ((input instanceof IFileEditorInput)) { - file = new File(((IFileEditorInput) input).getFile().getLocationURI()); + IFileEditorInput eclipseInputFile = (IFileEditorInput) input; + file = new File(eclipseInputFile.getFile().getLocationURI()); + eclipseProject = eclipseInputFile.getFile().getProject(); } else { throw new PartInitException(Messages.PDFEditor_ErrorMsg1); @@ -553,7 +557,7 @@ public int forwardSearch(String file, int lineNr) { try { //FIXME: Create a job for this ISynctexParser p = createSimpleSynctexParser(syncTeXFile); - //System.out.println("Start Forward search"); + p.setEclipseProject(eclipseProject); p.setForwardSearchInformation(file, lineNr); p.startForward(); p.close(); diff --git a/de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/ISynctexParser.java b/de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/ISynctexParser.java index 9e30164..e8e03ae 100644 --- a/de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/ISynctexParser.java +++ b/de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/ISynctexParser.java @@ -12,6 +12,8 @@ import java.io.IOException; +import org.eclipse.core.resources.IProject; + /** * {@link ISynctexParser} defines the API for the Synctex search feature. * @@ -21,20 +23,22 @@ */ public interface ISynctexParser { - void setReverseSearchInformation(int currentPage, double pdfX, double pdfY); + void setReverseSearchInformation(int currentPage, double pdfX, double pdfY); + + void startReverse() throws IOException; - void startReverse() throws IOException; + void startForward() throws IOException; - void startForward() throws IOException; + void close() throws IOException; - void close() throws IOException; + String getSourceFilePath(); - String getSourceFilePath(); + int getSourceLineNr(); - int getSourceLineNr(); + void setForwardSearchInformation(String file, int lineNr); - void setForwardSearchInformation(String file, int lineNr); + double[] getForwardSearchResult(); - double[] getForwardSearchResult(); + void setEclipseProject(IProject eclipseProject); } diff --git a/de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/SimpleSynctexParser.java b/de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/SimpleSynctexParser.java index 74f6963..2701519 100644 --- a/de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/SimpleSynctexParser.java +++ b/de.vonloesch.pdf4eclipse/src/de/vonloesch/synctex/SimpleSynctexParser.java @@ -18,6 +18,8 @@ import java.util.Stack; import java.util.TreeMap; +import org.eclipse.core.resources.IProject; + /** * A simple parser for SyncTeX files. Allows forward and reverse search. * More information about SyncTeX and the official C-implementation can be found @@ -83,7 +85,17 @@ public SimpleSynctexParser(BufferedReader r) { dist = Double.MAX_VALUE; } + + /** + * {@inheritDoc} + */ + @Override + public void setEclipseProject(IProject eclipseProject) { + + } + + /** * Closes the SimpleSynctexParser and releases any * system resources associated with it. * Once the stream has been closed, further