use of org.eclipse.text.edits.ReplaceEdit in project webtools.sourceediting by eclipse.
the class BasicRefactorSearchRequestor method acceptSearchMatch.
/**
* @see org.eclipse.jdt.core.search.SearchRequestor#acceptSearchMatch(org.eclipse.jdt.core.search.SearchMatch)
*/
public void acceptSearchMatch(SearchMatch javaMatch) throws CoreException {
String matchDocumentPath = javaMatch.getResource().getFullPath().toString();
SearchDocument searchDoc = JSPSearchSupport.getInstance().getSearchDocument(matchDocumentPath);
if (searchDoc != null && searchDoc instanceof JavaSearchDocumentDelegate) {
String renameText = getRenameText((JavaSearchDocumentDelegate) searchDoc, javaMatch);
// if rename text is null then don't create an edit for it
if (renameText != null) {
// 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 ModelManagerImpl method convertLineDelimiters.
/*
* Note: This method appears in both ModelManagerImpl and JSEditor (with
* just a minor difference). They should be kept the same.
*
* @deprecated - handled by platform
*/
private void convertLineDelimiters(IDocument document, IFile iFile) throws CoreException {
// Note: calculateType(iFile) returns a default xml model handler if
// content type is null.
String contentTypeId = calculateType(iFile).getAssociatedContentTypeId();
String endOfLineCode = ContentBasedPreferenceGateway.getPreferencesString(contentTypeId, CommonEncodingPreferenceNames.END_OF_LINE_CODE);
// endOfLineCode == "" means no translation
if (endOfLineCode != null && endOfLineCode.length() > 0) {
// $NON-NLS-1$
String lineDelimiterToUse = System.getProperty("line.separator");
if (endOfLineCode.equals(CommonEncodingPreferenceNames.CR))
lineDelimiterToUse = CommonEncodingPreferenceNames.STRING_CR;
else if (endOfLineCode.equals(CommonEncodingPreferenceNames.LF))
lineDelimiterToUse = CommonEncodingPreferenceNames.STRING_LF;
else if (endOfLineCode.equals(CommonEncodingPreferenceNames.CRLF))
lineDelimiterToUse = CommonEncodingPreferenceNames.STRING_CRLF;
TextEdit multiTextEdit = new MultiTextEdit();
int lineCount = document.getNumberOfLines();
try {
for (int i = 0; i < lineCount; i++) {
IRegion lineInfo = document.getLineInformation(i);
int lineStartOffset = lineInfo.getOffset();
int lineLength = lineInfo.getLength();
int lineEndOffset = lineStartOffset + lineLength;
if (i < lineCount - 1) {
String currentLineDelimiter = document.getLineDelimiter(i);
if (currentLineDelimiter != null && currentLineDelimiter.compareTo(lineDelimiterToUse) != 0)
multiTextEdit.addChild(new ReplaceEdit(lineEndOffset, currentLineDelimiter.length(), lineDelimiterToUse));
}
}
if (multiTextEdit.getChildrenSize() > 0)
multiTextEdit.apply(document);
} catch (BadLocationException exception) {
// deleted.
throw new RuntimeException(exception.getMessage());
}
}
}
use of org.eclipse.text.edits.ReplaceEdit in project webtools.sourceediting by eclipse.
the class JSPTranslationExtension method getJspEdit.
/**
* Returns a corresponding TextEdit for the JSP file given a TextEdit for
* a Java file.
*
* @param javaEdit
* @return the corresponding JSP edits (not applied to the document yet)
*/
public TextEdit getJspEdit(TextEdit javaEdit) {
if (javaEdit == null)
return null;
List jspEdits = new ArrayList();
int offset = javaEdit.getOffset();
int length = javaEdit.getLength();
if (javaEdit instanceof MultiTextEdit && javaEdit.getChildren().length > 0) {
IRegion r = TextEdit.getCoverage(getAllEdits(javaEdit));
offset = r.getOffset();
length = r.getLength();
}
// get java ranges that will be affected by the edit
Position[] javaPositions = getJavaRanges(offset, length);
// record position data before the change
Position[] jspPositions = new Position[javaPositions.length];
PositionDelta[] deltas = new PositionDelta[javaPositions.length];
for (int i = 0; i < javaPositions.length; i++) {
deltas[i] = new PositionDelta(javaPositions[i].offset, javaPositions[i].length);
// mapping from java <-> jsp (eg. an import statement)
if (!isIndirect(javaPositions[i].offset))
jspPositions[i] = (Position) getJava2JspMap().get(javaPositions[i]);
}
if (DEBUG) {
// $NON-NLS-1$
System.out.println("================================================");
// $NON-NLS-1$
System.out.println("deltas:");
String javaText = getJavaText();
for (int i = 0; i < deltas.length; i++) // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
System.out.println("pos[" + deltas[i].preOffset + ":" + deltas[i].preLength + "]" + javaText.substring(deltas[i].preOffset, deltas[i].preOffset + deltas[i].preLength));
// $NON-NLS-1$
System.out.println("===============================================");
}
UndoEdit undo = null;
// apply the edit to the java document
try {
undo = javaEdit.apply(getJavaDocument());
} catch (MalformedTreeException e) {
Logger.logException(e);
} catch (BadLocationException e) {
Logger.logException(e);
}
// now at this point Java positions are unreliable since they were
// updated after applying java edit.
String newJavaText = getJavaDocument().get();
if (DEBUG)
// $NON-NLS-1$
System.out.println("java post format text:\n" + newJavaText);
// record post edit data
for (int i = 0; i < javaPositions.length; i++) deltas[i].setPostEditData(javaPositions[i].offset, javaPositions[i].length, javaPositions[i].isDeleted);
// create appropriate text edits for deltas
Position jspPos = null;
// $NON-NLS-1$
String replaceText = "";
for (int i = 0; i < deltas.length; i++) {
jspPos = jspPositions[i];
if (jspPos != null) {
if (deltas[i].isDeleted) {
jspEdits.add(new DeleteEdit(jspPos.offset, jspPos.length));
} else {
replaceText = newJavaText.substring(deltas[i].postOffset, deltas[i].postOffset + deltas[i].postLength);
// get rid of pre and post white space or fine tuned
// adjustment later.
// fix text here...
replaceText = fixJspReplaceText(replaceText, jspPos);
if (// Unwanted TextEdit can lead to MalformedTreeException.See: Bug 321977
!(replaceText.length() == 0 && jspPos.length == 0))
jspEdits.add(new ReplaceEdit(jspPos.offset, jspPos.length, replaceText));
}
if (DEBUG)
debugReplace(deltas, jspPos, replaceText, i);
} else {
// possible new import?
if (isImport(javaPositions[i].getOffset()) && replaceText.lastIndexOf("import ") != -1) {
// $NON-NLS-1$
replaceText = newJavaText.substring(deltas[i].postOffset, deltas[i].postOffset + deltas[i].postLength);
// $NON-NLS-1$ //$NON-NLS-2$
String importText = replaceText.substring(replaceText.lastIndexOf("import "), replaceText.indexOf(";"));
// evenutally need to check if it's XML-JSP
// $NON-NLS-1$ //$NON-NLS-2$
importText = "<%@page import=\"" + importText + "\" %>\n";
jspEdits.add(new InsertEdit(0, importText));
}
}
}
TextEdit allJspEdits = createMultiTextEdit((TextEdit[]) jspEdits.toArray(new TextEdit[jspEdits.size()]));
// editor)
if (undo != null) {
try {
undo.apply(getJavaDocument());
} catch (MalformedTreeException e) {
Logger.logException(e);
} catch (BadLocationException e) {
Logger.logException(e);
}
}
return allJspEdits;
}
use of org.eclipse.text.edits.ReplaceEdit in project webtools.sourceediting by eclipse.
the class DefaultXMLPartitionFormatter method compressContent.
private void compressContent(TextEdit textEdit, IStructuredDocumentRegion region, int startOffset, int indentLevel, int lineWidth, String text) {
int length = text.length();
int start = 0, end = 0;
char c = 0;
int resultLength = 0;
boolean joinLines = fPreferences.getJoinCommentLines();
boolean onOwnLine = false;
String indent = getIndentString(indentLevel + 1);
for (int i = 0; i < length; i++) {
c = text.charAt(i);
// Compress whitespace unless its a line delimiter and formatting does not permit joining lines
if (Character.isWhitespace(c)) {
if ((c != '\r' && c != '\n') || joinLines) {
// Just came off of a word
if (start == end) {
start = end = i;
}
end++;
resultLength++;
} else {
// correct the indent of this line
lineWidth = fPreferences.getMaxLineWidth();
resultLength = 0;
onOwnLine = true;
// Compress any whitespace before the line delimiter
if (start != end) {
int replaceLength = end - start;
textEdit.addChild(new ReplaceEdit(start + startOffset, replaceLength, EMPTY));
start = end = i;
}
}
} else {
// Transitioned to a new word
if (start != end) {
int replaceLength = end - start;
if (onOwnLine) {
// If content is on its own line, replace leading whitespace with proper indent
textEdit.addChild(new ReplaceEdit(start + startOffset, replaceLength, indent));
resultLength -= (replaceLength - indent.length());
onOwnLine = false;
} else if (!(replaceLength == 1 && text.charAt(start) == ' ')) {
textEdit.addChild(new ReplaceEdit(start + startOffset, replaceLength, SPACE));
resultLength -= (replaceLength - 1);
}
start = end = i;
// Make sure the word starts on a new line
if (resultLength > lineWidth) {
lineWidth = fPreferences.getMaxLineWidth();
resultLength = 0;
textEdit.addChild(new InsertEdit(start + startOffset, getLineDelimiter(region) + indent));
}
}
// Word is immediately after line delimiters, indent appropriately
if (onOwnLine) {
textEdit.addChild(new InsertEdit(i + startOffset, indent));
onOwnLine = false;
}
resultLength++;
}
}
// Clean up any dangling whitespace
int replaceLength = end - start;
indent = getIndentString(indentLevel);
if (replaceLength == 0) {
// No trailing whitespace
textEdit.addChild(new InsertEdit(length + startOffset, (onOwnLine) ? indent : SPACE));
} else {
String whitespace = text.substring(start);
String replacement = (onOwnLine) ? indent : SPACE;
if (!whitespace.equals(replacement)) {
textEdit.addChild(new ReplaceEdit(start + startOffset, replaceLength, replacement));
}
}
}
use of org.eclipse.text.edits.ReplaceEdit in project webtools.sourceediting by eclipse.
the class XMLComponentRenameParticipant method createChange.
// private RefactoringStatus createRenameChanges(final IProgressMonitor monitor) throws CoreException {
// Assert.isNotNull(monitor);
// final RefactoringStatus status= new RefactoringStatus();
// try {
// monitor.beginTask("RefactoringMessages.RenameComponentRefactoring_searching", 1);
// createRenameChanges(new SubProgressMonitor(monitor, 1));
// //updateChangeManager(new SubProgressMonitor(monitor, 1), status);
// } finally {
// monitor.done();
// }
// return status;
// }
public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
for (Iterator iter = matches.iterator(); iter.hasNext(); ) {
SearchMatch match = (SearchMatch) iter.next();
TextChange textChange = getChangeManager().get(match.getFile());
String newName = getArguments().getNewName();
String qualifier = "";
if (getArguments() instanceof ComponentRenameArguments) {
qualifier = ((ComponentRenameArguments) getArguments()).getQualifier();
}
if (match.getObject() instanceof Node) {
Node node = (Node) match.getObject();
if (node instanceof IDOMAttr) {
IDOMAttr attr = (IDOMAttr) node;
IDOMElement element = (IDOMElement) attr.getOwnerElement();
newName = getNewQName(element, qualifier, newName);
}
newName = RenameComponentProcessor.quoteString(newName);
}
ReplaceEdit replaceEdit = new ReplaceEdit(match.getOffset(), match.getLength(), newName);
String editName = RefactoringMessages.getString("RenameComponentProcessor.Component_Refactoring_update_reference");
TextChangeCompatibility.addTextEdit(textChange, editName, replaceEdit);
}
// don't create any change now, all the changes are in changeManger variable and will be combined in RenameComponentProcessor.postCreateChange method
return null;
}
Aggregations