Search in sources :

Example 6 with SpecfileEditor

use of org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileEditor 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 7 with SpecfileEditor

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

Example 8 with SpecfileEditor

use of org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileEditor in project linuxtools by eclipse.

the class FileTestCase method setUp.

@Before
public void setUp() throws CoreException {
    testProject = new SpecfileTestProject();
    String fileName = "test" + this.getClass().getSimpleName() + ".spec";
    testFile = testProject.createFile(fileName);
    editor = new SpecfileEditor();
    parser = new SpecfileParser();
    specfile = new Specfile();
}
Also used : Specfile(org.eclipse.linuxtools.rpm.ui.editor.parser.Specfile) SpecfileEditor(org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileEditor) SpecfileParser(org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser) Before(org.junit.Before)

Example 9 with SpecfileEditor

use of org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileEditor in project linuxtools by eclipse.

the class SpecfileCompletionProcessorTest method initEditor.

private SpecfileEditor initEditor(String contents) throws Exception {
    newFile(contents);
    IEditorPart openEditor = IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), testFile);
    return (SpecfileEditor) openEditor;
}
Also used : SpecfileEditor(org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileEditor) IEditorPart(org.eclipse.ui.IEditorPart)

Example 10 with SpecfileEditor

use of org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileEditor in project linuxtools by eclipse.

the class SpecfileChangelogFormatter method mergeChangelog.

@Override
public String mergeChangelog(String dateLine, String functionGuess, String defaultContent, IEditorPart changelog, String changeLogLocation, String fileLocation) {
    if (changelog instanceof SpecfileEditor) {
        SpecfileEditor specEditor = (SpecfileEditor) changelog;
        IDocument doc = specEditor.getDocumentProvider().getDocument(specEditor.getEditorInput());
        String[] positionCategories = doc.getPositionCategories();
        String contentTypesPositionCategory = null;
        // we need to find the one we want
        for (String positionCategory : positionCategories) {
            if (positionCategory.startsWith("__content_types_category")) {
                // $NON-NLS-1$
                contentTypesPositionCategory = positionCategory;
            }
        }
        if (contentTypesPositionCategory != null) {
            try {
                Position[] sectionPositions = doc.getPositions(contentTypesPositionCategory);
                ITypedRegion changelogPartition = null;
                for (Position position : sectionPositions) {
                    int offset = position.getOffset();
                    ITypedRegion partition = doc.getPartition(offset);
                    if (partition.getType().equals(SpecfilePartitionScanner.SPEC_CHANGELOG)) {
                        changelogPartition = partition;
                    }
                }
                // Temporary buffer for changelog text
                StringBuilder buf = new StringBuilder();
                String changelogText = EMPTY_STRING;
                String[] changelogLines = new String[] {};
                int offset = doc.getLength();
                int length = 0;
                // there was no changelog partition add it.
                if (changelogPartition == null) {
                    // make sure there are at least 2 newlines before
                    // the changelog section
                    String endString = doc.get(doc.getLength() - 2, 2);
                    if (endString.charAt(0) != '\n') {
                        buf.append('\n');
                    }
                    if (endString.charAt(1) != '\n') {
                        buf.append('\n');
                    }
                    // $NON-NLS-1$
                    buf.append("%changelog\n");
                // or get the old text and add the header
                } else {
                    offset = changelogPartition.getOffset();
                    length = changelogPartition.getLength();
                    changelogText = doc.get(offset, length);
                    // get old changelog text
                    // $NON-NLS-1$
                    changelogLines = changelogText.split("\n");
                    // add the %changelog header
                    buf.append(changelogLines[0]).append('\n');
                }
                // now add the entry stub
                buf.append(dateLine);
                buf.append('\n');
                // $NON-NLS-1$
                buf.append("- \n");
                // set the cursor at the end of the entry,
                // count back 2 '\n's
                int newCursorOffset = offset + buf.length() - 1;
                for (int i = 1; i < changelogLines.length; i++) {
                    buf.append('\n').append(changelogLines[i]);
                }
                // always terminate the file with a new line
                if (changelogLines.length > 0) {
                    buf.append('\n');
                }
                doc.replace(offset, length, buf.toString());
                specEditor.selectAndReveal(newCursorOffset, 0);
                specEditor.setFocus();
            } catch (BadPositionCategoryException | BadLocationException e) {
                SpecfileLog.logError(e);
            }
        }
    }
    return EMPTY_STRING;
}
Also used : Position(org.eclipse.jface.text.Position) SpecfileEditor(org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileEditor) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

SpecfileEditor (org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileEditor)11 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)4 SpecfileCompletionProcessor (org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileCompletionProcessor)4 Test (org.junit.Test)3 BadLocationException (org.eclipse.jface.text.BadLocationException)2 IDocument (org.eclipse.jface.text.IDocument)2 SpecfileParser (org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser)2 StringTokenizer (com.ibm.icu.util.StringTokenizer)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 IFile (org.eclipse.core.resources.IFile)1 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)1 IRegion (org.eclipse.jface.text.IRegion)1 ITypedRegion (org.eclipse.jface.text.ITypedRegion)1 Position (org.eclipse.jface.text.Position)1 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)1 URLHyperlink (org.eclipse.jface.text.hyperlink.URLHyperlink)1 SourceViewer (org.eclipse.jface.text.source.SourceViewer)1 SpecfileConfiguration (org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileConfiguration)1 SpecfileSource (org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource)1