Class TextChange
- java.lang.Object
-
- org.eclipse.ltk.core.refactoring.Change
-
- org.eclipse.ltk.core.refactoring.TextEditBasedChange
-
- org.eclipse.ltk.core.refactoring.TextChange
-
- All Implemented Interfaces:
IAdaptable
- Direct Known Subclasses:
DocumentChange,TextFileChange
public abstract class TextChange extends TextEditBasedChange
A text change is a special change object that applies atext edit treeto a document. The text change manages the text edit tree. Access to the document must be provided by concrete subclasses via the methodaquireDocument,commitDocument, andreleaseDocument.A text change offers the ability to access the original content of the document as well as creating a preview of the change. The edit tree gets copied when creating any kind of preview. Therefore no region updating on the original edit tree takes place when requesting a preview (for more information on region updating see class
TextEdit. If region tracking is required for a preview, it can be enabled via a call to the methodsetKeepPreviewEdits. If enabled, the text change keeps the copied edit tree executed for the preview, allowing clients to map an original edit to an executed edit. The executed edit can then be used to determine its position in the preview.Note: this class is not intended to be subclassed outside the refactoring framework.
- Since:
- 3.0
- Restriction:
- This class is not intended to be subclassed by clients.
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedTextChange(String name)Creates a new text change with the specified name.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract IDocumentacquireDocument(IProgressMonitor pm)Acquires a reference to the document to be changed by this text change.voidaddEdit(TextEdit edit)Adds the given edit to the edit tree.voidaddTextEditChangeGroup(TextEditChangeGroup group)Adds atext edit change group.voidaddTextEditGroup(TextEditGroup group)Adds atext edit group.protected abstract voidcommit(IDocument document, IProgressMonitor pm)Commits the document acquired via a call toaquireDocument.protected abstract ChangecreateUndoChange(UndoEdit edit)Hook to create an undo change for the given undo edit.StringgetCurrentContent(IProgressMonitor pm)Returns the current content of the document this text change is associated with.StringgetCurrentContent(IRegion region, boolean expandRegionToFullLine, int surroundingLines, IProgressMonitor pm)Returns the current content of the text edit change clipped to a specific region.IDocumentgetCurrentDocument(IProgressMonitor pm)Returns the document this text change is associated to.TextEditgetEdit()Returns the root text edit.StringgetPreviewContent(IProgressMonitor pm)Returns the preview content as a string.StringgetPreviewContent(TextEditBasedChangeGroup[] changeGroups, IRegion region, boolean expandRegionToFullLine, int surroundingLines, IProgressMonitor pm)Returns a preview of the text change clipped to a specific region.StringgetPreviewContent(TextEditChangeGroup[] changeGroups, IRegion region, boolean expandRegionToFullLine, int surroundingLines, IProgressMonitor pm)Returns a preview of the text change clipped to a specific region.IDocumentgetPreviewDocument(IProgressMonitor pm)Returns a document containing a preview of the text change.TextEditgetPreviewEdit(TextEdit original)Returns the edit that got executed during preview generation instead of the given original.TextEdit[]getPreviewEdits(TextEdit[] originals)Returns the edits that were executed during preview generation instead of the given array of original edits.TextEditChangeGroup[]getTextEditChangeGroups()Returns thetext edit change groupsmanaged by this text change.Changeperform(IProgressMonitor pm)Performs this change.protected UndoEditperformEdits(IDocument document)Executes the text edits on the given document.protected abstract voidreleaseDocument(IDocument document, IProgressMonitor pm)Releases the document acquired via a call toaquireDocument.voidsetEdit(TextEdit edit)Sets the root text edit that should be applied to the document represented by this text change.voidsetKeepPreviewEdits(boolean keep)Controls whether the text edit change should keep executed edits during preview generation.-
Methods inherited from class org.eclipse.ltk.core.refactoring.TextEditBasedChange
addChangeGroup, getChangeGroups, getKeepPreviewEdits, getName, getTextType, hasOneGroupCategory, setEnabled, setTextType
-
Methods inherited from class org.eclipse.ltk.core.refactoring.Change
dispose, getAdapter, getAffectedObjects, getDescriptor, getModifiedElement, getParent, initializeValidationData, isEnabled, isValid, setEnabledShallow
-
-
-
-
Constructor Detail
-
TextChange
protected TextChange(String name)
Creates a new text change with the specified name. The name is a human-readable value that is displayed to users. The name does not need to be unique, but it must not benull.The text type of this text change is set to
txt.- Parameters:
name- the name of the text change- See Also:
TextEditBasedChange.setTextType(String)
-
-
Method Detail
-
setEdit
public void setEdit(TextEdit edit)
Sets the root text edit that should be applied to the document represented by this text change.- Parameters:
edit- the root text edit. The root text edit can only be set once.
-
getEdit
public TextEdit getEdit()
Returns the root text edit.- Returns:
- the root text edit or
nullif no root edit has been set
-
addTextEditGroup
public void addTextEditGroup(TextEditGroup group)
Adds atext edit group. This method is a convenience method for callingchange.addTextEditChangeGroup(new TextEditChangeGroup(change, group));.- Overrides:
addTextEditGroupin classTextEditBasedChange- Parameters:
group- the text edit group to add
-
addTextEditChangeGroup
public void addTextEditChangeGroup(TextEditChangeGroup group)
Adds atext edit change group. Calling this method requires that a root edit has been set via the methodsetEdit. The edits managed by the given text edit change group must be part of the change's root edit.- Parameters:
group- the text edit change group to add
-
getTextEditChangeGroups
public TextEditChangeGroup[] getTextEditChangeGroups()
Returns thetext edit change groupsmanaged by this text change.- Returns:
- the text edit change groups
-
addEdit
public void addEdit(TextEdit edit) throws MalformedTreeException
Adds the given edit to the edit tree. The edit is added as a top level edit.- Parameters:
edit- the text edit to add- Throws:
MalformedTreeException- if the edit can't be added. Reason is that is overlaps with an already existing edit- Since:
- 3.1
-
acquireDocument
protected abstract IDocument acquireDocument(IProgressMonitor pm) throws CoreException
Acquires a reference to the document to be changed by this text change. A document acquired by this call MUST be released via a call toreleaseDocument(IDocument, IProgressMonitor).The method
releaseDocumentmust be called as many times asaquireDocumenthas been called.- Parameters:
pm- a progress monitor- Returns:
- a reference to the document to be changed
- Throws:
CoreException- if the document can't be acquired
-
commit
protected abstract void commit(IDocument document, IProgressMonitor pm) throws CoreException
Commits the document acquired via a call toaquireDocument. It is up to the implementors of this method to decide what committing a document means. Typically, the content of the document is written back to the file system.- Parameters:
document- the document to commitpm- a progress monitor- Throws:
CoreException- if the document can't be committed
-
releaseDocument
protected abstract void releaseDocument(IDocument document, IProgressMonitor pm) throws CoreException
Releases the document acquired via a call toaquireDocument.- Parameters:
document- the document to releasepm- a progress monitor- Throws:
CoreException- if the document can't be released
-
createUndoChange
protected abstract Change createUndoChange(UndoEdit edit)
Hook to create an undo change for the given undo edit. This hook gets called while performing the change to construct the corresponding undo change object.- Parameters:
edit- theUndoEditto create an undo change for- Returns:
- the undo change or
nullif no undo change can be created. Returningnullresults in the fact that the whole change tree can't be undone. So returningnullis only recommended if an exception occurred during the creation of the undo change.
-
perform
public Change perform(IProgressMonitor pm) throws CoreException
Description copied from class:ChangePerforms this change. If this method is called on an invalid or disabled change object the result is unspecified. Changes should in general not respond toIProgressMonitor.isCanceled()since canceling a change tree in the middle of its execution leaves the workspace in a half changed state.- Specified by:
performin classChange- Parameters:
pm- a progress monitor- Returns:
- the undo change for this change object or
nullif no undo is provided - Throws:
CoreException- if an error occurred during change execution
-
performEdits
protected UndoEdit performEdits(IDocument document) throws BadLocationException, MalformedTreeException
Executes the text edits on the given document. Subclasses that override this method should callsuper.performEdits(document).- Parameters:
document- the document- Returns:
- an object representing the undo of the executed edits
- Throws:
MalformedTreeException- is thrown if the edit tree isn't in a valid state. This exception is thrown before any edit is executed. So the document is still in its original state.BadLocationException- is thrown if one of the edits in the tree can't be executed. The state of the document is undefined if this exception is thrown.- Since:
- 3.5
-
getCurrentDocument
public IDocument getCurrentDocument(IProgressMonitor pm) throws CoreException
Returns the document this text change is associated to. The document returned is computed at the point in time when this method is called. So calling this method multiple times may return different document instances.The returned document must not be modified by the client. Doing so will result in an unexpected behavior when the change is performed.
- Parameters:
pm- a progress monitor to report progress ornullif no progress reporting is desired- Returns:
- the document this change is working on
- Throws:
CoreException- if the document can't be acquired
-
getCurrentContent
public String getCurrentContent(IProgressMonitor pm) throws CoreException
Description copied from class:TextEditBasedChangeReturns the current content of the document this text change is associated with.- Specified by:
getCurrentContentin classTextEditBasedChange- Parameters:
pm- a progress monitor to report progress ornullif no progress reporting is desired- Returns:
- the current content of the text edit change
- Throws:
CoreException- if the content can't be accessed
-
getCurrentContent
public String getCurrentContent(IRegion region, boolean expandRegionToFullLine, int surroundingLines, IProgressMonitor pm) throws CoreException
Description copied from class:TextEditBasedChangeReturns the current content of the text edit change clipped to a specific region. The region is determined as follows:- if
expandRegionToFullLineisfalsethen the parameterregiondetermines the clipping. - if
expandRegionToFullLineistruethen the region determined by the parameterregionis extended to cover full lines. - if
surroundingLines> 0 then the given number of surrounding lines is added. The value ofsurroundingLinesis only considered ifexpandRegionToFullLineistrue
- Specified by:
getCurrentContentin classTextEditBasedChange- Parameters:
region- the starting region for the text to be returnedexpandRegionToFullLine- iftrueis passed the region is extended to cover full linessurroundingLines- the number of surrounding lines to be added to the clipping region. Is only considered ifexpandRegionToFullLineistruepm- a progress monitor to report progress ornullif no progress reporting is desired- Returns:
- the current content of the text edit change clipped to a region determined by the given parameters.
- Throws:
CoreException- if an exception occurs while accessing the current content
- if
-
getPreviewEdit
public TextEdit getPreviewEdit(TextEdit original)
Returns the edit that got executed during preview generation instead of the given original. The method requires thatsetKeepPreviewEditsis set totrueand that a preview has been requested via one of thegetPreview*methods.The method returns
nullif the original isn't managed by this text change.- Parameters:
original- the original edit managed by this text change- Returns:
- the edit executed during preview generation
-
getPreviewEdits
public TextEdit[] getPreviewEdits(TextEdit[] originals)
Returns the edits that were executed during preview generation instead of the given array of original edits. The method requires thatsetKeepPreviewEditsis set totrueand that a preview has been requested via one of thegetPreview*methods.The method returns an empty array if none of the original edits is managed by this text change.
- Parameters:
originals- an array of original edits managed by this text change- Returns:
- an array of edits containing the corresponding edits executed during preview generation
-
getPreviewDocument
public IDocument getPreviewDocument(IProgressMonitor pm) throws CoreException
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 addedtext edit change groups.- Parameters:
pm- a progress monitor to report progress ornullif no progress reporting is desired- Returns:
- a document containing the preview of the text change
- Throws:
CoreException- if the preview can't be created
-
getPreviewContent
public String getPreviewContent(IProgressMonitor pm) throws CoreException
Description copied from class:TextEditBasedChangeReturns the preview content as a string.- Specified by:
getPreviewContentin classTextEditBasedChange- Parameters:
pm- a progress monitor to report progress ornullif no progress reporting is desired- Returns:
- the preview
- Throws:
CoreException- if the preview can't be created
-
getPreviewContent
public String getPreviewContent(TextEditChangeGroup[] changeGroups, IRegion region, boolean expandRegionToFullLine, int surroundingLines, IProgressMonitor pm) throws CoreException
Returns a preview of the text change clipped to a specific region. The preview is created by applying the text edits managed by the given array oftext edit change groups. The region is determined as follows:- if
expandRegionToFullLineisfalsethen the parameterregiondetermines the clipping. - if
expandRegionToFullLineistruethen the region determined by the parameterregionis extended to cover full lines. - if
surroundingLines> 0 then the given number of surrounding lines is added. The value ofsurroundingLinesis only considered ifexpandRegionToFullLineistrue
- Parameters:
changeGroups- a set of change groups for which a preview is to be generatedregion- the starting region for the clippingexpandRegionToFullLine- iftrueis passed the region is extended to cover full linessurroundingLines- the number of surrounding lines to be added to the clipping region. Is only considered ifexpandRegionToFullLineistruepm- a progress monitor to report progress ornullif no progress reporting is desired- Returns:
- the current content of the text change clipped to a region determined by the given parameters.
- Throws:
CoreException- if an exception occurs while generating the preview- See Also:
getCurrentContent(IRegion, boolean, int, IProgressMonitor)
- if
-
getPreviewContent
public String getPreviewContent(TextEditBasedChangeGroup[] changeGroups, IRegion region, boolean expandRegionToFullLine, int surroundingLines, IProgressMonitor pm) throws CoreException
Returns a preview of the text change clipped to a specific region. The preview is created by applying the text edits managed by the given array oftext edit change groups. The region is determined as follows:- if
expandRegionToFullLineisfalsethen the parameterregiondetermines the clipping. - if
expandRegionToFullLineistruethen the region determined by the parameterregionis extended to cover full lines. - if
surroundingLines> 0 then the given number of surrounding lines is added. The value ofsurroundingLinesis only considered ifexpandRegionToFullLineistrue
- Specified by:
getPreviewContentin classTextEditBasedChange- Parameters:
changeGroups- a set of change groups for which a preview is to be generatedregion- the starting region for the clippingexpandRegionToFullLine- iftrueis passed the region is extended to cover full linessurroundingLines- the number of surrounding lines to be added to the clipping region. Is only considered ifexpandRegionToFullLineistruepm- a progress monitor to report progress ornullif no progress reporting is desired- Returns:
- the current content of the text change clipped to a region determined by the given parameters.
- Throws:
CoreException- if an exception occurs while generating the preview- Since:
- 3.2
- See Also:
getCurrentContent(IRegion, boolean, int, IProgressMonitor)
- if
-
setKeepPreviewEdits
public void setKeepPreviewEdits(boolean keep)
Description copied from class:TextEditBasedChangeControls whether the text edit change should keep executed edits during preview generation.- Overrides:
setKeepPreviewEditsin classTextEditBasedChange- Parameters:
keep- iftrueexecuted preview edits are kept
-
-