Search in sources :

Example 1 with SpecfileParser

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

the class SpecfileCompletionProcessor method computeCompletionProposals.

@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
    List<ICompletionProposal> result = new ArrayList<>();
    Specfile specfile = new SpecfileParser().parse(viewer.getDocument());
    String prefix = completionWord(viewer.getDocument(), offset);
    Region region = new Region(offset - prefix.length(), prefix.length());
    // RPM macro's are useful in the whole specfile.
    List<ICompletionProposal> rpmMacroProposals = computeRpmMacroProposals(region, specfile, prefix);
    // Sources completion
    List<ICompletionProposal> sourcesProposals = computeSourcesProposals(region, specfile, prefix);
    result.addAll(sourcesProposals);
    // Get the current content type
    String currentContentType = viewer.getDocument().getDocumentPartitioner().getContentType(region.getOffset());
    if (currentContentType.equals(SpecfilePartitionScanner.SPEC_PREP)) {
        List<ICompletionProposal> patchesProposals = computePatchesProposals(region, specfile, prefix);
        result.addAll(patchesProposals);
    }
    if (currentContentType.equals(SpecfilePartitionScanner.SPEC_PACKAGES)) {
        // don't show template in the RPM packages content type.
        // (when the line begin with Requires, BuildRequires etc...)
        List<ICompletionProposal> rpmPackageProposals = computeRpmPackageProposals(region, prefix);
        result.addAll(rpmPackageProposals);
        result.addAll(rpmMacroProposals);
    } else {
        // don't show RPM packages proposals in all others content type.
        List<? extends ICompletionProposal> templateProposals = computeTemplateProposals(viewer, region, specfile, prefix);
        result.addAll(templateProposals);
        result.addAll(rpmMacroProposals);
    }
    if (currentContentType.equals(SpecfilePartitionScanner.SPEC_GROUP)) {
        IDocument document = viewer.getDocument();
        try {
            int lineNumber = document.getLineOfOffset(region.getOffset());
            int lineOffset = document.getLineOffset(lineNumber);
            if (region.getOffset() - lineOffset > 5) {
                result.clear();
                String groupPrefix = getGroupPrefix(viewer, offset);
                result.addAll(computeRpmGroupProposals(region, groupPrefix));
            }
        } catch (BadLocationException e) {
            SpecfileLog.logError(e);
        }
    }
    return result.toArray(new ICompletionProposal[result.size()]);
}
Also used : Specfile(org.eclipse.linuxtools.rpm.ui.editor.parser.Specfile) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) SpecfileParser(org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with SpecfileParser

use of org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser 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 3 with SpecfileParser

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

the class SpecfileEditorPrepareSourcesActionDelegate method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final Shell shell = HandlerUtil.getActiveShellChecked(event);
    final SpecfileParser specparser = new SpecfileParser();
    final IResource resource = RPMHandlerUtils.getResource(event);
    final RPMProject rpj = RPMHandlerUtils.getRPMProject(resource);
    final IFile workFile = (IFile) rpj.getSpecFile();
    final Specfile specfile = specparser.parse(workFile);
    if (!downloadFile(shell, rpj, specfile)) {
        return null;
    }
    Job job = new // $NON-NLS-1$
    Job(// $NON-NLS-1$
    "Preparing sources") {

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            monitor.beginTask(NLS.bind(Messages.PrepareSources_prepareSources, rpj.getSpecFile().getName()), IProgressMonitor.UNKNOWN);
            // $NON-NLS-1$
            int offset = rpj.getSpecFile().getName().lastIndexOf(".");
            MessageConsoleStream out = getConsole(rpj.getSpecFile().getName().substring(0, offset)).newMessageStream();
            IStatus is = null;
            try {
                is = rpj.buildPrep(out);
            } catch (CoreException e) {
                SpecfileLog.logError(Messages.PrepareSources_coreException, e);
                RPMUtils.showErrorDialog(shell, Messages.PrepareSources_error, Messages.PrepareSources_coreException);
                return is;
            } finally {
                monitor.done();
            }
            return is;
        }
    };
    // suppress UI. That's done in encapsulated
    job.setUser(true);
    job.schedule();
    return null;
}
Also used : Specfile(org.eclipse.linuxtools.rpm.ui.editor.parser.Specfile) Shell(org.eclipse.swt.widgets.Shell) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) RPMProject(org.eclipse.linuxtools.rpm.core.RPMProject) MessageConsoleStream(org.eclipse.ui.console.MessageConsoleStream) DownloadJob(org.eclipse.linuxtools.rpm.core.utils.DownloadJob) Job(org.eclipse.core.runtime.jobs.Job) SpecfileParser(org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser) IResource(org.eclipse.core.resources.IResource)

