~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.SourceEditor2/MonoDevelop.SourceEditor/DefaultFormatter.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
Import upstream version 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// 
2
 
// DefaultFormatter.cs
3
 
//  
4
 
// Author:
5
 
//       Mike Krüger <mkrueger@novell.com>
6
 
// 
7
 
// Copyright (c) 2009 Novell, Inc (http://www.novell.com)
8
 
// 
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:
15
 
// 
16
 
// The above copyright notice and this permission notice shall be included in
17
 
// all copies or substantial portions of the Software.
18
 
// 
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
25
 
// THE SOFTWARE.
26
 
 
27
 
using System;
28
 
using System.Collections.Generic;
29
 
using System.Text;
30
 
using MonoDevelop.Ide.Gui.Content;
31
 
using MonoDevelop.Projects;
32
 
using MonoDevelop.Projects.Text;
33
 
using MonoDevelop.Projects.Policies;
34
 
using MonoDevelop.Ide;
35
 
 
36
 
namespace MonoDevelop.SourceEditor
37
 
{
38
 
        public class DefaultFormatter : AbstractFormatter
39
 
        {
40
 
                public override bool CanFormat (string mimeType)
41
 
                {
42
 
                        return true;
43
 
                }
44
 
                
45
 
                static int GetNextTabstop (int currentColumn, int tabSize)
46
 
                {
47
 
                        int result = currentColumn + tabSize;
48
 
                        return (result / tabSize) * tabSize;
49
 
                }
50
 
                
51
 
                protected override string InternalFormat (PolicyContainer policyParent, string mimeType, string input, int startOffset, int endOffset)
52
 
                {
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);
57
 
                        
58
 
                        input = input ?? "";
59
 
                        int line = 0, col = 0;
60
 
                        string eolMarker = currentPolicy.GetEolMarker ();
61
 
                        StringBuilder result = new StringBuilder ();
62
 
                        
63
 
                        for (int i = startOffset; i <= endOffset; i++) {
64
 
                                char ch = input[i];
65
 
                                switch (ch) {
66
 
                                case '\t':
67
 
                                        if (currentPolicy.TabsToSpaces) {
68
 
                                                int tabWidth = GetNextTabstop (col, currentPolicy.TabWidth) - col;
69
 
                                                result.Append (new string (' ', tabWidth));
70
 
                                                col += tabWidth;
71
 
                                        } else 
72
 
                                                goto default;
73
 
                                        break;
74
 
                                case '\r':
75
 
                                        if (i + 1 < input.Length && input[i + 1] == '\n')
76
 
                                                i++;
77
 
                                        goto case '\n';
78
 
                                case '\n':
79
 
                                        result.Append (eolMarker);
80
 
                                        line++;
81
 
                                        col = 0;
82
 
                                        break;
83
 
                                default:
84
 
                                        result.Append (ch);
85
 
                                        col++;
86
 
                                        break;
87
 
                                }
88
 
                        }
89
 
                        return result.ToString ();
90
 
                }
91
 
        }
92
 
}