Search in sources :

Example 16 with IHyperlink

use of org.eclipse.jface.text.hyperlink.IHyperlink in project xtext-xtend by eclipse.

the class HyperlinkingTest method testOpenInferredType.

@Test
public void testOpenInferredType() throws Exception {
    String modelAsString = "class Baz { def void foo() { var myVar='' println(myVar) } }";
    XtextResource resource = (XtextResource) testHelper.xtendFile("Baz", modelAsString).eResource();
    int indexOf_x_FieldRef = modelAsString.indexOf("myVar");
    IHyperlink[] hyperlinks = hyperlinkHelper.createHyperlinksByOffset(resource, indexOf_x_FieldRef, true);
    assertEquals(1, hyperlinks.length);
    assertEquals("Open Inferred Type - String", hyperlinks[0].getHyperlinkText());
}
Also used : IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) XtextResource(org.eclipse.xtext.resource.XtextResource) Test(org.junit.Test)

Example 17 with IHyperlink

use of org.eclipse.jface.text.hyperlink.IHyperlink in project linuxtools by eclipse.

the class MailHyperlinkDetector method detectHyperlinks.

@Override
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
    if (region == null || textViewer == null) {
        return null;
    }
    if (editor == null) {
        editor = this.getAdapter(SpecfileEditor.class);
        if (editor == null) {
            return null;
        }
    }
    IDocument document = textViewer.getDocument();
    int offset = region.getOffset();
    String urlString = null;
    if (document == null) {
        return null;
    }
    IRegion lineInfo;
    String line;
    String mail;
    int mailLength = 0;
    int mailOffsetInLine;
    try {
        lineInfo = document.getLineInformationOfOffset(offset);
        line = document.get(lineInfo.getOffset(), lineInfo.getLength());
    } catch (BadLocationException ex) {
        ex.printStackTrace();
        return null;
    }
    int startSeparator = line.indexOf('<');
    mailOffsetInLine = startSeparator + 1;
    if (startSeparator != -1) {
        int endSeparator = line.indexOf('>');
        if (endSeparator < 5) {
            return null;
        }
        mail = line.substring(startSeparator + 1, endSeparator).trim();
        mailLength = mail.length();
        // Some cleanups, maybe we can add more.
        // $NON-NLS-1$ //$NON-NLS-2$
        mail = mail.replaceAll("(?i) at ", "@");
        // $NON-NLS-1$ //$NON-NLS-2$
        mail = mail.replaceAll("(?i) dot ", ".");
        // $NON-NLS-1$ //$NON-NLS-2$
        mail = mail.replaceAll("(?i)_at_", "@");
        // $NON-NLS-1$ //$NON-NLS-2$
        mail = mail.replaceAll("(?i)_dot_", ".");
        // $NON-NLS-1$ //$NON-NLS-2$
        mail = mail.replaceAll(" +", " ");
        if (mail.split(" ").length == 3) {
            // $NON-NLS-1$
            if (mail.indexOf('@') == -1) {
                // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
                mail = mail.replaceFirst(" ", "@").replaceFirst(" ", ".");
            }
        }
        // $NON-NLS-1$ //$NON-NLS-2$
        mail = mail.replaceAll(" ", "");
    } else {
        int offsetInLine = offset - lineInfo.getOffset();
        boolean startDoubleQuote = false;
        mailOffsetInLine = 0;
        int mailSeparatorOffset = line.indexOf('@');
        while (mailSeparatorOffset >= 0) {
            // (left to "@")
            mailOffsetInLine = mailSeparatorOffset;
            char ch;
            do {
                mailOffsetInLine--;
                ch = ' ';
                if (mailOffsetInLine > -1) {
                    ch = line.charAt(mailOffsetInLine);
                }
                startDoubleQuote = ch == '"';
            } while (Character.isLetterOrDigit(ch) || ch == '.' || ch == '_' || ch == '-');
            mailOffsetInLine++;
            // a valid mail contain a left part.
            if (mailOffsetInLine == mailSeparatorOffset) {
                return null;
            }
            // Right to "@"
            // $NON-NLS-1$
            StringTokenizer tokenizer = new StringTokenizer(line.substring(mailSeparatorOffset + 3), " \t\n\r\f<>", false);
            if (!tokenizer.hasMoreTokens()) {
                return null;
            }
            mailLength = tokenizer.nextToken().length() + 3 + mailSeparatorOffset - mailOffsetInLine;
            if (offsetInLine >= mailOffsetInLine && offsetInLine <= mailOffsetInLine + mailLength) {
                break;
            }
            mailSeparatorOffset = line.indexOf('@', mailSeparatorOffset + 1);
        }
        if (mailSeparatorOffset < 0) {
            return null;
        }
        if (startDoubleQuote) {
            int endOffset = -1;
            int nextDoubleQuote = line.indexOf('"', mailOffsetInLine);
            int nextWhitespace = line.indexOf(' ', mailOffsetInLine);
            if (nextDoubleQuote != -1 && nextWhitespace != -1) {
                endOffset = Math.min(nextDoubleQuote, nextWhitespace);
            } else if (nextDoubleQuote != -1) {
                endOffset = nextDoubleQuote;
            } else if (nextWhitespace != -1) {
                endOffset = nextWhitespace;
            }
            if (endOffset != -1) {
                mailLength = endOffset - mailOffsetInLine;
            }
        }
        if (mailLength == 0) {
            return null;
        }
        mail = line.substring(mailOffsetInLine, mailOffsetInLine + mailLength);
    }
    try {
        // mail address contain at less one '@' and one '.' character.
        if (!mail.contains("@") || !mail.contains(".")) {
            // $NON-NLS-1$ //$NON-NLS-2$
            return null;
        }
        // $NON-NLS-1$
        urlString = "mailto:" + mail;
        char separator = '?';
        String subject = getSubject();
        if (subject != null) {
            // $NON-NLS-1$
            urlString += separator + "subject=" + subject;
            separator = '&';
        }
        String body = getBody();
        if (body != null) {
            // $NON-NLS-1$
            urlString += separator + "body=" + body;
        }
        // url don't like %
        // $NON-NLS-1$ //$NON-NLS-2$
        urlString = urlString.replaceAll("\\%", "\\%25");
        new URL(urlString);
    } catch (MalformedURLException ex) {
        ex.printStackTrace();
        urlString = null;
        return null;
    }
    IRegion urlRegion = new Region(lineInfo.getOffset() + mailOffsetInLine, mailLength);
    return new IHyperlink[] { new MailHyperlink(urlRegion, urlString) };
}
Also used : MalformedURLException(java.net.MalformedURLException) IRegion(org.eclipse.jface.text.IRegion) URL(java.net.URL) StringTokenizer(java.util.StringTokenizer) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) SpecfileEditor(org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileEditor) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 18 with IHyperlink

