Search in sources :

Example 1 with IPresentationDamager

use of org.eclipse.jface.text.presentation.IPresentationDamager in project xtext-eclipse by eclipse.

the class XtextSourceViewerConfiguration method getPresentationReconciler.

@Override
public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
    XtextPresentationReconciler reconciler = getPresentationReconcilerProvider().get();
    reconciler.setDocumentPartitioning(getDocumentPartitioning(sourceViewer));
    IPresentationRepairer repairer = repairerProvider.get();
    IPresentationDamager damager = damagerProvider.get();
    String[] types = partitionTypesMapper.getSupportedPartitionTypes();
    for (String partitionType : types) {
        reconciler.setRepairer(repairer, partitionType);
        reconciler.setDamager(damager, partitionType);
    }
    return reconciler;
}
Also used : IPresentationRepairer(org.eclipse.jface.text.presentation.IPresentationRepairer) IPresentationDamager(org.eclipse.jface.text.presentation.IPresentationDamager)

Example 2 with IPresentationDamager

use of org.eclipse.jface.text.presentation.IPresentationDamager in project webtools.sourceediting by eclipse.

the class StructuredPresentationReconciler method getDamageEndOffset.

/**
 * Returns the end offset of the damage. If a partition has been split by
 * the given document event also the second half of the original
 * partition must be considered. This is achieved by using the remembered
 * partition range.
 *
 * @param e the event describing the change
 * @return the damage end offset (excluding)
 * @exception BadLocationException if method accesses invalid offset
 */
int getDamageEndOffset(DocumentEvent e) throws BadLocationException {
    IDocument d = e.getDocument();
    int length = 0;
    if (e.getText() != null) {
        length = e.getText().length();
        if (length > 0)
            --length;
    }
    ITypedRegion partition = getPartition(d, e.getOffset() + length);
    int endOffset = partition.getOffset() + partition.getLength();
    if (endOffset == e.getOffset())
        return -1;
    int end = fRememberedPosition == null ? -1 : fRememberedPosition.getOffset() + fRememberedPosition.getLength();
    if (endOffset < end && end < d.getLength())
        partition = getPartition(d, end);
    // if there is not damager for the partition then use the endOffset of the partition
    IPresentationDamager damager = getDamager(partition.getType());
    if (damager != null) {
        IRegion r = damager.getDamageRegion(partition, e, fDocumentPartitioningChanged);
        endOffset = r.getOffset() + r.getLength();
    }
    return endOffset;
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) IPresentationDamager(org.eclipse.jface.text.presentation.IPresentationDamager) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion)

Example 3 with IPresentationDamager

use of org.eclipse.jface.text.presentation.IPresentationDamager in project webtools.sourceediting by eclipse.

the class StructuredPresentationReconciler method getDamage.

/**
 * Checks for the first and the last affected partition affected by a
 * document event and calls their damagers. Invalidates everything from the
 * start of the damage for the first partition until the end of the damage
 * for the last partition.
 *
 * @param e the event describing the document change
 * @param optimize <code>true</code> if partition changes should be
 *        considered for optimization
 * @return the damaged caused by the change or <code>null</code> if
 *         computing the partitioning failed
 * @since 3.0
 */
