use of org.eclipse.jface.text.TypedPosition in project webtools.sourceediting by eclipse.
the class StructuredFormattingStrategy method format.
/*
* @see org.eclipse.jface.text.formatter.ContextBasedFormattingStrategy#format()
*/
public void format() {
super.format();
final IDocument document = (IDocument) fDocuments.removeFirst();
final TypedPosition partition = (TypedPosition) fPartitions.removeFirst();
if (document != null && partition != null && fRegion != null && fFormatProcessor != null) {
try {
fFormatProcessor.formatDocument(document, fRegion.getOffset(), fRegion.getLength());
} catch (IOException e) {
// log for now, unless we find reason not to
Logger.log(Logger.INFO, e.getMessage());
} catch (CoreException e) {
// log for now, unless we find reason not to
Logger.log(Logger.INFO, e.getMessage());
}
}
}
use of org.eclipse.jface.text.TypedPosition in project webtools.sourceediting by eclipse.
the class StructuredTextMultiPassContentFormatter method formatSlave.
/*
* Overwritten to check for additional slave formatting strategies
* contributed via the editorConfiguration extension point.
*/
protected void formatSlave(IFormattingContext context, IDocument document, int offset, int length, String type) {
List installedTypes = getInstalledPartitionTypes();
if (installedTypes.contains(type)) {
// we've already set the slave formatter strategy so just perform
// as normal
super.formatSlave(context, document, offset, length, type);
} else {
boolean findExtendedSlaveFormatter = true;
// need to figure out if there's already a slave formatter set, so
// just attempt to format as normal
super.formatSlave(context, document, offset, length, type);
// now, determine if slave formatter was already set by checking
// context (it would be set if there's already one)
Object contextPartition = context.getProperty(FormattingContextProperties.CONTEXT_PARTITION);
if (contextPartition instanceof TypedPosition) {
String contextType = ((TypedPosition) contextPartition).getType();
if (contextType == type) {
// there's already a slave formatter set, so just add it
// to the list of installed partition types for future
// reference
installedTypes.add(type);
findExtendedSlaveFormatter = false;
}
}
// via the editorConfiguration extension point
if (findExtendedSlaveFormatter) {
Object configuration = ExtendedConfigurationBuilder.getInstance().getConfiguration(SLAVE_FORMATTING_STRATEGY_EXTENDED_ID, type);
if (configuration instanceof IFormattingStrategy) {
// found a formatter, so add it in
setSlaveStrategy((IFormattingStrategy) configuration, type);
// try to format slave again now that one is set
super.formatSlave(context, document, offset, length, type);
}
// note that we've already checked this partition type for
// future reference
installedTypes.add(type);
}
}
}
use of org.eclipse.jface.text.TypedPosition in project xtext-eclipse by eclipse.
the class DocumentPartitioner method getContentType.
/**
* {@inheritDoc}
* <p>
* May be replaced or extended by subclasses.
* </p>
*
* @since 2.2
*/
@Override
public synchronized String getContentType(int offset) {
checkInitialization();
TypedPosition p = findClosestPosition(offset);
if (p != null && p.includes(offset))
return p.getType();
return IDocument.DEFAULT_CONTENT_TYPE;
}
use of org.eclipse.jface.text.TypedPosition in project xtext-eclipse by eclipse.
the class DocumentPartitioner method computePartitioning.
/**
* {@inheritDoc}
* <p>
* May be replaced or extended by subclasses.
* </p>
*
* @since 2.2
*/
@Override
public synchronized ITypedRegion[] computePartitioning(int offset, int length, boolean includeZeroLengthPartitions) {
checkInitialization();
List<ITypedRegion> list = new ArrayList<ITypedRegion>();
try {
int endOffset = offset + length;
Position[] category = getPositions();
TypedPosition previous = null, current = null;
int start, end, gapOffset;
Position gap = new Position(0);
int startIndex = getFirstIndexEndingAfterOffset(category, offset);
int endIndex = getFirstIndexStartingAfterOffset(category, endOffset);
for (int i = startIndex; i < endIndex; i++) {
current = (TypedPosition) category[i];
gapOffset = (previous != null) ? previous.getOffset() + previous.getLength() : 0;
gap.setOffset(gapOffset);
gap.setLength(current.getOffset() - gapOffset);
if ((includeZeroLengthPartitions && overlapsOrTouches(gap, offset, length)) || (gap.getLength() > 0 && gap.overlapsWith(offset, length))) {
start = Math.max(offset, gapOffset);
end = Math.min(endOffset, gap.getOffset() + gap.getLength());
list.add(new TypedRegion(start, end - start, IDocument.DEFAULT_CONTENT_TYPE));
}
if (current.overlapsWith(offset, length)) {
start = Math.max(offset, current.getOffset());
end = Math.min(endOffset, current.getOffset() + current.getLength());
list.add(new TypedRegion(start, end - start, current.getType()));
}
previous = current;
}
if (previous != null) {
gapOffset = previous.getOffset() + previous.getLength();
gap.setOffset(gapOffset);
int gapLength = fDocument.getLength() - gapOffset;
if (gapLength < 0) {
clearPositionCache();
return new TypedRegion[0];
} else {
gap.setLength(gapLength);
if ((includeZeroLengthPartitions && overlapsOrTouches(gap, offset, length)) || (gap.getLength() > 0 && gap.overlapsWith(offset, length))) {
start = Math.max(offset, gapOffset);
end = Math.min(endOffset, fDocument.getLength());
list.add(new TypedRegion(start, end - start, IDocument.DEFAULT_CONTENT_TYPE));
}
}
}
if (list.isEmpty())
list.add(new TypedRegion(offset, length, IDocument.DEFAULT_CONTENT_TYPE));
} catch (BadPositionCategoryException ex) {
// Make sure we clear the cache
clearPositionCache();
} catch (RuntimeException ex) {
// Make sure we clear the cache
clearPositionCache();
throw ex;
}
TypedRegion[] result = new TypedRegion[list.size()];
list.toArray(result);
return result;
}
use of org.eclipse.jface.text.TypedPosition in project tmdm-studio-se by Talend.
the class XMLDocumentPartitioner method findPreviousNonWhiteSpacePosition.
public TypedPosition findPreviousNonWhiteSpacePosition(int offset) {
try {
int index = document.computeIndexInCategory(positionCategory, offset);
Position[] positions = document.getPositions(positionCategory);
if (positions.length == 0) {
return null;
}
if (index == positions.length) {
index--;
}
for (; index >= 0; index--) {
TypedPosition position = (TypedPosition) positions[index];
if (position instanceof XMLNode) {
XMLNode node = (XMLNode) position;
if (!node.isEmpty())
return node;
}
}
} catch (BadLocationException e) {
e.printStackTrace();
} catch (BadPositionCategoryException e) {
e.printStackTrace();
}
return null;
}
Aggregations