// Returns a stream with the content of the file. // project and language parameters are optional public virtual Stream CreateFileContent(SolutionItem policyParent, Project project, string language, string fileName, string identifier) { Dictionary <string, string> tags = new Dictionary <string, string> (); ModifyTags(policyParent, project, language, identifier, fileName, ref tags); string content = CreateContent(project, tags, language); content = StringParserService.Parse(content, tags); string mime = DesktopService.GetMimeTypeForUri(fileName); CodeFormatter formatter = !string.IsNullOrEmpty(mime) ? CodeFormatterService.GetFormatter(mime) : null; if (formatter != null) { var formatted = formatter.FormatText(policyParent != null ? policyParent.Policies : null, content); if (formatted != null) { content = formatted; } } MemoryStream ms = new MemoryStream(); byte[] data; if (AddStandardHeader) { string header = StandardHeaderService.GetHeader(policyParent, fileName, true); data = System.Text.Encoding.UTF8.GetBytes(header); ms.Write(data, 0, data.Length); } Mono.TextEditor.TextDocument doc = new Mono.TextEditor.TextDocument(); doc.Text = content; TextStylePolicy textPolicy = policyParent != null?policyParent.Policies.Get <TextStylePolicy> ("text/plain") : MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy <TextStylePolicy> ("text/plain"); string eolMarker = TextStylePolicy.GetEolMarker(textPolicy.EolMarker); byte[] eolMarkerBytes = System.Text.Encoding.UTF8.GetBytes(eolMarker); var tabToSpaces = textPolicy.TabsToSpaces? new string (' ', textPolicy.TabWidth) : null; foreach (Mono.TextEditor.DocumentLine line in doc.Lines) { var lineText = doc.GetTextAt(line.Offset, line.Length); if (tabToSpaces != null) { lineText = lineText.Replace("\t", tabToSpaces); } data = System.Text.Encoding.UTF8.GetBytes(lineText); ms.Write(data, 0, data.Length); ms.Write(eolMarkerBytes, 0, eolMarkerBytes.Length); } ms.Position = 0; return(ms); }
static string StripHeaderAndBlankLines(string text, CodeDomProvider provider) { Mono.TextEditor.TextDocument doc = new Mono.TextEditor.TextDocument(); doc.Text = text; int realStartLine = 0; for (int i = 1; i <= doc.LineCount; i++) { string lineText = doc.GetTextAt(doc.GetLine(i)); // Microsoft.NET generates "auto-generated" tags where Mono generates "autogenerated" tags. if (lineText.Contains("</autogenerated>") || lineText.Contains("</auto-generated>")) { realStartLine = i + 2; break; } } // The Mono provider inserts additional blank lines, so strip them out // But blank lines might actually be significant in other languages. // We reformat the C# generated output to the user's coding style anyway, but the reformatter preserves blank lines if (provider is Microsoft.CSharp.CSharpCodeProvider) { bool previousWasBlank = false; for (int i = 1; i <= doc.LineCount; i++) { Mono.TextEditor.DocumentLine line = doc.GetLine(i); bool isBlank, isBracket; CheckLine(doc, line, out isBlank, out isBracket); if (isBlank && previousWasBlank && line.LengthIncludingDelimiter > 0) { doc.Remove(line.Offset, line.LengthIncludingDelimiter); i--; } previousWasBlank = isBlank || isBracket; } } int offset = doc.GetLine(realStartLine).Offset; return(doc.GetTextAt(offset, doc.TextLength - offset)); }
static string AddIndent(string text, string indent) { Mono.TextEditor.TextDocument doc = new Mono.TextEditor.TextDocument(); doc.Text = text; StringBuilder result = new StringBuilder(); foreach (var line in doc.Lines) { result.Append(indent); result.Append(doc.GetTextAt(line.SegmentIncludingDelimiter)); } return(result.ToString()); }
// Returns a stream with the content of the file. // project and language parameters are optional public virtual Stream CreateFileContent (SolutionItem policyParent, Project project, string language, string fileName, string identifier) { Dictionary<string, string> tags = new Dictionary<string, string> (); ModifyTags (policyParent, project, language, identifier, fileName, ref tags); string content = CreateContent (project, tags, language); content = StringParserService.Parse (content, tags); string mime = DesktopService.GetMimeTypeForUri (fileName); CodeFormatter formatter = !string.IsNullOrEmpty (mime) ? CodeFormatterService.GetFormatter (mime) : null; if (formatter != null) { var formatted = formatter.FormatText (policyParent != null ? policyParent.Policies : null, content); if (formatted != null) content = formatted; } MemoryStream ms = new MemoryStream (); byte[] data; if (AddStandardHeader) { string header = StandardHeaderService.GetHeader (policyParent, fileName, true); data = System.Text.Encoding.UTF8.GetBytes (header); ms.Write (data, 0, data.Length); } Mono.TextEditor.TextDocument doc = new Mono.TextEditor.TextDocument (); doc.Text = content; TextStylePolicy textPolicy = policyParent != null ? policyParent.Policies.Get<TextStylePolicy> ("text/plain") : MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy<TextStylePolicy> ("text/plain"); string eolMarker = TextStylePolicy.GetEolMarker (textPolicy.EolMarker); byte[] eolMarkerBytes = System.Text.Encoding.UTF8.GetBytes (eolMarker); var tabToSpaces = textPolicy.TabsToSpaces? new string (' ', textPolicy.TabWidth) : null; foreach (Mono.TextEditor.LineSegment line in doc.Lines) { var lineText = doc.GetTextAt (line.Offset, line.Length); if (tabToSpaces != null) lineText = lineText.Replace ("\t", tabToSpaces); data = System.Text.Encoding.UTF8.GetBytes (lineText); ms.Write (data, 0, data.Length); ms.Write (eolMarkerBytes, 0, eolMarkerBytes.Length); } ms.Position = 0; return ms; }
static string StripHeaderAndBlankLines (string text, CodeDomProvider provider) { Mono.TextEditor.TextDocument doc = new Mono.TextEditor.TextDocument (); doc.Text = text; int realStartLine = 0; for (int i = 1; i <= doc.LineCount; i++) { string lineText = doc.GetTextAt (doc.GetLine (i)); // Microsoft.NET generates "auto-generated" tags where Mono generates "autogenerated" tags. if (lineText.Contains ("</autogenerated>") || lineText.Contains ("</auto-generated>")) { realStartLine = i + 2; break; } } // The Mono provider inserts additional blank lines, so strip them out // But blank lines might actually be significant in other languages. // We reformat the C# generated output to the user's coding style anyway, but the reformatter preserves blank lines if (provider is Microsoft.CSharp.CSharpCodeProvider) { bool previousWasBlank = false; for (int i = 1; i <= doc.LineCount; i++) { Mono.TextEditor.LineSegment line = doc.GetLine (i); bool isBlank, isBracket; CheckLine (doc, line, out isBlank, out isBracket); if (isBlank && previousWasBlank && line.LengthIncludingDelimiter > 0) { doc.Remove (line.Offset, line.LengthIncludingDelimiter); i--; } previousWasBlank = isBlank || isBracket; } } int offset = doc.GetLine (realStartLine).Offset; return doc.GetTextAt (offset, doc.TextLength - offset); }
static string AddIndent (string text, string indent) { Mono.TextEditor.TextDocument doc = new Mono.TextEditor.TextDocument (); doc.Text = text; StringBuilder result = new StringBuilder (); foreach (var line in doc.Lines) { result.Append (indent); result.Append (doc.GetTextAt (line)); } return result.ToString (); }
// Returns a stream with the content of the file. // project and language parameters are optional public virtual Stream CreateFileContent(SolutionItem policyParent, Project project, string language, string fileName, string identifier) { var model = GetTagModel(policyParent, project, language, identifier, fileName); //HACK: for API compat, CreateContent just gets the override, not the base model // but ProcessContent gets the entire model string content = CreateContent(project, model.OverrideTags, language); content = ProcessContent(content, model); string mime = DesktopService.GetMimeTypeForUri(fileName); CodeFormatter formatter = !string.IsNullOrEmpty(mime) ? CodeFormatterService.GetFormatter(mime) : null; if (formatter != null) { var formatted = formatter.FormatText(policyParent != null ? policyParent.Policies : null, content); if (formatted != null) { content = formatted; } } var ms = new MemoryStream(); var bom = Encoding.UTF8.GetPreamble(); ms.Write(bom, 0, bom.Length); byte[] data; if (AddStandardHeader) { string header = StandardHeaderService.GetHeader(policyParent, fileName, true); data = System.Text.Encoding.UTF8.GetBytes(header); ms.Write(data, 0, data.Length); } Mono.TextEditor.TextDocument doc = new Mono.TextEditor.TextDocument(); doc.Text = content; TextStylePolicy textPolicy = policyParent != null?policyParent.Policies.Get <TextStylePolicy> ("text/plain") : MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy <TextStylePolicy> ("text/plain"); string eolMarker = TextStylePolicy.GetEolMarker(textPolicy.EolMarker); byte[] eolMarkerBytes = System.Text.Encoding.UTF8.GetBytes(eolMarker); var tabToSpaces = textPolicy.TabsToSpaces? new string (' ', textPolicy.TabWidth) : null; foreach (Mono.TextEditor.DocumentLine line in doc.Lines) { var lineText = doc.GetTextAt(line.Offset, line.Length); if (tabToSpaces != null) { lineText = lineText.Replace("\t", tabToSpaces); } data = System.Text.Encoding.UTF8.GetBytes(lineText); ms.Write(data, 0, data.Length); ms.Write(eolMarkerBytes, 0, eolMarkerBytes.Length); } ms.Position = 0; return(ms); }
// Returns a stream with the content of the file. // project and language parameters are optional public virtual Stream CreateFileContent (SolutionItem policyParent, Project project, string language, string fileName, string identifier) { var model = GetTagModel (policyParent, project, language, identifier, fileName); //HACK: for API compat, CreateContent just gets the override, not the base model // but ProcessContent gets the entire model string content = CreateContent (project, model.OverrideTags, language); content = ProcessContent (content, model); string mime = DesktopService.GetMimeTypeForUri (fileName); CodeFormatter formatter = !string.IsNullOrEmpty (mime) ? CodeFormatterService.GetFormatter (mime) : null; if (formatter != null) { var formatted = formatter.FormatText (policyParent != null ? policyParent.Policies : null, content); if (formatted != null) content = formatted; } var ms = new MemoryStream (); var bom = Encoding.UTF8.GetPreamble (); ms.Write (bom, 0, bom.Length); byte[] data; if (AddStandardHeader) { string header = StandardHeaderService.GetHeader (policyParent, fileName, true); data = System.Text.Encoding.UTF8.GetBytes (header); ms.Write (data, 0, data.Length); } Mono.TextEditor.TextDocument doc = new Mono.TextEditor.TextDocument (); doc.Text = content; TextStylePolicy textPolicy = policyParent != null ? policyParent.Policies.Get<TextStylePolicy> ("text/plain") : MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy<TextStylePolicy> ("text/plain"); string eolMarker = TextStylePolicy.GetEolMarker (textPolicy.EolMarker); byte[] eolMarkerBytes = System.Text.Encoding.UTF8.GetBytes (eolMarker); var tabToSpaces = textPolicy.TabsToSpaces? new string (' ', textPolicy.TabWidth) : null; foreach (Mono.TextEditor.DocumentLine line in doc.Lines) { var lineText = doc.GetTextAt (line.Offset, line.Length); if (tabToSpaces != null) lineText = lineText.Replace ("\t", tabToSpaces); data = System.Text.Encoding.UTF8.GetBytes (lineText); ms.Write (data, 0, data.Length); ms.Write (eolMarkerBytes, 0, eolMarkerBytes.Length); } ms.Position = 0; return ms; }
string MonoDevelop.Core.Text.ITextSource.GetTextAt(int offset, int length) { return(snapshot.GetTextAt(offset, length)); }
static string AddIndent (string text, string indent) { var doc = new Mono.TextEditor.TextDocument (); doc.Text = text; var result = new StringBuilder (); foreach (var line in doc.Lines) { result.Append (indent); result.Append (doc.GetTextAt (line.SegmentIncludingDelimiter)); } return result.ToString (); }