template = createNewTemplate(template, StringUtils.replaceAll(pattern, "\t", spacesIndentString));
changed = true;
}
} else {
if (pattern.indexOf(spacesIndentString) != -1) {
FastStringBuffer newPattern = new FastStringBuffer();
FastStringBuffer newTabsIndent = new FastStringBuffer();
for (int i = 0; i < splitted.size(); i++) {
String string = splitted.get(i);
int spacesFound = 0;
while (string.length() > 0 && string.charAt(0) == ' ') {
string = string.substring(1);
spacesFound += 1;
}
int tabsToAdd = 0;
if (spacesFound > 0) {
tabsToAdd = spacesFound / spacesIndentString.length();
if (spacesFound % spacesIndentString.length() != 0) {
tabsToAdd += 1;
}
newTabsIndent.clear();
for (int j = 0; j < tabsToAdd; j++) {
newTabsIndent.append("\t");
}
newPattern.append(newTabsIndent);
}
newPattern.append(string);
}
template = createNewTemplate(template, newPattern.toString());
changed = true;
}
}
//recreate it (if needed).
if (changed) {
pattern = template.getPattern();
splitted = StringUtils.splitInLines(pattern);
}
String indentToStr = indentTo != null ? indentTo : "";
String endLineDelim = PySelection.getDelimiter(this.getDocument());
int size = splitted.size();
if (size > 0) {
FastStringBuffer buffer = new FastStringBuffer("", (pattern.length() + (size * 2))
+ ((size + 1) * indentToStr.length()));
for (int i = 0; i < size; i++) { //we don't want to get the first line
if (i != 0) {
//the 1st line is not indented (that's where the user requested the completion -- others should be indented to it)
buffer.append(indentToStr);
}
String str = splitted.get(i);
//we have to make the new line delimiter correct:
//https://sourceforge.net/tracker/index.php?func=detail&aid=2019419&group_id=85796&atid=577329
boolean hasNewLine = false;
if (str.endsWith("\r") || str.endsWith("\n")) {
hasNewLine = true;
if (str.endsWith("\r\n")) {
str = str.substring(0, str.length() - 2);
} else {
str = str.substring(0, str.length() - 1);
}
}
buffer.append(str);
if (hasNewLine) {
buffer.append(endLineDelim);
}
}
//just to change the pattern...
template = createNewTemplate(template, buffer.toString());
}
try {
TemplateTranslator translator = new TemplateTranslator();
TemplateBuffer templateBuffer = translator.translate(template);