Search in sources :

Example 21 with OutlineItem

use of org.eclipse.mylyn.wikitext.parser.outline.OutlineItem in project mylyn.docs by eclipse.

the class MarkupToEclipseToc method parse.

public String parse(String markupContent) {
    if (markupLanguage == null) {
        // $NON-NLS-1$
        throw new IllegalStateException("Must set markupLanguage");
    }
    OutlineParser parser = new OutlineParser(markupLanguage);
    OutlineItem root = parser.parse(markupContent);
    return createToc(root);
}
Also used : OutlineParser(org.eclipse.mylyn.wikitext.parser.outline.OutlineParser) OutlineItem(org.eclipse.mylyn.wikitext.parser.outline.OutlineItem)

Example 22 with OutlineItem

use of org.eclipse.mylyn.wikitext.parser.outline.OutlineItem in project mylyn.docs by eclipse.

the class OutlineDropTargetListener method getDropItems.

/**
 * get the outline items being dropped, or null if there are none or if the event does not qualify for a drop.
 */
@SuppressWarnings("unchecked")
private List<OutlineItem> getDropItems(DropTargetEvent event) {
    if (event.operations == DND.DROP_NONE || event.item == null) {
        return null;
    }
    Object targetData = event.item.getData();
    if (!(targetData instanceof OutlineItem)) {
        return null;
    }
    ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
    if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
        IStructuredSelection structuredSelection = (IStructuredSelection) selection;
        List<?> list = structuredSelection.toList();
        if (!list.isEmpty()) {
            for (Object i : list) {
                if (!(i instanceof OutlineItem)) {
                    return null;
                }
            }
            return (List<OutlineItem>) list;
        }
    }
    return null;
}
Also used : ISelection(org.eclipse.jface.viewers.ISelection) List(java.util.List) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) OutlineItem(org.eclipse.mylyn.wikitext.parser.outline.OutlineItem)

Example 23 with OutlineItem

use of org.eclipse.mylyn.wikitext.parser.outline.OutlineItem in project mylyn.docs by eclipse.

the class MoveSectionsCommand method doCommand.

@Override
protected void doCommand(IDocument document) throws BadLocationException {
    // relocated.
    for (int x = 0; x < items.size(); ++x) {
        OutlineItem item = items.get(x);
        for (int y = 0; y < x; ++y) {
            OutlineItem previousItem = items.get(y);
            if (previousItem.contains(item)) {
                items.remove(x);
                --x;
                break;
            } else if (item.contains(previousItem)) {
                items.remove(y);
                --x;
                --y;
            }
        }
    }
    if (items.size() > 1) {
        Collections.sort(items, new OffsetComparator());
    }
    final Section[] sections = new Section[items.size()];
    for (int x = 0; x < items.size(); ++x) {
        OutlineItem item = items.get(x);
        sections[x] = new Section(item.getOffset(), item.getSectionLength());
    }
    int insertLocation = target.getOffset();
    if (location == InsertLocation.WITHIN) {
        List<OutlineItem> children = target.getChildren();
        if (children.isEmpty()) {
            insertLocation += target.getLength();
            // insert at the first position found after the header text and any following newlines
            while (insertLocation < document.getLength()) {
                char c = document.getChar(insertLocation);
                if (c == '\r' || c == '\n') {
                    ++insertLocation;
                } else {
                    break;
                }
            }
        } else {
            // In this case we handle within as before the first child.  Even though this may
            // be somewhat non-intuitive, it actually makes sense.  Without this we would see
            // text content after the target heading but before the first child be magically appended
            // to the end of the dropped items -- which in almost all cases is undesirable.
            insertLocation = children.get(0).getOffset();
        }
    } else if (location == InsertLocation.AFTER) {
        // insert afte the section denoted by the heading
        insertLocation += target.getSectionLength();
    }
    String twoNewlines = Text.DELIMITER + Text.DELIMITER;
    for (int x = 0; x < sections.length; ++x) {
        Section section = sections[x];
        String text = document.get(section.offset, section.length);
        // $NON-NLS-1$
        document.replace(section.offset, section.length, "");
        // whitespace hack: if the section we're moving doesn't end with a blank line
        // then we add one.
        int adjustmentFactor = 0;
        if (!text.endsWith(twoNewlines)) {
            text += twoNewlines;
            adjustmentFactor = twoNewlines.length();
            section.length += adjustmentFactor;
        }
        if (section.offset < insertLocation) {
            insertLocation -= section.length;
        }
        final int insertedLocation = insertLocation;
        document.replace(insertedLocation, 0, text);
        insertLocation += section.length;
        // not be adjusted because we first cut and then pasted
        for (int y = x + 1; y < sections.length; ++y) {
            Section section2 = sections[y];
            if (section2.offset < insertedLocation) {
                section2.offset -= section.length;
            } else if (adjustmentFactor > 0) {
                section2.offset += adjustmentFactor;
            }
        }
    }
}
Also used : OutlineItem(org.eclipse.mylyn.wikitext.parser.outline.OutlineItem)

