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;
}
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;
}
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);
}
}
}
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();
}
}
Aggregations