static FormattingChanges GetFormattingChanges(PolicyContainer policyParent, IEnumerable <string> mimeTypeChain, MonoDevelop.Ide.Gui.Document document, string input, DomRegion formattingRegion, ref int formatStartOffset, ref int formatLength, bool formatLastStatementOnly)
        {
            using (var stubData = TextEditorData.CreateImmutable(input)) {
                stubData.Document.FileName = document.FileName;
                var  parser          = document.HasProject ? new CSharpParser(TypeSystemParser.GetCompilerArguments(document.Project)) : new CSharpParser();
                var  compilationUnit = parser.Parse(stubData);
                bool hadErrors       = parser.HasErrors;
                if (hadErrors)
                {
                    using (var stubData2 = TextEditorData.CreateImmutable(input + "}")) {
                        compilationUnit = parser.Parse(stubData2);
                        hadErrors       = parser.HasErrors;
                    }
                }
                // try it out, if the behavior is better when working only with correct code.
                if (hadErrors)
                {
                    return(null);
                }

                var policy = policyParent.Get <CSharpFormattingPolicy> (mimeTypeChain);

                var formattingVisitor = new ICSharpCode.NRefactory.CSharp.CSharpFormatter(policy.CreateOptions(), document.Editor.CreateNRefactoryTextEditorOptions());
                formattingVisitor.FormattingMode = FormattingMode.Intrusive;
                formattingVisitor.AddFormattingRegion(formattingRegion);


                var changes = formattingVisitor.AnalyzeFormatting(stubData.Document, compilationUnit);

                if (formatLastStatementOnly)
                {
                    AstNode node = compilationUnit.GetAdjacentNodeAt <Statement> (stubData.OffsetToLocation(formatStartOffset + formatLength - 1));
                    if (node != null)
                    {
                        while (node.Role == Roles.EmbeddedStatement || node.Role == IfElseStatement.TrueRole || node.Role == IfElseStatement.FalseRole)
                        {
                            node = node.Parent;
                        }
                        // include indentation if node starts in new line
                        var formatNode = node.GetPrevNode();
                        if (formatNode.Role != Roles.NewLine)
                        {
                            formatNode = node;
                        }
                        var start = stubData.LocationToOffset(formatNode.StartLocation);
                        if (start > formatStartOffset)
                        {
                            var end = stubData.LocationToOffset(node.EndLocation);
                            formatStartOffset = start;
                            formatLength      = end - start;
                        }
                    }
                }
                return(changes);
            }
        }
Пример #2
0
        static AstFormattingVisitor GetFormattingChanges(PolicyContainer policyParent, IEnumerable <string> mimeTypeChain, MonoDevelop.Ide.Gui.Document document, string input, DomRegion formattingRegion)
        {
            using (var stubData = TextEditorData.CreateImmutable(input)) {
                stubData.Document.FileName = document.FileName;
                var  parser          = document.HasProject ? new ICSharpCode.NRefactory.CSharp.CSharpParser(TypeSystemParser.GetCompilerArguments(document.Project)) : new ICSharpCode.NRefactory.CSharp.CSharpParser();
                var  compilationUnit = parser.Parse(stubData);
                bool hadErrors       = parser.HasErrors;
                if (hadErrors)
                {
                    using (var stubData2 = TextEditorData.CreateImmutable(input + "}")) {
                        compilationUnit = parser.Parse(stubData2);
                        hadErrors       = parser.HasErrors;
                    }
                }

                // try it out, if the behavior is better when working only with correct code.
                if (hadErrors)
                {
                    return(null);
                }

                var policy = policyParent.Get <CSharpFormattingPolicy> (mimeTypeChain);

                var formattingVisitor = new AstFormattingVisitor(policy.CreateOptions(), stubData.Document, document.Editor.CreateNRefactoryTextEditorOptions())
                {
                    HadErrors        = hadErrors,
                    FormattingRegion = formattingRegion
                };

                compilationUnit.AcceptVisitor(formattingVisitor);
                return(formattingVisitor);
            }
        }