Java Code Examples for org.eclipse.jface.text.Position#getLength()

The following examples show how to use org.eclipse.jface.text.Position#getLength() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnnotationExpandHover.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the distance to the ruler line.
 *
 * @param position the position
 * @param document the document
 * @param line the line number
 * @return the distance to the ruler line
 */
protected int compareRulerLine(Position position, IDocument document, int line) {

	if (position.getOffset() > -1 && position.getLength() > -1) {
		try {
			int firstLine= document.getLineOfOffset(position.getOffset());
			if (line == firstLine)
				return 1;
			if (firstLine <= line && line <= document.getLineOfOffset(position.getOffset() + position.getLength()))
				return 2;
		} catch (BadLocationException x) {
		}
	}

	return 0;
}
 
Example 2
Source File: AnnotationWithQuickFixesHover.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected Object getHoverInfoInternal(ITextViewer textViewer, final int lineNumber, final int offset) {
	AnnotationInfo result = recentAnnotationInfo;
	if (result != null)
		return result;
	List<Annotation> annotations = getAnnotations(lineNumber, offset);
	for (Annotation annotation : sortBySeverity(annotations)) {
		Position position = getAnnotationModel().getPosition(annotation);
		if (annotation.getText() != null && position != null) {
			final QuickAssistInvocationContext invocationContext = new QuickAssistInvocationContext(sourceViewer, position.getOffset(), position.getLength(), true);
			CompletionProposalRunnable runnable = new CompletionProposalRunnable(invocationContext);
			// Note: the resolutions have to be retrieved from the UI thread, otherwise
			// workbench.getActiveWorkbenchWindow() will return null in LanguageSpecificURIEditorOpener and
			// cause an exception
			Display.getDefault().syncExec(runnable);
			if (invocationContext.isMarkedCancelled()) {
				return null;
			}
			result = new AnnotationInfo(annotation, position, sourceViewer, runnable.proposals);
			recentAnnotationInfo = result;
			return result;
		}
	}
	return null;
}
 
Example 3
Source File: CorrectionMarkerResolutionGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static IProblemLocation findProblemLocation(IEditorInput input, IMarker marker) {
	IAnnotationModel model= JavaPlugin.getDefault().getCompilationUnitDocumentProvider().getAnnotationModel(input);
	if (model != null) { // open in editor
		Iterator<Annotation> iter= model.getAnnotationIterator();
		while (iter.hasNext()) {
			Annotation curr= iter.next();
			if (curr instanceof JavaMarkerAnnotation) {
				JavaMarkerAnnotation annot= (JavaMarkerAnnotation) curr;
				if (marker.equals(annot.getMarker())) {
					Position pos= model.getPosition(annot);
					if (pos != null) {
						return new ProblemLocation(pos.getOffset(), pos.getLength(), annot);
					}
				}
			}
		}
	} else { // not open in editor
		ICompilationUnit cu= getCompilationUnit(marker);
		return createFromMarker(marker, cu);
	}
	return null;
}
 
Example 4
Source File: JavaCorrectionProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static ProblemLocation getProblemLocation(IJavaAnnotation javaAnnotation, IAnnotationModel model) {
	int problemId= javaAnnotation.getId();
	if (problemId != -1) {
		Position pos= model.getPosition((Annotation) javaAnnotation);
		if (pos != null) {
			return new ProblemLocation(pos.getOffset(), pos.getLength(), javaAnnotation); // java problems all handled by the quick assist processors
		}
	}
	return null;
}
 
Example 5
Source File: AnnotationWithQuickFixesHover.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected Region getHoverRegionInternal(final int lineNumber, final int offset) {
	recentAnnotationInfo = null;
	List<Annotation> annotations = getAnnotations(lineNumber, offset);
	for (Annotation annotation : sortBySeverity(annotations)) {
		Position position = sourceViewer.getAnnotationModel().getPosition(annotation);
		if (position != null) {
			final int start = position.getOffset();
			return new Region(start, position.getLength());	
		}
	}
	return null;
}
 
