Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.CodeEditor/3.4/LanguageFeatures/CodeCompletion/CodeCompletionStrategy.cs @ 11937

Last change on this file since 11937 was 11937, checked in by jkarder, 10 years ago

#2077: merged r11807:11811, r11816, r11819, r11822, r11825, r11834, r11835, r11836, r11933 and r11936 into stable

File size: 3.7 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 System.Threading;
24using System.Threading.Tasks;
25using ICSharpCode.AvalonEdit.CodeCompletion;
26
27namespace HeuristicLab.CodeEditor {
28  internal abstract class CodeCompletionStrategy : ICodeCompletionStrategy {
29    protected readonly CodeEditor codeEditor;
30    protected readonly Task backgroundParser;
31
32    protected CodeCompletionStrategy(CodeEditor codeEditor) {
33      this.codeEditor = codeEditor;
34      backgroundParser = new Task(DoBackgroundParsing);
35    }
36
37    public virtual void DoCodeCompletion(bool controlSpace) {
38      var codeCompletionResult = GetCodeCompletionResult(controlSpace);
39      ApplyCodeCompletionData(codeCompletionResult);
40    }
41
42    public virtual void Initialize() {
43      if (backgroundParser.Status == TaskStatus.Created)
44        backgroundParser.Start();
45    }
46
47    protected abstract CodeCompletionResult GetCodeCompletionResult(bool controlSpace);
48    protected abstract void DoParseStep();
49
50    protected virtual void ApplyCodeCompletionData(CodeCompletionResult codeCompletionResult) {
51      var textArea = codeEditor.TextEditor.TextArea;
52      var document = codeEditor.TextEditor.Document;
53      int offset = codeEditor.TextEditor.CaretOffset;
54
55      if (codeEditor.OverloadInsightWindow == null && codeCompletionResult.OverloadProvider != null) {
56        var iw = codeEditor.OverloadInsightWindow = new OverloadInsightWindow(textArea);
57        iw.Provider = codeCompletionResult.OverloadProvider;
58        iw.Show();
59        iw.Closed += (sender, args) => codeEditor.OverloadInsightWindow = null;
60      }
61
62      if (codeEditor.CompletionWindow == null && codeCompletionResult.CompletionData.Any()) {
63        var cw = codeEditor.CompletionWindow = new CompletionWindow(textArea);
64        cw.CloseWhenCaretAtBeginning = true;
65        cw.StartOffset -= codeCompletionResult.TriggerWordLength;
66        cw.Closed += (sender, args) => codeEditor.CompletionWindow = null;
67
68        var data = cw.CompletionList.CompletionData;
69        var newData = codeCompletionResult.CompletionData.OrderBy(x => x.Text).ToArray();
70        foreach (var completion in newData)
71          data.Add(completion);
72
73        if (codeCompletionResult.TriggerWordLength > 0)
74          cw.CompletionList.SelectItem(codeCompletionResult.TriggerWord);
75
76        cw.Show();
77      }
78
79      if (codeEditor.OverloadInsightWindow != null) {
80        var iw = codeEditor.OverloadInsightWindow;
81        var provider = iw.Provider as IUpdatableOverloadProvider;
82        if (provider != null) {
83          provider.Update(document, offset);
84          if (provider.RequestClose) {
85            iw.Close();
86          }
87        }
88      }
89    }
90
91    protected virtual void DoBackgroundParsing() {
92      while (!codeEditor.IsDisposed) {
93        DoParseStep();
94        Thread.Sleep(1000);
95      }
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.