public override void OnTheFlyFormat(PolicyContainer policyParent, IEnumerable <string> mimeTypeChain, TextEditorData data, int startOffset, int endOffset) { var parser = new CSharpParser(); var compilationUnit = parser.ParseSnippet(data); if (compilationUnit == null) { Console.WriteLine("couldn't parse : " + data.Text); return; } if (parser.HasErrors) { return; } var policy = policyParent.Get <CSharpFormattingPolicy> (mimeTypeChain); var adapter = new TextEditorDataAdapter(data); var factory = new FormattingActionFactory(data); var formattingVisitor = new ICSharpCode.NRefactory.CSharp.AstFormattingVisitor(policy.CreateOptions(), adapter, factory) { HadErrors = parser.HasErrors }; compilationUnit.AcceptVisitor(formattingVisitor, null); var changes = new List <ICSharpCode.NRefactory.CSharp.Refactoring.Action> (); changes.AddRange(formattingVisitor.Changes. Where(c => (startOffset <= c.Offset && c.Offset < endOffset))); var startPositionChange = factory.CreateTextReplaceAction(startOffset, 0, null); changes.Add(startPositionChange); var endPositionChange = factory.CreateTextReplaceAction(endOffset, 0, null); changes.Add(endPositionChange); using (var undo = data.OpenUndoGroup()) { MDRefactoringContext.MdScript.RunActions(changes, null); } if (data.IsSomethingSelected) { var range = data.SelectionRange; if (range.Offset == startOffset && range.EndOffset == endOffset) { data.SetSelection(startPositionChange.Offset, endPositionChange.Offset); } } }
public string FormatText (CSharpFormattingPolicy 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.Text = input; // System.Console.WriteLine ("-----"); // System.Console.WriteLine (data.Text.Replace (" ", ".").Replace ("\t", "->")); // System.Console.WriteLine ("-----"); var parser = new CSharpParser (); 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 adapter = new TextEditorDataAdapter (data); var factory = new FormattingActionFactory (data); var formattingVisitor = new ICSharpCode.NRefactory.CSharp.AstFormattingVisitor (policy.CreateOptions (), adapter, factory) { HadErrors = hadErrors }; compilationUnit.AcceptVisitor (formattingVisitor, null); var changes = new List<ICSharpCode.NRefactory.CSharp.Refactoring.Action> (); changes.AddRange (formattingVisitor.Changes. Where (c => (startOffset <= c.Offset && c.Offset < endOffset))); var endPositionChange = factory.CreateTextReplaceAction (endOffset, 0, null); changes.Add (endPositionChange); MDRefactoringContext.MdScript.RunActions (changes, null); // check if the formatter has produced errors parser = new CSharpParser (); 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)); } /* System.Console.WriteLine ("-----"); System.Console.WriteLine (data.Text.Replace (" ", "^").Replace ("\t", "->")); System.Console.WriteLine ("-----");*/ string result = data.GetTextBetween (startOffset, Math.Min (data.Length, endPositionChange.Offset)); data.Dispose (); return result; }
public string FormatText(CSharpFormattingPolicy 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.Text = input; // System.Console.WriteLine ("-----"); // System.Console.WriteLine (data.Text.Replace (" ", ".").Replace ("\t", "->")); // System.Console.WriteLine ("-----"); var parser = new CSharpParser(); 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 adapter = new TextEditorDataAdapter(data); var factory = new FormattingActionFactory(data); var formattingVisitor = new ICSharpCode.NRefactory.CSharp.AstFormattingVisitor(policy.CreateOptions(), adapter, factory) { HadErrors = hadErrors }; compilationUnit.AcceptVisitor(formattingVisitor, null); var changes = new List <ICSharpCode.NRefactory.CSharp.Refactoring.Action> (); changes.AddRange(formattingVisitor.Changes. Where(c => (startOffset <= c.Offset && c.Offset < endOffset))); var endPositionChange = factory.CreateTextReplaceAction(endOffset, 0, null); changes.Add(endPositionChange); MDRefactoringContext.MdScript.RunActions(changes, null); // check if the formatter has produced errors parser = new CSharpParser(); 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))); } /* System.Console.WriteLine ("-----"); * System.Console.WriteLine (data.Text.Replace (" ", "^").Replace ("\t", "->")); * System.Console.WriteLine ("-----");*/ string result = data.GetTextBetween(startOffset, Math.Min(data.Length, endPositionChange.Offset)); data.Dispose(); return(result); }
public override void OnTheFlyFormat (PolicyContainer policyParent, IEnumerable<string> mimeTypeChain, TextEditorData data, int startOffset, int endOffset) { var parser = new CSharpParser (); var compilationUnit = parser.ParseSnippet (data); if (compilationUnit == null) { Console.WriteLine ("couldn't parse : " + data.Text); return; } if (parser.HasErrors) return; var policy = policyParent.Get<CSharpFormattingPolicy> (mimeTypeChain); var adapter = new TextEditorDataAdapter (data); var factory = new FormattingActionFactory (data); var formattingVisitor = new ICSharpCode.NRefactory.CSharp.AstFormattingVisitor (policy.CreateOptions (), adapter, factory) { HadErrors = parser.HasErrors }; compilationUnit.AcceptVisitor (formattingVisitor, null); var changes = new List<ICSharpCode.NRefactory.CSharp.Refactoring.Action> (); changes.AddRange (formattingVisitor.Changes. Where (c => (startOffset <= c.Offset && c.Offset < endOffset))); var startPositionChange = factory.CreateTextReplaceAction (startOffset, 0, null); changes.Add (startPositionChange); var endPositionChange = factory.CreateTextReplaceAction (endOffset, 0, null); changes.Add (endPositionChange); using (var undo = data.OpenUndoGroup ()) { MDRefactoringContext.MdScript.RunActions (changes, null); } if (data.IsSomethingSelected) { var range = data.SelectionRange; if (range.Offset == startOffset && range.EndOffset == endOffset) { data.SetSelection (startPositionChange.Offset, endPositionChange.Offset); } } }