Search in sources :

Example 6 with SpecfileDefine

use of org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine in project linuxtools by eclipse.

the class SpecfileCompletionProcessor method getDefines.

/**
 * Get defines as a String key->value pair for a given specfile and prefix.
 *
 * @param specfile
 *            to get defines from.
 * @param prefix
 *            used to find defines.
 * @return a <code>HashMap</code> of defines.
 */
private Map<String, String> getDefines(Specfile specfile, String prefix) {
    Collection<SpecfileDefine> defines = specfile.getDefines();
    Map<String, String> ret = new HashMap<>();
    String defineName;
    for (SpecfileDefine define : defines) {
        // $NON-NLS-1$
        defineName = "%" + define.getName();
        if (defineName.startsWith(prefix.replaceFirst("\\{", EMPTY_STRING))) {
            // $NON-NLS-1$
            ret.put(defineName, define.getStringValue());
        }
    }
    return ret;
}
Also used : HashMap(java.util.HashMap) SpecfileDefine(org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine)

Example 7 with SpecfileDefine

use of org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine in project linuxtools by eclipse.

the class SpecfileHover method getHoverInfo.

@Override
public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
    if (hoverRegion == null || hoverRegion.getLength() == 0) {
        return null;
    }
    Specfile spec = new SpecfileParser().parse(textViewer.getDocument());
    String currentSelection;
    try {
        currentSelection = textViewer.getDocument().get(hoverRegion.getOffset() + 1, hoverRegion.getLength() - 1);
    } catch (BadLocationException e) {
        return null;
    }
    // First we try to get a define based on the given name
    SpecfileDefine define = spec.getDefine(currentSelection);
    // $NON-NLS-1$
    String value = currentSelection + ": ";
    if (define != null) {
        return value + define.getStringValue();
    }
    String macroLower = currentSelection.toLowerCase();
    // If there's no such define we try to see if it corresponds to
    // a Source or Patch declaration
    String retrivedValue = RPMUtils.getSourceOrPatchValue(spec, macroLower);
    if (retrivedValue != null) {
        return value + retrivedValue;
    } else {
        // If it does not correspond to a Patch or Source macro, try to find
        // it
        // in the macro proposals list.
        retrivedValue = RPMUtils.getMacroValueFromMacroList(currentSelection);
        if (retrivedValue != null) {
            return value + retrivedValue;
        } else {
            // If it does not correspond to a macro in the list, try to find
            // it
            // in the RPM list.
            retrivedValue = Activator.getDefault().getRpmPackageList().getValue(// $NON-NLS-1$ //$NON-NLS-2$
            currentSelection.replaceFirst(":", ""));
            if (retrivedValue != null) {
                return retrivedValue;
            }
        }
    }
    // for unrecognized macros and RPM packages.
    return null;
}
Also used : Specfile(org.eclipse.linuxtools.rpm.ui.editor.parser.Specfile) SpecfileDefine(org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine) SpecfileParser(org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 8 with SpecfileDefine

use of org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine in project linuxtools by eclipse.

the class UiUtils method resolveDefines.

/**
 * Resolve defines for a given string. Defines in the string that are not
 * found or have some other error will remain unchanged in the returned
 * string.
 *
 * @param specfile
 *            The specfile containing the string to resolve.
 * @param stringToResolve
 *            The string to resolve.
 * @return The resolved string.
 */
public static String resolveDefines(Specfile specfile, String stringToResolve) {
    String workingString = stringToResolve;
    SpecfileDefine define;
    try {
        // $NON-NLS-1$
        Pattern variablePattern = Pattern.compile("%\\{(\\S+?)\\}");
        Matcher variableMatcher = variablePattern.matcher(stringToResolve);
        Set<String> variablesFound = new HashSet<>();
        while (variableMatcher.find()) {
            String variable = variableMatcher.group(1);
            if (variablesFound.contains(variable)) {
                continue;
            }
            define = specfile.getDefine(variable);
            if (define != null && !stringToResolve.equals(define.getUnresolvedStringValue())) {
                // $NON-NLS-1$ //$NON-NLS-2$
                workingString = workingString.replaceAll("\\%\\{" + variable + "\\}", define.getStringValue());
            }
        }
        return workingString;
    } catch (Exception e) {
        return stringToResolve;
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) SpecfileDefine(org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 9 with SpecfileDefine

use of org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine in project linuxtools by eclipse.

the class SpecfileElementHyperlinkDetector method detectHyperlinks.

@Override
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
    if (region == null || textViewer == null) {
        return null;
    }
    IDocument document = textViewer.getDocument();
    if (document == null) {
        return null;
    }
    // until a SpecfileEditor#editorSaved is called
    if (specfile == null) {
        SpecfileEditor a = this.getAdapter(SpecfileEditor.class);
        if (a != null && a.getSpecfile() != null) {
            specfile = a.getSpecfile();
        } else {
            SpecfileParser parser = new SpecfileParser();
            specfile = parser.parse(document);
        }
    }
    int offset = region.getOffset();
    IRegion lineInfo;
    String line;
    try {
        lineInfo = document.getLineInformationOfOffset(offset);
        line = document.get(lineInfo.getOffset(), lineInfo.getLength());
    } catch (BadLocationException ex) {
        return null;
    }
    int offsetInLine = offset - lineInfo.getOffset();
    StringTokenizer tokens = new StringTokenizer(line);
    // $NON-NLS-1$
    String word = "";
    int tempLineOffset = 0;
    int wordOffsetInLine = 0;
    while (tokens.hasMoreTokens()) {
        String tempWord = tokens.nextToken();
        // $NON-NLS-1$
        Pattern defineRegexp = Pattern.compile("%\\{(.*?)\\}");
        Matcher fit = defineRegexp.matcher(tempWord);
        while (fit.find()) {
            if ((fit.start() + tempLineOffset <= offsetInLine) && (offsetInLine <= fit.end() + tempLineOffset)) {
                tempWord = fit.group();
                wordOffsetInLine = fit.start();
                tempLineOffset += fit.start();
                break;
            }
        }
        tempLineOffset += tempWord.length();
        word = tempWord;
        if (tempLineOffset > offsetInLine) {
            break;
        }
    }
    if (word.startsWith(SOURCE_IDENTIFIER)) {
        int sourceNumber = Integer.valueOf(word.substring(SOURCE_IDENTIFIER.length(), word.length() - 1)).intValue();
        SpecfileSource source = specfile.getSource(sourceNumber);
        if (source != null) {
            return prepareHyperlink(lineInfo, line, word, source);
        }
    } else if (word.startsWith(PATCH_IDENTIFIER)) {
        int sourceNumber = Integer.valueOf(word.substring(PATCH_IDENTIFIER.length(), word.length())).intValue();
        SpecfileSource source = specfile.getPatch(sourceNumber);
        if (source != null) {
            return prepareHyperlink(lineInfo, line, word, source);
        }
    } else {
        String defineName = getDefineName(word);
        SpecfileDefine define = specfile.getDefine(defineName);
        if (define != null) {
            return prepareHyperlink(lineInfo, line, defineName, define, wordOffsetInLine);
        }
    }
    return null;
}
Also used : Pattern(java.util.regex.Pattern) StringTokenizer(com.ibm.icu.util.StringTokenizer) SpecfileSource(org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource) Matcher(java.util.regex.Matcher) SpecfileDefine(org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine) SpecfileEditor(org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileEditor) SpecfileParser(org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 10 with SpecfileDefine

use of org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine in project linuxtools by eclipse.

the class SpecfileElementHyperlinkDetectorTest method testDetectHyperlinks.

@Test
public void testDetectHyperlinks() throws PartInitException {
    String testText = "%define smth other\nSource0: test.zip\nPatch0: first.patch\n" + "%build\n %{SOURCE0}\n%patch0\n%{smth}\n";
    newFile(testText);
    SpecfileElementHyperlinkDetector elementDetector = new SpecfileElementHyperlinkDetector();
    elementDetector.setSpecfile(specfile);
    IEditorPart openEditor = IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), testFile, "org.eclipse.linuxtools.rpm.ui.editor.SpecfileEditor");
    editor = (SpecfileEditor) openEditor;
    editor.doRevertToSaved();
    // test source element
    IRegion region = new Region(74, 0);
    IHyperlink[] returned = elementDetector.detectHyperlinks(editor.getSpecfileSourceViewer(), region, false);
    SpecfileElementHyperlink element = (SpecfileElementHyperlink) returned[0];
    assertTrue(element.getSource() instanceof SpecfileSource);
    SpecfileSource source = (SpecfileSource) element.getSource();
    assertEquals(source.getSourceType(), SpecfileSource.SourceType.SOURCE);
    assertEquals(source.getFileName(), "test.zip");
    // test patch element
    region = new Region(83, 0);
    returned = elementDetector.detectHyperlinks(editor.getSpecfileSourceViewer(), region, false);
    element = (SpecfileElementHyperlink) returned[0];
    assertTrue(element.getSource() instanceof SpecfileSource);
    source = (SpecfileSource) element.getSource();
    assertEquals(source.getSourceType(), SpecfileSource.SourceType.PATCH);
    assertEquals(source.getFileName(), "first.patch");
    // test define
    region = new Region(89, 0);
    returned = elementDetector.detectHyperlinks(editor.getSpecfileSourceViewer(), region, false);
    element = (SpecfileElementHyperlink) returned[0];
    assertTrue(element.getSource() instanceof SpecfileDefine);
    SpecfileDefine define = (SpecfileDefine) element.getSource();
    assertEquals(define.getName(), "smth");
    assertEquals(define.getStringValue(), "other");
}
Also used : SpecfileElementHyperlink(org.eclipse.linuxtools.internal.rpm.ui.editor.hyperlink.SpecfileElementHyperlink) SpecfileSource(org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) SpecfileDefine(org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IEditorPart(org.eclipse.ui.IEditorPart) SpecfileElementHyperlinkDetector(org.eclipse.linuxtools.internal.rpm.ui.editor.hyperlink.SpecfileElementHyperlinkDetector) IRegion(org.eclipse.jface.text.IRegion) Test(org.junit.Test)

Aggregations

SpecfileDefine (org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine)12 Test (org.junit.Test)7 BadLocationException (org.eclipse.jface.text.BadLocationException)3 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 IDocument (org.eclipse.jface.text.IDocument)2 IRegion (org.eclipse.jface.text.IRegion)2 SpecfileSource (org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource)2 Specfile (org.eclipse.linuxtools.rpm.ui.editor.parser.Specfile)2 SpecfileParser (org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser)2 StringTokenizer (com.ibm.icu.util.StringTokenizer)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Region (org.eclipse.jface.text.Region)1 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)1 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)1 SpecfileEditor (org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileEditor)1 SpecfileElementHyperlink (org.eclipse.linuxtools.internal.rpm.ui.editor.hyperlink.SpecfileElementHyperlink)1 SpecfileElementHyperlinkDetector (org.eclipse.linuxtools.internal.rpm.ui.editor.hyperlink.SpecfileElementHyperlinkDetector)1