String origChange = command.text;
int position = (command.offset == document.getLength() ? command.offset - 1 : command.offset);
ICFDocument cfd = (ICFDocument) document;
// CfmlTagItem node = cfd.getTagAt(command.offset, command.offset,
// true);
DocItem node = editor.getSelectionCursorListener().getCurrentDocItem();
if (node == null) {
return;
}
// eat any WS before the insertion to the beginning of the line
int firstLine = 1; // don't format the first line if it has other
// content before it
IRegion line = document.getLineInformationOfOffset(command.offset);
String notSelected = document.get(line.getOffset(), command.offset - line.getOffset());
if (notSelected.trim().length() == 0) {
command.length += notSelected.length();
command.offset = line.getOffset();
firstLine = 0;
}
// handle the indentation computation inside a temporary document
Document temp = new Document(command.text);
// indent the first and second line
// compute the relative indentation difference from the second line
// (as the first might be partially selected) and use the value to
// indent all other lines.
boolean isIndentDetected = false;
StringBuffer addition = new StringBuffer();
int insertLength = 0;
int lines = temp.getNumberOfLines();
for (int l = firstLine; l < lines; l++) { // we don't change the
// number of lines while
// adding indents
IRegion r = temp.getLineInformation(l);
int lineOffset = r.getOffset();
int lineLength = r.getLength();
if (lineLength == 0) { // don't modify empty lines
continue;
}
if (!isIndentDetected) {
// indent the first pasted line
StringBuffer current = XmlDocumentFormatter.getLeadingWhitespace(lineOffset, temp);
StringBuffer correct = XmlDocumentFormatter.getLeadingWhitespace(node.getStartPosition(), document);
// relatively indent all pasted lines
if (doIndent(document, command)) {
correct.append(XmlDocumentFormatter.createIndent());
}
insertLength = subtractIndent(correct, current, addition);