Пример #1
0
        protected override void Run(ITextEditor editor, string clipboardText)
        {
            // TODO: reimplement as C#-specific refactoring with NR5;
            // because CodeDom introduces redundant parentheses
            CodeDomProvider codeDomProvider = null;
            IProject        project         = ProjectService.CurrentProject;

            if (project != null)
            {
                codeDomProvider = project.CreateCodeDomProvider();
            }
            if (codeDomProvider == null)
            {
                codeDomProvider = new CSharpCodeProvider();
            }

            CodeExpression expression = null;

            using (StringReader reader = new StringReader(clipboardText)) {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    CodeExpression newExpr = new CodePrimitiveExpression(line);
                    if (expression == null)
                    {
                        expression = newExpr;
                    }
                    else
                    {
                        expression = new CodeBinaryOperatorExpression(
                            expression,
                            CodeBinaryOperatorType.Add,
                            new CodePropertyReferenceExpression(
                                new CodeTypeReferenceExpression("Environment"),
                                "NewLine"
                                ));
                        expression = new CodeBinaryOperatorExpression(
                            expression, CodeBinaryOperatorType.Add, newExpr);
                    }
                }
            }
            if (expression == null)
            {
                return;
            }
            string indentation           = DocumentUtilities.GetIndentation(editor.Document, editor.Caret.Line);
            CodeGeneratorOptions options = new CodeGeneratorOptions();

            options.IndentString = editor.Options.IndentationString;
            StringWriter writer = new StringWriter();

            codeDomProvider.GenerateCodeFromExpression(expression, writer, options);
            editor.Document.Insert(editor.Caret.Offset, writer.ToString().Trim());
        }
Пример #2
0
            public override void Execute(EditorRefactoringContext context)
            {
                SD.AnalyticsMonitor.TrackFeature(typeof(SuppressIssueContextAction), issueName);
                var lineNo   = context.CaretLocation.Line;
                var document = context.Editor.Document;

                var    line        = document.GetLineByNumber(lineNo);
                string indentation = DocumentUtilities.GetIndentation(document, lineNo);
                string newLine     = DocumentUtilities.GetLineTerminator(document, lineNo);

                document.Insert(line.Offset, indentation + "// disable once " + issueName + newLine);
            }
        void SaveInitializeComponents(CodeMemberMethod codeMethod)
        {
            var            bodyRegion = initializeComponents.BodyRegion;
            DocumentScript script     = GetScript(bodyRegion.FileName);

            string newline     = DocumentUtilities.GetLineTerminator(script.OriginalDocument, bodyRegion.BeginLine);
            string indentation = DocumentUtilities.GetIndentation(script.OriginalDocument, bodyRegion.BeginLine);
            string code        = "{" + newline + GenerateInitializeComponents(codeMethod, indentation, newline) + indentation + "}";

            int startOffset = script.GetCurrentOffset(bodyRegion.Begin);
            int endOffset   = script.GetCurrentOffset(bodyRegion.End);

            script.Replace(startOffset, endOffset - startOffset, code);
        }
        void CreateField(CodeMemberField newField)
        {
            // insert new field below InitializeComponents()

            var            bodyRegion  = initializeComponents.BodyRegion;
            DocumentScript script      = GetScript(bodyRegion.FileName);
            string         newline     = DocumentUtilities.GetLineTerminator(script.OriginalDocument, bodyRegion.BeginLine);
            string         indentation = DocumentUtilities.GetIndentation(script.OriginalDocument, bodyRegion.BeginLine);

            var    insertionLocation = new TextLocation(bodyRegion.EndLine + 1, 1);
            int    insertionOffset   = script.GetCurrentOffset(insertionLocation);
            string code = indentation + GenerateField(newField) + newline;

            script.InsertText(insertionOffset, code);
        }