IRegion getDamage(DocumentEvent e, boolean optimize) {
    int length = e.getText() == null ? 0 : e.getText().length();
    if (fDamagers == null || fDamagers.isEmpty()) {
        length = Math.max(e.getLength(), length);
        length = Math.min(e.getDocument().getLength() - e.getOffset(), length);
        return new Region(e.getOffset(), length);
    }
    boolean isDeletion = length == 0;
    IRegion damage = null;
    try {
        int offset = e.getOffset();
        if (isDeletion)
            offset = Math.max(0, offset - 1);
        ITypedRegion partition = getPartition(e.getDocument(), offset);
        IPresentationDamager damager = getDamager(partition.getType());
        if (damager == null)
            return null;
        IRegion r = damager.getDamageRegion(partition, e, fDocumentPartitioningChanged);
        if (!fDocumentPartitioningChanged && optimize && !isDeletion) {
            damage = r;
        } else {
            int damageEnd = getDamageEndOffset(e);
            int parititionDamageEnd = -1;
            if (fChangedDocumentPartitions != null)
                parititionDamageEnd = fChangedDocumentPartitions.getOffset() + fChangedDocumentPartitions.getLength();
            int end = Math.max(damageEnd, parititionDamageEnd);
            damage = end == -1 ? r : new Region(r.getOffset(), end - r.getOffset());
        }
    } catch (BadLocationException x) {
    /* ignored in platform PresentationReconciler, too */
    }
    return damage;
}
Also used : ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IRegion(org.eclipse.jface.text.IRegion) Region(org.eclipse.jface.text.Region) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IPresentationDamager(org.eclipse.jface.text.presentation.IPresentationDamager) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 4 with IPresentationDamager

use of org.eclipse.jface.text.presentation.IPresentationDamager in project webtools.sourceediting by eclipse.

the class StructuredPresentationReconciler method setDocumentToDamagers.

/**
 * Informs all registered damagers about the document on which they will work.
 *
 * @param document the document on which to work
 */
protected void setDocumentToDamagers(IDocument document) {
    if (fDamagers != null) {
        Iterator e = fDamagers.values().iterator();
        while (e.hasNext()) {
            IPresentationDamager damager = (IPresentationDamager) e.next();
            damager.setDocument(document);
        }
    }
}
Also used : Iterator(java.util.Iterator) IPresentationDamager(org.eclipse.jface.text.presentation.IPresentationDamager)

Example 5 with IPresentationDamager

use of org.eclipse.jface.text.presentation.IPresentationDamager in project n4js by eclipse.

the class EditorContentExtractor method getDescriptorForSemanticElement.

/**
 * Optionally returns with the semantic AST node element (given as the element URI) as a {@link StyledTextDescriptor
 * styled text descriptor}. If the element cannot be resolved or the styled text cannot be computed this method
 * returns with and {@link Optional#absent() absent} instance but never {@code null}.
 *
 * @param uri
 *            the URI of the semantic element in the AST.
 * @return a styled text descriptor representing the extracted code for the semantic AST node given with its unique
 *         URI.
 */
