Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CodeEditor/3.2/CodeCompletionData.cs @ 2806

Last change on this file since 2806 was 2659, checked in by epitzer, 15 years ago

Add new Plugin that provides a code editor with syntax highlighting and code completion (#842)

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