static InsertionPoint GetInsertionPosition (IReadonlyTextDocument doc, int line, int column)
{
int bodyEndOffset = doc.LocationToOffset (line, column) + 1;
var curLine = doc.GetLine (line);
if (curLine != null) {
if (bodyEndOffset < curLine.Offset + curLine.Length) {
// case1: positition is somewhere inside the start line
return new InsertionPoint (new DocumentLocation (line, column + 1), NewLineInsertion.Eol, NewLineInsertion.BlankLine);
}
}
// -> if position is at line end check next line
var nextLine = doc.GetLine (line + 1);
if (nextLine == null) // check for 1 line case.
return new InsertionPoint (new DocumentLocation (line, column + 1), NewLineInsertion.BlankLine, NewLineInsertion.BlankLine);
for (int i = nextLine.Offset; i < nextLine.EndOffset; i++) {
char ch = doc.GetCharAt (i);
if (!char.IsWhiteSpace (ch)) {
// case2: next line contains non ws chars.
return new InsertionPoint (new DocumentLocation (line + 1, 1), NewLineInsertion.Eol, NewLineInsertion.BlankLine);
}
}
var nextLine2 = doc.GetLine (line + 2);
if (nextLine2 != null) {
for (int i = nextLine2.Offset; i < nextLine2.EndOffset; i++) {
char ch = doc.GetCharAt (i);
if (!char.IsWhiteSpace (ch)) {
// case3: one blank line
return new InsertionPoint (new DocumentLocation (line + 1, 1), NewLineInsertion.Eol, NewLineInsertion.Eol);
}
}
}
// case4: more than 1 blank line
return new InsertionPoint (new DocumentLocation (line + 1, 1), NewLineInsertion.Eol, NewLineInsertion.None);
}