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 PlayScriptParser(TypeSystemParser.GetCompilerArguments(document.Project)) : new PlayScriptParser(); 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 <PlayScriptFormattingPolicy> (mimeTypeChain); var formattingVisitor = new ICSharpCode.NRefactory.PlayScript.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 FormattingVisitor(CSharpFormatter formatter, IDocument document, FormattingChanges changes, CancellationToken token) { if (formatter == null) { throw new ArgumentNullException("formatter"); } if (document == null) { throw new ArgumentNullException("document"); } if (changes == null) { throw new ArgumentNullException("changes"); } this.formatter = formatter; this.changes = changes; this.document = document; this.token = token; curIndent = new Indent(formatter.TextEditorOptions); }
public string FormatText(PlayScriptFormattingPolicy policy, TextStylePolicy textPolicy, string mimeType, string input, int startOffset, int endOffset) { var data = new TextEditorData(); data.Document.SuppressHighlightUpdate = true; data.Document.MimeType = mimeType; data.Document.FileName = "toformat.cs"; if (textPolicy != null) { data.Options.TabsToSpaces = textPolicy.TabsToSpaces; data.Options.TabSize = textPolicy.TabWidth; data.Options.IndentationSize = textPolicy.IndentWidth; data.Options.IndentStyle = textPolicy.RemoveTrailingWhitespace ? IndentStyle.Virtual : IndentStyle.Smart; } data.Text = input; // System.Console.WriteLine ("-----"); // System.Console.WriteLine (data.Text.Replace (" ", ".").Replace ("\t", "->")); // System.Console.WriteLine ("-----"); var parser = new PlayScriptParser(); var compilationUnit = parser.Parse(data); bool hadErrors = parser.HasErrors; if (hadErrors) { // foreach (var e in parser.ErrorReportPrinter.Errors) // Console.WriteLine (e.Message); return(input.Substring(startOffset, Math.Max(0, Math.Min(endOffset, input.Length) - startOffset))); } var originalVersion = data.Document.Version; var textEditorOptions = data.CreateNRefactoryTextEditorOptions(); var formattingVisitor = new ICSharpCode.NRefactory.PlayScript.CSharpFormatter( policy.CreateOptions(), textEditorOptions ) { FormattingMode = FormattingMode.Intrusive }; var changes = formattingVisitor.AnalyzeFormatting(data.Document, compilationUnit); try { changes.ApplyChanges(startOffset, endOffset - startOffset); } catch (Exception e) { LoggingService.LogError("Error in code formatter", e); return(input.Substring(startOffset, Math.Max(0, Math.Min(endOffset, input.Length) - startOffset))); } // check if the formatter has produced errors parser = new PlayScriptParser(); parser.Parse(data); if (parser.HasErrors) { LoggingService.LogError("C# formatter produced source code errors. See console for output."); return(input.Substring(startOffset, Math.Max(0, Math.Min(endOffset, input.Length) - startOffset))); } var currentVersion = data.Document.Version; string result = data.GetTextBetween(startOffset, originalVersion.MoveOffsetTo(currentVersion, endOffset, ICSharpCode.NRefactory.Editor.AnchorMovementType.Default)); data.Dispose(); return(result); }