コード例 #1
0
        // 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);
        }
コード例 #2
0
        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));
        }
コード例 #3
0
        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());
        }
コード例 #4
0
		// 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;
		}
コード例 #5
0
ファイル: GuiBuilderService.cs プロジェクト: txdv/monodevelop
		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);
		}
コード例 #6
0
		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 ();
		}
コード例 #7
0
        // 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);
        }
コード例 #8
0
		// 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;
		}
コード例 #9
0
 string MonoDevelop.Core.Text.ITextSource.GetTextAt(int offset, int length)
 {
     return(snapshot.GetTextAt(offset, length));
 }
コード例 #10
0
		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 ();
		}