Example 6
Source File: ProblemAnnotationHover.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected Region getHoverRegionInternal(final int lineNumber, final int offset) {
	List<Annotation> annotations = getAnnotations(lineNumber, offset);
	for (Annotation annotation : sortBySeverity(annotations)) {
		Position position = sourceViewer.getAnnotationModel().getPosition(annotation);
		if (position != null) {
			final int start = position.getOffset();
			return new Region(start, position.getLength());	
		}
	}
	return null;
}
 
Example 7
Source File: PartitionCodeReader.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isPositionValid(Position position, String contentType) {
    if (fSupportKeepPositions || (fForward && position.getOffset() + position.getLength() >= fOffset || !fForward
            && position.getOffset() <= fOffset)) {
        if (position instanceof TypedPosition) {
            TypedPosition typedPosition = (TypedPosition) position;
            if (contentType != null && !contentType.equals(ALL_CONTENT_TYPES_AVAILABLE)) {
                if (!contentType.equals(typedPosition.getType())) {
                    return false;
                }
            }
        }
        return true;
    }
    return false;
}
 
Example 8
Source File: TexProjectionAnnotation.java    From texlipse with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Checks whether the given offset is contained within this annotation
 * 
 * @param offset The offset inside the document that this annotation belongs to
 * @return True if the offset is contained, false otherwise
 */
public boolean contains(int offset) {
	Position pos = node.getPosition();
	if (offset >= pos.getOffset() 
			&& offset < (pos.getOffset() + pos.getLength()))
		return true;
	return false;
}
 
Example 9
Source File: TexAnnotationHover.java    From texlipse with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isRulerLine(Position position, IDocument document, int line) {
    if (position.getOffset() > -1 && position.getLength() > -1) {
        try {
            return line == document.getLineOfOffset(position.getOffset());
        } catch (BadLocationException x) {
        }
    }
    return false;
}
 
Example 10
Source File: FastPartitioner.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the index of the first position which ends after the given offset.
 *
 * @param positions the positions in linear order
 * @param offset the offset
 * @return the index of the first position which ends after the offset
 */
private int getFirstIndexEndingAfterOffset(Position[] positions, int offset) {
    int i = -1, j = positions.length;
    while (j - i > 1) {
        int k = (i + j) >> 1;
        Position p = positions[k];
        if (p.getOffset() + p.getLength() > offset) {
            j = k;
        } else {
            i = k;
        }
    }
    return j;
}
 
Example 11
Source File: PyFoldingAction.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param position
 * @param elements
 * @return
 */
protected boolean isInside(Position position, List elements) {
    for (Iterator iter = elements.iterator(); iter.hasNext();) {
        Position element = (Position) iter.next();
        if (position.getOffset() > element.getOffset()
                && position.getOffset() < element.getOffset() + element.getLength()) {
            return true;
        }
    }
    return false;
}
 
Example 12
Source File: ScriptDocumentProvider.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
public void beforeChangeText()
{		
	for (Iterator e= getAnnotationIterator(true); e.hasNext();) {
		Object o= e.next();
		if (o instanceof MarkerAnnotation) {
			MarkerAnnotation a= (MarkerAnnotation) o;
			
			//System.out.println(a.getType( ));
			IMarker mark = a.getMarker( );
			try
			{
				if (mark == null || !getId( ).equals( mark.getAttribute( SUBNAME ) ))
				{
					continue;
				}
				if (!(ScriptDocumentProvider.MARK_TYPE.equals( a.getMarker( ).getType( ))))
				{
					continue;
				}
			}
			catch ( CoreException e1 )
			{
				continue;
			}
			Position p= getPosition( a );
			if (p != null && !p.isDeleted( )) {
				Position tempCopy = new Position(p.getOffset(),p.getLength());
				markMap.put( a, tempCopy );
			}
		}
	}

	change = true;
}
 
Example 13
Source File: TLAAnnotationHover.java    From tlaplus with MIT License 5 votes vote down vote up
private boolean compareRulerLine(final Position position, final IDocument document, final int line) {
	try {
		if ((position.getOffset() > -1) && (position.getLength() > -1)) {
			return (document.getLineOfOffset(position.getOffset()) == line);
		}
	} catch (final BadLocationException e) { }
	return false;
}
 
