Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2077: improved code completion

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      CompletionText = DisplayText = entity.Name;
47      Image = CompletionImage.GetImage(entity);
48    }
49
50    private string description;
51    public override string Description {
52      get {
53        if (string.IsNullOrEmpty(description)) {
54          description = GetText(Entity);
55          if (HasOverloads)
56            description += string.Format(" (+{0} overloads)", OverloadedData.Count());
57          description += Environment.NewLine + XmlDocumentationToText(Entity.Documentation);
58        }
59        return description;
60      }
61      set { description = value; }
62    }
63
64    public static string XmlDocumentationToText(string xmlDoc) {
65      System.Diagnostics.Debug.WriteLine(xmlDoc);
66      var b = new StringBuilder();
67      try {
68        using (var reader = new XmlTextReader(new StringReader("<root>" + xmlDoc + "</root>"))) {
69          reader.XmlResolver = null;
70          while (reader.Read()) {
71            switch (reader.NodeType) {
72              case XmlNodeType.Text:
73                b.Append(reader.Value);
74                break;
75              case XmlNodeType.Element:
76                switch (reader.Name) {
77                  case "filterpriority":
78                    reader.Skip();
79                    break;
80                  case "returns":
81                    b.AppendLine();
82                    b.Append("Returns: ");
83                    break;
84                  case "param":
85                    b.AppendLine();
86                    b.Append(reader.GetAttribute("name") + ": ");
87                    break;
88                  case "remarks":
89                    b.AppendLine();
90                    b.Append("Remarks: ");
91                    break;
92                  case "see":
93                    if (reader.IsEmptyElement) {
94                      b.Append(reader.GetAttribute("cref"));
95                    } else {
96                      reader.MoveToContent();
97                      if (reader.HasValue) {
98                        b.Append(reader.Value);
99                      } else {
100                        b.Append(reader.GetAttribute("cref"));
101                      }
102                    }
103                    break;
104                }
105                break;
106            }
107          }
108        }
109        return b.ToString();
110      } catch (XmlException) {
111        return xmlDoc;
112      }
113    }
114
115    #region Helpers
116    private static string GetText(IEntity entity) {
117      Ambience.ConversionFlags = ConversionFlags.StandardConversionFlags;
118      if (entity is ITypeDefinition) Ambience.ConversionFlags |= ConversionFlags.UseFullyQualifiedEntityNames;
119      if (entity is IMethod) {
120        var reducedForm = ((IMethod)entity).ReducedFrom;
121        if (reducedForm != null) entity = reducedForm;
122      }
123      return Ambience.ConvertSymbol(entity);
124    }
125    #endregion
126  }
127}
Note: See TracBrowser for help on using the repository browser.