public override void FormatText(IEnumerable <AstNode> nodes)
        {
            var syntaxTree = SyntaxTree.Parse(currentDocument, "dummy.cs");
            var formatter  = new CSharpFormatter(FormattingOptions, Options);
            var segments   = new List <ISegment>();

            foreach (var node in nodes.OrderByDescending(n => n.StartLocation))
            {
                var segment = GetSegment(node);

                formatter.AddFormattingRegion(new ICSharpCode.NRefactory.TypeSystem.DomRegion(
                                                  currentDocument.GetLocation(segment.Offset),
                                                  currentDocument.GetLocation(segment.EndOffset)
                                                  ));
                segments.Add(segment);
            }
            if (segments.Count == 0)
            {
                return;
            }
            var changes = formatter.AnalyzeFormatting(currentDocument, syntaxTree);

            foreach (var segment in segments)
            {
                changes.ApplyChanges(segment.Offset, segment.Length - 1);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Formats the specified part of the document.
        /// </summary>
        public static void Format(ITextEditor editor, int offset, int length, CSharpFormattingOptions options)
        {
            var formatter = new CSharpFormatter(options, editor.ToEditorOptions());

            formatter.AddFormattingRegion(new DomRegion(editor.Document.GetLocation(offset), editor.Document.GetLocation(offset + length)));
            var changes = formatter.AnalyzeFormatting(editor.Document, SyntaxTree.Parse(editor.Document));

            changes.ApplyChanges(offset, length);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Formats the specified part of the document.
        /// </summary>
        public static void Format(ITextEditor editor, int offset, int length, CSharpFormattingOptionsContainer optionsContainer)
        {
            TextEditorOptions editorOptions = editor.ToEditorOptions();

            optionsContainer.CustomizeEditorOptions(editorOptions);
            var formatter = new CSharpFormatter(optionsContainer.GetEffectiveOptions(), editorOptions);

            formatter.AddFormattingRegion(new DomRegion(editor.Document.GetLocation(offset), editor.Document.GetLocation(offset + length)));
            var changes = formatter.AnalyzeFormatting(editor.Document, SyntaxTree.Parse(editor.Document));

            changes.ApplyChanges(offset, length);
        }
        /// <summary>
        /// Formats the specified part of the document.
        /// </summary>
        public static void Format(ITextEditor editor, int offset, int length, CSharpFormattingOptionsContainer optionsContainer)
        {
            SyntaxTree syntaxTree = SyntaxTree.Parse(editor.Document);

            if (syntaxTree.Errors.Count > 0)
            {
                // Don't format files containing syntax errors!
                return;
            }

            TextEditorOptions editorOptions = editor.ToEditorOptions();

            optionsContainer.CustomizeEditorOptions(editorOptions);
            var formatter = new CSharpFormatter(optionsContainer.GetEffectiveOptions(), editorOptions);

            formatter.AddFormattingRegion(new DomRegion(editor.Document.GetLocation(offset), editor.Document.GetLocation(offset + length)));
            var changes = formatter.AnalyzeFormatting(editor.Document, syntaxTree);

            changes.ApplyChanges(offset, length);
        }
        public override void Complete(TextArea textArea, ISegment completionSegment, EventArgs insertionRequestEventArgs)
        {
            if (declarationBegin > completionSegment.Offset)
            {
                base.Complete(textArea, completionSegment, insertionRequestEventArgs);
                return;
            }
            var b = new TypeSystemAstBuilder(new CSharpResolver(contextAtCaret))
            {
                ShowTypeParameterConstraints = false,
                GenerateBody = true
            };

            var entityDeclaration = b.ConvertEntity(this.Entity);

            entityDeclaration.Modifiers &= ~(Modifiers.Virtual | Modifiers.Abstract);
            entityDeclaration.Modifiers |= Modifiers.Override;

            if (!this.Entity.IsAbstract)
            {
                // modify body to call the base method
                if (this.Entity.SymbolKind == SymbolKind.Method)
                {
                    var baseCall = new BaseReferenceExpression().Invoke(this.Entity.Name, ParametersToExpressions(this.Entity));
                    var body     = entityDeclaration.GetChildByRole(Roles.Body);
                    body.Statements.Clear();
                    if (((IMethod)this.Entity).ReturnType.IsKnownType(KnownTypeCode.Void))
                    {
                        body.Statements.Add(new ExpressionStatement(baseCall));
                    }
                    else
                    {
                        body.Statements.Add(new ReturnStatement(baseCall));
                    }
                }
                else if (this.Entity.SymbolKind == SymbolKind.Indexer || this.Entity.SymbolKind == SymbolKind.Property)
                {
                    Expression baseCall;
                    if (this.Entity.SymbolKind == SymbolKind.Indexer)
                    {
                        baseCall = new BaseReferenceExpression().Indexer(ParametersToExpressions(this.Entity));
                    }
                    else
                    {
                        baseCall = new BaseReferenceExpression().Member(this.Entity.Name);
                    }
                    var getterBody = entityDeclaration.GetChildByRole(PropertyDeclaration.GetterRole).Body;
                    if (!getterBody.IsNull)
                    {
                        getterBody.Statements.Clear();
                        getterBody.Add(new ReturnStatement(baseCall.Clone()));
                    }
                    var setterBody = entityDeclaration.GetChildByRole(PropertyDeclaration.SetterRole).Body;
                    if (!setterBody.IsNull)
                    {
                        setterBody.Statements.Clear();
                        setterBody.Add(new AssignmentExpression(baseCall.Clone(), new IdentifierExpression("value")));
                    }
                }
            }

            var          document          = textArea.Document;
            StringWriter w                 = new StringWriter();
            var          formattingOptions = FormattingOptionsFactory.CreateSharpDevelop();
            var          segmentDict       = SegmentTrackingOutputFormatter.WriteNode(w, entityDeclaration, formattingOptions, textArea.Options);

            string newText = w.ToString().TrimEnd();

            document.Replace(declarationBegin, completionSegment.EndOffset - declarationBegin, newText);
            var throwStatement = entityDeclaration.Descendants.FirstOrDefault(n => n is ThrowStatement);

            if (throwStatement != null)
            {
                var segment = segmentDict[throwStatement];
                textArea.Selection = new RectangleSelection(textArea, new TextViewPosition(textArea.Document.GetLocation(declarationBegin + segment.Offset)), new TextViewPosition(textArea.Document.GetLocation(declarationBegin + segment.Offset + segment.Length)));
            }

            //format the inserted code nicely
            var formatter = new CSharpFormatter(formattingOptions);

            formatter.AddFormattingRegion(new DomRegion(document.GetLocation(declarationBegin), document.GetLocation(declarationBegin + newText.Length)));
            var syntaxTree = new CSharpParser().Parse(document);

            formatter.AnalyzeFormatting(document, syntaxTree).ApplyChanges();
        }
Ejemplo n.º 6
0
        public override void FormatText(IEnumerable<AstNode> nodes)
        {
            var syntaxTree = SyntaxTree.Parse(currentDocument, "dummy.cs");
            var formatter = new CSharpFormatter(FormattingOptions, Options);
            var segments = new List<ISegment>();
            foreach (var node in nodes.OrderByDescending (n => n.StartLocation)) {
                var segment = GetSegment(node);

                formatter.AddFormattingRegion (new ICSharpCode.NRefactory.TypeSystem.DomRegion (
                    currentDocument.GetLocation (segment.Offset),
                    currentDocument.GetLocation (segment.EndOffset)
                    ));
                segments.Add(segment);
            }
            if (segments.Count == 0)
                return;
            var changes = formatter.AnalyzeFormatting (currentDocument, syntaxTree);
            foreach (var segment in segments) {
                changes.ApplyChanges(segment.Offset, segment.Length - 1);
            }
        }