Java Code Examples for org.eclipse.jdt.core.ISourceRange#getLength()
The following examples show how to use
org.eclipse.jdt.core.ISourceRange#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: StructureSelectNextAction.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
@Override ISourceRange internalGetNewSelectionRange(ISourceRange oldSourceRange, ISourceReference sr, SelectionAnalyzer selAnalyzer) throws JavaModelException{ if (oldSourceRange.getLength() == 0 && selAnalyzer.getLastCoveringNode() != null) { ASTNode previousNode= NextNodeAnalyzer.perform(oldSourceRange.getOffset(), selAnalyzer.getLastCoveringNode()); if (previousNode != null) return getSelectedNodeSourceRange(sr, previousNode); } ASTNode first= selAnalyzer.getFirstSelectedNode(); if (first == null) return getLastCoveringNodeRange(oldSourceRange, sr, selAnalyzer); ASTNode parent= first.getParent(); if (parent == null) return getLastCoveringNodeRange(oldSourceRange, sr, selAnalyzer); ASTNode lastSelectedNode= selAnalyzer.getSelectedNodes()[selAnalyzer.getSelectedNodes().length - 1]; ASTNode nextNode= getNextNode(parent, lastSelectedNode); if (nextNode == parent) return getSelectedNodeSourceRange(sr, first.getParent()); int offset= oldSourceRange.getOffset(); int end= Math.min(sr.getSourceRange().getLength(), nextNode.getStartPosition() + nextNode.getLength() - 1); return StructureSelectionAction.createSourceRange(offset, end); }
Example 2
Source File: ASTNodeSearchUtil.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static ASTNode findNode(ISourceRange range, CompilationUnit cuNode){ NodeFinder nodeFinder= new NodeFinder(cuNode, range.getOffset(), range.getLength()); ASTNode coveredNode= nodeFinder.getCoveredNode(); if (coveredNode != null) return coveredNode; else return nodeFinder.getCoveringNode(); }
Example 3
Source File: DOMFinder.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public ASTNode search() throws JavaModelException { ISourceRange range = null; if (this.element instanceof IMember && !(this.element instanceof IInitializer)) range = ((IMember) this.element).getNameRange(); else range = this.element.getSourceRange(); this.rangeStart = range.getOffset(); this.rangeLength = range.getLength(); this.ast.accept(this); return this.foundNode; }
Example 4
Source File: MatchLocator.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public MatchLocator( SearchPattern pattern, SearchRequestor requestor, IJavaSearchScope scope, IProgressMonitor progressMonitor) { this.pattern = pattern; this.patternLocator = PatternLocator.patternLocator(this.pattern); this.matchContainer = this.patternLocator == null ? 0 : this.patternLocator.matchContainer(); this.requestor = requestor; this.scope = scope; this.progressMonitor = progressMonitor; if (pattern instanceof PackageDeclarationPattern) { this.searchPackageDeclaration = true; } else if (pattern instanceof OrPattern) { this.searchPackageDeclaration = ((OrPattern)pattern).hasPackageDeclaration(); } else { this.searchPackageDeclaration = false; } if (pattern instanceof MethodPattern) { IType type = ((MethodPattern) pattern).declaringType; if (type != null && !type.isBinary()) { SourceType sourceType = (SourceType) type; IMember local = sourceType.getOuterMostLocalContext(); if (local instanceof IMethod) { // remember this method's range so we don't purge its statements. try { ISourceRange range = local.getSourceRange(); this.sourceStartOfMethodToRetain = range.getOffset(); this.sourceEndOfMethodToRetain = this.sourceStartOfMethodToRetain + range.getLength() - 1; // offset is 0 based. } catch (JavaModelException e) { // drop silently. } } } } }
Example 5
Source File: GoToNextPreviousMemberAction.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public void update() { boolean enabled= false; ISourceReference ref= getSourceReference(); if (ref != null) { ISourceRange range; try { range= ref.getSourceRange(); enabled= range != null && range.getLength() > 0; } catch (JavaModelException e) { // enabled= false; } } setEnabled(enabled); }
Example 6
Source File: RenameNonVirtualMethodProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private void addDeclarationUpdate(TextChangeManager manager) throws CoreException { if (getDelegateUpdating()) { // create the delegate CompilationUnitRewrite rewrite= new CompilationUnitRewrite(getDeclaringCU()); rewrite.setResolveBindings(true); MethodDeclaration methodDeclaration= ASTNodeSearchUtil.getMethodDeclarationNode(getMethod(), rewrite.getRoot()); DelegateMethodCreator creator= new DelegateMethodCreator(); creator.setDeclaration(methodDeclaration); creator.setDeclareDeprecated(getDeprecateDelegates()); creator.setSourceRewrite(rewrite); creator.setCopy(true); creator.setNewElementName(getNewElementName()); creator.prepareDelegate(); creator.createEdit(); CompilationUnitChange cuChange= rewrite.createChange(true); if (cuChange != null) { cuChange.setKeepPreviewEdits(true); manager.manage(getDeclaringCU(), cuChange); } } String editName= RefactoringCoreMessages.RenameMethodRefactoring_update_declaration; ISourceRange nameRange= getMethod().getNameRange(); ReplaceEdit replaceEdit= new ReplaceEdit(nameRange.getOffset(), nameRange.getLength(), getNewElementName()); addTextEdit(manager.get(getDeclaringCU()), editName, replaceEdit); }
Example 7
Source File: CompilationUnitEditor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Returns the length of the given Java element. * * @param element Java element * @return Length of the given Java element */ private int getLength(IJavaElement element) { if (element instanceof ISourceReference) { ISourceReference sr= (ISourceReference) element; try { ISourceRange srcRange= sr.getSourceRange(); if (srcRange != null) return srcRange.getLength(); } catch (JavaModelException e) { } } return -1; }
Example 8
Source File: MoveStaticMembersProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private boolean isWithinMemberToMove(SearchMatch result) throws JavaModelException { ICompilationUnit referenceCU= SearchUtils.getCompilationUnit(result); if (! referenceCU.equals(fSource.getCu())) return false; int referenceStart= result.getOffset(); for (int i= 0; i < fMembersToMove.length; i++) { ISourceRange range= fMembersToMove[i].getSourceRange(); if (range.getOffset() <= referenceStart && range.getOffset() + range.getLength() >= referenceStart) return true; } return false; }
Example 9
Source File: HierarchyProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
protected boolean isMovedReference(final SearchMatch match) throws JavaModelException { ISourceRange range= null; for (int index= 0; index < fMembersToMove.length; index++) { range= fMembersToMove[index].getSourceRange(); if (range.getOffset() <= match.getOffset() && range.getOffset() + range.getLength() >= match.getOffset()) return true; } return false; }
Example 10
Source File: CodeLensHandler.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private boolean overlaps(ISourceRange typeRange, ISourceRange methodRange) { if (typeRange == null || methodRange == null) { return false; } //method range is overlapping if it appears before or actually overlaps the type's range return methodRange.getOffset() < typeRange.getOffset() || methodRange.getOffset() >= typeRange.getOffset() && methodRange.getOffset() <= (typeRange.getOffset() + typeRange.getLength()); }
Example 11
Source File: RenameFieldProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private void addDeclarationUpdate() throws CoreException { ISourceRange nameRange= fField.getNameRange(); TextEdit textEdit= new ReplaceEdit(nameRange.getOffset(), nameRange.getLength(), getNewElementName()); ICompilationUnit cu= fField.getCompilationUnit(); String groupName= RefactoringCoreMessages.RenameFieldRefactoring_Update_field_declaration; addTextEdit(fChangeManager.get(cu), groupName, textEdit); }
Example 12
Source File: GoToNextPreviousMemberAction.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
@Override public final void run() { ITextSelection selection= getTextSelection(); ISourceRange newRange= getNewSelectionRange(createSourceRange(selection), null); // Check if new selection differs from current selection if (selection.getOffset() == newRange.getOffset() && selection.getLength() == newRange.getLength()) return; fEditor.selectAndReveal(newRange.getOffset(), newRange.getLength()); }
Example 13
Source File: ASTNodeSearchUtil.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private static ASTNode findNode(ISourceRange range, CompilationUnit cuNode){ NodeFinder nodeFinder= new NodeFinder(cuNode, range.getOffset(), range.getLength()); ASTNode coveredNode= nodeFinder.getCoveredNode(); if (coveredNode != null) { return coveredNode; } else { return nodeFinder.getCoveringNode(); } }
Example 14
Source File: TopLevelTypeProblemsLabelDecorator.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
@Override protected boolean isInside(int pos, ISourceReference sourceElement) throws CoreException { // XXX: Work in progress for problem decorator being a workbench decorator // IDecoratorManager decoratorMgr= PlatformUI.getWorkbench().getDecoratorManager(); // if (!decoratorMgr.getEnabled("org.eclipse.jdt.ui.problem.decorator")) //$NON-NLS-1$ // return false; if (!(sourceElement instanceof IType) || ((IType)sourceElement).getDeclaringType() != null) return false; ICompilationUnit cu= ((IType)sourceElement).getCompilationUnit(); if (cu == null) return false; IType[] types= cu.getTypes(); if (types.length < 1) return false; int firstTypeStartOffset= -1; ISourceRange range= types[0].getSourceRange(); if (range != null) firstTypeStartOffset= range.getOffset(); int lastTypeEndOffset= -1; range= types[types.length-1].getSourceRange(); if (range != null) lastTypeEndOffset= range.getOffset() + range.getLength() - 1; return pos < firstTypeStartOffset || pos > lastTypeEndOffset || isInside(pos, sourceElement.getSourceRange()); }
Example 15
Source File: JavaEditor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
@Override public void run() { final JavaSourceViewer viewer= (JavaSourceViewer) getSourceViewer(); if (viewer.isEditable() && ElementValidator.check(getInputJavaElement(), getSite().getShell(), JavaEditorMessages.JavaEditor_FormatElementDialog_label, true)) { final Point selection= viewer.rememberSelection(); try { viewer.setRedraw(false); boolean emptySelection= selection.y == 0; if (emptySelection) { final ITypedRegion partition= TextUtilities.getPartition(viewer.getDocument(), IJavaPartitions.JAVA_PARTITIONING, selection.x, true); String type= partition.getType(); if (IJavaPartitions.JAVA_DOC.equals(type) || IJavaPartitions.JAVA_MULTI_LINE_COMMENT.equals(type) || IJavaPartitions.JAVA_SINGLE_LINE_COMMENT.equals(type)) { viewer.setSelectedRange(partition.getOffset(), partition.getLength()); viewer.doOperation(ISourceViewer.FORMAT); return; } } final IJavaElement element= getElementAt(selection.x, true); if (element != null && element.exists()) { try { final int kind= element.getElementType(); if (kind == IJavaElement.TYPE || kind == IJavaElement.METHOD || kind == IJavaElement.INITIALIZER) { final ISourceReference reference= (ISourceReference) element; final ISourceRange range= reference.getSourceRange(); final ISourceRange nameRange= reference.getNameRange(); final boolean seletionInNameRange= nameRange != null && selection.x >= nameRange.getOffset() && selection.x + selection.y <= nameRange.getOffset() + nameRange.getLength(); if (range != null && (emptySelection || seletionInNameRange)) viewer.setSelectedRange(range.getOffset(), range.getLength()); } } catch (JavaModelException exception) { // Should not happen } } viewer.doOperation(ISourceViewer.FORMAT); } catch (BadLocationException e) { // Cannot happen } finally { viewer.setRedraw(true); viewer.restoreSelection(); } } }
Example 16
Source File: CallHierarchyUI.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Opens the element in the editor or shows an error dialog if that fails. * * @param element the element to open * @param shell parent shell for error dialog * @param activateOnOpen <code>true</code> if the editor should be activated * @return <code>true</code> iff no error occurred while trying to open the editor, * <code>false</code> iff an error dialog was raised. */ public static boolean openInEditor(Object element, Shell shell, boolean activateOnOpen) { CallLocation callLocation= CallHierarchy.getCallLocation(element); try { IMember enclosingMember; int selectionStart; int selectionLength; if (callLocation != null) { enclosingMember= callLocation.getMember(); selectionStart= callLocation.getStart(); selectionLength= callLocation.getEnd() - selectionStart; } else if (element instanceof MethodWrapper) { enclosingMember= ((MethodWrapper) element).getMember(); ISourceRange selectionRange= enclosingMember.getNameRange(); if (selectionRange == null) selectionRange= enclosingMember.getSourceRange(); if (selectionRange == null) return true; selectionStart= selectionRange.getOffset(); selectionLength= selectionRange.getLength(); } else { return true; } IEditorPart methodEditor = JavaUI.openInEditor(enclosingMember, activateOnOpen, false); if (methodEditor instanceof ITextEditor) { ITextEditor editor = (ITextEditor) methodEditor; editor.selectAndReveal(selectionStart, selectionLength); } return true; } catch (JavaModelException e) { JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, CallHierarchyMessages.CallHierarchyUI_open_in_editor_error_message, e)); ErrorDialog.openError(shell, CallHierarchyMessages.OpenLocationAction_error_title, CallHierarchyMessages.CallHierarchyUI_open_in_editor_error_message, e.getStatus()); return false; } catch (PartInitException x) { String name; if (callLocation != null) name= callLocation.getCalledMember().getElementName(); else if (element instanceof MethodWrapper) name= ((MethodWrapper) element).getName(); else name= ""; //$NON-NLS-1$ MessageDialog.openError(shell, CallHierarchyMessages.OpenLocationAction_error_title, Messages.format( CallHierarchyMessages.CallHierarchyUI_open_in_editor_error_messageArgs, new String[] { name, x.getMessage() })); return false; } }
Example 17
Source File: JavaStatusContextViewer.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private static IRegion createRegion(ISourceRange range) { return new Region(range.getOffset(), range.getLength()); }
Example 18
Source File: ASTFragmentFactory.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private static boolean isEmptySelectionCoveredByANode(ISourceRange range, SelectionAnalyzer sa) { return range.getLength() == 0 && sa.getFirstSelectedNode() == null && sa.getLastCoveringNode() != null; }
Example 19
Source File: Util.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
public static int getEndExclusive(ISourceRange sourceRange) { return sourceRange.getOffset() + sourceRange.getLength(); }
Example 20
Source File: JavaReplaceWithEditionActionImpl.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private void performReplace(IMember input, IFile file, ITextFileBuffer textFileBuffer, IDocument document, ITypedElement ti) throws CoreException, JavaModelException, InvocationTargetException, InterruptedException { if (ti instanceof IStreamContentAccessor) { boolean inEditor= beingEdited(file); String content= JavaCompareUtilities.readString((IStreamContentAccessor)ti); String newContent= trimTextBlock(content, TextUtilities.getDefaultLineDelimiter(document), input.getJavaProject()); if (newContent == null) { showError(); return; } ICompilationUnit compilationUnit= input.getCompilationUnit(); CompilationUnit root= parsePartialCompilationUnit(compilationUnit); ISourceRange nameRange= input.getNameRange(); if (nameRange == null) nameRange= input.getSourceRange(); // workaround for bug in getNameRange(): for AnnotationMembers length is negative int length= nameRange.getLength(); if (length < 0) length= 1; ASTNode node2= NodeFinder.perform(root, new SourceRange(nameRange.getOffset(), length)); ASTNode node; if (node2.getNodeType() == ASTNode.INITIALIZER) node= node2; else node= ASTNodes.getParent(node2, BodyDeclaration.class); if (node == null) node= ASTNodes.getParent(node2, AnnotationTypeDeclaration.class); if (node == null) node= ASTNodes.getParent(node2, EnumDeclaration.class); //ASTNode node= getBodyContainer(root, input); if (node == null) { showError(); return; } ASTRewrite rewriter= ASTRewrite.create(root.getAST()); rewriter.replace(node, rewriter.createStringPlaceholder(newContent, node.getNodeType()), null); if (inEditor) { JavaEditor je= getEditor(file); if (je != null) je.setFocus(); } Map<String, String> options= null; IJavaProject javaProject= compilationUnit.getJavaProject(); if (javaProject != null) options= javaProject.getOptions(true); applyChanges(rewriter, document, textFileBuffer, getShell(), inEditor, options); } }