5
// Mike Krüger <mkrueger@novell.com>
7
// Copyright (c) 2009 Novell, Inc (http://www.novell.com)
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
using System.Collections.Generic;
30
using MonoDevelop.Ide.Gui.Content;
31
using MonoDevelop.Projects;
32
using MonoDevelop.Projects.Text;
33
using MonoDevelop.Projects.Policies;
34
using MonoDevelop.Ide;
36
namespace MonoDevelop.SourceEditor
38
public class DefaultFormatter : AbstractFormatter
40
public override bool CanFormat (string mimeType)
45
static int GetNextTabstop (int currentColumn, int tabSize)
47
int result = currentColumn + tabSize;
48
return (result / tabSize) * tabSize;
51
protected override string InternalFormat (PolicyContainer policyParent, string mimeType, string input, int startOffset, int endOffset)
53
IEnumerable<string> mtypes = DesktopService.GetMimeTypeInheritanceChain (mimeType);
54
TextStylePolicy currentPolicy = policyParent != null
55
? policyParent.Get<TextStylePolicy> (mtypes)
56
: MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy<TextStylePolicy> (mtypes);
59
int line = 0, col = 0;
60
string eolMarker = currentPolicy.GetEolMarker ();
61
StringBuilder result = new StringBuilder ();
63
for (int i = startOffset; i <= endOffset; i++) {
67
if (currentPolicy.TabsToSpaces) {
68
int tabWidth = GetNextTabstop (col, currentPolicy.TabWidth) - col;
69
result.Append (new string (' ', tabWidth));
75
if (i + 1 < input.Length && input[i + 1] == '\n')
79
result.Append (eolMarker);
89
return result.ToString ();