下面列出了org.eclipse.jface.text.IDocument#getPositions ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Get all positions of the given position category
* In sexp's these positions are typically skipped
*
* @param doc
* @param pos_names
* @return the positions to exclude
*/
public static List<Position> getExclusions(IDocument doc, String[] pos_names) {
List<Position> excludePositions = new LinkedList<Position>();
String cat = getTypeCategory(doc);
if (cat != null) {
try {
Position[] xpos = doc.getPositions(cat);
for (int j = 0; j < xpos.length; j++) {
if (xpos[j] instanceof TypedPosition) {
for (int jj = 0; jj < pos_names.length; jj++) {
if (((TypedPosition) xpos[j]).getType().contains(pos_names[jj])) {
excludePositions.add(xpos[j]);
}
}
}
}
} catch (BadPositionCategoryException e) {}
}
return excludePositions;
}
/**
* Note: this just gets the positions in the document. To cover for holes, use
* StringUtils.sortAndMergePositions with the result of this call.
*/
public static Position[] getDocumentTypedPositions(IDocument document, String defaultContentType) {
if (ALL_CONTENT_TYPES_AVAILABLE.equals(defaultContentType)) {
//Consider the whole document
return new Position[] { new TypedPosition(0, document.getLength(), defaultContentType) };
}
Position[] positions;
try {
IDocumentPartitionerExtension2 partitioner = (IDocumentPartitionerExtension2) document
.getDocumentPartitioner();
String[] managingPositionCategories = partitioner.getManagingPositionCategories();
Assert.isTrue(managingPositionCategories.length == 1);
positions = document.getPositions(managingPositionCategories[0]);
if (positions == null) {
positions = new Position[] { new TypedPosition(0, document.getLength(), defaultContentType) };
}
} catch (Exception e) {
Log.log("Unable to get positions for: " + defaultContentType, e); //Shouldn't happen, but if it does, consider the whole doc.
positions = new Position[] { new TypedPosition(0, document.getLength(), defaultContentType) };
}
return positions;
}
@SuppressWarnings("restriction")
private void processIOConsole(IOConsole ioConsole) {
IDocument document = ioConsole.getDocument();
try {
Position[] positions = document.getPositions(ConsoleHyperlinkPosition.HYPER_LINK_CATEGORY);
Arrays.sort(positions, new Comparator<Position>() {
@Override
public int compare(Position o1, Position o2) {
return Integer.compare(o1.getOffset(), o2.getOffset());
}
});
if (positions.length > 0) {
Position p = positions[positions.length - 1];
if (p instanceof ConsoleHyperlinkPosition) {
ConsoleHyperlinkPosition consoleHyperlinkPosition = (ConsoleHyperlinkPosition) p;
IHyperlink hyperLink = consoleHyperlinkPosition.getHyperLink();
hyperLink.linkActivated();
}
}
} catch (BadPositionCategoryException e) {
Log.log(e);
}
}
public FileLink getFileLink(int offset) {
try {
IDocument document = getDocument();
if (document != null) {
Position[] positions = document.getPositions(ERROR_MARKER_CATEGORY);
Position position = findPosition(offset, positions);
if (position instanceof MarkerPosition) {
return ((MarkerPosition) position).getFileLink();
}
}
} catch (BadPositionCategoryException e) {
}
return null;
}
public List<HighlightedPositionCore> reconciled(IDocument document, ASTNode ast, boolean forced, IProgressMonitor progressMonitor) throws BadPositionCategoryException {
// ensure at most one thread can be reconciling at any time
synchronized (fReconcileLock) {
if (fIsReconciling) {
return emptyList();
} else {
fIsReconciling= true;
}
}
fJobPresenter= fPresenter;
fJobSemanticHighlightings= fSemanticHighlightings;
fJobHighlightings= fHighlightings;
try {
if (ast == null) {
return emptyList();
}
ASTNode[] subtrees= getAffectedSubtrees(ast);
if (subtrees.length == 0) {
return emptyList();
}
startReconcilingPositions();
if (!fJobPresenter.isCanceled()) {
fJobDeprecatedMemberHighlighting= null;
for (int i= 0, n= fJobSemanticHighlightings.length; i < n; i++) {
SemanticHighlightingCore semanticHighlighting= fJobSemanticHighlightings[i];
if (semanticHighlighting instanceof DeprecatedMemberHighlighting) {
fJobDeprecatedMemberHighlighting = fJobHighlightings.get(i);
break;
}
}
reconcilePositions(subtrees);
}
// We need to manually install the position category
if (!document.containsPositionCategory(fPresenter.getPositionCategory())) {
document.addPositionCategory(fPresenter.getPositionCategory());
}
updatePresentation(document, fAddedPositions, fRemovedPositions);
stopReconcilingPositions();
Position[] positions = document.getPositions(fPresenter.getPositionCategory());
for (Position position : positions) {
Preconditions.checkState(position instanceof HighlightedPositionCore);
}
return FluentIterable.from(Arrays.asList(positions)).filter(HighlightedPositionCore.class).toList();
} finally {
fJobPresenter= null;
fJobSemanticHighlightings= null;
fJobHighlightings= null;
fJobDeprecatedMemberHighlighting= null;
synchronized (fReconcileLock) {
fIsReconciling= false;
}
}
}
private String getTokenScopeFragments(ITextViewer viewer, IDocument document, int offset)
{
if (!(viewer instanceof ISourceViewer))
{
return null;
}
try
{
// Force adding the category in case it doesn't exist yet...
document.addPositionCategory(ICommonConstants.SCOPE_CATEGORY);
Position[] scopes = document.getPositions(ICommonConstants.SCOPE_CATEGORY);
int index = document.computeIndexInCategory(ICommonConstants.SCOPE_CATEGORY, offset);
if (scopes == null || scopes.length == 0)
{
return null;
}
if (index >= scopes.length)
{
index = scopes.length - 1;
}
Position scope = scopes[index];
if (scope == null)
{
return null;
}
if (!scope.includes(offset))
{
if (index > 0)
{
scope = scopes[--index];
if (scope == null || !scope.includes(offset))
{
return null;
}
}
else
{
return null;
}
}
if (scope instanceof TypedPosition)
{
TypedPosition pos = (TypedPosition) scope;
return pos.getType();
}
}
catch (Exception e)
{
IdeLog.logError(CommonEditorPlugin.getDefault(), e);
}
return null;
}