public static void Main(string[] args) { if (args.Length != 2) { Console.WriteLine("Usage: CheeseCake FileName OptionFileName"); return; } var sourceFileName = args [0]; var optionFileName = args [1]; var policy = LoadFormattingProfile(optionFileName); var options = LoadTextEditorProfile(optionFileName); var settings = new CompilerSettings(); settings.ConditionalSymbols.Add("UNITY_EDITOR"); settings.ConditionalSymbols.Add("UNITY_IOS"); settings.ConditionalSymbols.Add("UNITY_ANDROID"); var formatter = new CSharpFormatter(policy, options); var text = File.ReadAllText(sourceFileName); var document = new StringBuilderDocument(text); var syntaxTree = SyntaxTree.Parse(document, document.FileName, settings); var changes = formatter.AnalyzeFormatting(document, syntaxTree); changes.ApplyChanges(); File.WriteAllText(sourceFileName, document.Text, Encoding.UTF8); }
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); } }
/// <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); }
protected static FormattingChanges GetChanges(CSharpFormattingOptions policy, string input, out StringBuilderDocument document, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null) { options = GetActualOptions(options); input = NormalizeNewlines(input); document = new StringBuilderDocument(input); var visitor = new CSharpFormatter(policy, options); visitor.FormattingMode = mode; var syntaxTree = new CSharpParser().Parse(document, "test.cs"); return(visitor.AnalyzeFormatting(document, syntaxTree)); }
public static string FormatCode(string text, CSharpFormattingOptions options, CancellationToken ct) { var formatter = new CSharpFormatter(options) { FormattingMode = FormattingMode.Intrusive }; text = text.Replace("\r\n\r\n", "\r\n"); var doc = new StringBuilderDocument(text); var syntaxTree = SyntaxTree.Parse(doc, doc.FileName, null, ct); formatter.AnalyzeFormatting(doc, syntaxTree, ct).ApplyChanges(); return(doc.Text); }
/*public static string ApplyChanges (string text, List<TextReplaceAction> changes) * { * changes.Sort ((x, y) => y.Offset.CompareTo (x.Offset)); * StringBuilder b = new StringBuilder(text); * foreach (var change in changes) { * //Console.WriteLine ("---- apply:" + change); * // Console.WriteLine (adapter.Text); * if (change.Offset > b.Length) * continue; * b.Remove(change.Offset, change.RemovedChars); * b.Insert(change.Offset, change.InsertedText); * } * // Console.WriteLine ("---result:"); * // Console.WriteLine (adapter.Text); * return b.ToString(); * }*/ protected static IDocument GetResult(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive) { input = NormalizeNewlines(input); var document = new StringBuilderDocument(input); var options = new TextEditorOptions(); options.EolMarker = "\n"; options.WrapLineLength = 80; var visitor = new CSharpFormatter(policy, options); visitor.FormattingMode = mode; var syntaxTree = new CSharpParser().Parse(document, "test.cs"); var changes = visitor.AnalyzeFormatting(document, syntaxTree); changes.ApplyChanges(); return(document); }
/// <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(); }
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); } }