use of org.eclipse.jface.text.hyperlink.IHyperlink in project linuxtools by eclipse.

the class SourcesFileHyperlinkDetector method detectHyperlinks.

@Override
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
    if (region == null || textViewer == null) {
        return null;
    }
    if (editor == null) {
        editor = this.getAdapter(SpecfileEditor.class);
        if (editor == null) {
            return null;
        }
    }
    IDocument document = textViewer.getDocument();
    int offset = region.getOffset();
    if (document == null) {
        return null;
    }
    IRegion lineInfo;
    String line;
    try {
        lineInfo = document.getLineInformationOfOffset(offset);
        line = document.get(lineInfo.getOffset(), lineInfo.getLength());
    } catch (BadLocationException ex) {
        return null;
    }
    List<IHyperlink> tempHList = new ArrayList<>();
    // !! it feels like there is duplicate code, fix that !!
    if (editor.getEditorInput() instanceof FileEditorInput) {
        IFile original = ((FileEditorInput) editor.getEditorInput()).getFile();
        if (line.startsWith(SOURCE_IDENTIFIER) || line.startsWith(PATCH_IDENTIFIER) || line.startsWith(URL_IDENTIFIER)) {
            int delimiterIndex = line.indexOf(':') + 1;
            String identifierValue = line.substring(delimiterIndex).trim();
            boolean validURL = RPMUtils.isValidUrl(identifierValue);
            // if valid URL, get its file name; else make file name the original identifier value
            String fileName = validURL ? RPMUtils.getURLFilename(identifierValue) : identifierValue;
            String resolvedFileName = UiUtils.resolveDefines(editor.getSpecfile(), fileName);
            boolean fileExists = RPMUtils.fileExistsInSources(original, resolvedFileName);
            if (region.getOffset() > lineInfo.getOffset() + line.indexOf(identifierValue)) {
                IRegion fileNameRegion = new Region(lineInfo.getOffset() + line.indexOf(identifierValue), identifierValue.length());
                if (fileExists) {
                    // add "Open" file option
                    tempHList.add(new SourcesFileHyperlink(original, resolvedFileName, fileNameRegion));
                } else {
                    if (line.startsWith(PATCH_IDENTIFIER) && !identifierValue.endsWith("/")) {
                        // $NON-NLS-1$
                        // add "Create" patch option using filename
                        tempHList.add(new SourcesFileCreateHyperlink(original, resolvedFileName, fileNameRegion));
                    }
                }
                // if valid URL and has a valid file
                if (validURL && !identifierValue.endsWith("/")) {
                    // $NON-NLS-1$
                    // add "Download" option
                    tempHList.add(new SourcesFileDownloadHyperlink(original, UiUtils.resolveDefines(editor.getSpecfile(), identifierValue), fileNameRegion));
                }
            }
        }
    }
    return tempHList.isEmpty() ? null : tempHList.toArray(new IHyperlink[tempHList.size()]);
}
Also used : IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) IRegion(org.eclipse.jface.text.IRegion) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) FileEditorInput(org.eclipse.ui.part.FileEditorInput) SpecfileEditor(org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileEditor) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 19 with IHyperlink

