public AddFormattingRegion ( DomRegion region ) : void | ||
region | DomRegion | |
return | void |
/// <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); }
/// <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); }
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); } }
/// <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); }
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; } }
public override void Complete(ICSharpCode.AvalonEdit.Editing.TextArea textArea, ISegment completionSegment, EventArgs insertionRequestEventArgs) { if (declarationBegin > completionSegment.Offset) { base.Complete(textArea, completionSegment, insertionRequestEventArgs); return; } TypeSystemAstBuilder b = new TypeSystemAstBuilder(new CSharpResolver(contextAtCaret)); b.ShowTypeParameterConstraints = false; b.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.EntityType == EntityType.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.EntityType == EntityType.Indexer || this.Entity.EntityType == EntityType.Property) { Expression baseCall; if (this.Entity.EntityType == EntityType.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(); }