/// <summary> /// Generates code for <paramref name="nodes"/> and inserts it into <paramref name="document"/> /// after the line <paramref name="insertLine"/>. /// </summary> protected void InsertCodeAfter(int insertLine, IRefactoringDocument document, string indentation, bool startWithEmptyLine, params AbstractNode[] nodes) { StringBuilder b = new StringBuilder(); for (int i = 0; i < nodes.Length; i++) { if (options.EmptyLinesBetweenMembers) { if (startWithEmptyLine || i > 0) { b.AppendLine(indentation); } } b.Append(GenerateCode(nodes[i], indentation)); } if (insertLine < document.TotalNumberOfLines) { IRefactoringDocumentLine lineSegment = document.GetLine(insertLine + 1); document.Insert(lineSegment.Offset, b.ToString()); } else { b.Insert(0, Environment.NewLine); document.Insert(document.TextLength, b.ToString()); } }
void SetDocumentLineText(int lineNumber, string text, int offset) { IRefactoringDocumentLine documentLine = MockRepository.GenerateStub <IRefactoringDocumentLine>(); documentLine.Stub(line => line.Text).Return(text); documentLine.Stub(line => line.Offset).Return(offset); document.Stub(doc => doc.GetLine(lineNumber)).Return(documentLine); }
/// <summary> /// Ensure that code is inserted correctly in {} code blocks - SD2-1180 /// </summary> public override void InsertCodeAtEnd(DomRegion region, IRefactoringDocument document, params AbstractNode[] nodes) { string beginLineIndentation = GetIndentation(document, region.BeginLine); int insertionLine = region.EndLine - 1; IRefactoringDocumentLine endLine = document.GetLine(region.EndLine); string endLineText = endLine.Text; int originalPos = region.EndColumn - 2; // -1 for column coordinate => offset, -1 because EndColumn is after the '}' int pos = originalPos; if (pos < 0 || pos >= endLineText.Length || endLineText[pos] != '}') { LoggingService.Warn("CSharpCodeGenerator.InsertCodeAtEnd: position is invalid (not pointing to '}')" + " endLineText=" + endLineText + ", pos=" + pos); } else { for (pos--; pos >= 0; pos--) { if (!char.IsWhiteSpace(endLineText[pos])) { // range before '}' is not empty: we cannot simply insert in the line before the '}', so // pos++; // set pos to first whitespace character / the '{' character if (pos < originalPos) { // remove whitespace between last non-white character and the '}' document.Remove(endLine.Offset + pos, originalPos - pos); } // insert newline and same indentation as used in beginLine before the '}' document.Insert(endLine.Offset + pos, Environment.NewLine + beginLineIndentation); insertionLine++; pos = region.BeginColumn - 1; if (region.BeginLine == region.EndLine && pos >= 1 && pos < endLineText.Length) { // The whole block was in on a single line, e.g. "get { return field; }". // Insert an additional newline after the '{'. originalPos = pos = endLineText.IndexOf('{', pos); if (pos >= 0 && pos < region.EndColumn - 1) { // find next non-whitespace after originalPos originalPos++; // point to insertion position for newline after { for (pos++; pos < endLineText.Length; pos++) { if (!char.IsWhiteSpace(endLineText[pos])) { // remove all between originalPos and pos if (originalPos < pos) { document.Remove(endLine.Offset + originalPos, pos - originalPos); } document.Insert(endLine.Offset + originalPos, Environment.NewLine + beginLineIndentation + '\t'); insertionLine++; break; } } } } break; } } } InsertCodeAfter(insertionLine, document, beginLineIndentation + this.Options.IndentString, nodes); }
int GetPartialKeywordInsertOffset() { IRefactoringDocumentLine line = Document.GetLine(Class.Region.BeginLine); int offset = line.Text.IndexOf(" class", StringComparison.OrdinalIgnoreCase); if (offset >= 0) { return(offset + line.Offset + 1); } throw new ApplicationException("Unable to find 'class' declaration."); }
int GetVirtualKeywordInsertOffset() { IRefactoringDocumentLine line = Document.GetLine(Method.Region.BeginLine); int offset = line.Text.IndexOf("public ", StringComparison.OrdinalIgnoreCase); if (offset >= 0) { int publicKeywordLength = 6; return(offset + line.Offset + publicKeywordLength + 1); } throw new ApplicationException("Unable to find 'method' declaration."); }