void UpdateExample(string example)
        {
            string text;

            if (!string.IsNullOrEmpty(example))
            {
                text = Environment.NewLine != "\n" ? example.Replace("\n", Environment.NewLine) : example;
            }
            else
            {
                text = "";
            }

            var types      = DesktopService.GetMimeTypeInheritanceChain(MonoDevelop.CSharp.Formatting.CSharpFormatter.MimeType);
            var textPolicy = MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy <TextStylePolicy> (types);

            texteditor.Text = CSharpFormatter.FormatText(profile, textPolicy, text, 0, text.Length);
        }
Exemplo n.º 2
0
        void RunFormatter()
        {
            if (PropertyService.Get("OnTheFlyFormatting", false) && textEditorData != null)
            {
                textEditorData.Paste -= TextEditorDataPaste;
                //		textEditorData.Document.TextReplaced -= TextCut;
                ProjectDom dom = ProjectDomService.GetProjectDom(Document.Project);
                if (dom == null)
                {
                    dom = ProjectDomService.GetFileDom(Document.FileName);
                }

                DomLocation location = new DomLocation(textEditorData.Caret.Location.Line, textEditorData.Caret.Location.Column);
                CSharpFormatter.Format(textEditorData, dom, Document.CompilationUnit, location);
//				OnTheFlyFormatter.Format (textEditorData, dom, location);

                //		textEditorData.Document.TextReplaced += TextCut;
                textEditorData.Paste += TextEditorDataPaste;
            }
        }
Exemplo n.º 3
0
        public override void CreateNewType(AstNode newType, NewTypeContext ntctx)
        {
            if (newType == null)
            {
                throw new System.ArgumentNullException("newType");
            }
            var correctFileName = MoveTypeToFile.GetCorrectFileName(context, (EntityDeclaration)newType);

            var content = context.TextEditor.Text;

            var types = new List <EntityDeclaration> (context.Unit.GetTypes());

            types.Sort((x, y) => y.StartLocation.CompareTo(x.StartLocation));

            foreach (var removeType in types)
            {
                var start = context.GetOffset(removeType.StartLocation);
                var end   = context.GetOffset(removeType.EndLocation);
                content = content.Remove(start, end - start);
            }

            var insertLocation = types.Count > 0 ? context.GetOffset(types.Last().StartLocation) : -1;

            if (insertLocation < 0 || insertLocation > content.Length)
            {
                insertLocation = content.Length;
            }
            content = content.Substring(0, insertLocation) + newType.ToString(FormattingOptions) + content.Substring(insertLocation);

            var formatter  = new MonoDevelop.CSharp.Formatting.CSharpFormatter();
            var policy     = context.Project.Policies.Get <CSharpFormattingPolicy> ();
            var textPolicy = context.Project.Policies.Get <TextStylePolicy> ();

            content = formatter.FormatText(policy, textPolicy, MonoDevelop.CSharp.Formatting.CSharpFormatter.MimeType, content, 0, content.Length);

            File.WriteAllText(correctFileName, content);
            context.Project.AddFile(correctFileName);
            IdeApp.ProjectOperations.Save(context.Project);
            IdeApp.Workbench.OpenDocument(correctFileName);
        }
