Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.CodeEditor/3.4/LanguageFeatures/CodeCompletion/CodeCompletionStrategy.cs @ 16044

Last change on this file since 16044 was 12654, checked in by ascheibe, 9 years ago

#2419 properly stop timer for parsing when CodeEditor is closed

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