Example 14
Source File: DefaultCellSearchStrategy.java    From translationstudio8 with GNU General Public License v2.0 4 votes vote down vote up
/** burke 修改find/replace界面修改  不需要fuzzySearch*/
//private boolean fuzzySearch;

public IRegion executeSearch(String findValue, String dataValue) {
	// 如果查找的字符和单元格中的数据长度为 0,,则直接返回没有找到。
	if (findValue == null || findValue.length() == 0 || dataValue == null || dataValue.length() == 0) {
		return null;
	}
	Document doc = new Document(dataValue);
	FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(doc);
	IRegion region;
	try {
		if (startOffset == -1) {
			if (searchForward) {
				startOffset = 0;
			} else {
				startOffset = adapter.length() - 1;
			}
		}
		region = adapter.find(startOffset, findValue, searchForward, caseSensitive, wholeWord, regExSearch);
		while (region != null) {
			boolean inTag = false;
			for (int i = region.getOffset(); i < region.getOffset() + region.getLength(); i++) {
				Position tagRange = InnerTagUtil.getStyledTagRange(dataValue, i);
				if (tagRange != null) {
					if (searchForward) {
						if (tagRange.getOffset() + tagRange.getLength() == dataValue.length()) {
							return null; // 如果句首是一个标记,则直接返回 null,会继续查找上一个文本段。
						}
						startOffset = tagRange.getOffset() + tagRange.getLength();
					} else {
						if (tagRange.offset == 0) {
							return null; // 如果句首是一个标记,则直接返回 null,会继续查找上一个文本段。
						}
						startOffset = tagRange.getOffset() - 1;
					}
					inTag = true;
					break;
				}
			}
			if (inTag) {
				region = adapter.find(startOffset, findValue, searchForward, caseSensitive, wholeWord, regExSearch);
			} else {
				break;
			}
		}
		return region;
	} catch (BadLocationException e) {
		return null;
	}
}
 
Example 15
Source File: JavaEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * Overrides the default implementation to handle {@link IJavaAnnotation}.
 * </p>
 *
 * @param offset the region offset
 * @param length the region length
 * @param forward <code>true</code> for forwards, <code>false</code> for backward
 * @param annotationPosition the position of the found annotation
 * @return the found annotation
 * @since 3.2
 */
@Override
protected Annotation findAnnotation(final int offset, final int length, boolean forward, Position annotationPosition) {

	Annotation nextAnnotation= null;
	Position nextAnnotationPosition= null;
	Annotation containingAnnotation= null;
	Position containingAnnotationPosition= null;
	boolean currentAnnotation= false;

	IDocument document= getDocumentProvider().getDocument(getEditorInput());
	int endOfDocument= document.getLength();
	int distance= Integer.MAX_VALUE;

	IAnnotationModel model= getDocumentProvider().getAnnotationModel(getEditorInput());
	if (model == null)
		return null;

	Iterator<Annotation> e= new JavaAnnotationIterator(model.getAnnotationIterator(), true);
	while (e.hasNext()) {
		Annotation a= e.next();
		if ((a instanceof IJavaAnnotation) && ((IJavaAnnotation)a).hasOverlay() || !isNavigationTarget(a))
			continue;

		Position p= model.getPosition(a);
		if (p == null)
			continue;

		if (forward && p.offset == offset || !forward && p.offset + p.getLength() == offset + length) {// || p.includes(offset)) {
			if (containingAnnotation == null || (forward && p.length >= containingAnnotationPosition.length || !forward && p.length >= containingAnnotationPosition.length)) {
				containingAnnotation= a;
				containingAnnotationPosition= p;
				currentAnnotation= p.length == length;
			}
		} else {
			int currentDistance= 0;

			if (forward) {
				currentDistance= p.getOffset() - offset;
				if (currentDistance < 0)
					currentDistance= endOfDocument + currentDistance;

				if (currentDistance < distance || currentDistance == distance && p.length < nextAnnotationPosition.length) {
					distance= currentDistance;
					nextAnnotation= a;
					nextAnnotationPosition= p;
				}
			} else {
				currentDistance= offset + length - (p.getOffset() + p.length);
				if (currentDistance < 0)
					currentDistance= endOfDocument + currentDistance;

				if (currentDistance < distance || currentDistance == distance && p.length < nextAnnotationPosition.length) {
					distance= currentDistance;
					nextAnnotation= a;
					nextAnnotationPosition= p;
				}
			}
		}
	}
	if (containingAnnotationPosition != null && (!currentAnnotation || nextAnnotation == null)) {
		annotationPosition.setOffset(containingAnnotationPosition.getOffset());
		annotationPosition.setLength(containingAnnotationPosition.getLength());
		return containingAnnotation;
	}
	if (nextAnnotationPosition != null) {
		annotationPosition.setOffset(nextAnnotationPosition.getOffset());
		annotationPosition.setLength(nextAnnotationPosition.getLength());
	}

	return nextAnnotation;
}
 