Exemplo n.º 4
0
		public static void Format (TextEditorData data, IType type, IMember member, ProjectDom dom, ICompilationUnit unit, DomLocation caretLocation)
		{
			if (type == null)
				return;
			if (member == null) {
				//				member = type;
				return;
			}
			string wrapper;
			int endPos;
			wrapper = CreateWrapperClassForMember (member, member != type, data, out endPos);
			if (string.IsNullOrEmpty (wrapper) || endPos < 0)
				return;
		//			Console.WriteLine (wrapper);
			
			int bracketIndex = wrapper.IndexOf ('{') + 1;
			int col = GetColumn (wrapper, bracketIndex, data.Options.TabSize);
			
			CSharpFormatter formatter = new CSharpFormatter ();
			formatter.startIndentLevel = System.Math.Max (0, col / data.Options.TabSize - 1);
			
			string formattedText = formatter.InternalFormat (dom != null && dom.Project != null ? dom.Project.Policies : null, MimeType, wrapper, 0, wrapper.Length);
			
			if (formatter.hasErrors)
				return;
			
			int startPos = data.Document.LocationToOffset (member.Location.Line, 1) - 1;
			InFormat = true;
			if (member != type) {
				int len1 = formattedText.IndexOf ('{') + 1;
				int last = formattedText.LastIndexOf ('}');
				formattedText = formattedText.Substring (len1, last - len1 - 1).TrimStart ('\n', '\r');
			} else {
				startPos++;
			}
			
			//Console.WriteLine ("formattedText0:" + formattedText.Replace ("\t", "->").Replace (" ", "°"));
			if (member != type) {
				string indentToRemove = GetIndent (formattedText);
		//				Console.WriteLine ("Remove:" + indentToRemove.Replace ("\t", "->").Replace (" ", "°"));
				formattedText = RemoveIndent (formattedText, indentToRemove);
			} else {
				formattedText = formattedText.TrimStart ();
			}
			//Console.WriteLine ("Indent:" + GetIndent (data, member.Location.Line - 1).Replace ("\t", "->").Replace (" ", "°"));
			//Console.WriteLine ("formattedText1:" + formattedText.Replace ("\t", "->").Replace (" ", "°"));
			formattedText = AddIndent (formattedText, GetIndent (data, member.Location.Line));
			
			Document doc = new Document ();
			doc.Text = formattedText;
			for (int i = doc.LineCount; i --> DocumentLocation.MinLine;) {
				LineSegment lineSegment = doc.GetLine (i);
				if (doc.IsEmptyLine (lineSegment))
					((IBuffer)doc).Remove (lineSegment.Offset, lineSegment.Length);
			}
			formattedText = doc.Text;
			
			//Console.WriteLine ("formattedText3:" + formattedText.Replace ("\t", "->").Replace (" ", "°").Replace ("\n", "\\n").Replace ("\r", "\\r"));
	
			int textLength = CanInsertFormattedText (data, startPos, data.Document.LocationToOffset (caretLocation.Line, caretLocation.Column), formattedText);
			if (textLength > 0) {
//				Console.WriteLine (formattedText.Substring (0, textLength));
				InsertFormattedText (data, startPos, formattedText.Substring (0, textLength).TrimEnd ());
			} else {
				Console.WriteLine ("Can't insert !!!");
			}
			InFormat = false;
		}
		public override void CreateNewType (AstNode newType, NewTypeContext ntctx)
		{
			if (newType == null)
				throw new System.ArgumentNullException ("newType");
			var correctFileName = MoveTypeToFile.GetCorrectFileName (context, (EntityDeclaration)newType);
			
			var content = context.TextEditor.Text;
			
			var types = new List<EntityDeclaration> (context.Unit.GetTypes ());
			types.Sort ((x, y) => y.StartLocation.CompareTo (x.StartLocation));

			foreach (var removeType in types) {
				var start = context.GetOffset (removeType.StartLocation);
				var end = context.GetOffset (removeType.EndLocation);
				content = content.Remove (start, end - start);
			}
			
			var insertLocation = types.Count > 0 ? context.GetOffset (types.Last ().StartLocation) : -1;
			if (insertLocation < 0 || insertLocation > content.Length)
				insertLocation = content.Length;
			content = content.Substring (0, insertLocation) + newType.ToString (FormattingOptions) + content.Substring (insertLocation);

			var formatter = new MonoDevelop.CSharp.Formatting.CSharpFormatter ();
			var policy = context.Project.Policies.Get<CSharpFormattingPolicy> ();
			var textPolicy = context.Project.Policies.Get<TextStylePolicy> ();

			content = formatter.FormatText (policy, textPolicy, MonoDevelop.CSharp.Formatting.CSharpFormatter.MimeType, content, 0, content.Length);

			File.WriteAllText (correctFileName, content);
			context.Project.AddFile (correctFileName);
			IdeApp.ProjectOperations.Save (context.Project);
			IdeApp.Workbench.OpenDocument (correctFileName);
		}
        public void FormatSample()
        {
            texteditor.Options = DefaultSourceEditorOptions.Instance.WithTextStyle(textStylePolicy);

            texteditor.Text = CSharpFormatter.FormatText(policy, textStylePolicy, example, 0, example.Length);
        }
        public void FormatSample()
        {
            var formatter = new CSharpFormatter();

            texteditor.Document.Text = formatter.FormatText(policy, null, CSharpFormatter.MimeType, example, 0, example.Length);
        }
		void UpdateExample (string example)
		{
			CSharpFormatter formatter = new CSharpFormatter ();
			var parent = new MonoDevelop.Projects.DotNetAssemblyProject ();
			parent.Policies.Set<CSharpFormattingPolicy> (profile, CSharpFormatter.MimeType);
			texteditor.Document.Text  = formatter.FormatText (parent.Policies, CSharpFormatter.MimeType, example);
		}
		public CSharpFormattingPolicyPanelWidget ()
		{
			this.Build ();
			buttonNew.Clicked += HandleButtonNewClicked;
			buttonImport.Clicked += HandleButtonImportClicked; 
			buttonExport.Clicked += HandleButtonExportClicked;
			buttonEdit.Clicked += HandleButtonEditClicked;
			buttonRemove.Clicked += HandleButtonRemoveClicked;
			
			var options = MonoDevelop.SourceEditor.DefaultSourceEditorOptions.Instance;
			texteditor.Options.FontName = options.FontName;
			texteditor.Options.ColorScheme = options.ColorScheme;
			texteditor.Options.ShowFoldMargin = false;
			texteditor.Options.ShowIconMargin = false;
			texteditor.Options.ShowLineNumberMargin = false;
			texteditor.Options.ShowInvalidLines = false;
			texteditor.Document.ReadOnly = true;
			texteditor.Document.MimeType = CSharpFormatter.MimeType;
			scrolledwindow1.Child = texteditor;
			policies.AddRange (FormattingProfileService.Profiles);
			comboboxProfiles.Model = model;
			comboboxProfiles.Changed += delegate {
				if (comboboxProfiles.Active < 0)
					return;
				var profile = policies[comboboxProfiles.Active];
				CSharpFormatter formatter = new CSharpFormatter ();
				var parent = new MonoDevelop.Projects.DotNetAssemblyProject ();
				parent.Policies.Set<CSharpFormattingPolicy> (profile, CSharpFormatter.MimeType);
				texteditor.Document.Text  = formatter.FormatText (parent.Policies, CSharpFormatter.MimeType, example);
			};
			ShowAll ();
			InitComboBox ();
		}
		public void FormatSample ()
		{
			var formatter = new CSharpFormatter ();
			texteditor.Document.Text = formatter.FormatText (policy, null, CSharpFormatter.MimeType, example, 0, example.Length);
		}
		public void FormatSample ()
		{
			var formatter = new CSharpFormatter ();
			if (textStylePolicy != null) {
				texteditor.Options.IndentationSize = textStylePolicy.IndentWidth;
				texteditor.Options.TabSize = textStylePolicy.TabWidth;
				texteditor.Options.TabsToSpaces = textStylePolicy.TabsToSpaces;
			}
			texteditor.Document.Text = formatter.FormatText (policy, textStylePolicy, CSharpFormatter.MimeType, example, 0, example.Length);
		}
