PageRenderTime 19ms CodeModel.GetById 12ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/ILSpy/TreeNodes/FieldTreeNode.cs

http://github.com/icsharpcode/ILSpy
C# | 96 lines | 61 code | 15 blank | 20 comment | 14 complexity | c6b323854c6b898108e2cb56ce1108d0 MD5 | raw file
 1// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
 2// 
 3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
 4// software and associated documentation files (the "Software"), to deal in the Software
 5// without restriction, including without limitation the rights to use, copy, modify, merge,
 6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
 7// to whom the Software is furnished to do so, subject to the following conditions:
 8// 
 9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11// 
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.Windows.Media;
21using ICSharpCode.Decompiler;
22using ICSharpCode.Decompiler.TypeSystem;
23
24namespace ICSharpCode.ILSpy.TreeNodes
25{
26	/// <summary>
27	/// Represents a field in the TreeView.
28	/// </summary>
29	public sealed class FieldTreeNode : ILSpyTreeNode, IMemberTreeNode
30	{
31		public IField FieldDefinition { get; }
32
33		public FieldTreeNode(IField field)
34		{
35			this.FieldDefinition = field ?? throw new ArgumentNullException(nameof(field));
36		}
37
38		public override object Text => GetText(FieldDefinition, Language) + FieldDefinition.MetadataToken.ToSuffixString();
39
40		public static object GetText(IField field, Language language)
41		{
42			return language.FieldToString(field, includeDeclaringTypeName: false, includeNamespace: false, includeNamespaceOfDeclaringTypeName: false);
43		}
44
45		public override object Icon => GetIcon(FieldDefinition);
46
47		public static ImageSource GetIcon(IField field)
48		{
49			if (field.DeclaringType.Kind == TypeKind.Enum && field.ReturnType.Kind == TypeKind.Enum)
50				return Images.GetIcon(MemberIcon.EnumValue, MethodTreeNode.GetOverlayIcon(field.Accessibility), false);
51
52			if (field.IsConst)
53				return Images.GetIcon(MemberIcon.Literal, MethodTreeNode.GetOverlayIcon(field.Accessibility), false);
54
55			if (field.IsReadOnly)
56				return Images.GetIcon(MemberIcon.FieldReadOnly, MethodTreeNode.GetOverlayIcon(field.Accessibility), field.IsStatic);
57
58			return Images.GetIcon(MemberIcon.Field, MethodTreeNode.GetOverlayIcon(field.Accessibility), field.IsStatic);
59		}
60
61		public override FilterResult Filter(FilterSettings settings)
62		{
63			if (settings.ShowApiLevel == ApiVisibility.PublicOnly && !IsPublicAPI)
64				return FilterResult.Hidden;
65			if (settings.SearchTermMatches(FieldDefinition.Name) && (settings.ShowApiLevel == ApiVisibility.All || settings.Language.ShowMember(FieldDefinition)))
66				return FilterResult.Match;
67			else
68				return FilterResult.Hidden;
69		}
70
71		public override void Decompile(Language language, ITextOutput output, DecompilationOptions options)
72		{
73			language.DecompileField(FieldDefinition, output, options);
74		}
75		
76		public override bool IsPublicAPI {
77			get {
78				switch (FieldDefinition.Accessibility) {
79					case Accessibility.Public:
80					case Accessibility.Protected:
81					case Accessibility.ProtectedOrInternal:
82						return true;
83					default:
84						return false;
85				}
86			}
87		}
88
89		IEntity IMemberTreeNode.Member => FieldDefinition;
90
91		public override string ToString()
92		{
93			return FieldDefinition.Name;
94		}
95	}
96}