Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CodeEditor/HeuristicLab.CodeEditor/3.4/LanguageFeatures/CodeCompletion/CodeCompletionStrategy.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: 3.6 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 RunBackgroundParser() {
43      backgroundParser.Start();
44    }
45
46    protected abstract CodeCompletionResult GetCodeCompletionResult(bool controlSpace);
47    protected abstract void DoParseStep();
48
49    protected virtual void ApplyCodeCompletionData(CodeCompletionResult codeCompletionResult) {
50      var textArea = codeEditor.TextEditor.TextArea;
51      var document = codeEditor.TextEditor.Document;
52      int offset = codeEditor.TextEditor.CaretOffset;
53
54      if (codeEditor.OverloadInsightWindow == null && codeCompletionResult.OverloadProvider != null) {
55        var iw = codeEditor.OverloadInsightWindow = new OverloadInsightWindow(textArea);
56        iw.Provider = codeCompletionResult.OverloadProvider;
57        iw.Show();
58        iw.Closed += (sender, args) => codeEditor.OverloadInsightWindow = null;
59      }
60
61      if (codeEditor.CompletionWindow == null && codeCompletionResult.CompletionData.Any()) {
62        var cw = codeEditor.CompletionWindow = new CompletionWindow(textArea);
63        cw.CloseWhenCaretAtBeginning = true;
64        cw.StartOffset -= codeCompletionResult.TriggerWordLength;
65        cw.Closed += (sender, args) => codeEditor.CompletionWindow = null;
66
67        var data = cw.CompletionList.CompletionData;
68        var newData = codeCompletionResult.CompletionData.OrderBy(x => x.Text).ToArray();
69        foreach (var completion in newData)
70          data.Add(completion);
71
72        if (codeCompletionResult.TriggerWordLength > 0)
73          cw.CompletionList.SelectItem(codeCompletionResult.TriggerWord);
74
75        cw.Show();
76      }
77
78      if (codeEditor.OverloadInsightWindow != null) {
79        var iw = codeEditor.OverloadInsightWindow;
80        var provider = iw.Provider as IUpdatableOverloadProvider;
81        if (provider != null) {
82          provider.Update(document, offset);
83          if (provider.RequestClose) {
84            iw.Close();
85          }
86        }
87      }
88    }
89
90    protected virtual void DoBackgroundParsing() {
91      while (!codeEditor.IsDisposed) {
92        DoParseStep();
93        Thread.Sleep(1000);
94      }
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.