Exemplo n.º 12
0
        public static void Format(TextEditorData data, IType type, IMember member, ProjectDom dom, ICompilationUnit unit, DomLocation caretLocation)
        {
            if (type == null)
            {
                return;
            }
            if (member == null)
            {
                //				member = type;
                return;
            }
            string wrapper;
            int    endPos;

            wrapper = CreateWrapperClassForMember(member, member != type, data, out endPos);
            if (string.IsNullOrEmpty(wrapper) || endPos < 0)
            {
                return;
            }
            //			Console.WriteLine (wrapper);

            int bracketIndex = wrapper.IndexOf('{') + 1;
            int col          = GetColumn(wrapper, bracketIndex, data.Options.TabSize);

            CSharpFormatter formatter = new CSharpFormatter();

            formatter.startIndentLevel = System.Math.Max(0, col / data.Options.TabSize - 1);

            string formattedText = formatter.InternalFormat(dom != null && dom.Project != null ? dom.Project.Policies : null, MimeType, wrapper, 0, wrapper.Length);

            if (formatter.hasErrors)
            {
                return;
            }

            int startPos = data.Document.LocationToOffset(member.Location.Line - 1, 0) - 1;

            InFormat = true;
            if (member != type)
            {
                int len1 = formattedText.IndexOf('{') + 1;
                int last = formattedText.LastIndexOf('}');
                formattedText = formattedText.Substring(len1, last - len1 - 1).TrimStart('\n', '\r');
            }
            else
            {
                startPos++;
            }

            //Console.WriteLine ("formattedText0:" + formattedText.Replace ("\t", "->").Replace (" ", "°"));
            if (member != type)
            {
                string indentToRemove = GetIndent(formattedText);
                //				Console.WriteLine ("Remove:" + indentToRemove.Replace ("\t", "->").Replace (" ", "°"));
                formattedText = RemoveIndent(formattedText, indentToRemove);
            }
            else
            {
                formattedText = formattedText.TrimStart();
            }
            //Console.WriteLine ("Indent:" + GetIndent (data, member.Location.Line - 1).Replace ("\t", "->").Replace (" ", "°"));
            //Console.WriteLine ("formattedText1:" + formattedText.Replace ("\t", "->").Replace (" ", "°"));
            formattedText = AddIndent(formattedText, GetIndent(data, member.Location.Line - 1));

            Document doc = new Document();

            doc.Text = formattedText;
            for (int i = doc.LineCount - 1; i >= 0; i--)
            {
                LineSegment lineSegment = doc.GetLine(i);
                if (doc.IsEmptyLine(lineSegment))
                {
                    ((IBuffer)doc).Remove(lineSegment.Offset, lineSegment.Length);
                }
            }
            formattedText = doc.Text;

            //Console.WriteLine ("formattedText3:" + formattedText.Replace ("\t", "->").Replace (" ", "°").Replace ("\n", "\\n").Replace ("\r", "\\r"));

            int textLength = CanInsertFormattedText(data, startPos, data.Document.LocationToOffset(caretLocation.Line, caretLocation.Column), formattedText);

            if (textLength > 0)
            {
//				Console.WriteLine (formattedText.Substring (0, textLength));
                InsertFormattedText(data, startPos, formattedText.Substring(0, textLength).TrimEnd());
            }
            else
            {
                Console.WriteLine("Can't insert !!!");
            }
            InFormat = false;
        }