diff --git a/com.vogella.ide.editor.asciidoc/src/com/vogella/ide/editor/asciidoc/AsciiDocConstants.java b/com.vogella.ide.editor.asciidoc/src/com/vogella/ide/editor/asciidoc/AsciiDocConstants.java new file mode 100644 index 0000000..bf3585f --- /dev/null +++ b/com.vogella.ide.editor.asciidoc/src/com/vogella/ide/editor/asciidoc/AsciiDocConstants.java @@ -0,0 +1,6 @@ +package com.vogella.ide.editor.asciidoc; + +public class AsciiDocConstants { + public static final String IMAGESDIR = "imagesdir"; + public static final String IMG_DIRECTORY = "img"; +} diff --git a/com.vogella.ide.editor.asciidoc/src/com/vogella/ide/editor/asciidoc/ImageHover.java b/com.vogella.ide.editor.asciidoc/src/com/vogella/ide/editor/asciidoc/ImageHover.java index 89b6360..a60647f 100644 --- a/com.vogella.ide.editor.asciidoc/src/com/vogella/ide/editor/asciidoc/ImageHover.java +++ b/com.vogella.ide.editor.asciidoc/src/com/vogella/ide/editor/asciidoc/ImageHover.java @@ -1,26 +1,22 @@ package com.vogella.ide.editor.asciidoc; +import java.io.File; + import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.IPath; -import org.eclipse.e4.core.contexts.IEclipseContext; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.ITextHover; import org.eclipse.jface.text.ITextViewer; import org.eclipse.jface.text.Region; -import org.eclipse.ui.IEditorInput; -import org.eclipse.ui.IEditorPart; -import org.eclipse.ui.PlatformUI; -import java.io.File; +import com.vogella.ide.editor.asciidoc.util.AsciiDocResourceUtil; public class ImageHover implements ITextHover { private static final String IMAGE_PREFIX = "image::"; - private static final String IMAGE_DIRECTORY = "img"; // Directory for images @Override public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) { @@ -42,15 +38,22 @@ public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) { String imageName = lineContent.substring(startIndex, endIndex).trim(); - IContainer parent = getParentFolder(); - IContainer imgFolder = parent.getFolder(IPath.fromOSString("img")); + IContainer parent = AsciiDocResourceUtil.getParentFolder(); + if (parent == null || !parent.isAccessible()) { + return ""; + } + + IContainer imgFolder = parent.getFolder(IPath.fromOSString(AsciiDocConstants.IMG_DIRECTORY)); + if (!imgFolder.isAccessible()) { + return "Image folder '" + AsciiDocConstants.IMG_DIRECTORY + "' not found"; + } - - IFile imageFile = imgFolder.getFile(IPath.fromOSString(imageName)); // Replace "filename.ext" with your actual file name + IFile imageFile = imgFolder.getFile(IPath.fromOSString(imageName)); - // Check if image file exists - if (imageFile.exists()) { + + // Check if image file exists - use isAccessible to avoid rule conflicts + if (imageFile.isAccessible()) { // Load and display the image in the hover (assuming HTML rendering is supported) return "\"""; } else { @@ -73,19 +76,4 @@ public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) { public IRegion getHoverRegion(ITextViewer textViewer, int offset) { return new Region(offset, 0); } - - - private IContainer getParentFolder() { - IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class); - Object object = context.get("activeEditor"); - - if (object instanceof IEditorPart activeEditor) { - - IEditorInput editorInput = activeEditor.getEditorInput(); - IResource adapter = editorInput.getAdapter(IResource.class); - IContainer parent = adapter.getParent(); - return parent; - } - return null; - } } diff --git a/com.vogella.ide.editor.asciidoc/src/com/vogella/ide/editor/asciidoc/ImageHyperlinkDetector.java b/com.vogella.ide.editor.asciidoc/src/com/vogella/ide/editor/asciidoc/ImageHyperlinkDetector.java index 83d9278..ac82e24 100644 --- a/com.vogella.ide.editor.asciidoc/src/com/vogella/ide/editor/asciidoc/ImageHyperlinkDetector.java +++ b/com.vogella.ide.editor.asciidoc/src/com/vogella/ide/editor/asciidoc/ImageHyperlinkDetector.java @@ -22,6 +22,8 @@ import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.PlatformUI; +import com.vogella.ide.editor.asciidoc.util.AsciiDocResourceUtil; + public class ImageHyperlinkDetector extends AbstractHyperlinkDetector { private static final String IMAGE_PROPERTY = "image::"; @@ -45,8 +47,15 @@ public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boo Region targetRegion = new Region(lineInformationOfOffset.getOffset() + IMAGE_PROPERTY.length(), lineInformationOfOffset.getLength() - IMAGE_PROPERTY.length()); - IContainer parent = getParentFolder(); - IContainer imgFolder = parent.getFolder(IPath.fromOSString("img")); + IContainer parent = AsciiDocResourceUtil.getParentFolder(); + if (parent == null || !parent.isAccessible()) { + return null; + } + + IContainer imgFolder = parent.getFolder(IPath.fromOSString(AsciiDocConstants.IMG_DIRECTORY)); + if (!imgFolder.isAccessible()) { + return null; + } // Only take resources, which have the "png" file extension IHyperlink[] result = Arrays.stream(imgFolder.members()) @@ -67,20 +76,6 @@ public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boo return null; } - - private IContainer getParentFolder() { - IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class); - Object object = context.get("activeEditor"); - - if (object instanceof IEditorPart activeEditor) { - - IEditorInput editorInput = activeEditor.getEditorInput(); - IResource adapter = editorInput.getAdapter(IResource.class); - IContainer parent = adapter.getParent(); - return parent; - } - return null; - } } diff --git a/com.vogella.ide.editor.asciidoc/src/com/vogella/ide/editor/asciidoc/IncludeHyperlinkDetector.java b/com.vogella.ide.editor.asciidoc/src/com/vogella/ide/editor/asciidoc/IncludeHyperlinkDetector.java index cff53ce..640ffe6 100644 --- a/com.vogella.ide.editor.asciidoc/src/com/vogella/ide/editor/asciidoc/IncludeHyperlinkDetector.java +++ b/com.vogella.ide.editor.asciidoc/src/com/vogella/ide/editor/asciidoc/IncludeHyperlinkDetector.java @@ -19,6 +19,8 @@ import org.eclipse.ui.IEditorPart; import org.eclipse.ui.PlatformUI; +import com.vogella.ide.editor.asciidoc.util.AsciiDocResourceUtil; + public class IncludeHyperlinkDetector extends AbstractHyperlinkDetector { private static final String HYPERLINK_PROPERTY = "include::"; @@ -27,7 +29,7 @@ public class IncludeHyperlinkDetector extends AbstractHyperlinkDetector { public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) { IDocument document = textViewer.getDocument(); - IContainer parent = getParentFolder(); + IContainer parent = AsciiDocResourceUtil.getParentFolder(); try { int offset = region.getOffset(); @@ -72,7 +74,8 @@ public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boo parent = subfolder; } - if (!parent.exists()) { + // Check if parent is accessible - avoid potential rule conflicts during builds + if (parent == null || !parent.isAccessible()) { return null; } @@ -102,18 +105,4 @@ public static boolean containsSubfolder(String relativePath) { return lastIndexOfParent != -1 && normalizedPath.indexOf("/", lastIndexOfParent + 3) != -1; } - private IContainer getParentFolder() { - IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class); - Object object = context.get("activeEditor"); - - if (object instanceof IEditorPart activeEditor) { - - IEditorInput editorInput = activeEditor.getEditorInput(); - IResource adapter = editorInput.getAdapter(IResource.class); - IContainer parent = adapter.getParent(); - return parent; - } - return null; - } - } \ No newline at end of file diff --git a/com.vogella.ide.editor.asciidoc/src/com/vogella/ide/editor/asciidoc/LinkHyperlinkDetector.java b/com.vogella.ide.editor.asciidoc/src/com/vogella/ide/editor/asciidoc/LinkHyperlinkDetector.java index b8b87bd..d58ef31 100644 --- a/com.vogella.ide.editor.asciidoc/src/com/vogella/ide/editor/asciidoc/LinkHyperlinkDetector.java +++ b/com.vogella.ide.editor.asciidoc/src/com/vogella/ide/editor/asciidoc/LinkHyperlinkDetector.java @@ -16,6 +16,7 @@ import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector; import org.eclipse.jface.text.hyperlink.IHyperlink; import org.eclipse.swt.program.Program; +import com.vogella.ide.editor.asciidoc.util.AsciiDocResourceUtil; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.PlatformUI; @@ -28,7 +29,7 @@ public class LinkHyperlinkDetector extends AbstractHyperlinkDetector { public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) { IDocument document = textViewer.getDocument(); - IContainer parent = getParentFolder(); + IContainer parent = AsciiDocResourceUtil.getParentFolder(); try { int offset = region.getOffset(); @@ -74,9 +75,10 @@ public void open() { }; } else if (target.startsWith("./") || target.startsWith("../")) { // Internal file link → open in Eclipse editor - if (parent != null && parent.exists()) { + // Use isAccessible to avoid rule conflicts during builds + if (parent != null && parent.isAccessible()) { IResource resource = parent.findMember(new Path(target)); - if (resource instanceof IFile file) { + if (resource instanceof IFile file && file.isAccessible()) { return new IHyperlink[] { new ResourceHyperlink(targetRegion, resource.getName(), file) }; @@ -90,18 +92,4 @@ public void open() { return null; } - - private IContainer getParentFolder() { - IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class); - Object object = context.get("activeEditor"); - - if (object instanceof IEditorPart activeEditor) { - IEditorInput editorInput = activeEditor.getEditorInput(); - IResource adapter = editorInput.getAdapter(IResource.class); - if (adapter != null) { - return adapter.getParent(); - } - } - return null; - } } diff --git a/com.vogella.ide.editor.asciidoc/src/com/vogella/ide/editor/asciidoc/util/AsciiDocResourceUtil.java b/com.vogella.ide.editor.asciidoc/src/com/vogella/ide/editor/asciidoc/util/AsciiDocResourceUtil.java new file mode 100644 index 0000000..fb06eef --- /dev/null +++ b/com.vogella.ide.editor.asciidoc/src/com/vogella/ide/editor/asciidoc/util/AsciiDocResourceUtil.java @@ -0,0 +1,35 @@ +package com.vogella.ide.editor.asciidoc.util; + +import java.util.concurrent.atomic.AtomicReference; + +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IResource; +import org.eclipse.e4.core.contexts.IEclipseContext; +import org.eclipse.swt.widgets.Display; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.PlatformUI; + +public class AsciiDocResourceUtil { + + public static IContainer getParentFolder() { + // Use AtomicReference to hold the result from the UI thread + final AtomicReference containerRef = new AtomicReference<>(); + + // Ensure UI-related code runs on the UI thread + Display.getDefault().syncExec(() -> { + IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class); + Object object = context.get("activeEditor"); + + if (object instanceof IEditorPart activeEditor) { + IEditorInput editorInput = activeEditor.getEditorInput(); + IResource adapter = editorInput.getAdapter(IResource.class); + if (adapter != null) { + containerRef.set(adapter.getParent()); + } + } + }); + + return containerRef.get(); + } +}