* @param c
*/
private void smartPaste(IDocument d, DocumentCommand c) {
try {
String content = d.get(0, c.offset) + c.text;
Document temp = new Document(content);
DocumentRewriteSession session = temp
.startRewriteSession(DocumentRewriteSessionType.STRICTLY_SEQUENTIAL);
installStuff(temp);
int offset = c.offset;
int line = temp.getLineOfOffset(offset);
String lastIndent = getLineIndent(temp, line);
int firstLineOffset = temp.getLineOffset(line);
String commonIndent = temp.get(firstLineOffset, c.offset - firstLineOffset);
if (StringUtils.isNotBlank(commonIndent)) {
// do not copy non whitespace content as indent
// and only use leading white spaces
String notWSContent = commonIndent.replaceAll("^\\s+", "");
commonIndent = StringUtils.removeEnd(commonIndent, notWSContent);
}
line++;
try {
while (getDocumentLine(temp, line).trim().length() == 0)
line++;
offset = temp.getLineOffset(line);
} catch (BadLocationException e) {
offset = temp.getLength();
}
while (offset < temp.getLength()) {
String resultIndent = calcLineIndent(temp, line, false, temp.getLineOffset(line));
String currentIndent = getLineIndent(temp, line);
if (resultIndent == null) {
resultIndent = commonIndent + currentIndent;
// TODO commented to retain indent of pasted blocks
// if (getPhysicalLength(resultIndent) > getPhysicalLength(lastIndent))
// resultIndent = lastIndent;
}
temp.replace(offset, currentIndent.length(), resultIndent);
String currentLine = getDocumentLine(temp, line);
if (currentLine.trim().length() > 0 && (!currentLine.trim().startsWith("//"))) {
lastIndent = resultIndent;
}
if (temp.getLineOffset(line) + temp.getLineLength(line) == temp.getLength())
break;
line++;
offset = temp.getLineOffset(line);
}
temp.stopRewriteSession(session);
removeStuff(temp);
c.text = temp.get(c.offset, temp.getLength() - c.offset);
} catch (BadLocationException e) {
e.printStackTrace();
}
}