/// <summary> /// Used for formatting the entire document /// </summary> public override string FormatText(PolicyContainer policyParent, IEnumerable<string> mimeTypeChain, string input, int startOffset, int endOffset) { var policy = policyParent.Get<DFormattingPolicy> (mimeTypeChain); var textPolicy = policyParent.Get<TextStylePolicy> (mimeTypeChain); var data = new TextEditorData{ Text = input }; if(IndentCorrectionOnly) { using(var s = data.OpenStream()) using(var r = new StreamReader(s)) D_Parser.Formatting.Indent.IndentEngineWrapper.CorrectIndent(r, startOffset, endOffset, data.Document.Replace, policy.Options, new TextStyleAdapter(textPolicy)); return data.Text; } var ast = DParser.ParseString(input, false, true) as DModule; var formattingVisitor = new DFormattingVisitor(policy.Options, new DocAdapt(data.Document), ast, new TextStyleAdapter(textPolicy)); // Only clip to a region if it's necessary if(startOffset > 0 || endOffset < input.Length-1) { formattingVisitor.CheckFormattingBoundaries = true; var dl = data.Document.OffsetToLocation(startOffset); formattingVisitor.FormattingStartLocation = new D_Parser.Dom.CodeLocation(dl.Column, dl.Line); dl = data.Document.OffsetToLocation(endOffset); formattingVisitor.FormattingEndLocation = new D_Parser.Dom.CodeLocation(dl.Column, dl.Line); } formattingVisitor.WalkThroughAst(); formattingVisitor.ApplyChanges(data.Document.Replace); return data.Text; }
// CorrectIndenting is completely unused in the entire MonoDevelopment code environment - doesn't have to be implemented /// <summary> /// Used for formatting selected code /// </summary> public override void OnTheFlyFormat(Ide.Gui.Document _doc, int startOffset, int endOffset) { var doc = _doc.Editor.Document; DFormattingPolicy policy = null; TextStylePolicy textStyle = null; if(_doc.HasProject) { policy = _doc.Project.Policies.Get<DFormattingPolicy>(Indentation.DTextEditorIndentation.mimeTypes); textStyle = _doc.Project.Policies.Get<TextStylePolicy>(Indentation.DTextEditorIndentation.mimeTypes); } else { policy = PolicyService.GetDefaultPolicy<DFormattingPolicy> (Indentation.DTextEditorIndentation.mimeTypes); textStyle = PolicyService.GetDefaultPolicy<TextStylePolicy> (Indentation.DTextEditorIndentation.mimeTypes); } if(IndentCorrectionOnly) { using(doc.OpenUndoGroup()) using(var r = doc.CreateReader()) D_Parser.Formatting.Indent.IndentEngineWrapper.CorrectIndent(r, startOffset, endOffset, doc.Replace, policy.Options, new TextStyleAdapter(textStyle) { KeepAlignmentSpaces = policy.KeepAlignmentSpaces }); return; } var ast = _doc.GetDAst(); if(ast == null) return; var formattingVisitor = new DFormattingVisitor(policy.Options, new DocAdapt(doc), ast, new TextStyleAdapter(textStyle)); formattingVisitor.CheckFormattingBoundaries = true; var dl = doc.OffsetToLocation(startOffset); formattingVisitor.FormattingStartLocation = new CodeLocation(dl.Column, dl.Line); dl = doc.OffsetToLocation(endOffset); formattingVisitor.FormattingEndLocation = new CodeLocation(dl.Column, dl.Line); formattingVisitor.WalkThroughAst(); using(doc.OpenUndoGroup()) formattingVisitor.ApplyChanges(doc.Replace); }
public static string FormatCode(string code, DModule ast = null, IDocumentAdapter document = null, DFormattingOptions options = null, ITextEditorOptions textStyle = null) { options = options ?? DFormattingOptions.CreateDStandard(); textStyle = textStyle ?? TextEditorOptions.Default; ast = ast ?? DParser.ParseString(code) as DModule; var formattingVisitor = new DFormattingVisitor(options, document ?? new TextDocument{ Text = code }, ast, textStyle); formattingVisitor.WalkThroughAst(); var sb = new StringBuilder(code); formattingVisitor.ApplyChanges((int start, int length, string insertedText) => { sb.Remove(start,length); sb.Insert(start,insertedText); }); return sb.ToString(); }
public static string FormatCode(string code, DModule ast = null, IDocumentAdapter document = null, DFormattingOptions options = null, ITextEditorOptions textStyle = null) { options = options ?? DFormattingOptions.CreateDStandard(); textStyle = textStyle ?? TextEditorOptions.Default; ast = ast ?? DParser.ParseString(code) as DModule; var formattingVisitor = new DFormattingVisitor(options, document ?? new TextDocument { Text = code }, ast, textStyle); formattingVisitor.WalkThroughAst(); var sb = new StringBuilder(code); formattingVisitor.ApplyChanges((int start, int length, string insertedText) => { sb.Remove(start, length); sb.Insert(start, insertedText); }); return(sb.ToString()); }
public void FormatDocument(DFormattingOptions policy = null) { policy = policy ?? DFormattingOptions.CreateDStandard(); var formatter = new DFormattingVisitor(policy, new DocAdapter(dEditor.Editor.Document), dEditor.SyntaxTree); formatter.WalkThroughAst(); dEditor.Editor.Document.UndoStack.StartUndoGroup(); try { formatter.ApplyChanges(dEditor.Editor.Document.Replace); } catch(Exception ex) { ErrorLogger.Log(ex, ErrorType.Error, ErrorOrigin.Parser); } dEditor.Editor.Document.UndoStack.EndUndoGroup(); }