public Optional<StyledTextDescriptor> getDescriptorForSemanticElement(final URI uri) {
    if (null == uri) {
        return absent();
    }
    final URI trimmedUri = uri.hasFragment() ? uri.trimFragment() : uri;
    final IN4JSProject project = core.findProject(trimmedUri).orNull();
    if (project == null) {
        return absent();
    }
    final ResourceSet resSet = core.createResourceSet(Optional.of(project));
    final IResourceDescriptions index = core.getXtextIndex(resSet);
    final IResourceDescription resDesc = index.getResourceDescription(trimmedUri);
    if (null == resDesc) {
        return absent();
    }
    final TModule module = core.loadModuleFromIndex(resSet, resDesc, false);
    if (null == module || null == module.eResource() || null == module.eResource().getResourceSet()) {
        return absent();
    }
    final URI moduleUri = module.eResource().getURI();
    final IFile file = getWorkspace().getRoot().getFile(new Path(moduleUri.toPlatformString(true)));
    if (null == file || !file.exists()) {
        return absent();
    }
    final FileEditorInput editorInput = new FileEditorInput(file);
    try {
        docProvider.connect(editorInput);
    } catch (final CoreException e) {
        LOGGER.error("Error while connecting editor input with document provider: " + e);
        return absent();
    }
    final IDocument doc = docProvider.getDocument(editorInput);
    if (null == doc) {
        return absent();
    }
    final XtextResource xtextResource = (XtextResource) module.eResource();
    final ResourceSet resourceSet = xtextResource.getResourceSet();
    final EObject object = resourceSet.getEObject(uri, true);
    if (null == object) {
        return absent();
    }
    final ITextRegion textRegion = locationInFileProvider.getFullTextRegion(object);
    if (null == textRegion) {
        return absent();
    }
    try {
        final int lineOfOffset = doc.getLineOfOffset(textRegion.getOffset());
        final int lineOffset = doc.getLineOffset(lineOfOffset);
        final int offset = lineOffset;
        final int length = textRegion.getLength() + (textRegion.getOffset() - lineOffset);
        final String text = doc.get(offset, length);
        final IPresentationRepairer repairer = repairerProvider.get();
        final IPresentationDamager damager = damagerProvider.get();
        for (final String contentType : partitionTypeMapper.getSupportedPartitionTypes()) {
            reconciler.setRepairer(repairer, contentType);
            repairer.setDocument(doc);
            reconciler.setDamager(damager, contentType);
            damager.setDocument(doc);
        }
        final Region region = new Region(offset, length);
        final TextPresentation textPresentation = reconciler.createRepairDescription(region, doc);
        final Iterator<?> rangeItr = textPresentation.getAllStyleRangeIterator();
        final Collection<StyleRange> ranges = newLinkedList();
        while (rangeItr.hasNext()) {
            final Object next = rangeItr.next();
            if (next instanceof StyleRange) {
                ranges.add((StyleRange) next);
            }
        }
        final Range<Integer> textRange = Range.closed(offset, offset + length);
        for (final Iterator<StyleRange> itr = ranges.iterator(); itr.hasNext(); ) /* nothing */
        {
            final StyleRange range = itr.next();
            if (!textRange.contains(range.start) || !textRange.contains(range.start + range.length)) {
                itr.remove();
            } else {
                range.start = range.start - offset;
            }
        }
        return fromNullable(new StyledTextDescriptorImpl(text, ranges));
    } catch (final BadLocationException e) {
        LOGGER.error("Error while trying to extract text from document.", e);
        return absent();
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) StyleRange(org.eclipse.swt.custom.StyleRange) XtextResource(org.eclipse.xtext.resource.XtextResource) URI(org.eclipse.emf.common.util.URI) IPresentationRepairer(org.eclipse.jface.text.presentation.IPresentationRepairer) IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) EObject(org.eclipse.emf.ecore.EObject) TModule(org.eclipse.n4js.ts.types.TModule) IPresentationDamager(org.eclipse.jface.text.presentation.IPresentationDamager) TextPresentation(org.eclipse.jface.text.TextPresentation) Path(org.eclipse.core.runtime.Path) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) CoreException(org.eclipse.core.runtime.CoreException) ITextRegion(org.eclipse.xtext.util.ITextRegion) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) FileEditorInput(org.eclipse.ui.part.FileEditorInput) Region(org.eclipse.jface.text.Region) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

IPresentationDamager (org.eclipse.jface.text.presentation.IPresentationDamager)5 BadLocationException (org.eclipse.jface.text.BadLocationException)2 IDocument (org.eclipse.jface.text.IDocument)2 IRegion (org.eclipse.jface.text.IRegion)2 ITypedRegion (org.eclipse.jface.text.ITypedRegion)2 Region (org.eclipse.jface.text.Region)2 IPresentationRepairer (org.eclipse.jface.text.presentation.IPresentationRepairer)2 Iterator (java.util.Iterator)1 IFile (org.eclipse.core.resources.IFile)1 CoreException (org.eclipse.core.runtime.CoreException)1 Path (org.eclipse.core.runtime.Path)1 URI (org.eclipse.emf.common.util.URI)1 EObject (org.eclipse.emf.ecore.EObject)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 TextPresentation (org.eclipse.jface.text.TextPresentation)1 IN4JSProject (org.eclipse.n4js.projectModel.IN4JSProject)1 TModule (org.eclipse.n4js.ts.types.TModule)1 StyleRange (org.eclipse.swt.custom.StyleRange)1 FileEditorInput (org.eclipse.ui.part.FileEditorInput)1 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)1