Example 4 with SpecfileParser

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

the class SpecStructureCreator method parseSpecfile.

private void parseSpecfile(DocumentRangeNode root, IDocument doc, IFile file) {
    SpecfileParser parser = new SpecfileParser();
    // FIXME: error markers do not show
    if (file != null) {
        FileEditorInput fei = new FileEditorInput(file);
        // without it, the compare editor is blank
        try {
            SpecfileEditor.getSpecfileDocumentProvider().disconnect(fei);
            SpecfileEditor.getSpecfileDocumentProvider().connect(fei);
        } catch (CoreException e) {
            SpecfileLog.logError(e);
        }
        parser.setErrorHandler(new SpecfileErrorHandler(fei, doc));
        parser.setTaskHandler(new SpecfileTaskHandler(fei, doc));
        Specfile specfile = parser.parse(doc);
        String id = specfile.getName();
        // Be a child under parent node of specfileSectionRoot (would be
        // rootNode)
        SpecNode fileNode = new SpecNode((DocumentRangeNode) root.getParentNode(), 1, id, doc, 0, doc.getLength());
        for (SpecfileSection sec : specfile.getSections()) {
            try {
                addNode(root, doc, sec.getName(), doc.getLineOffset(sec.getLineNumber()), doc.getLineOffset(sec.getSectionEndLine()) - doc.getLineOffset(sec.getLineNumber()), 2);
            } catch (BadLocationException e) {
                SpecfileLog.logError(e);
            }
        }
        // Be a child under the parent file node
        for (SpecfilePackage sPackage : specfile.getPackages().getPackages()) {
            try {
                SpecNode pNode = addNode(fileNode, doc, sPackage.getPackageName(), doc.getLineOffset(sPackage.getLineNumber()), doc.getLineOffset(sPackage.getSectionEndLine()) - doc.getLineOffset(sPackage.getLineNumber()), 3);
                for (SpecfileSection section : sPackage.getSections()) {
                    addNode(pNode, doc, section.getName(), doc.getLineOffset(section.getLineNumber()), doc.getLineOffset(section.getSectionEndLine()) - doc.getLineOffset(section.getLineNumber()), 4);
                }
            } catch (BadLocationException e) {
                SpecfileLog.logError(e);
            }
        }
    }
}
Also used : Specfile(org.eclipse.linuxtools.rpm.ui.editor.parser.Specfile) SpecfileSection(org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileSection) SpecfileTaskHandler(org.eclipse.linuxtools.rpm.ui.editor.markers.SpecfileTaskHandler) CoreException(org.eclipse.core.runtime.CoreException) FileEditorInput(org.eclipse.ui.part.FileEditorInput) SpecfileErrorHandler(org.eclipse.linuxtools.rpm.ui.editor.markers.SpecfileErrorHandler) SpecfileParser(org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser) SpecfilePackage(org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfilePackage) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 5 with SpecfileParser

use of org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser 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)

Aggregations

SpecfileParser (org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser)10 BadLocationException (org.eclipse.jface.text.BadLocationException)6 Specfile (org.eclipse.linuxtools.rpm.ui.editor.parser.Specfile)6 IDocument (org.eclipse.jface.text.IDocument)5 IFile (org.eclipse.core.resources.IFile)2 IResource (org.eclipse.core.resources.IResource)2 CoreException (org.eclipse.core.runtime.CoreException)2 Job (org.eclipse.core.runtime.jobs.Job)2 IRegion (org.eclipse.jface.text.IRegion)2 SpecfileEditor (org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileEditor)2 SpecfileSource (org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource)2 RPMProject (org.eclipse.linuxtools.rpm.core.RPMProject)2 DownloadJob (org.eclipse.linuxtools.rpm.core.utils.DownloadJob)2 SpecfileDefine (org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine)2 Shell (org.eclipse.swt.widgets.Shell)2 IEditorPart (org.eclipse.ui.IEditorPart)2 StringTokenizer (com.ibm.icu.util.StringTokenizer)1 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1