public static TooltipInformation CreateTooltipInformation(ICompilation compilation, CSharpUnresolvedFile file, CSharpResolver resolver, TextEditorData textEditorData, MonoDevelop.CSharp.Formatting.CSharpFormattingPolicy formattingPolicy, IEntity entity, bool smartWrap, bool createFooter = false) { var tooltipInfo = new TooltipInformation(); if (resolver == null) { resolver = file != null?file.GetResolver(compilation, textEditorData.Caret.Location) : new CSharpResolver(compilation); } var sig = new SignatureMarkupCreator(resolver, formattingPolicy.CreateOptions()); sig.BreakLineAfterReturnType = smartWrap; try { tooltipInfo.SignatureMarkup = sig.GetMarkup(entity); } catch (Exception e) { LoggingService.LogError("Got exception while creating markup for :" + entity, e); return(new TooltipInformation()); } tooltipInfo.SummaryMarkup = AmbienceService.GetSummaryMarkup(entity) ?? ""; if (entity is IMember) { var evt = (IMember)entity; if (evt.ReturnType.Kind == TypeKind.Delegate) { tooltipInfo.AddCategory(GettextCatalog.GetString("Delegate Info"), sig.GetDelegateInfo(evt.ReturnType)); } } if (entity is IMethod) { var method = (IMethod)entity; if (method.IsExtensionMethod) { tooltipInfo.AddCategory(GettextCatalog.GetString("Extension Method from"), method.DeclaringTypeDefinition.FullName); } } if (createFooter) { tooltipInfo.FooterMarkup = sig.CreateFooter(entity); } return(tooltipInfo); }
public static TooltipInformation CreateTooltipInformation(ICompilation compilation, CSharpUnresolvedFile file, TextEditorData textEditorData, MonoDevelop.CSharp.Formatting.CSharpFormattingPolicy formattingPolicy, IType type, bool smartWrap, bool createFooter = false) { var tooltipInfo = new TooltipInformation(); if (type.Kind == TypeKind.Unknown) { return(tooltipInfo); } var resolver = file != null?file.GetResolver(compilation, textEditorData.Caret.Location) : new CSharpResolver(compilation); var sig = new SignatureMarkupCreator(resolver, formattingPolicy.CreateOptions()); sig.BreakLineAfterReturnType = smartWrap; try { tooltipInfo.SignatureMarkup = sig.GetMarkup(type.IsParameterized ? type.GetDefinition() : type); } catch (Exception e) { LoggingService.LogError("Got exception while creating markup for :" + type, e); return(new TooltipInformation()); } if (type.IsParameterized) { var typeInfo = new StringBuilder(); for (int i = 0; i < type.TypeParameterCount; i++) { typeInfo.AppendLine(type.GetDefinition().TypeParameters [i].Name + " is " + sig.GetTypeReferenceString(type.TypeArguments [i])); } tooltipInfo.AddCategory("Type Parameters", typeInfo.ToString()); } var def = type.GetDefinition(); if (def != null) { if (createFooter) { tooltipInfo.FooterMarkup = sig.CreateFooter(def); } tooltipInfo.SummaryMarkup = AmbienceService.GetSummaryMarkup(def) ?? ""; } return(tooltipInfo); }
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.Options.DefaultEolMarker = textPolicy.GetEolMarker(); } data.Options.OverrideDocumentEolMarker = true; 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 formattingVisitor = new ICSharpCode.NRefactory.CSharp.AstFormattingVisitor(policy.CreateOptions(), adapter, new FormattingActionFactory(data)) { 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))); 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."); Console.WriteLine(data.Text); return(input.Substring(startOffset, Math.Max(0, Math.Min(endOffset, input.Length) - startOffset))); } int end = endOffset; foreach (TextReplaceAction c in changes) { end -= c.RemovedChars; if (c.InsertedText != null) { end += c.InsertedText.Length; } } /* System.Console.WriteLine ("-----"); * System.Console.WriteLine (data.Text.Replace (" ", "^").Replace ("\t", "->")); * System.Console.WriteLine ("-----");*/ string result = data.GetTextBetween(startOffset, Math.Min(data.Length, end)); data.Dispose(); return(result); }
public static TooltipInformation CreateTooltipInformation(ICompilation compilation, CSharpUnresolvedFile file, CSharpResolver resolver, TextEditorData textEditorData, MonoDevelop.CSharp.Formatting.CSharpFormattingPolicy formattingPolicy, IEntity entity, bool smartWrap, bool createFooter = false) { var tooltipInfo = new TooltipInformation(); if (resolver == null) { resolver = file != null?file.GetResolver(compilation, textEditorData.Caret.Location) : new CSharpResolver(compilation); } var sig = new SignatureMarkupCreator(resolver, formattingPolicy.CreateOptions()); sig.BreakLineAfterReturnType = smartWrap; try { tooltipInfo.SignatureMarkup = sig.GetMarkup(entity); } catch (Exception e) { LoggingService.LogError("Got exception while creating markup for :" + entity, e); return(new TooltipInformation()); } tooltipInfo.SummaryMarkup = AmbienceService.GetSummaryMarkup(entity) ?? ""; if (entity is IMember) { var evt = (IMember)entity; if (evt.ReturnType.Kind == TypeKind.Delegate) { tooltipInfo.AddCategory(GettextCatalog.GetString("Delegate Info"), sig.GetDelegateInfo(evt.ReturnType)); } } if (entity is IMethod) { var method = (IMethod)entity; if (method.IsExtensionMethod) { tooltipInfo.AddCategory(GettextCatalog.GetString("Extension Method from"), method.DeclaringTypeDefinition.FullName); } } if (createFooter) { if (entity is IType) { var type = entity as IType; var def = type.GetDefinition(); if (def != null) { if (!string.IsNullOrEmpty(def.ParentAssembly.AssemblyName)) { var project = def.GetSourceProject(); if (project != null) { var relPath = FileService.AbsoluteToRelativePath(project.BaseDirectory, def.Region.FileName); tooltipInfo.FooterMarkup = "<small>" + GettextCatalog.GetString("Project:\t{0}", AmbienceService.EscapeText(def.ParentAssembly.AssemblyName)) + "</small>" + Environment.NewLine + "<small>" + GettextCatalog.GetString("File:\t\t{0} (line {1})", AmbienceService.EscapeText(relPath), def.Region.Begin.Line) + "</small>"; } } } } else if (entity.DeclaringTypeDefinition != null) { var project = entity.DeclaringTypeDefinition.GetSourceProject(); if (project != null) { var relPath = FileService.AbsoluteToRelativePath(project.BaseDirectory, entity.Region.FileName); tooltipInfo.FooterMarkup = "<small>" + GettextCatalog.GetString("Project:\t{0}", AmbienceService.EscapeText(project.Name)) + "</small>" + Environment.NewLine + "<small>" + GettextCatalog.GetString("From:\t{0}", AmbienceService.EscapeText(entity.DeclaringType.FullName)) + "</small>" + Environment.NewLine + "<small>" + GettextCatalog.GetString("File:\t\t{0} (line {1})", AmbienceService.EscapeText(relPath), entity.Region.Begin.Line) + "</small>"; } } } return(tooltipInfo); }
public static 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.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 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 originalVersion = data.Document.Version; var textEditorOptions = data.CreateNRefactoryTextEditorOptions(); var formattingVisitor = new ICSharpCode.NRefactory.CSharp.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 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))); } var currentVersion = data.Document.Version; string result = data.GetTextBetween(startOffset, originalVersion.MoveOffsetTo(currentVersion, endOffset)); data.Dispose(); return(result); }
public static string FormatText(CSharpFormattingPolicy policy, TextStylePolicy textPolicy, string input, int startOffset, int endOffset) { var inputTree = CSharpSyntaxTree.ParseText(input); var root = inputTree.GetRoot(); var doc = Formatter.Format(root, new TextSpan(startOffset, endOffset - startOffset), TypeSystemService.Workspace, policy.CreateOptions(textPolicy)); var result = doc.ToFullString(); return(result.Substring(startOffset, endOffset + result.Length - input.Length - startOffset)); }
public void FormatSample() { texteditor.Options = DefaultSourceEditorOptions.Instance.WithTextStyle(textStylePolicy); texteditor.Text = CSharpFormatter.FormatText(policy.CreateOptions(textStylePolicy), example, 0, example.Length); }