Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2077:

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