5
// Mike Krüger <mkrueger@novell.com>
7
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
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:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
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.
31
using System.Collections.Generic;
35
namespace MonoDevelop.Projects.Text
37
public static class TextFileService
39
static List<IFormatter> formatters = new List<IFormatter>();
40
static List<IPrettyPrinter> prettyPrinters = new List<IPrettyPrinter>();
42
// static void PrintCategory (CodeFormatCategory c)
44
// System.Console.WriteLine (c);
45
// foreach (var o in c.Options) {
46
// System.Console.WriteLine(o);
48
// foreach (var sub in c.SubCategories) {
49
// PrintCategory (sub);
53
static TextFileService ()
55
AddinManager.AddExtensionNodeHandler ("/MonoDevelop/ProjectModel/TextFormatters", FormatterExtHandler);
56
AddinManager.AddExtensionNodeHandler ("/MonoDevelop/ProjectModel/TextPrettyPrinters", PrettyPrinterExtHandler);
59
static List<CodeFormatSettings> settings = new List<CodeFormatSettings> ();
61
public static IEnumerable<CodeFormatSettings> GetAvailableSettings (CodeFormatDescription description)
66
public static CodeFormatSettings GetSettings (string description, string name)
68
return GetSettings (GetFormatDescription (description), name);
71
public static CodeFormatSettings GetSettings (CodeFormatDescription description, string name)
73
if (description == null)
75
return GetAvailableSettings (description).FirstOrDefault (s => s.Name == name);
78
public static void SetSettings (CodeFormatDescription description, IEnumerable<CodeFormatSettings> settings)
80
TextFileService.settings = new List<CodeFormatSettings> (settings);
83
static void FormatterExtHandler (object sender, ExtensionNodeEventArgs args)
85
switch (args.Change) {
86
case ExtensionChange.Add:
87
formatters.Add ((IFormatter) args.ExtensionObject);
89
case ExtensionChange.Remove:
90
formatters.Remove ((IFormatter) args.ExtensionObject);
95
static void PrettyPrinterExtHandler (object sender, ExtensionNodeEventArgs args)
97
switch (args.Change) {
98
case ExtensionChange.Add:
99
prettyPrinters.Add ((IPrettyPrinter) args.ExtensionObject);
101
case ExtensionChange.Remove:
102
prettyPrinters.Remove ((IPrettyPrinter) args.ExtensionObject);
107
static void DefinitionExtHandler (object sender, ExtensionNodeEventArgs args)
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);
116
case ExtensionChange.Remove:
117
descriptions.RemoveAll (d => d.MimeType == descr.MimeType);
123
public static CodeFormatDescription GetFormatDescription (string mimeType)
125
return descriptions.Find (d => d.MimeType == mimeType);
128
public static Formatter GetFormatter (string mimeType)
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);
138
public static void FireLineCountChanged (ITextFile textFile, int lineNumber, int lineCount, int column)
140
if (LineCountChanged != null)
141
LineCountChanged (textFile, new LineCountEventArgs (textFile, lineNumber, lineCount, column));
144
public static void FireResetCountChanges (ITextFile textFile)
146
if (ResetCountChanges != null)
147
ResetCountChanges (textFile, new TextFileEventArgs (textFile));
150
public static void FireCommitCountChanges (ITextFile textFile)
152
if (CommitCountChanges != null)
153
CommitCountChanges (textFile, new TextFileEventArgs (textFile));
156
public static event EventHandler<LineCountEventArgs> LineCountChanged;
157
public static event EventHandler<TextFileEventArgs> ResetCountChanges;
158
public static event EventHandler<TextFileEventArgs> CommitCountChanges;
161
public class TextFileEventArgs : EventArgs
165
public ITextFile TextFile {
171
public TextFileEventArgs (ITextFile textFile)
173
this.textFile = textFile;
177
public class LineCountEventArgs : TextFileEventArgs
183
public int LineNumber {
189
public int LineCount {
201
public LineCountEventArgs (ITextFile textFile, int lineNumber, int lineCount, int column) : base (textFile)
203
this.lineNumber = lineNumber;
204
this.lineCount = lineCount;
205
this.column = column;
208
public override string ToString ()
210
return String.Format ("[LineCountEventArgs: LineNumber={0}, LineCount={1}, Column={2}]", lineNumber, lineCount, column);