Пример #5
0
        protected override void Run(ITextEditor editor, string clipboardText)
        {
            string       indentation   = DocumentUtilities.GetIndentation(editor.Document, editor.Caret.Line);
            IAmbience    ambience      = AmbienceService.GetCurrentAmbience();
            int          maxLineLength = editor.Options.VerticalRulerColumn - VisualIndentationLength(editor, indentation);
            StringWriter insertedText  = new StringWriter();

            insertedText.NewLine = DocumentUtilities.GetLineTerminator(editor.Document, editor.Caret.Line);
            using (StringReader reader = new StringReader(clipboardText)) {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    AppendTextLine(indentation, ambience, maxLineLength, insertedText, line);
                }
            }
            IDocument document     = editor.Document;
            int       insertionPos = document.GetLineByNumber(editor.Caret.Line).Offset + indentation.Length;

            document.Insert(insertionPos, insertedText.ToString());
        }
        void CreateField(CodeMemberField newField)
        {
            // insert new field below the last field or InitializeComponents()
            IField field = null;

            if (formClass != null)
            {
                field = formClass.Fields.LastOrDefault(f => string.Equals(f.Region.FileName,
                                                                          initializeComponents.Region.FileName,
                                                                          StringComparison.OrdinalIgnoreCase));
            }
            var            bodyRegion  = field != null ? field.BodyRegion : initializeComponents.BodyRegion;
            DocumentScript script      = GetScript(bodyRegion.FileName);
            string         newline     = DocumentUtilities.GetLineTerminator(script.OriginalDocument, bodyRegion.BeginLine);
            string         indentation = DocumentUtilities.GetIndentation(script.OriginalDocument, bodyRegion.BeginLine);

            var    insertionLocation = new TextLocation(bodyRegion.EndLine + 1, 1);
            int    insertionOffset   = script.GetCurrentOffset(insertionLocation);
            string code = indentation + GenerateField(newField) + newline;

            script.InsertText(insertionOffset, code);
        }
        static void TryIndent(ITextEditor editor, int begin, int end)
        {
            string         currentIndentation = "";
            Stack <string> tagStack           = new Stack <string>();
            IDocument      document           = editor.Document;

            string      tab             = editor.Options.IndentationString;
            int         nextLine        = begin; // in #dev coordinates
            bool        wasEmptyElement = false;
            XmlNodeType lastType        = XmlNodeType.XmlDeclaration;

            using (StringReader stringReader = new StringReader(document.Text)) {
                XmlTextReader r = new XmlTextReader(stringReader);
                r.XmlResolver = null;                 // prevent XmlTextReader from loading external DTDs
                while (r.Read())
                {
                    if (wasEmptyElement)
                    {
                        wasEmptyElement = false;
                        if (tagStack.Count == 0)
                        {
                            currentIndentation = "";
                        }
                        else
                        {
                            currentIndentation = tagStack.Pop();
                        }
                    }
                    if (r.NodeType == XmlNodeType.EndElement)
                    {
                        if (tagStack.Count == 0)
                        {
                            currentIndentation = "";
                        }
                        else
                        {
                            currentIndentation = tagStack.Pop();
                        }
                    }

                    while (r.LineNumber >= nextLine)
                    {
                        if (nextLine > end)
                        {
                            break;
                        }
                        if (lastType == XmlNodeType.CDATA || lastType == XmlNodeType.Comment)
                        {
                            nextLine++;
                            continue;
                        }
                        // set indentation of 'nextLine'
                        IDocumentLine line     = document.GetLine(nextLine);
                        string        lineText = document.GetText(line);

                        string newText;
                        // special case: opening tag has closing bracket on extra line: remove one indentation level
                        if (lineText.Trim() == ">")
                        {
                            newText = tagStack.Peek() + lineText.Trim();
                        }
                        else
                        {
                            newText = currentIndentation + lineText.Trim();
                        }

                        document.SmartReplaceLine(line, newText);
                        nextLine++;
                    }
                    if (r.LineNumber > end)
                    {
                        break;
                    }
                    wasEmptyElement = r.NodeType == XmlNodeType.Element && r.IsEmptyElement;
                    string attribIndent = null;
                    if (r.NodeType == XmlNodeType.Element)
                    {
                        tagStack.Push(currentIndentation);
                        if (r.LineNumber < begin)
                        {
                            currentIndentation = DocumentUtilities.GetIndentation(editor.Document, r.LineNumber);
                        }
                        if (r.Name.Length < 16)
                        {
                            attribIndent = currentIndentation + new string(' ', 2 + r.Name.Length);
                        }
                        else
                        {
                            attribIndent = currentIndentation + tab;
                        }
                        currentIndentation += tab;
                    }
                    lastType = r.NodeType;
                    if (r.NodeType == XmlNodeType.Element && r.HasAttributes)
                    {
                        int startLine = r.LineNumber;
                        r.MoveToAttribute(0);                         // move to first attribute
                        if (r.LineNumber != startLine)
                        {
                            attribIndent = currentIndentation;                             // change to tab-indentation
                        }
                        r.MoveToAttribute(r.AttributeCount - 1);
                        while (r.LineNumber >= nextLine)
                        {
                            if (nextLine > end)
                            {
                                break;
                            }
                            // set indentation of 'nextLine'
                            IDocumentLine line    = document.GetLine(nextLine);
                            string        newText = attribIndent + document.GetText(line).Trim();
                            document.SmartReplaceLine(line, newText);
                            nextLine++;
                        }
                    }
                }
                r.Close();
            }
        }