Skip to content

Commit

Permalink
Update of Asciidoc hyperlinking
Browse files Browse the repository at this point in the history
  • Loading branch information
vogella committed Dec 19, 2024
1 parent eb75d1b commit ab14a8f
Show file tree
Hide file tree
Showing 5 changed files with 6,698 additions and 2,433 deletions.
2 changes: 1 addition & 1 deletion com.vogella.ide.editor.asciidoc/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<extension
point="org.eclipse.tm4e.registry.grammars">
<grammar
path="syntaxes/asciidoc.tmLanguage"
path="syntaxes/asciidoc.json"
scopeName="com.vogella.ide.editor.asciidoc.grammar">
</grammar>
<scopeNameContentTypeBinding
Expand Down
27 changes: 0 additions & 27 deletions com.vogella.ide.editor.asciidoc/pom-for-review.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.jface.text.BadLocationException;
Expand All @@ -18,9 +17,6 @@
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

public class IncludeHyperlinkDetector extends AbstractHyperlinkDetector {
Expand All @@ -47,23 +43,31 @@ public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boo
lineInformationOfOffset.getLength() - HYPERLINK_PROPERTY.length());

String fileName;
// // we support subfolder in the same level as we are
if (dependentResourceName.startsWith("../")||dependentResourceName.startsWith("./") ) {

String folder = dependentResourceName.substring(0, dependentResourceName.lastIndexOf("/"));
dependentResourceName = dependentResourceName.substring(dependentResourceName.lastIndexOf("/")+1, dependentResourceName.length());
IContainer subfolder = parent.getFolder(new Path(folder));
parent = subfolder;

// Check if the dependent resource starts with "./" or "../"
if (dependentResourceName.startsWith("../") || dependentResourceName.startsWith("./")) {

if (containsSubfolder(dependentResourceName)) {
// Handle cases with subdirectories
int lastSlashIndex = dependentResourceName.lastIndexOf("/");
String folder = dependentResourceName.substring(0, lastSlashIndex);
dependentResourceName = dependentResourceName.substring(lastSlashIndex + 1);
IContainer subfolder = parent.getFolder(new Path(folder));
parent = subfolder;
} else {
// Handle cases like "../exercise_settings.adoc" without a folder structure
dependentResourceName = dependentResourceName.substring(3); // Remove "../"
}
}

if (!parent.exists()) {
return null;
}
fileName= dependentResourceName;

fileName = dependentResourceName;
IResource[] members = parent.members();
// Only take resources, which have the "adoc" file extension and skip the
// current resource itself
// Only take resources with the "adoc" file extension and skip the current
// resource itself
IHyperlink[] result = Arrays.stream(members)
.filter(res -> res instanceof IFile && res.getName().equals(fileName))
.map(res -> new ResourceHyperlink(targetRegion, res.getName(), (IFile) res))
Expand All @@ -80,6 +84,12 @@ public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boo
return null;
}

public static boolean containsSubfolder(String relativePath) {
String normalizedPath = relativePath.replace("\\", "/"); // Normalize for cross-platform
int lastIndexOfParent = normalizedPath.lastIndexOf("../");
return lastIndexOfParent != -1 && normalizedPath.indexOf("/", lastIndexOfParent + 3) != -1;
}

private IContainer getParentFolder() {
IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
Object object = context.get("activeEditor");
Expand Down
Loading

0 comments on commit ab14a8f

Please sign in to comment.