use of org.eclipse.text.edits.ReplaceEdit in project webtools.sourceediting by eclipse.
the class BasicRefactorSearchRequestor method acceptSearchMatch.
/**
* @see org.eclipse.wst.jsdt.core.search.SearchRequestor#acceptSearchMatch(org.eclipse.wst.jsdt.core.search.SearchMatch)
*/
public void acceptSearchMatch(SearchMatch javaMatch) throws CoreException {
String matchDocumentPath = javaMatch.getResource().getFullPath().toString();
SearchDocument searchDoc = JsSearchSupport.getInstance().getSearchDocument(matchDocumentPath);
if (searchDoc != null && searchDoc instanceof JSDTSearchDocumentDelegate) {
String renameText = getRenameText((JSDTSearchDocumentDelegate) searchDoc, javaMatch);
// add it for the correct document
addJavaEdit(searchDoc.getPath(), new ReplaceEdit(javaMatch.getOffset(), javaMatch.getLength(), renameText));
}
}
use of org.eclipse.text.edits.ReplaceEdit in project webtools.sourceediting by eclipse.
the class RenameComponentProcessor method addDeclarationUpdate.
final void addDeclarationUpdate(TextChangeManager manager, IFile file) throws CoreException {
String componentName = selectedComponent.getName();
String componentNamespace = selectedComponent.getNamespaceURI();
QualifiedName elementQName = new QualifiedName(componentNamespace, componentName);
QualifiedName typeQName = selectedComponent.getTypeQName();
SearchScope scope = new WorkspaceSearchScope();
if (file != null) {
scope = new SelectionSearchScope(new IResource[] { file });
}
CollectingSearchRequestor requestor = new CollectingSearchRequestor();
SearchPattern pattern = new XMLComponentDeclarationPattern(file, elementQName, typeQName);
SearchEngine searchEngine = new SearchEngine();
HashMap map = new HashMap();
if (singleFileOnly) {
map.put("searchDirtyContent", Boolean.TRUE);
}
searchEngine.search(pattern, requestor, scope, map, new NullProgressMonitor());
List results = requestor.getResults();
// more than one declaration found, so use offset as additional check
Position offsetPosition = null;
if (results.size() > 1) {
IDOMElement selectedElement = selectedComponent.getElement();
if (selectedElement != null) {
int startOffset = selectedElement.getStartOffset();
offsetPosition = new Position(startOffset, (selectedElement.getEndOffset() - startOffset));
}
}
for (Iterator iter = results.iterator(); iter.hasNext(); ) {
SearchMatch match = (SearchMatch) iter.next();
if (match != null) {
boolean addTextChange = true;
// additional check to verify correct declaration is changed
if (offsetPosition != null) {
addTextChange = offsetPosition.overlapsWith(match.getOffset(), match.getLength());
}
if (addTextChange) {
TextChange textChange = manager.get(match.getFile());
String newName = getNewElementName();
newName = quoteString(newName);
ReplaceEdit replaceEdit = new ReplaceEdit(match.getOffset(), match.getLength(), newName);
String editName = RefactoringMessages.getString("RenameComponentProcessor.Component_Refactoring_update_declatation");
TextChangeCompatibility.addTextEdit(textChange, editName, replaceEdit);
}
}
}
}
use of org.eclipse.text.edits.ReplaceEdit in project webtools.sourceediting by eclipse.
the class DefaultXMLPartitionFormatter method indentIfNotAlreadyIndented.
/**
* Indent if whitespaceRun does not already contain an indent
*
* @param textEdit
* @param indentLevel
* @param indentStartOffset
* @param maxAvailableLineWidth
* @param whitespaceRun
* @return new available line width up to where indented
*/
private int indentIfNotAlreadyIndented(TextEdit textEdit, IStructuredDocumentRegion currentRegion, int indentLevel, int indentStartOffset, String whitespaceRun) {
int maxAvailableLineWidth = getFormattingPreferences().getMaxLineWidth();
int availableLineWidth;
String indentString = getIndentString(indentLevel);
String lineDelimiter = getLineDelimiter(currentRegion);
String newLineAndIndent = lineDelimiter + indentString;
TextEdit indentation = null;
// if not already correctly indented
if (!newLineAndIndent.equals(whitespaceRun)) {
if (getFormattingPreferences().getClearAllBlankLines()) {
if (whitespaceRun != null) {
// replace existing whitespace run
indentation = new ReplaceEdit(indentStartOffset, whitespaceRun.length(), newLineAndIndent);
} else {
// just insert correct indent
indentation = new InsertEdit(indentStartOffset, newLineAndIndent);
}
} else // Keep the empty lines
{
// just insert correct indent
if (whitespaceRun == null)
indentation = new InsertEdit(indentStartOffset, newLineAndIndent);
else // Need to preserve the number of empty lines, but still indent on the current line properly
{
String existingDelimiters = extractLineDelimiters(whitespaceRun, currentRegion);
if (existingDelimiters != null && existingDelimiters.length() > 0) {
String formatted = existingDelimiters + indentString;
// Don't perform a replace if the formatted string is the same as the existing whitespaceRun
if (!formatted.equals(whitespaceRun))
indentation = new ReplaceEdit(indentStartOffset, whitespaceRun.length(), formatted);
} else
// No blank lines to preserve - correct the indent
indentation = new ReplaceEdit(indentStartOffset, whitespaceRun.length(), newLineAndIndent);
}
}
}
if (indentation != null)
textEdit.addChild(indentation);
// update line width
availableLineWidth = maxAvailableLineWidth - indentString.length();
return availableLineWidth;
}
use of org.eclipse.text.edits.ReplaceEdit in project webtools.sourceediting by eclipse.
the class DefaultXMLPartitionFormatter method collapseAndIndent.
private int collapseAndIndent(TextEdit textEdit, int spaceStartOffset, int availableLineWidth, int indentLevel, String whitespaceRun, IStructuredDocumentRegion currentRegion) {
// Need to keep blank lines, but still collapse the whitespace
String lineDelimiters = null;
if (!getFormattingPreferences().getClearAllBlankLines()) {
lineDelimiters = extractLineDelimiters(whitespaceRun, currentRegion);
String formattedLine = lineDelimiters + getIndentString(indentLevel);
if (lineDelimiters.length() > 0 && !formattedLine.equals(whitespaceRun)) {
textEdit.addChild(new ReplaceEdit(spaceStartOffset, whitespaceRun.length(), formattedLine));
availableLineWidth = getFormattingPreferences().getMaxLineWidth() - indentLevel;
}
}
if (lineDelimiters == null || lineDelimiters.length() == 0) {
availableLineWidth = collapseSpaces(textEdit, spaceStartOffset, availableLineWidth, whitespaceRun);
}
return availableLineWidth;
}
use of org.eclipse.text.edits.ReplaceEdit in project webtools.sourceediting by eclipse.
the class DefaultXMLPartitionFormatter method replaceSpaces.
private int replaceSpaces(TextEdit textEdit, int spaceStartOffset, int availableLineWidth, String whitespaceRun) {
StringBuffer buff = new StringBuffer(whitespaceRun);
for (int i = 0; i < buff.length(); i++) {
// $NON-NLS-1$
buff.setCharAt(i, ' ');
}
String replacementString = buff.toString();
if (!replacementString.equals(whitespaceRun)) {
ReplaceEdit replaceEdit = new ReplaceEdit(spaceStartOffset, whitespaceRun.length(), replacementString);
textEdit.addChild(replaceEdit);
}
return availableLineWidth;
}
Aggregations