@Override public String getAdditionalProposalInfo(IProgressMonitor monitor) throws CoreException { StringBuffer buf= new StringBuffer(); TextChange change = getTextChange(); change.setKeepPreviewEdits(true); IDocument previewDocument = change.getPreviewDocument(monitor); TextEdit rootEdit = change.getPreviewEdit(change.getEdit()); EditAnnotator ea = new EditAnnotator(buf, previewDocument); rootEdit.accept(ea); ea.unchangedUntil(previewDocument.getLength()); // Final pre-existing // region return buf.toString(); }
private PreviewAndRegion getPreviewDocument(TextEditBasedChangeGroup[] changes, IProgressMonitor pm) throws CoreException { IDocument document= new Document(getCurrentDocument(pm).get()); boolean trackChanges= getKeepPreviewEdits(); setKeepPreviewEdits(true); TextEditProcessor processor= changes == ALL_EDITS ? createTextEditProcessor(document, TextEdit.NONE, true) : createTextEditProcessor(document, TextEdit.NONE, changes); try { processor.performEdits(); return new PreviewAndRegion(document, getNewRegion(changes)); } catch (BadLocationException e) { throw Changes.asCoreException(e); } finally { setKeepPreviewEdits(trackChanges); } }
/** * Creates a preview of the content of the compilation unit after applying the change. * * @return the preview of the changed compilation unit * @throws CoreException if the creation of the change failed * * @noreference This method is not intended to be referenced by clients. */ public String getPreviewContent() throws CoreException { return getTextChange().getPreviewContent(new NullProgressMonitor()); }
public static void addTextEdit(TextChange change, String name, TextEdit edit) { Assert.isNotNull(change); Assert.isNotNull(name); Assert.isNotNull(edit); TextEdit root= change.getEdit(); if (root == null) { root= new MultiTextEdit(); change.setEdit(root); } insert(root, edit); change.addTextEditGroup(new TextEditGroup(name, edit)); }
/** * Returns the <code>TextChange</code> associated with the given file. * If the manager does not already manage an association it creates a one. * * @param file the file for which the text buffer change is requested * @return the text change associated with the given file. */ public TextChange get(IFile file) { TextChange result= (TextChange)fMap.get(file); if (result == null) { result= new TextFileChange(file.toString(), file); result.setKeepPreviewEdits(fKeepExecutedTextEdits); result.initializeValidationData(new NullProgressMonitor()); fMap.put(file, result); } return result; }
public static Change removeFields(ICompilationUnit cu, List<String> fields) throws CoreException { AccessorClassModifier sourceModification= new AccessorClassModifier(cu); String message= Messages.format(NLSMessages.AccessorClassModifier_remove_fields_from_accessor, BasicElementLabels.getFileName(cu)); TextChange change= new CompilationUnitChange(message, cu); MultiTextEdit multiTextEdit= new MultiTextEdit(); change.setEdit(multiTextEdit); for (int i= 0; i < fields.size(); i++) { String field= fields.get(i); NLSSubstitution substitution= new NLSSubstitution(NLSSubstitution.EXTERNALIZED, field, null, null, null); sourceModification.removeKey(substitution, change); } if (change.getChangeGroups().length == 0) return null; change.addEdit(sourceModification.getTextEdit()); return change; }
public static void addTextEdit(TextChange change, String name, TextEdit edit, GroupCategorySet groupCategories) throws MalformedTreeException { Assert.isNotNull(change); Assert.isNotNull(name); Assert.isNotNull(edit); TextEdit root= change.getEdit(); if (root == null) { root= new MultiTextEdit(); change.setEdit(root); } insert(root, edit); change.addTextEditChangeGroup(new TextEditChangeGroup( change, new CategorizedTextEditGroup(name, edit, groupCategories))); }
IRegion currentRegion= getRegion(changeGroups); Assert.isTrue(region.getOffset() <= currentRegion.getOffset() && currentRegion.getOffset() + currentRegion.getLength() <= region.getOffset() + region.getLength()); TextEdit root= getEdit(); Assert.isNotNull(root, "No root edit"); //$NON-NLS-1$ for (int c= 0; c < changeGroups.length; c++) { PreviewAndRegion result= getPreviewDocument(changeGroups, pm); int delta; if (result.region == null) { // all edits were delete edits so no new region delta= result.region.getLength() - currentRegion.getLength(); return getContent(result.document, new Region(region.getOffset(), region.getLength() + delta), expandRegionToFullLine, surroundingLines);
private void createEdits(ICompilationUnit unit, ASTRewrite rewriter, List<TextEditGroup> groups, ImportRewrite importRewrite) throws CoreException { TextChange change= fChangeManager.get(unit); MultiTextEdit root= new MultiTextEdit(); change.setEdit(root); root.addChild(importRewrite.rewriteImports(null)); root.addChild(rewriter.rewriteAST()); for (Iterator<TextEditGroup> iter= groups.iterator(); iter.hasNext();) { change.addTextEditGroup(iter.next()); } }
/** * Adds a new text change to this composite change. * <p> * The text change which is added is not changed in any way. Rather * the contents of the text change are retrieved and stored internally * in this composite text change. * </p> * * @param change * the text change to add */ public final void addChange(final TextChange change) { Assert.isNotNull(change); final ComposableBufferChange result= new ComposableBufferChange(); result.setEdit(change.getEdit()); final TextEditBasedChangeGroup[] groups= change.getChangeGroups(); final List list= new ArrayList(groups.length); for (int index= 0; index < groups.length; index++) { final TextEditBasedChangeGroup group= new ComposableBufferChangeGroup(this, groups[index].getTextEditGroup()); list.add(group); addChangeGroup(group); } result.setGroups(list); fChanges.add(result); }
/** * Returns the <code>TextChange</code> associated with the given compilation unit. * If the manager does not already manage an association it creates a one. * * @param cu the compilation unit for which the text buffer change is requested * @return the text change associated with the given compilation unit. */ public TextChange get(ICompilationUnit cu) { TextChange result= (TextChange)fMap.get(cu); if (result == null) { result= new CompilationUnitChange(cu.getElementName(), cu); result.setKeepPreviewEdits(fKeepExecutedTextEdits); fMap.put(cu, result); } return result; }
/** * Creates the text change for this proposal. * This method is only called once and only when no text change has been passed in * {see #CUCorrectionProposal(String, ICompilationUnit, TextChange, int, Image)}. * * @return the created text change * @throws CoreException if the creation of the text change failed */ public TextChange createTextChange() throws CoreException { TextChange change = getNewChange(); // initialize text change IDocument document= change.getCurrentDocument(new NullProgressMonitor()); addEdits(document, change.getEdit()); return change; }
fChange.setEdit(rootEdit); fChange.setKeepPreviewEdits(true); fChange.addTextEditGroup(new TextEditGroup(RefactoringCoreMessages.RenameTempRefactoring_changeName, allRenameEdits[i]));
/** * {@inheritDoc} */ public String getCurrentContent(IProgressMonitor pm) throws CoreException { return getCurrentDocument(pm).get(); }
/** * {@inheritDoc} */ public String getCurrentContent(IRegion region, boolean expandRegionToFullLine, int surroundingLines, IProgressMonitor pm) throws CoreException { Assert.isNotNull(region); Assert.isTrue(surroundingLines >= 0); IDocument document= getCurrentDocument(pm); Assert.isTrue(document.getLength() >= region.getOffset() + region.getLength()); return getContent(document, region, expandRegionToFullLine, surroundingLines); }
/** * Returns a document containing a preview of the text change. The * preview is computed by executing the all managed text edits. The * method considers the active state of the added {@link TextEditChangeGroup * text edit change groups}. * * @param pm a progress monitor to report progress or <code>null</code> * if no progress reporting is desired * @return a document containing the preview of the text change * * @throws CoreException if the preview can't be created */ public IDocument getPreviewDocument(IProgressMonitor pm) throws CoreException { PreviewAndRegion result= getPreviewDocument(ALL_EDITS, pm); return result.document; }
private static void convertTextChange(WorkspaceEdit root, IJavaElement element, TextChange textChange) { TextEdit textEdits = textChange.getEdit(); if (textEdits == null) { return; } ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT); convertTextEdit(root, compilationUnit, textEdits); }
private void addKey(NLSSubstitution sub, TextChange change) throws CoreException { String name= Messages.format(NLSMessages.AccessorClassModifier_add_entry, sub.getKey()); TextEditGroup editGroup= new TextEditGroup(name); change.addTextEditGroup(editGroup); addKey(sub, change, editGroup); }
public static Change removeKeys(IPath propertyFilePath, List<String> keys) throws CoreException { String name= Messages.format(NLSMessages.NLSPropertyFileModifier_remove_from_property_file, BasicElementLabels.getPathLabel(propertyFilePath, false)); TextChange textChange= new TextFileChange(name, getPropertyFile(propertyFilePath)); textChange.setTextType("properties"); //$NON-NLS-1$ PropertyFileDocumentModel model= new PropertyFileDocumentModel(textChange.getCurrentDocument(new NullProgressMonitor())); for (Iterator<String> iterator= keys.iterator(); iterator.hasNext();) { String key= iterator.next(); TextEdit edit= model.remove(key); if (edit != null) { TextChangeCompatibility.addTextEdit(textChange, Messages.format(NLSMessages.NLSPropertyFileModifier_remove_entry, BasicElementLabels.getJavaElementName(key)), edit); } } return textChange; }
private TextEditProcessor createTextEditProcessor(IDocument document, int flags, boolean preview) { if (fEdit == null) return new TextEditProcessor(document, new MultiTextEdit(0,0), flags); List<TextEdit> excludes= new ArrayList<>(0); TextEditBasedChangeGroup[] groups= getChangeGroups(); for (int index= 0; index < groups.length; index++) { TextEditBasedChangeGroup edit= groups[index]; if (!edit.isEnabled()) { excludes.addAll(Arrays.asList(edit.getTextEditGroup().getTextEdits())); } } if (preview) { fCopier= new TextEditCopier(fEdit); TextEdit copiedEdit= fCopier.perform(); boolean keep= getKeepPreviewEdits(); if (keep) flags= flags | TextEdit.UPDATE_REGIONS; LocalTextEditProcessor result= new LocalTextEditProcessor(document, copiedEdit, flags); result.setExcludes(mapEdits( excludes.toArray(new TextEdit[excludes.size()]), fCopier)); if (!keep) fCopier= null; return result; } else { LocalTextEditProcessor result= new LocalTextEditProcessor(document, fEdit, flags | TextEdit.UPDATE_REGIONS); result.setExcludes(excludes.toArray(new TextEdit[excludes.size()])); return result; } }