Search in sources :

Example 1 with IPresentationRepairer

use of org.eclipse.jface.text.presentation.IPresentationRepairer 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 IPresentationRepairer

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

the class StructuredPresentationReconciler method createPresentation.

/**
 * Constructs a "repair description" for the given damage and returns this
 * description as a text presentation. For this, it queries the partitioning
 * of the damage region and asks the appropriate presentation repairer for
 * each partition to construct the "repair description" for this partition.
 *
 * @param damage the damage to be repaired
 * @param document the document whose presentation must be repaired
 * @return the presentation repair description as text presentation or
 *         <code>null</code> if the partitioning could not be computed
 */
protected TextPresentation createPresentation(IRegion damage, IDocument document) {
    try {
        int validLength = Math.min(damage.getLength(), document.getLength() - damage.getOffset());
        if (fRepairers == null || fRepairers.isEmpty()) {
            TextPresentation presentation = new TextPresentation(damage, 1);
            presentation.setDefaultStyleRange(new StyleRange(damage.getOffset(), validLength, null, null));
            return presentation;
        }
        TextPresentation presentation = new TextPresentation(damage, 1000);
        ITypedRegion[] partitions = TextUtilities.computePartitioning(document, getDocumentPartitioning(), damage.getOffset(), validLength, false);
        for (int i = 0; i < partitions.length; i++) {
            ITypedRegion r = partitions[i];
            IPresentationRepairer repairer = getRepairer(r.getType());
            if (repairer != null)
                repairer.createPresentation(presentation, r);
        }
        return presentation;
    } catch (BadLocationException x) {
    /* ignored in platform PresentationReconciler, too */
    }
    return null;
}
Also used : IPresentationRepairer(org.eclipse.jface.text.presentation.IPresentationRepairer) StyleRange(org.eclipse.swt.custom.StyleRange) ITypedRegion(org.eclipse.jface.text.ITypedRegion) TextPresentation(org.eclipse.jface.text.TextPresentation) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 3 with IPresentationRepairer

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

the class StructuredPresentationReconciler method setDocumentToRepairers.

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

Example 4 with IPresentationRepairer

use of org.eclipse.jface.text.presentation.IPresentationRepairer 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

IPresentationRepairer (org.eclipse.jface.text.presentation.IPresentationRepairer)4 BadLocationException (org.eclipse.jface.text.BadLocationException)2 TextPresentation (org.eclipse.jface.text.TextPresentation)2 IPresentationDamager (org.eclipse.jface.text.presentation.IPresentationDamager)2 StyleRange (org.eclipse.swt.custom.StyleRange)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 IDocument (org.eclipse.jface.text.IDocument)1 ITypedRegion (org.eclipse.jface.text.ITypedRegion)1 Region (org.eclipse.jface.text.Region)1 IN4JSProject (org.eclipse.n4js.projectModel.IN4JSProject)1 TModule (org.eclipse.n4js.ts.types.TModule)1 FileEditorInput (org.eclipse.ui.part.FileEditorInput)1 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)1 IResourceDescriptions (org.eclipse.xtext.resource.IResourceDescriptions)1