use of org.eclipse.text.edits.InsertEdit in project webtools.sourceediting by eclipse.
the class JSPTranslationUtil method translateTextEdit.
public TextEdit translateTextEdit(TextEdit textEdit) {
TextEdit translatedTextEdit = null;
int javaOffset = textEdit.getOffset();
int jspOffset = getTranslation().getJspOffset(textEdit.getOffset());
int length = textEdit.getLength();
if (textEdit instanceof MultiTextEdit) {
translatedTextEdit = new MultiTextEdit();
TextEdit[] children = ((MultiTextEdit) textEdit).getChildren();
for (int i = 0; i < children.length; i++) {
TextEdit translatedChildTextEdit = translateTextEdit(children[i]);
if (translatedChildTextEdit != null)
((MultiTextEdit) translatedTextEdit).addChild(translatedChildTextEdit);
}
} else if (textEdit instanceof ReplaceEdit) {
if (jspOffset == -1)
return null;
if (!getTranslation().javaSpansMultipleJspPartitions(javaOffset, length))
translatedTextEdit = new ReplaceEdit(jspOffset, length, ((ReplaceEdit) textEdit).getText());
} else if (textEdit instanceof InsertEdit) {
translatedTextEdit = new InsertEdit(jspOffset, ((InsertEdit) textEdit).getText());
} else if (textEdit instanceof DeleteEdit) {
translatedTextEdit = new DeleteEdit(jspOffset, length);
TextEdit[] children = ((DeleteEdit) textEdit).getChildren();
for (int i = 0; i < children.length; i++) {
TextEdit translatedChildTextEdit = translateTextEdit(children[i]);
if (translatedChildTextEdit != null)
((DeleteEdit) translatedTextEdit).addChild(translatedChildTextEdit);
}
} else if (textEdit instanceof CopySourceEdit) {
translatedTextEdit = new CopySourceEdit(jspOffset, length);
((CopySourceEdit) translatedTextEdit).setTargetEdit(((CopySourceEdit) textEdit).getTargetEdit());
((CopySourceEdit) translatedTextEdit).setSourceModifier(((CopySourceEdit) textEdit).getSourceModifier());
} else if (textEdit instanceof CopyTargetEdit) {
translatedTextEdit = new CopyTargetEdit(jspOffset);
((CopyTargetEdit) textEdit).getSourceEdit().setTargetEdit((CopyTargetEdit) translatedTextEdit);
} else if (textEdit instanceof MoveSourceEdit) {
translatedTextEdit = new MoveSourceEdit(jspOffset, length);
((MoveSourceEdit) translatedTextEdit).setTargetEdit(((MoveSourceEdit) textEdit).getTargetEdit());
} else if (textEdit instanceof MoveTargetEdit) {
translatedTextEdit = new MoveTargetEdit(jspOffset);
((MoveTargetEdit) textEdit).getSourceEdit().setTargetEdit((MoveTargetEdit) translatedTextEdit);
} else {
// $NON-NLS-1$
System.out.println("Need to translate " + textEdit);
}
return translatedTextEdit;
}
use of org.eclipse.text.edits.InsertEdit in project webtools.sourceediting by eclipse.
the class ElementNodeCleanupHandler method insertRequiredAttrs.
private IDOMNode insertRequiredAttrs(IDOMNode node) {
boolean insertRequiredAttrs = getCleanupPreferences().getInsertRequiredAttrs();
IDOMNode newNode = node;
if (insertRequiredAttrs) {
List requiredAttrs = getRequiredAttrs(newNode);
if (requiredAttrs.size() > 0) {
NamedNodeMap currentAttrs = node.getAttributes();
List insertAttrs = new ArrayList();
if (currentAttrs.getLength() == 0)
insertAttrs.addAll(requiredAttrs);
else {
for (int i = 0; i < requiredAttrs.size(); i++) {
String requiredAttrName = ((CMAttributeDeclaration) requiredAttrs.get(i)).getAttrName();
boolean found = false;
for (int j = 0; j < currentAttrs.getLength(); j++) {
String currentAttrName = currentAttrs.item(j).getNodeName();
if (requiredAttrName.compareToIgnoreCase(currentAttrName) == 0) {
found = true;
break;
}
}
if (!found)
insertAttrs.add(requiredAttrs.get(i));
}
}
if (insertAttrs.size() > 0) {
IStructuredDocumentRegion startStructuredDocumentRegion = newNode.getStartStructuredDocumentRegion();
int index = startStructuredDocumentRegion.getEndOffset();
ITextRegion lastRegion = startStructuredDocumentRegion.getLastRegion();
if (lastRegion.getType() == DOMRegionContext.XML_TAG_CLOSE) {
index--;
lastRegion = startStructuredDocumentRegion.getRegionAtCharacterOffset(index - 1);
} else if (lastRegion.getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
index = index - 2;
lastRegion = startStructuredDocumentRegion.getRegionAtCharacterOffset(index - 1);
}
MultiTextEdit multiTextEdit = new MultiTextEdit();
try {
for (int i = insertAttrs.size() - 1; i >= 0; i--) {
CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) insertAttrs.get(i);
String requiredAttributeName = attrDecl.getAttrName();
String defaultValue = attrDecl.getDefaultValue();
if (defaultValue == null)
// $NON-NLS-1$
defaultValue = "";
// $NON-NLS-1$
String nameAndDefaultValue = " ";
if (i == 0 && lastRegion.getLength() > lastRegion.getTextLength())
// $NON-NLS-1$
nameAndDefaultValue = "";
// $NON-NLS-1$ //$NON-NLS-2$
nameAndDefaultValue += requiredAttributeName + "=\"" + defaultValue + "\"";
multiTextEdit.addChild(new InsertEdit(index, nameAndDefaultValue));
// BUG3381: MultiTextEdit applies all child
// TextEdit's basing on offsets
// in the document before the first TextEdit, not
// after each
// child TextEdit. Therefore, do not need to
// advance the index.
// index += nameAndDefaultValue.length();
}
multiTextEdit.apply(newNode.getStructuredDocument());
} catch (BadLocationException e) {
// log or now, unless we find reason not to
Logger.log(Logger.INFO, e.getMessage());
}
}
}
}
return newNode;
}
use of org.eclipse.text.edits.InsertEdit in project webtools.sourceediting by eclipse.
the class InsertRequiredAttrsQuickAssistProposal method apply.
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer,
* char, int, int)
*/
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
IDOMNode node = (IDOMNode) ContentAssistUtils.getNodeAt(viewer, offset);
IStructuredDocumentRegion startStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
int index = startStructuredDocumentRegion.getEndOffset();
ITextRegion lastRegion = startStructuredDocumentRegion.getLastRegion();
if (lastRegion.getType() == DOMRegionContext.XML_TAG_CLOSE) {
index--;
lastRegion = startStructuredDocumentRegion.getRegionAtCharacterOffset(index - 1);
} else if (lastRegion.getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
index = index - 2;
lastRegion = startStructuredDocumentRegion.getRegionAtCharacterOffset(index - 1);
}
MultiTextEdit multiTextEdit = new MultiTextEdit();
try {
for (int i = 0; i < fRequiredAttrs.size(); i++) {
CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) fRequiredAttrs.get(i);
String requiredAttributeName = attrDecl.getAttrName();
String defaultValue = attrDecl.getDefaultValue();
if (defaultValue == null) {
// $NON-NLS-1$
defaultValue = "";
}
// $NON-NLS-1$
String nameAndDefaultValue = " ";
if ((i == 0) && (lastRegion.getLength() > lastRegion.getTextLength())) {
// $NON-NLS-1$
nameAndDefaultValue = "";
}
// $NON-NLS-1$//$NON-NLS-2$
nameAndDefaultValue += requiredAttributeName + "=\"" + defaultValue + "\"";
multiTextEdit.addChild(new InsertEdit(index, nameAndDefaultValue));
// BUG3381: MultiTextEdit applies all child TextEdit's basing
// on offsets
// in the document before the first TextEdit, not after each
// child TextEdit. Therefore, do not need to advance the
// index.
// index += nameAndDefaultValue.length();
}
multiTextEdit.apply(viewer.getDocument());
} catch (BadLocationException e) {
// log, for now, unless we find there's reasons why we get some
// here.
Logger.log(Logger.INFO, e.getMessage());
}
}
use of org.eclipse.text.edits.InsertEdit 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.InsertEdit in project webtools.sourceediting by eclipse.
the class DefaultXMLPartitionFormatter method collapseSpaces.
private int collapseSpaces(TextEdit textEdit, int spaceStartOffset, int availableLineWidth, String whitespaceRun) {
// prefer to use use existing whitespace
int existingWhitespaceOffset = whitespaceRun.indexOf(' ');
if (existingWhitespaceOffset > -1) {
// delete whitespaces before and after existing whitespace
if (existingWhitespaceOffset > 0) {
DeleteEdit deleteEdit = new DeleteEdit(spaceStartOffset, existingWhitespaceOffset);
textEdit.addChild(deleteEdit);
}
if (existingWhitespaceOffset < whitespaceRun.length() - 1) {
int nextOffset = existingWhitespaceOffset + 1;
DeleteEdit deleteEdit = new DeleteEdit(spaceStartOffset + nextOffset, whitespaceRun.length() - nextOffset);
textEdit.addChild(deleteEdit);
}
} else {
// delete all whitespace and insert new one
// collapse whitespace by deleting whitespace
DeleteEdit deleteEdit = new DeleteEdit(spaceStartOffset, whitespaceRun.length());
textEdit.addChild(deleteEdit);
// then insert one space
InsertEdit insertEdit = new InsertEdit(spaceStartOffset, SPACE);
textEdit.addChild(insertEdit);
}
// remember to account for space added
--availableLineWidth;
return availableLineWidth;
}
Aggregations