Example 24 with OutlineItem

use of org.eclipse.mylyn.wikitext.parser.outline.OutlineItem in project mylyn.docs by eclipse.

the class WikiTextSourceEditor method updateOutline.

private void updateOutline() {
    if (!outlineDirty) {
        return;
    }
    if (getSourceViewer().getTextWidget().isDisposed()) {
        return;
    }
    // we maintain the outline even if the outline page is not in use, which allows us to use the outline for
    // content assist and other things
    MarkupLanguage markupLanguage = getMarkupLanguage();
    if (markupLanguage == null) {
        return;
    }
    final MarkupLanguage language = markupLanguage.clone();
    final Display display = getSourceViewer().getTextWidget().getDisplay();
    final String content = document.get();
    final int contentGeneration;
    synchronized (WikiTextSourceEditor.this) {
        contentGeneration = documentGeneration;
        initializeOutlineParser();
    }
    // we parse the outline in another thread so that the UI remains responsive
    Job parseOutlineJob = new // $NON-NLS-1$
    Job(// $NON-NLS-1$
    WikiTextSourceEditor.class.getSimpleName() + "#updateOutline") {

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            outlineParser.setMarkupLanguage(language);
            if (shouldCancel()) {
                return Status.CANCEL_STATUS;
            }
            final OutlineItem rootItem = outlineParser.parse(content);
            if (shouldCancel()) {
                return Status.CANCEL_STATUS;
            }
            display.asyncExec(new Runnable() {

                public void run() {
                    updateOutline(contentGeneration, rootItem);
                }
            });
            return Status.OK_STATUS;
        }

        private boolean shouldCancel() {
            synchronized (WikiTextSourceEditor.this) {
                if (contentGeneration != documentGeneration) {
                    return true;
                }
            }
            return false;
        }
    };
    parseOutlineJob.setPriority(Job.INTERACTIVE);
    parseOutlineJob.setSystem(true);
    parseOutlineJob.schedule();
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) AbstractMarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage) MarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage) Job(org.eclipse.core.runtime.jobs.Job) UIJob(org.eclipse.ui.progress.UIJob) Point(org.eclipse.swt.graphics.Point) Display(org.eclipse.swt.widgets.Display) OutlineItem(org.eclipse.mylyn.wikitext.parser.outline.OutlineItem)

Example 25 with OutlineItem

use of org.eclipse.mylyn.wikitext.parser.outline.OutlineItem in project mylyn.docs by eclipse.

the class WikiTextSourceEditor method updateOutlineNow.

private void updateOutlineNow() {
    if (!outlineDirty) {
        return;
    }
    if (getSourceViewer().getTextWidget().isDisposed()) {
        return;
    }
    // we maintain the outline even if the outline page is not in use, which allows us to use the outline for
    // content assist and other things
    MarkupLanguage markupLanguage = getMarkupLanguage();
    if (markupLanguage == null) {
        return;
    }
    final MarkupLanguage language = markupLanguage.clone();
    final String content = document.get();
    final int contentGeneration;
    synchronized (WikiTextSourceEditor.this) {
        contentGeneration = documentGeneration;
        initializeOutlineParser();
    }
    outlineParser.setMarkupLanguage(language);
    OutlineItem rootItem = outlineParser.parse(content);
    updateOutline(contentGeneration, rootItem);
}
Also used : AbstractMarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage) MarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage) Point(org.eclipse.swt.graphics.Point) OutlineItem(org.eclipse.mylyn.wikitext.parser.outline.OutlineItem)

Aggregations

OutlineItem (org.eclipse.mylyn.wikitext.parser.outline.OutlineItem)58 OutlineParser (org.eclipse.mylyn.wikitext.parser.outline.OutlineParser)18 MediaWikiLanguage (org.eclipse.mylyn.wikitext.mediawiki.MediaWikiLanguage)9 Point (org.eclipse.swt.graphics.Point)8 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)7 ITextSelection (org.eclipse.jface.text.ITextSelection)5 Attributes (org.eclipse.mylyn.wikitext.parser.Attributes)5 MarkupLanguage (org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage)5 BufferedOutputStream (java.io.BufferedOutputStream)4 FileOutputStream (java.io.FileOutputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4 Writer (java.io.Writer)4 BuildException (org.apache.tools.ant.BuildException)4 ISelection (org.eclipse.jface.viewers.ISelection)4 AbstractMarkupLanguage (org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage)4 File (java.io.File)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 IMenuManager (org.eclipse.jface.action.IMenuManager)3 MenuManager (org.eclipse.jface.action.MenuManager)3