Ejemplo n.º 1
0
		// 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);
		}