#region License Information
/* HeuristicLab
* Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using ICSharpCode.AvalonEdit.CodeCompletion;
using ICSharpCode.NRefactory.Completion;
using ICSharpCode.NRefactory.CSharp;
using ICSharpCode.NRefactory.TypeSystem;
namespace HeuristicLab.CodeEditor {
internal class EntityCompletionData : CompletionData, IEntityCompletionData {
private static readonly CSharpAmbience Ambience = new CSharpAmbience();
#region IEntityCompletionData Members
public IEntity Entity { get; private set; }
#endregion
public EntityCompletionData(IEntity entity)
: base() {
if (entity == null) throw new ArgumentException("entity");
Entity = entity;
Ambience.ConversionFlags = entity is ITypeDefinition ? ConversionFlags.ShowTypeParameterList : ConversionFlags.None;
CompletionText = DisplayText = entity.Name;
Image = CompletionImage.GetImage(entity);
}
private string description;
public override string Description {
get {
if (string.IsNullOrEmpty(description)) {
description = GetText(Entity);
if (HasOverloads)
description += string.Format(" (+{0} overloads)", OverloadedData.Count());
description += Environment.NewLine + XmlDocumentationToText(Entity.Documentation);
}
return description;
}
set { description = value; }
}
public static string XmlDocumentationToText(string xmlDoc) {
System.Diagnostics.Debug.WriteLine(xmlDoc);
var b = new StringBuilder();
try {
using (var reader = new XmlTextReader(new StringReader("" + xmlDoc + ""))) {
reader.XmlResolver = null;
while (reader.Read()) {
switch (reader.NodeType) {
case XmlNodeType.Text:
b.Append(reader.Value);
break;
case XmlNodeType.Element:
switch (reader.Name) {
case "filterpriority":
reader.Skip();
break;
case "returns":
b.AppendLine();
b.Append("Returns: ");
break;
case "param":
b.AppendLine();
b.Append(reader.GetAttribute("name") + ": ");
break;
case "remarks":
b.AppendLine();
b.Append("Remarks: ");
break;
case "see":
if (reader.IsEmptyElement) {
b.Append(reader.GetAttribute("cref"));
} else {
reader.MoveToContent();
if (reader.HasValue) {
b.Append(reader.Value);
} else {
b.Append(reader.GetAttribute("cref"));
}
}
break;
}
break;
}
}
}
return b.ToString();
} catch (XmlException) {
return xmlDoc;
}
}
#region Helpers
private static string GetText(IEntity entity) {
Ambience.ConversionFlags = ConversionFlags.StandardConversionFlags;
if (entity is ITypeDefinition) Ambience.ConversionFlags |= ConversionFlags.UseFullyQualifiedEntityNames;
if (entity is IMethod) {
var reducedForm = ((IMethod)entity).ReducedFrom;
if (reducedForm != null) entity = reducedForm;
}
return Ambience.ConvertSymbol(entity);
}
#endregion
}
}