Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CodeEditor/HeuristicLab.CodeEditor/3.4/LanguageFeatures/CodeCompletion/CSharp/CSharpCodeCompletionStrategy.cs @ 11700

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

#2077: created branch and added first version

File size: 4.4 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 ICSharpCode.NRefactory.Completion;
25using ICSharpCode.NRefactory.CSharp;
26using ICSharpCode.NRefactory.CSharp.Completion;
27using ICSharpCode.NRefactory.TypeSystem;
28
29namespace HeuristicLab.CodeEditor {
30  internal class CSharpCodeCompletionStrategy : CodeCompletionStrategy {
31    private IProjectContent projectContent = new CSharpProjectContent();
32
33    public CSharpCodeCompletionStrategy(CodeEditor codeEditor)
34      : base(codeEditor) {
35      codeEditor.InternalAssembliesLoaded += (sender, args) => {
36        projectContent = projectContent.AddAssemblyReferences(args.Value);
37      };
38      codeEditor.InternalAssembliesUnloaded += (sender, args) => {
39        projectContent = projectContent.RemoveAssemblyReferences(args.Value);
40      };
41    }
42
43    protected override CodeCompletionResult GetCodeCompletionResult(bool controlSpace) {
44      var document = codeEditor.TextEditor.Document;
45      int offset = codeEditor.TextEditor.CaretOffset;
46
47      var result = new CodeCompletionResult();
48      var completionContext = new CSharpCodeCompletionContext(document, offset, projectContent);
49      var completionFactory = new CSharpCodeCompletionDataFactory(completionContext);
50      var cce = new CSharpCompletionEngine(
51        completionContext.Document,
52        completionContext.CompletionContextProvider,
53        completionFactory,
54        completionContext.ProjectContent,
55        completionContext.TypeResolveContextAtCaret
56      ) { EolMarker = Environment.NewLine };
57
58      char completionChar = completionContext.Document.GetCharAt(completionContext.Offset - 1);
59      int startPos, triggerWordLength;
60      IEnumerable<ICompletionData> completionData;
61
62      if (controlSpace) {
63        if (!cce.TryGetCompletionWord(completionContext.Offset, out startPos, out triggerWordLength)) {
64          startPos = completionContext.Offset;
65          triggerWordLength = 0;
66        }
67        completionData = cce.GetCompletionData(startPos, true);
68      } else {
69        startPos = completionContext.Offset;
70        if (char.IsLetterOrDigit(completionChar) || completionChar == '_') {
71          if (startPos > 1 && char.IsLetterOrDigit(completionContext.Document.GetCharAt((startPos - 2))))
72            return result;
73          completionData = cce.GetCompletionData(startPos, false);
74          triggerWordLength = 1;
75        } else {
76          completionData = cce.GetCompletionData(startPos, false);
77          triggerWordLength = 0;
78        }
79      }
80
81      result.TriggerWordLength = triggerWordLength;
82      result.TriggerWord = completionContext.Document.GetText(completionContext.Offset - triggerWordLength, triggerWordLength);
83
84      foreach (var completion in completionData) {
85        var cast = completion as CompletionData;
86        if (cast != null) {
87          cast.TriggerWord = result.TriggerWord;
88          cast.TriggerWordLength = result.TriggerWordLength;
89          result.CompletionData.Add(cast);
90        }
91      }
92
93      if (!controlSpace) {
94        var pce = new CSharpParameterCompletionEngine(
95          completionContext.Document,
96          completionContext.CompletionContextProvider,
97          completionFactory,
98          completionContext.ProjectContent,
99          completionContext.TypeResolveContextAtCaret
100        );
101
102        var parameterDataProvider = pce.GetParameterDataProvider(completionContext.Offset, completionChar);
103        result.OverloadProvider = parameterDataProvider as IUpdatableOverloadProvider;
104      }
105
106      return result;
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.