@Override public int getOffset() { int minStart= Integer.MAX_VALUE; for (ITrackedNodePosition node : fPos) { minStart= Math.min(minStart, node.getStartPosition()); } return minStart == Integer.MAX_VALUE ? -1 : minStart; }
@Override public int getLength() { int minStart= Integer.MAX_VALUE; int maxEnd= 0; for (ITrackedNodePosition node : fPos) { minStart= Math.min(minStart, node.getStartPosition()); maxEnd= Math.max(maxEnd, node.getStartPosition() + node.getLength()); } return minStart == Integer.MAX_VALUE ? 0 : maxEnd - getOffset(); }
@Override public int getOffset() { int minStart = Integer.MAX_VALUE; for (ITrackedNodePosition node : fPos) { minStart = Math.min(minStart, node.getStartPosition()); } return minStart == Integer.MAX_VALUE ? -1 : minStart; }
@Override public int getLength() { int minStart = Integer.MAX_VALUE; int maxEnd = 0; for (ITrackedNodePosition node : fPos) { minStart = Math.min(minStart, node.getStartPosition()); maxEnd = Math.max(maxEnd, node.getStartPosition() + node.getLength()); } return minStart == Integer.MAX_VALUE ? 0 : maxEnd - getOffset(); }
protected static BodyDeclaration createPlaceholderForProtectedTypeDeclaration(final BodyDeclaration bodyDeclaration, final CompilationUnit declaringCuNode, final ICompilationUnit declaringCu, final ASTRewrite rewrite, final boolean removeIndentation) throws JavaModelException { String text= null; try { final ASTRewrite rewriter= ASTRewrite.create(bodyDeclaration.getAST()); ModifierRewrite.create(rewriter, bodyDeclaration).setVisibility(Modifier.PROTECTED, null); final ITrackedNodePosition position= rewriter.track(bodyDeclaration); final IDocument document= new Document(declaringCu.getBuffer().getText(declaringCuNode.getStartPosition(), declaringCuNode.getLength())); rewriter.rewriteAST(document, declaringCu.getJavaProject().getOptions(true)).apply(document, TextEdit.UPDATE_REGIONS); text= document.get(position.getStartPosition(), position.getLength()); } catch (BadLocationException exception) { text= getNewText(bodyDeclaration, declaringCu, removeIndentation); } return (BodyDeclaration) rewrite.createStringPlaceholder(text, ASTNode.TYPE_DECLARATION); }
public String updateReplacementString(IDocument document, int offset, ImportRewrite importRewrite, boolean snippetStringSupport) throws CoreException, BadLocationException { Document recoveredDocument= new Document(); CompilationUnit unit= getRecoveredAST(document, offset, recoveredDocument); ImportRewriteContext context = new ContextSensitiveImportRewriteContext(unit, offset, importRewrite); ITypeBinding declaringType= null; ChildListPropertyDescriptor descriptor= null; ASTNode node= NodeFinder.perform(unit, offset, 1); node= ASTResolving.findParentType(node); String result = null; if (node instanceof AnonymousClassDeclaration) { declaringType= ((AnonymousClassDeclaration) node).resolveBinding(); descriptor= AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY; } else if (node instanceof AbstractTypeDeclaration) { AbstractTypeDeclaration declaration= (AbstractTypeDeclaration) node; descriptor= declaration.getBodyDeclarationsProperty(); declaringType= declaration.resolveBinding(); } if (declaringType != null) { ASTRewrite rewrite= ASTRewrite.create(unit.getAST()); IMethodBinding methodToOverride= Bindings.findMethodInHierarchy(declaringType, fMethodName, fParamTypes); if (methodToOverride == null && declaringType.isInterface()) { methodToOverride= Bindings.findMethodInType(node.getAST().resolveWellKnownType("java.lang.Object"), fMethodName, fParamTypes); //$NON-NLS-1$ } if (methodToOverride != null) { CodeGenerationSettings settings = PreferenceManager.getCodeGenerationSettings(fJavaProject.getProject()); MethodDeclaration stub = StubUtility2.createImplementationStub(fCompilationUnit, rewrite, importRewrite, context, methodToOverride, declaringType, settings, declaringType.isInterface(), declaringType, snippetStringSupport); ListRewrite rewriter= rewrite.getListRewrite(node, descriptor); rewriter.insertFirst(stub, null); ITrackedNodePosition position= rewrite.track(stub); try { Map<String, String> options = fJavaProject.getOptions(true); rewrite.rewriteAST(recoveredDocument, options).apply(recoveredDocument); String generatedCode = recoveredDocument.get(position.getStartPosition(), position.getLength()); String indentAt = getIndentAt(recoveredDocument, position.getStartPosition(), settings); int generatedIndent = IndentManipulation.measureIndentUnits(indentAt, settings.tabWidth, settings.indentWidth); // Kinda fishy but empirical data shows Override needs to change indent by at // least 1 generatedIndent = Math.max(1, generatedIndent); // Cancel generated code indent String delimiter = TextUtilities.getDefaultLineDelimiter(document); result = IndentManipulation.changeIndent(generatedCode, generatedIndent, settings.tabWidth, settings.indentWidth, "", delimiter); } catch (MalformedTreeException | BadLocationException exception) { JavaLanguageServerPlugin.logException("Unable to compute override proposal", exception); } } } return result; }
public void setEndPosition(ITrackedNodePosition position) { setEndPosition(LinkedProposalPositionGroup.createPositionInformation(position, 1)); }
public static PositionInformation createPositionInformation(ITrackedNodePosition pos, int sequenceRank) { return new TrackedNodePosition(pos, sequenceRank); }
public TrackedNodePosition(ITrackedNodePosition pos, int sequenceRank) { fPos= pos; fSequenceRank= sequenceRank; }
public void addPosition(ITrackedNodePosition position, int sequenceRank) { addPosition(createPositionInformation(position, sequenceRank)); }
public void addPosition(ITrackedNodePosition position, boolean isFirst) { addPosition(position, isFirst ? 0 : 1); }
public static PositionInformation createPositionInformation( ITrackedNodePosition pos, int sequenceRank) { return new TrackedNodePosition(pos, sequenceRank); }
public TrackedNodePosition(ITrackedNodePosition pos, int sequenceRank) { fPos = pos; fSequenceRank = sequenceRank; }
private String getUpdatedMember(IDocument document, BodyDeclaration declaration) throws BadLocationException { ITrackedNodePosition trackedPosition= (ITrackedNodePosition) declaration.getProperty(TRACKED_POSITION_PROPERTY); return Strings.trimIndentation(document.get(trackedPosition.getStartPosition(), trackedPosition.getLength()), fPreferences.tabWidth, fPreferences.indentWidth, false); }
/** * A position that contains all of the given tracked node positions. * * @param pos the positions */ public TrackedNodesPosition(Collection<ITrackedNodePosition> pos) { fPos= pos; }
/** * A position for the start of the given tracked node position. * * @param pos the position */ public StartPositionInformation(ITrackedNodePosition pos) { fPos= pos; }
/** * Adds a linked position to be shown when the proposal is applied. All * positions with the same group id are linked. * * @param position * The position to add. * @param isFirst * If set, the proposal is jumped to first. * @param groupID * The id of the group the proposal belongs to. All proposals in * the same group are linked. */ public void addLinkedPosition(ITrackedNodePosition position, boolean isFirst, String groupID) { getLinkedProposalModel().getPositionGroup(groupID, true).addPosition(position, isFirst); }
/** * Adds a linked position to be shown when the proposal is applied. All * positions with the same group id are linked. * * @param position * The position to add. * @param sequenceRank * The sequence rank, see TODO. * @param groupID * The id of the group the proposal belongs to. All proposals in * the same group are linked. */ public void addLinkedPosition(ITrackedNodePosition position, int sequenceRank, String groupID) { getLinkedProposalModel().getPositionGroup(groupID, true).addPosition(position, sequenceRank); }
/** * Sets the end position of the linked mode to the end of the passed range. * * @param position * The position that describes the end position of the linked * mode. */ public void setEndPosition(ITrackedNodePosition position) { getLinkedProposalModel().setEndPosition(position); }
/** * A position that contains all of the given tracked node positions. * * @param pos the positions */ public TrackedNodesPosition(Collection<ITrackedNodePosition> pos) { fPos = pos; }
/** * A position for the start of the given tracked node position. * * @param pos the position */ public StartPositionInformation(ITrackedNodePosition pos) { fPos = pos; }