~ubuntu-branches/ubuntu/saucy/monodevelop/saucy-proposed

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Core/MonoDevelop.Projects.Text/TextFileService.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2010-09-10 16:54:48 UTC
  • mfrom: (19.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20100910165448-0rybfk25zd4o9431
Tags: 2.4+dfsg-2
* debian/patches/inject_Mono.Debugger.Soft_source.patch,
  debian/patches/use_system_Mono.Debugger.Soft.patch,
  debian/control:
  + Build against system Soft Debugger, since we now have a new
    enough Mono to match MonoDevelop's required API

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// TextFileService.cs
 
3
//
 
4
// Author:
 
5
//   Mike Krüger <mkrueger@novell.com>
 
6
//
 
7
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
// 
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
using System;
 
30
using System.Linq;
 
31
using System.Collections.Generic;
 
32
using System.Xml;
 
33
using Mono.Addins;
 
34
 
 
35
namespace MonoDevelop.Projects.Text
 
36
{
 
37
        public static class TextFileService
 
38
        {
 
39
                static List<IFormatter> formatters = new List<IFormatter>();
 
40
                static List<IPrettyPrinter> prettyPrinters = new List<IPrettyPrinter>();
 
41
                
 
42
//              static void PrintCategory (CodeFormatCategory c)
 
43
//              {
 
44
//                      System.Console.WriteLine (c);
 
45
//                      foreach (var o in c.Options) {
 
46
//                              System.Console.WriteLine(o);
 
47
//                      }
 
48
//                      foreach (var sub in c.SubCategories) {
 
49
//                              PrintCategory (sub);
 
50
//                      }
 
51
//              }
 
52
                
 
53
                static TextFileService ()
 
54
                {
 
55
                        AddinManager.AddExtensionNodeHandler ("/MonoDevelop/ProjectModel/TextFormatters", FormatterExtHandler);
 
56
                        AddinManager.AddExtensionNodeHandler ("/MonoDevelop/ProjectModel/TextPrettyPrinters", PrettyPrinterExtHandler);
 
57
                }
 
58
                /*
 
59
                static List<CodeFormatSettings> settings = new List<CodeFormatSettings> ();
 
60
                
 
61
                public static IEnumerable<CodeFormatSettings> GetAvailableSettings (CodeFormatDescription description)
 
62
                {
 
63
                        return settings;
 
64
                }
 
65
                
 
66
                public static CodeFormatSettings GetSettings (string description, string name)
 
67
                {
 
68
                        return GetSettings (GetFormatDescription (description), name);
 
69
                }
 
70
                
 
71
                public static CodeFormatSettings GetSettings (CodeFormatDescription description, string name)
 
72
                {
 
73
                        if (description == null)
 
74
                                return null;
 
75
                        return GetAvailableSettings (description).FirstOrDefault (s => s.Name == name);
 
76
                }
 
77
                
 
78
                public static void SetSettings (CodeFormatDescription description, IEnumerable<CodeFormatSettings> settings)
 
79
                {
 
80
                        TextFileService.settings = new List<CodeFormatSettings> (settings);
 
81
                }*/
 
82
                
 
83
                static void FormatterExtHandler (object sender, ExtensionNodeEventArgs args)
 
84
                {
 
85
                        switch (args.Change) {
 
86
                        case ExtensionChange.Add:
 
87
                                formatters.Add ((IFormatter) args.ExtensionObject);
 
88
                                break;
 
89
                        case ExtensionChange.Remove:
 
90
                                formatters.Remove ((IFormatter) args.ExtensionObject);
 
91
                                break;
 
92
                        }
 
93
                }
 
94
                
 
95
                static void PrettyPrinterExtHandler (object sender, ExtensionNodeEventArgs args)
 
96
                {
 
97
                        switch (args.Change) {
 
98
                        case ExtensionChange.Add:
 
99
                                prettyPrinters.Add ((IPrettyPrinter) args.ExtensionObject);
 
100
                                break;
 
101
                        case ExtensionChange.Remove:
 
102
                                prettyPrinters.Remove ((IPrettyPrinter) args.ExtensionObject);
 
103
                                break;
 
104
                        }
 
105
                }
 
106
                /*
 
107
                static void DefinitionExtHandler (object sender, ExtensionNodeEventArgs args)
 
108
                {
 
109
                        XmlDefinitionCodon xmlDef = args.ExtensionNode as XmlDefinitionCodon;
 
110
                        using (XmlReader reader = xmlDef.Open ()) {
 
111
                                CodeFormatDescription descr = CodeFormatDescription.Read (reader);
 
112
                                switch (args.Change) {
 
113
                                case ExtensionChange.Add:
 
114
                                        descriptions.Add (descr);
 
115
                                        break;
 
116
                                case ExtensionChange.Remove:
 
117
                                        descriptions.RemoveAll (d => d.MimeType == descr.MimeType);
 
118
                                        break;
 
119
                                }
 
120
                        }
 
121
                }
 
122
                
 
123
                public static CodeFormatDescription GetFormatDescription (string mimeType)
 
124
                {
 
125
                        return descriptions.Find (d => d.MimeType == mimeType);
 
126
                }*/
 
127
                
 
128
                public static Formatter GetFormatter (string mimeType)
 
129
                {
 
130
                        IPrettyPrinter prettyPrinter = prettyPrinters.Find (x => x.CanFormat (mimeType));
 
131
                        IFormatter formatter = formatters.Find (x => x.CanFormat (mimeType));
 
132
                        if (prettyPrinter != null || formatter != null)
 
133
                                return new Formatter (mimeType, prettyPrinter, formatter);
 
134
                        else
 
135
                                return null;
 
136
                }
 
137
                
 
138
                public static void FireLineCountChanged (ITextFile textFile, int lineNumber, int lineCount, int column)
 
139
                {
 
140
                        if (LineCountChanged != null)
 
141
                                LineCountChanged (textFile, new LineCountEventArgs (textFile, lineNumber, lineCount, column));
 
142
                }
 
143
                
 
144
                public static void FireResetCountChanges (ITextFile textFile)
 
145
                {
 
146
                        if (ResetCountChanges != null)
 
147
                                ResetCountChanges (textFile, new TextFileEventArgs (textFile));
 
148
                }
 
149
                
 
150
                public static void FireCommitCountChanges (ITextFile textFile)
 
151
                {
 
152
                        if (CommitCountChanges != null)
 
153
                                CommitCountChanges (textFile, new TextFileEventArgs (textFile));
 
154
                }
 
155
                
 
156
                public static event EventHandler<LineCountEventArgs> LineCountChanged;
 
157
                public static event EventHandler<TextFileEventArgs> ResetCountChanges;
 
158
                public static event EventHandler<TextFileEventArgs> CommitCountChanges;
 
159
        }
 
160
 
 
161
        public class TextFileEventArgs : EventArgs
 
162
        {
 
163
                ITextFile textFile;
 
164
                
 
165
                public ITextFile TextFile {
 
166
                        get {
 
167
                                return textFile;
 
168
                        }
 
169
                }
 
170
 
 
171
                public TextFileEventArgs (ITextFile textFile)
 
172
                {
 
173
                        this.textFile = textFile;
 
174
                }
 
175
        }
 
176
        
 
177
        public class LineCountEventArgs : TextFileEventArgs
 
178
        {
 
179
                int lineNumber;
 
180
                int lineCount;
 
181
                int column;
 
182
                
 
183
                public int LineNumber {
 
184
                        get {
 
185
                                return lineNumber;
 
186
                        }
 
187
                }
 
188
                
 
189
                public int LineCount {
 
190
                        get {
 
191
                                return lineCount;
 
192
                        }
 
193
                }
 
194
                
 
195
                public int Column {
 
196
                        get {
 
197
                                return column;
 
198
                        }
 
199
                }
 
200
                
 
201
                public LineCountEventArgs (ITextFile textFile, int lineNumber, int lineCount, int column) : base (textFile)
 
202
                {
 
203
                        this.lineNumber = lineNumber;
 
204
                        this.lineCount  = lineCount;
 
205
                        this.column     = column;
 
206
                }
 
207
                
 
208
                public override string ToString ()
 
209
                {
 
210
                         return String.Format ("[LineCountEventArgs: LineNumber={0}, LineCount={1}, Column={2}]", lineNumber, lineCount, column);
 
211
                }
 
212
        }
 
213
}