コード例 #1
0
        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());
        }
コード例 #2
0
        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));
        }