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 for now, unless we find reason not to
Logger.log(Logger.INFO, e.getMessage());
}
}
}
}
return newNode;
}
use of org.eclipse.text.edits.InsertEdit in project n4js by eclipse.
the class ImportRewriter method addNewImportDeclaration.
private AliasLocation addNewImportDeclaration(QualifiedName moduleName, QualifiedName qualifiedName, String optionalAlias, int insertionOffset, MultiTextEdit result) {
final String spacer = lazySpacer.get();
String syntacticModuleName = syntacticModuleName(moduleName);
AliasLocation aliasLocation = null;
String importSpec = (insertionOffset != 0 ? lineDelimiter : "") + "import ";
if (!N4JSLanguageUtils.isDefaultExport(qualifiedName)) {
// not an 'default' export
importSpec = importSpec + "{" + spacer + qualifiedName.getLastSegment();
if (optionalAlias != null) {
importSpec = importSpec + " as ";
aliasLocation = new AliasLocation(insertionOffset, importSpec.length(), optionalAlias);
importSpec = importSpec + optionalAlias;
}
importSpec = importSpec + spacer + "}";
} else {
// import default exported element
if (optionalAlias == null) {
importSpec = importSpec + N4JSLanguageUtils.lastSegmentOrDefaultHost(qualifiedName);
} else {
aliasLocation = new AliasLocation(insertionOffset, importSpec.length(), optionalAlias);
importSpec = importSpec + optionalAlias;
}
}
result.addChild(new InsertEdit(insertionOffset, importSpec + " from " + syntacticModuleName + ";" + (insertionOffset != 0 ? "" : lineDelimiter)));
return aliasLocation;
}
use of org.eclipse.text.edits.InsertEdit in project liferay-ide by liferay.
the class InputContext method flushModel.
protected void flushModel(IDocument doc) {
boolean flushed = true;
if (fEditOperations.size() > 0) {
try {
MultiTextEdit edit = new MultiTextEdit();
if (isNewlineNeeded(doc)) {
insert(edit, new InsertEdit(doc.getLength(), TextUtilities.getDefaultLineDelimiter(doc)));
}
for (int i = 0; i < fEditOperations.size(); i++) {
insert(edit, (TextEdit) fEditOperations.get(i));
}
if (_fModel instanceof IEditingModel) {
((IEditingModel) _fModel).setStale(true);
}
edit.apply(doc);
fEditOperations.clear();
} catch (MalformedTreeException mte) {
LiferayUIPlugin.logError(mte);
flushed = false;
} catch (BadLocationException ble) {
LiferayUIPlugin.logError(ble);
flushed = false;
}
}
if (flushed && (_fModel instanceof IEditable)) {
((IEditable) _fModel).setDirty(false);
}
}
use of org.eclipse.text.edits.InsertEdit in project eclipse.jdt.ls by eclipse.
the class CorrectPackageDeclarationProposal method addEdits.
@Override
protected void addEdits(IDocument doc, TextEdit root) throws CoreException {
super.addEdits(doc, root);
ICompilationUnit cu = getCompilationUnit();
IPackageFragment parentPack = (IPackageFragment) cu.getParent();
IPackageDeclaration[] decls = cu.getPackageDeclarations();
if (parentPack.isDefaultPackage() && decls.length > 0) {
for (int i = 0; i < decls.length; i++) {
ISourceRange range = decls[i].getSourceRange();
root.addChild(new DeleteEdit(range.getOffset(), range.getLength()));
}
return;
}
if (!parentPack.isDefaultPackage() && decls.length == 0) {
String lineDelim = "\n";
// $NON-NLS-1$
String str = "package " + parentPack.getElementName() + ';' + lineDelim + lineDelim;
root.addChild(new InsertEdit(0, str));
return;
}
root.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), parentPack.getElementName()));
}
use of org.eclipse.text.edits.InsertEdit in project flux by eclipse.
the class Utils method getOffsetAdjustment.
public static int getOffsetAdjustment(TextEdit edit, final int offset) {
final int[] holder = new int[] { 0 };
edit.accept(new TextEditVisitor() {
@Override
public boolean visit(DeleteEdit edit) {
if (offset >= edit.getOffset()) {
holder[0] -= edit.getLength();
}
return super.visit(edit);
}
@Override
public boolean visit(InsertEdit edit) {
if (offset >= edit.getOffset()) {
holder[0] += edit.getText().length();
}
return super.visit(edit);
}
@Override
public boolean visit(ReplaceEdit edit) {
if (offset >= edit.getOffset()) {
holder[0] += edit.getText().length() - edit.getLength();
}
return super.visit(edit);
}
});
return holder[0];
}
Aggregations