예제 #1
0
        // CorrectIndenting is completely unused in the entire MonoDevelopment code environment - doesn't have to be implemented

        /// <summary>
        /// Used for formatting selected code
        /// </summary>
        public override void OnTheFlyFormat(Ide.Gui.Document _doc, int startOffset, int endOffset)
        {
            var doc = _doc.Editor.Document;

            DFormattingPolicy policy    = null;
            TextStylePolicy   textStyle = null;

            if (_doc.HasProject)
            {
                policy    = _doc.Project.Policies.Get <DFormattingPolicy>(Indentation.DTextEditorIndentation.mimeTypes);
                textStyle = _doc.Project.Policies.Get <TextStylePolicy>(Indentation.DTextEditorIndentation.mimeTypes);
            }
            else
            {
                policy    = PolicyService.GetDefaultPolicy <DFormattingPolicy> (Indentation.DTextEditorIndentation.mimeTypes);
                textStyle = PolicyService.GetDefaultPolicy <TextStylePolicy> (Indentation.DTextEditorIndentation.mimeTypes);
            }

            if (IndentCorrectionOnly)
            {
                using (doc.OpenUndoGroup())
                    using (var r = doc.CreateReader())
                        D_Parser.Formatting.Indent.IndentEngineWrapper.CorrectIndent(r,
                                                                                     startOffset, endOffset,
                                                                                     doc.Replace, policy.Options,
                                                                                     new TextStyleAdapter(textStyle)
                        {
                            KeepAlignmentSpaces = policy.KeepAlignmentSpaces
                        });
                return;
            }

            var dpd = _doc.ParsedDocument as ParsedDModule;

            if (dpd == null)
            {
                return;
            }

            var formattingVisitor = new DFormattingVisitor(policy.Options, new DocAdapt(doc), dpd.DDom, new TextStyleAdapter(textStyle));

            formattingVisitor.CheckFormattingBoundaries = true;
            var dl = doc.OffsetToLocation(startOffset);

            formattingVisitor.FormattingStartLocation = new CodeLocation(dl.Column, dl.Line);
            dl = doc.OffsetToLocation(endOffset);
            formattingVisitor.FormattingEndLocation = new CodeLocation(dl.Column, dl.Line);

            formattingVisitor.WalkThroughAst();

            using (doc.OpenUndoGroup())
                formattingVisitor.ApplyChanges(doc.Replace);
        }
예제 #2
0
        /// <summary>
        /// Used for formatting the entire document
        /// </summary>
        public override string FormatText(PolicyContainer policyParent, IEnumerable <string> mimeTypeChain, string input, int startOffset, int endOffset)
        {
            var policy     = policyParent.Get <DFormattingPolicy> (mimeTypeChain);
            var textPolicy = policyParent.Get <TextStylePolicy> (mimeTypeChain);
            var data       = new TextEditorData {
                Text = input
            };

            if (IndentCorrectionOnly)
            {
                using (data.Document.OpenUndoGroup())
                    using (var s = data.OpenStream())
                        using (var r = new StreamReader(s))
                            D_Parser.Formatting.Indent.IndentEngineWrapper.CorrectIndent(r,
                                                                                         startOffset, endOffset,
                                                                                         data.Document.Replace, policy.Options,
                                                                                         new TextStyleAdapter(textPolicy)
                            {
                                KeepAlignmentSpaces = policy.KeepAlignmentSpaces
                            });
                return(data.Text);
            }

            var ast = DParser.ParseString(input, false, true);
            var formattingVisitor = new DFormattingVisitor(policy.Options, new DocAdapt(data.Document), ast, new TextStyleAdapter(textPolicy));

            // Only clip to a region if it's necessary
            if (startOffset > 0 || endOffset < input.Length - 1)
            {
                formattingVisitor.CheckFormattingBoundaries = true;
                var dl = data.Document.OffsetToLocation(startOffset);
                formattingVisitor.FormattingStartLocation = new CodeLocation(dl.Column, dl.Line);
                dl = data.Document.OffsetToLocation(endOffset);
                formattingVisitor.FormattingEndLocation = new CodeLocation(dl.Column, dl.Line);
            }

            formattingVisitor.WalkThroughAst();

            using (data.Document.OpenUndoGroup())
                formattingVisitor.ApplyChanges(data.Document.Replace);

            return(data.Text);
        }
예제 #3
0
        public void FormatDocument(DFormattingOptions policy = null)
        {
            policy = policy ?? DFormattingOptions.CreateDStandard();

            var formatter = new DFormattingVisitor(policy, new DocAdapter(dEditor.Editor.Document), dEditor.SyntaxTree);

            formatter.WalkThroughAst();

            dEditor.Editor.Document.UndoStack.StartUndoGroup();
            try
            {
                formatter.ApplyChanges(dEditor.Editor.Document.Replace);
            }
            catch (Exception ex)
            {
                ErrorLogger.Log(ex, ErrorType.Error, ErrorOrigin.Parser);
            }
            dEditor.Editor.Document.UndoStack.EndUndoGroup();
        }