Example 16
Source File: DefaultCellSearchStrategy.java    From tmxeditor8 with GNU General Public License v2.0 4 votes vote down vote up
/** burke 修改find/replace界面修改  不需要fuzzySearch*/
//private boolean fuzzySearch;

public IRegion executeSearch(String findValue, String dataValue) {
	// 如果查找的字符和单元格中的数据长度为 0,,则直接返回没有找到。
	if (findValue == null || findValue.length() == 0 || dataValue == null || dataValue.length() == 0) {
		return null;
	}
	Document doc = new Document(dataValue);
	FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(doc);
	IRegion region;
	try {
		if (startOffset == -1) {
			if (searchForward) {
				startOffset = 0;
			} else {
				startOffset = adapter.length() - 1;
			}
		}
		region = adapter.find(startOffset, findValue, searchForward, caseSensitive, wholeWord, regExSearch);
		while (region != null) {
			boolean inTag = false;
			for (int i = region.getOffset(); i < region.getOffset() + region.getLength(); i++) {
				Position tagRange = InnerTagUtil.getStyledTagRange(dataValue, i);
				if (tagRange != null) {
					if (searchForward) {
						if (tagRange.getOffset() + tagRange.getLength() == dataValue.length()) {
							return null; // 如果句首是一个标记,则直接返回 null,会继续查找上一个文本段。
						}
						startOffset = tagRange.getOffset() + tagRange.getLength();
					} else {
						if (tagRange.offset == 0) {
							return null; // 如果句首是一个标记,则直接返回 null,会继续查找上一个文本段。
						}
						startOffset = tagRange.getOffset() - 1;
					}
					inTag = true;
					break;
				}
			}
			if (inTag) {
				region = adapter.find(startOffset, findValue, searchForward, caseSensitive, wholeWord, regExSearch);
			} else {
				break;
			}
		}
		return region;
	} catch (BadLocationException e) {
		return null;
	}
}
 
Example 17
Source File: JavaStatementPostfixContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public JavaStatementPostfixContext(TemplateContextType type,
		IDocument document, Position completionPosition,
		ICompilationUnit compilationUnit) {
	this(type, document, completionPosition.getOffset(), completionPosition.getLength(), compilationUnit, null, null);
}
 
Example 18
Source File: TLAFastPartitioner.java    From tlaplus with MIT License 2 votes vote down vote up
/**
 * Returns <code>true</code> if the given ranges overlap with or touch each other.
 *
 * @param gap the first range
 * @param offset the offset of the second range
 * @param length the length of the second range
 * @return <code>true</code> if the given ranges overlap with or touch each other
 */
private boolean overlapsOrTouches(Position gap, int offset, int length) {
    return gap.getOffset() <= offset + length && offset <= gap.getOffset() + gap.getLength();
}
 
Example 19
Source File: FastPartitioner.java    From Pydev with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Pretty print a <code>Position</code>.
 *
 * @param position the position to format
 * @return a formatted string
 */
private String toString(Position position) {
    return "P[" + position.getOffset() + "+" + position.getLength() + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
 
Example 20
Source File: DocumentPartitioner.java    From xtext-eclipse with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Pretty print a <code>Position</code>.
 * 
 * @param position
 *            the position to format
 * @return a formatted string
 */
private String toString(Position position) {
	return "P[" + position.getOffset() + "+" + position.getLength() + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}