Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP.Grammar.Editor/HeuristicLab.CodeEditor/3.3/CodeCompletionData.cs @ 6593

Last change on this file since 6593 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 4.9 KB
Line 
1/*
2 * Erstellt mit SharpDevelop.
3 * Benutzer: grunwald
4 * Datum: 27.08.2007
5 * Zeit: 14:25
6 *
7 * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
8 */
9
10using System.Collections.Generic;
11using System.IO;
12using System.Linq;
13using System.Text;
14using System.Xml;
15using ICSharpCode.SharpDevelop.Dom;
16using ICSharpCode.SharpDevelop.Dom.CSharp;
17using ICSharpCode.TextEditor.Gui.CompletionWindow;
18
19namespace HeuristicLab.CodeEditor {
20  /// <summary>
21  /// Represents an item in the code completion window.
22  /// </summary>
23  class CodeCompletionData : DefaultCompletionData, ICompletionData {
24
25    List<IMember> members;
26    IClass c;
27    static CSharpAmbience csharpAmbience = new CSharpAmbience();
28
29    public CodeCompletionData(IMember member)
30      : base(member.Name, null, GetMemberImageIndex(member)) {
31      this.members = new List<IMember>() { member };
32    }
33
34    public CodeCompletionData(IClass c)
35      : base(c.Name, null, GetClassImageIndex(c)) {
36      this.c = c;
37    }
38
39    int overloads = 0;
40
41    public void AddOverload(IMember m) {
42      overloads++;
43      members.Add(m);
44    }
45
46    static int GetMemberImageIndex(IMember member) {
47      // Missing: different icons for private/public member
48      if (member is IMethod)
49        return 1;
50      if (member is IProperty)
51        return 2;
52      if (member is IField)
53        return 3;
54      if (member is IEvent)
55        return 6;
56      return 3;
57    }
58
59    static int GetClassImageIndex(IClass c) {
60      switch (c.ClassType) {
61        case ClassType.Enum:
62          return 4;
63        default:
64          return 0;
65      }
66    }
67
68    string description;
69
70    // DefaultCompletionData.Description is not virtual, but we can reimplement
71    // the interface to get the same effect as overriding.
72    string ICompletionData.Description {
73      get {
74        if (description == null) {
75          if (members != null)
76            description = string.Join("\n\n\n", members.Select(m => GetDocumentation(m)).ToArray());
77          else
78            description = GetDocumentation(c);
79        }
80        return description;
81      }
82    }
83
84    public static string GetDocumentation(IEntity entity) {
85      return string.Format("{0}\n{1}",
86        GetText(entity),
87        XmlDocumentationToText(entity.Documentation));
88    }
89
90    /// <summary>
91    /// Converts a member to text.
92    /// Returns the declaration of the member as C# or VB code, e.g.
93    /// "public void MemberName(string parameter)"
94    /// </summary>
95    static string GetText(IEntity entity) {
96      IAmbience ambience = csharpAmbience;
97      if (entity is IMethod)
98        return ambience.Convert(entity as IMethod);
99      if (entity is IProperty)
100        return ambience.Convert(entity as IProperty);
101      if (entity is IEvent)
102        return ambience.Convert(entity as IEvent);
103      if (entity is IField)
104        return ambience.Convert(entity as IField);
105      if (entity is IClass)
106        return ambience.Convert(entity as IClass);
107      // unknown entity:
108      return entity.ToString();
109    }
110
111    public static string XmlDocumentationToText(string xmlDoc) {
112      System.Diagnostics.Debug.WriteLine(xmlDoc);
113      StringBuilder b = new StringBuilder();
114      try {
115        using (XmlTextReader reader = new XmlTextReader(new StringReader("<root>" + xmlDoc + "</root>"))) {
116          reader.XmlResolver = null;
117          while (reader.Read()) {
118            switch (reader.NodeType) {
119              case XmlNodeType.Text:
120                b.Append(reader.Value);
121                break;
122              case XmlNodeType.Element:
123                switch (reader.Name) {
124                  case "filterpriority":
125                    reader.Skip();
126                    break;
127                  case "returns":
128                    b.AppendLine();
129                    b.Append("Returns: ");
130                    break;
131                  case "param":
132                    b.AppendLine();
133                    b.Append(reader.GetAttribute("name") + ": ");
134                    break;
135                  case "remarks":
136                    b.AppendLine();
137                    b.Append("Remarks: ");
138                    break;
139                  case "see":
140                    if (reader.IsEmptyElement) {
141                      b.Append(reader.GetAttribute("cref"));
142                    } else {
143                      reader.MoveToContent();
144                      if (reader.HasValue) {
145                        b.Append(reader.Value);
146                      } else {
147                        b.Append(reader.GetAttribute("cref"));
148                      }
149                    }
150                    break;
151                }
152                break;
153            }
154          }
155        }
156        return b.ToString();
157      }
158      catch (XmlException) {
159        return xmlDoc;
160      }
161    }
162  }
163}
Note: See TracBrowser for help on using the repository browser.