Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CodeEditor/HeuristicLab.CodeEditor/3.4/LanguageFeatures/CodeCompletion/CSharp/CompletionData/EntityCompletionData.cs @ 11700

Last change on this file since 11700 was 11700, checked in by jkarder, 9 years ago

#2077: created branch and added first version

File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.IO;
24using System.Linq;
25using System.Text;
26using System.Xml;
27using ICSharpCode.AvalonEdit.CodeCompletion;
28using ICSharpCode.NRefactory.Completion;
29using ICSharpCode.NRefactory.CSharp;
30using ICSharpCode.NRefactory.TypeSystem;
31
32namespace HeuristicLab.CodeEditor {
33  internal class EntityCompletionData : CompletionData, IEntityCompletionData {
34    private static readonly CSharpAmbience Ambience = new CSharpAmbience();
35
36    #region IEntityCompletionData Members
37    public IEntity Entity { get; private set; }
38    #endregion
39
40    public EntityCompletionData(IEntity entity)
41      : base() {
42      if (entity == null) throw new ArgumentException("entity");
43
44      Entity = entity;
45      Ambience.ConversionFlags = entity is ITypeDefinition ? ConversionFlags.ShowTypeParameterList : ConversionFlags.None;
46      DisplayText = entity.Name;
47      CompletionText = Ambience.ConvertSymbol(entity);
48      Image = CompletionImage.GetImage(entity);
49    }
50
51    private string description;
52    public override string Description {
53      get {
54        if (string.IsNullOrEmpty(description)) {
55          description = GetText(Entity);
56          if (HasOverloads)
57            description += string.Format(" (+{0} overloads)", OverloadedData.Count());
58          description += Environment.NewLine + XmlDocumentationToText(Entity.Documentation);
59        }
60        return description;
61      }
62      set { description = value; }
63    }
64
65    public static string XmlDocumentationToText(string xmlDoc) {
66      System.Diagnostics.Debug.WriteLine(xmlDoc);
67      var b = new StringBuilder();
68      try {
69        using (var reader = new XmlTextReader(new StringReader("<root>" + xmlDoc + "</root>"))) {
70          reader.XmlResolver = null;
71          while (reader.Read()) {
72            switch (reader.NodeType) {
73              case XmlNodeType.Text:
74                b.Append(reader.Value);
75                break;
76              case XmlNodeType.Element:
77                switch (reader.Name) {
78                  case "filterpriority":
79                    reader.Skip();
80                    break;
81                  case "returns":
82                    b.AppendLine();
83                    b.Append("Returns: ");
84                    break;
85                  case "param":
86                    b.AppendLine();
87                    b.Append(reader.GetAttribute("name") + ": ");
88                    break;
89                  case "remarks":
90                    b.AppendLine();
91                    b.Append("Remarks: ");
92                    break;
93                  case "see":
94                    if (reader.IsEmptyElement) {
95                      b.Append(reader.GetAttribute("cref"));
96                    } else {
97                      reader.MoveToContent();
98                      if (reader.HasValue) {
99                        b.Append(reader.Value);
100                      } else {
101                        b.Append(reader.GetAttribute("cref"));
102                      }
103                    }
104                    break;
105                }
106                break;
107            }
108          }
109        }
110        return b.ToString();
111      } catch (XmlException) {
112        return xmlDoc;
113      }
114    }
115
116    #region Helpers
117    private static string GetText(IEntity entity) {
118      Ambience.ConversionFlags = ConversionFlags.StandardConversionFlags;
119      if (entity is ITypeDefinition) Ambience.ConversionFlags |= ConversionFlags.UseFullyQualifiedEntityNames;
120      if (entity is IMethod) {
121        var reducedForm = ((IMethod)entity).ReducedFrom;
122        if (reducedForm != null) entity = reducedForm;
123      }
124      return Ambience.ConvertSymbol(entity);
125    }
126    #endregion
127  }
128}
Note: See TracBrowser for help on using the repository browser.