region = document.getLineInformationOfOffset(offset);
}
// search backwards for a tmltag with body which might need to be closed
int pos = offset - 1;
while (pos >= 0) {
ITypedRegion previousRegion = document.getPartition(pos);
if (previousRegion.getType().equals(TMLPartitionScanner.TML_TAG_START) && !isTMLPartitionClosed(previousRegion, document)) {
String alreadyTyped = document.get(region.getOffset(), offset-region.getOffset());
if (alreadyTyped.contains("\n")) {
alreadyTyped = alreadyTyped.substring(alreadyTyped.lastIndexOf("\n"));
}
int posEnd = alreadyTyped.lastIndexOf(">");
if (posEnd != -1) {
alreadyTyped = alreadyTyped.substring(posEnd + 1);
}
posEnd = alreadyTyped.lastIndexOf("<");
if (posEnd != -1) {
alreadyTyped = alreadyTyped.substring(posEnd);
} else {
alreadyTyped = "";
}
String tagName = TMLPartitionScanner.determineTMLTagName(previousRegion, document);
if (document.getLineOfOffset(offset) == document.getLineOfOffset(region.getOffset())) {
// completion in same line
int start = previousRegion.getOffset() + previousRegion.getLength();
int end = offset;
String typedBody = document.get(start, end - start);
if (typedBody.trim().equals("")) {
// create multi line completion - bc. empty tagbody makes no sense
String display = "... </tml:" + tagName + ">";
StringBuffer replacement = new StringBuffer("\n");
replacement.append(computeIndent("\t", offset, document));
int cursorPosAfterReplace = replacement.length();
replacement.append(computeIndent("\n", offset, document));
replacement.append("</tml:" + tagName + ">");
if (replacement.toString().startsWith(alreadyTyped.trim())) {
int completionStartPos = alreadyTyped.lastIndexOf("<");
if (completionStartPos == -1) {
completionStartPos = offset;
} else {
completionStartPos = offset - (alreadyTyped.length() - completionStartPos);
}
proposals.add(new CompletionProposal(replacement.toString(), completionStartPos, offset - completionStartPos, cursorPosAfterReplace, null, display, null, null));
}
// create close current tag completionv
display = "/>";
replacement = new StringBuffer("/>");
cursorPosAfterReplace = replacement.length();
if (replacement.toString().startsWith(alreadyTyped.trim())) {
int completionStartPos = previousRegion.getOffset() + previousRegion.getLength() + 1 - replacement.length();
proposals.add(new CompletionProposal(replacement.toString(), completionStartPos, offset - completionStartPos, cursorPosAfterReplace, null, display, null, null));
}
break;
} else {
String replacement = "</tml:" + tagName + ">";
if (replacement.startsWith(alreadyTyped.trim())) {
int completionStartPos = alreadyTyped.lastIndexOf("<");
if (completionStartPos == -1) {
completionStartPos = offset;
} else {
completionStartPos = offset - (alreadyTyped.length() - completionStartPos);
}
proposals.add(new CompletionProposal(replacement, completionStartPos, offset - completionStartPos, replacement.length()));
break;
} else {
break;
}
}
} else {
// completion in following line
String replacement = "</tml:" + tagName + ">";
if (replacement.startsWith(alreadyTyped.trim())) {
int completionStartPos = alreadyTyped.lastIndexOf("<");
if (completionStartPos == -1) {
completionStartPos = offset;
} else {
completionStartPos = offset - (alreadyTyped.length() - completionStartPos);
}
proposals.add(new CompletionProposal(replacement, completionStartPos, offset - completionStartPos, replacement.length()));
break;
} else {
break;
}
}
}
pos = previousRegion.getOffset() - 1;
}
return proposals;
//return null;
}