Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CodeEditor/HeuristicLab.CodeEditor/3.4/LanguageFeatures/CodeCompletion/CodeCompletionStrategy.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: 3.1 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.Linq;
23using ICSharpCode.AvalonEdit.CodeCompletion;
24
25namespace HeuristicLab.CodeEditor {
26  internal abstract class CodeCompletionStrategy : ICodeCompletionStrategy {
27    protected readonly CodeEditor codeEditor;
28
29    protected CodeCompletionStrategy(CodeEditor codeEditor) {
30      this.codeEditor = codeEditor;
31    }
32
33    public virtual void DoCodeCompletion(bool controlSpace) {
34      var codeCompletionResult = GetCodeCompletionResult(controlSpace);
35      ApplyCodeCompletionData(codeCompletionResult);
36    }
37
38    protected abstract CodeCompletionResult GetCodeCompletionResult(bool controlSpace);
39
40    protected virtual void ApplyCodeCompletionData(CodeCompletionResult codeCompletionResult) {
41      var textArea = codeEditor.TextEditor.TextArea;
42      var document = codeEditor.TextEditor.Document;
43      int offset = codeEditor.TextEditor.CaretOffset;
44
45      if (codeEditor.OverloadInsightWindow == null && codeCompletionResult.OverloadProvider != null) {
46        var iw = codeEditor.OverloadInsightWindow = new OverloadInsightWindow(textArea);
47        iw.Provider = codeCompletionResult.OverloadProvider;
48        iw.Show();
49        iw.Closed += (sender, args) => codeEditor.OverloadInsightWindow = null;
50      }
51
52      if (codeEditor.CompletionWindow == null && codeCompletionResult.CompletionData.Any()) {
53        var cw = codeEditor.CompletionWindow = new CompletionWindow(textArea);
54        cw.CloseWhenCaretAtBeginning = true;
55        cw.StartOffset -= codeCompletionResult.TriggerWordLength;
56        cw.Closed += (sender, args) => codeEditor.CompletionWindow = null;
57
58        var data = cw.CompletionList.CompletionData;
59        foreach (var completion in codeCompletionResult.CompletionData.OrderBy(x => x.Text))
60          data.Add(completion);
61
62        if (codeCompletionResult.TriggerWordLength > 0)
63          cw.CompletionList.SelectItem(codeCompletionResult.TriggerWord);
64
65        cw.Show();
66      }
67
68      if (codeEditor.OverloadInsightWindow != null) {
69        var iw = codeEditor.OverloadInsightWindow;
70        var provider = iw.Provider as IUpdatableOverloadProvider;
71        if (provider != null) {
72          provider.Update(document, offset);
73          if (provider.RequestClose) {
74            iw.Close();
75          }
76        }
77      }
78    }
79  }
80}
Note: See TracBrowser for help on using the repository browser.