protected override ExpressionResult GetExpression(ICSharpCode.TextEditor.TextArea textArea) { ICSharpCode.TextEditor.Document.IDocument document = textArea.Document; IExpressionFinder expressionFinder = ParserService.GetExpressionFinder(fileName); if (expressionFinder == null) { return(new ExpressionResult(TextUtilities.GetExpressionBeforeOffset(textArea, textArea.Caret.Offset - 1))); } else { ExpressionResult res = expressionFinder.FindExpression(document.GetText(0, textArea.Caret.Offset - 1), textArea.Caret.Offset - 1); if (overrideContext != null) { res.Context = overrideContext; } return(res); } }
public static string ExtractCode(IClass c, DomRegion codeRegion, int indentationLine, out Action removeExtractedCodeAction) { ICSharpCode.TextEditor.Document.IDocument doc = GetDocument(c); if (indentationLine < 1) { indentationLine = 1; } if (indentationLine >= doc.TotalNumberOfLines) { indentationLine = doc.TotalNumberOfLines; } LineSegment segment = doc.GetLineSegment(indentationLine - 1); string mainLine = doc.GetText(segment); string indentation = mainLine.Substring(0, mainLine.Length - mainLine.TrimStart().Length); segment = doc.GetLineSegment(codeRegion.BeginLine - 1); int startOffset = segment.Offset; segment = doc.GetLineSegment(codeRegion.EndLine - 1); int endOffset = segment.Offset + segment.Length; StringReader reader = new StringReader(doc.GetText(startOffset, endOffset - startOffset)); removeExtractedCodeAction = delegate { doc.Remove(startOffset, endOffset - startOffset); doc.RequestUpdate(new ICSharpCode.TextEditor.TextAreaUpdate(ICSharpCode.TextEditor.TextAreaUpdateType.WholeTextArea)); doc.CommitUpdate(); }; // now remove indentation from extracted source code string line; StringBuilder b = new StringBuilder(); int endOfLastFilledLine = 0; while ((line = reader.ReadLine()) != null) { int startpos; for (startpos = 0; startpos < line.Length && startpos < indentation.Length; startpos++) { if (line[startpos] != indentation[startpos]) { break; } } if (startpos == line.Length) { // empty line if (b.Length > 0) { b.AppendLine(); } } else { b.Append(line, startpos, line.Length - startpos); b.AppendLine(); endOfLastFilledLine = b.Length; } } b.Length = endOfLastFilledLine; return(b.ToString()); }
protected string GetIndentation(ICSharpCode.TextEditor.Document.IDocument document, int line) { string lineText = document.GetText(document.GetLineSegment(line)); return(lineText.Substring(0, lineText.Length - lineText.TrimStart().Length)); }