Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2419: refactored CodeCompletionStrategy and CSharpCodeCompletionStrategy

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Collections.Generic;
23using System.Linq;
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        );
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      if (completionData.Any() && cce.AutoCompleteEmptyMatch) {
85        foreach (var completion in completionData) {
86          var cast = completion as CompletionData;
87          if (cast != null) {
88            cast.TriggerWord = result.TriggerWord;
89            cast.TriggerWordLength = result.TriggerWordLength;
90            result.CompletionData.Add(cast);
91          }
92        }
93      }
94
95      if (!controlSpace) {
96        var pce = new CSharpParameterCompletionEngine(
97          completionContext.Document,
98          completionContext.CompletionContextProvider,
99          completionFactory,
100          completionContext.ProjectContent,
101          completionContext.TypeResolveContextAtCaret
102        );
103
104        var parameterDataProvider = pce.GetParameterDataProvider(completionContext.Offset, completionChar);
105        result.OverloadProvider = parameterDataProvider as IUpdatableOverloadProvider;
106      }
107
108      return result;
109    }
110
111    protected override void DoParseStep() {
112      if (document == null) return;
113      var unresolvedFile = CSharpParsingHelpers.CreateCSharpUnresolvedFile(document);
114      projectContent = projectContent.AddOrUpdateFiles(unresolvedFile);
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.