Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.CodeEditor/3.4/LanguageFeatures/CodeCompletion/CSharp/CSharpCodeCompletionDataFactory.cs @ 15164

Last change on this file since 15164 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 6.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Collections.Generic;
24using System.Linq;
25using ICSharpCode.AvalonEdit.CodeCompletion;
26using ICSharpCode.NRefactory.Completion;
27using ICSharpCode.NRefactory.CSharp;
28using ICSharpCode.NRefactory.CSharp.Completion;
29using ICSharpCode.NRefactory.CSharp.Resolver;
30using ICSharpCode.NRefactory.TypeSystem;
31using ICompletionData = ICSharpCode.NRefactory.Completion.ICompletionData;
32
33namespace HeuristicLab.CodeEditor {
34  internal class CSharpCodeCompletionDataFactory : ICompletionDataFactory, IParameterCompletionDataFactory {
35    private readonly CSharpCodeCompletionContext context;
36
37    public CSharpCodeCompletionDataFactory(CSharpCodeCompletionContext context) {
38      this.context = context;
39    }
40
41    #region ICompletionDataFactory Members
42    public ICompletionData CreateEntityCompletionData(IEntity entity) {
43      return new EntityCompletionData(entity);
44    }
45
46    public ICompletionData CreateEntityCompletionData(IEntity entity, string text) {
47      return new EntityCompletionData(entity) { CompletionText = text, DisplayText = text };
48    }
49
50    public ICompletionData CreateTypeCompletionData(IType type, bool fullName, bool isInAttributeContext, bool addForTypeCreation) {
51      var typeDef = type.GetDefinition();
52      if (typeDef != null) { return new EntityCompletionData(typeDef); }
53
54      string name = fullName ? type.FullName : type.Name;
55      if (isInAttributeContext && name.EndsWith("Attribute", StringComparison.Ordinal) && name.Length > "Attribute".Length) {
56        name = name.Substring(0, name.Length - "Attribute".Length);
57      }
58      return new CompletionData(name);
59    }
60
61    public ICompletionData CreateMemberCompletionData(IType type, IEntity member) {
62      return new EntityCompletionData(member);
63    }
64
65    public ICompletionData CreateLiteralCompletionData(string title, string description = null, string insertText = null) {
66      return new CompletionData(title) {
67        Description = description,
68        CompletionText = insertText ?? title,
69        Image = CompletionImage.Literal.BaseImage,
70        Priority = 2
71      };
72    }
73
74    public ICompletionData CreateXmlDocCompletionData(string title, string description = null, string insertText = null) {
75      return new CompletionData(title) {
76        Description = description,
77        CompletionText = insertText ?? title,
78        Image = CompletionImage.Template.BaseImage,
79      };
80    }
81
82    public ICompletionData CreateNamespaceCompletionData(INamespace ns) {
83      return new CompletionData(ns.Name) { Image = CompletionImage.NamespaceImage };
84    }
85
86    public ICompletionData CreateVariableCompletionData(IVariable variable) {
87      return new VariableCompletionData(variable);
88    }
89
90    public ICompletionData CreateVariableCompletionData(ITypeParameter parameter) {
91      return new CompletionData(parameter.Name);
92    }
93
94    public ICompletionData CreateEventCreationCompletionData(string varName, IType delegateType, IEvent evt, string parameterDefinition, IUnresolvedMember currentMember, IUnresolvedTypeDefinition currentType) {
95      return new CompletionData(varName);
96    }
97
98    public ICompletionData CreateNewOverrideCompletionData(int declarationBegin, IUnresolvedTypeDefinition type, IMember m) {
99      return new CompletionData(m.Name);
100    }
101
102    public ICompletionData CreateNewPartialCompletionData(int declarationBegin, IUnresolvedTypeDefinition type, IUnresolvedMember m) {
103      return new CompletionData(m.Name);
104    }
105
106    public ICompletionData CreateImportCompletionData(IType type, bool useFullName, bool addForTypeCreation) {
107      return null;
108    }
109
110    public IEnumerable<ICompletionData> CreateCodeTemplateCompletionData() {
111      yield break;
112    }
113
114    public ICompletionData CreateFormatItemCompletionData(string format, string description, object example) {
115      return null;
116    }
117
118    public IEnumerable<ICompletionData> CreatePreProcessorDefinesCompletionData() {
119      yield break;
120    }
121    #endregion
122
123    #region IParameterCompletionDataFactory Members
124    public IParameterDataProvider CreateConstructorProvider(int startOffset, IType type, AstNode thisInitializer) {
125      return CreateConstructorProvider(startOffset, type);
126    }
127
128    public IParameterDataProvider CreateConstructorProvider(int startOffset, IType type) {
129      var constructors = FilterMethodsForAccessibility(type, type.GetConstructors());
130      return CreateMethodDataProvider(startOffset, constructors);
131    }
132
133    public IParameterDataProvider CreateDelegateDataProvider(int startOffset, IType type) {
134      var delegates = FilterMethodsForAccessibility(type, new[] { type.GetDelegateInvokeMethod() });
135      return CreateMethodDataProvider(startOffset, delegates);
136    }
137
138    public IParameterDataProvider CreateIndexerParameterDataProvider(int startOffset, IType type, IEnumerable<IProperty> accessibleIndexers, ICSharpCode.NRefactory.CSharp.AstNode resolvedNode) {
139      return null;
140    }
141
142    public IParameterDataProvider CreateMethodDataProvider(int startOffset, IEnumerable<IMethod> methods) {
143      return new CSharpOverloadProvider(context, startOffset, methods.Where(x => x != null).Select(x => new CSharpInsightItem(x)));
144    }
145
146    public IParameterDataProvider CreateTypeParameterDataProvider(int startOffset, IEnumerable<IMethod> methods) {
147      return CreateMethodDataProvider(startOffset, methods);
148    }
149
150    public IParameterDataProvider CreateTypeParameterDataProvider(int startOffset, IEnumerable<IType> types) {
151      return null;
152    }
153    #endregion
154
155    private IEnumerable<IMethod> FilterMethodsForAccessibility(IType type, IEnumerable<IMethod> methods) {
156      var typeResolveContext = context.TypeResolveContextAtCaret;
157      var lookup = new MemberLookup(typeResolveContext.CurrentTypeDefinition, typeResolveContext.Compilation.MainAssembly);
158      bool protectedAccessAllowed = lookup.IsProtectedAccessAllowed(type);
159      return protectedAccessAllowed ? methods : methods.Where(x => !x.IsProtected);
160    }
161  }
162}
Note: See TracBrowser for help on using the repository browser.