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