use of org.eclipse.jface.text.hyperlink.IHyperlink in project linuxtools by eclipse.

the class SpecfileElementHyperlinkDetector method prepareHyperlink.

private IHyperlink[] prepareHyperlink(IRegion lineInfo, String line, String word, SpecfileElement source, int lineIndex) {
    IRegion urlRegion = new Region(lineInfo.getOffset() + line.indexOf(word, lineIndex), word.length());
    // will only work with 1 active page
    // does not work with CompareEditor
    IWorkbench wb = PlatformUI.getWorkbench();
    IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
    IWorkbenchPage page = win.getActivePage();
    IEditorPart editor = page.getActiveEditor();
    // we can only provide this functionality for resources inside the workbench.
    if (editor.getEditorInput() instanceof FileEditorInput) {
        IFile original = ((FileEditorInput) editor.getEditorInput()).getFile();
        return new IHyperlink[] { new SpecfileElementHyperlink(urlRegion, source, original) };
    } else {
        return null;
    }
}
Also used : IWorkbench(org.eclipse.ui.IWorkbench) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IFile(org.eclipse.core.resources.IFile) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) FileEditorInput(org.eclipse.ui.part.FileEditorInput) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IEditorPart(org.eclipse.ui.IEditorPart) IRegion(org.eclipse.jface.text.IRegion)

Example 20 with IHyperlink

use of org.eclipse.jface.text.hyperlink.IHyperlink in project linuxtools by eclipse.

the class URLHyperlinkWithMacroDetector method detectHyperlinks.

@Override
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
    if (specfile == null) {
        SpecfileEditor a = this.getAdapter(SpecfileEditor.class);
        if (a != null) {
            specfile = a.getSpecfile();
        } else {
            return null;
        }
    }
    IHyperlink[] returned = super.detectHyperlinks(textViewer, region, canShowMultipleHyperlinks);
    if (returned != null && returned.length > 0) {
        IHyperlink hyperlink = returned[0];
        if (hyperlink instanceof URLHyperlink) {
            URLHyperlink urlHyperlink = (URLHyperlink) hyperlink;
            String newURLString = UiUtils.resolveDefines(specfile, urlHyperlink.getURLString());
            return new IHyperlink[] { new URLHyperlink(urlHyperlink.getHyperlinkRegion(), newURLString) };
        }
    }
    return returned;
}
Also used : URLHyperlink(org.eclipse.jface.text.hyperlink.URLHyperlink) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) SpecfileEditor(org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileEditor)

Aggregations

IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)47 Test (org.junit.Test)35 XtextResource (org.eclipse.xtext.resource.XtextResource)20 IRegion (org.eclipse.jface.text.IRegion)19 Region (org.eclipse.jface.text.Region)19 IEditorPart (org.eclipse.ui.IEditorPart)9 SourcesFileHyperlinkDetector (org.eclipse.linuxtools.internal.rpm.ui.editor.hyperlink.SourcesFileHyperlinkDetector)5 IFile (org.eclipse.core.resources.IFile)4 JdtHyperlink (org.eclipse.xtext.common.types.xtext.ui.JdtHyperlink)4 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)4 AbstractEditorTest (org.eclipse.xtext.ui.testing.AbstractEditorTest)4 IDocument (org.eclipse.jface.text.IDocument)3 URLHyperlink (org.eclipse.jface.text.hyperlink.URLHyperlink)3 SpecfileEditor (org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileEditor)3 XtextHyperlink (org.eclipse.xtext.ui.editor.hyperlinking.XtextHyperlink)3 XbaseImplementatorsHyperlink (org.eclipse.xtext.xbase.ui.navigation.XbaseImplementatorsHyperlink)3 JsonPointer (com.fasterxml.jackson.core.JsonPointer)2 SwaggerHyperlink (com.reprezen.swagedit.core.hyperlinks.SwaggerHyperlink)2 Field (java.lang.reflect.Field)2 ArrayList (java.util.ArrayList)2