source: trunk/sources/HeuristicLab.CodeEditor/3.4/LanguageFeatures/CodeCompletion/CSharp/CSharpCodeCompletionDataFactory.cs @ 11825

Last change on this file since 11825 was 11825, checked in by jkarder, 6 years ago

#2077:

  • do not offer completion for inaccessible protected members
  • fixed go to line logic
  • fixed comment in CSharpScriptTemplate
File size: 6.9 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.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      var typeDef = type.GetDefinition();
108      return new ImportCompletionData(typeDef, context.TypeResolveContextAtCaret, useFullName);
109    }
110
111    public IEnumerable<ICompletionData> CreateCodeTemplateCompletionData() {
112      yield break;
113    }
114
115    public ICompletionData CreateFormatItemCompletionData(string format, string description, object example) {
116      return null;
117    }
118
119    public IEnumerable<ICompletionData> CreatePreProcessorDefinesCompletionData() {
120      yield break;
121    }
122    #endregion
123
124    #region IParameterCompletionDataFactory Members
125    public IParameterDataProvider CreateConstructorProvider(int startOffset, IType type, AstNode thisInitializer) {
126      return CreateConstructorProvider(startOffset, type);
127    }
128
129    public IParameterDataProvider CreateConstructorProvider(int startOffset, IType type) {
130      var constructors = FilterMethodsForAccessibility(type, type.GetConstructors());
131      return CreateMethodDataProvider(startOffset, constructors);
132    }
133
134    public IParameterDataProvider CreateDelegateDataProvider(int startOffset, IType type) {
135      var delegates = FilterMethodsForAccessibility(type, new[] { type.GetDelegateInvokeMethod() });
136      return CreateMethodDataProvider(startOffset, delegates);
137    }
138
139    public IParameterDataProvider CreateIndexerParameterDataProvider(int startOffset, IType type, IEnumerable<IProperty> accessibleIndexers, ICSharpCode.NRefactory.CSharp.AstNode resolvedNode) {
140      return null;
141    }
142
143    public IParameterDataProvider CreateMethodDataProvider(int startOffset, IEnumerable<IMethod> methods) {
144      return new CSharpOverloadProvider(context, startOffset, methods.Where(x => x != null).Select(x => new CSharpInsightItem(x)));
145    }
146
147    public IParameterDataProvider CreateTypeParameterDataProvider(int startOffset, IEnumerable<IMethod> methods) {
148      return CreateMethodDataProvider(startOffset, methods);
149    }
150
151    public IParameterDataProvider CreateTypeParameterDataProvider(int startOffset, IEnumerable<IType> types) {
152      return null;
153    }
154    #endregion
155
156    private IEnumerable<IMethod> FilterMethodsForAccessibility(IType type, IEnumerable<IMethod> methods) {
157      var typeResolveContext = context.TypeResolveContextAtCaret;
158      var lookup = new MemberLookup(typeResolveContext.CurrentTypeDefinition, typeResolveContext.Compilation.MainAssembly);
159      bool protectedAccessAllowed = lookup.IsProtectedAccessAllowed(type);
160      return protectedAccessAllowed ? methods : methods.Where(x => !x.IsProtected);
161    }
162  }
163}
Note: See TracBrowser for help on using the repository browser.