/// <remarks> /// This method returns the expression before a specified offset. /// That method is used in code completion to determine the expression given /// to the parser for type resolve. /// </remarks> public static string GetExpressionBeforeOffset(TextArea textArea, int initialOffset) { Document document = textArea.Document; int offset = initialOffset; while (offset - 1 > 0) { switch (document.GetCharAt(offset - 1)) { case '\n': case '\r': case '}': goto done; // offset = SearchBracketBackward(document, offset - 2, '{','}'); // break; case ']': offset = SearchBracketBackward(document, offset - 2, '[', ']'); break; case ')': offset = SearchBracketBackward(document, offset - 2, '(', ')'); break; case '.': --offset; break; case '"': if (offset < initialOffset - 1) { return(null); } return("\"\""); case '\'': if (offset < initialOffset - 1) { return(null); } return("'a'"); case '>': if (document.GetCharAt(offset - 2) == '-') { offset -= 2; break; } goto done; default: if (Char.IsWhiteSpace(document.GetCharAt(offset - 1))) { --offset; break; } int start = offset - 1; if (!IsLetterDigitOrUnderscore(document.GetCharAt(start))) { goto done; } while (start > 0 && IsLetterDigitOrUnderscore(document.GetCharAt(start - 1))) { --start; } string word = document.GetText(start, offset - start).Trim(); switch (word) { case "ref": case "out": case "in": case "return": case "throw": case "case": goto done; } if (word.Length > 0 && !IsLetterDigitOrUnderscore(word[0])) { goto done; } offset = start; break; } } done: //// simple exit fails when : is inside comment line or any other character //// we have to check if we got several ids in resulting line, which usually happens when //// id. is typed on next line after comment one //// Would be better if lexer would parse properly such expressions. However this will cause //// modifications in this area too - to get full comment line and remove it afterwards if (offset < 0) { return(string.Empty); } string resText = document.GetText(offset, textArea.Caret.Offset - offset).Trim(); int pos = resText.LastIndexOf('\n'); if (pos >= 0) { offset += pos + 1; //// whitespaces and tabs, which might be inside, will be skipped by trim below } string expression = document.GetText(offset, textArea.Caret.Offset - offset).Trim(); return(expression); }
public static string GetLineAsString(Document document, int lineNumber) { LineSegment line = document.GetLineSegment(lineNumber); return(document.GetText(line.Offset, line.Length)); }