Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14908 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 4.0 KB
RevLine 
[11700]1#region License Information
2/* HeuristicLab
[14186]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11700]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
[12655]22using System;
[11700]23using System.Linq;
[11800]24using System.Threading.Tasks;
[11700]25using ICSharpCode.AvalonEdit.CodeCompletion;
[12655]26using ICSharpCode.NRefactory.Editor;
27using Timer = System.Timers.Timer;
[11700]28
29namespace HeuristicLab.CodeEditor {
30  internal abstract class CodeCompletionStrategy : ICodeCompletionStrategy {
31    protected readonly CodeEditor codeEditor;
[12655]32    protected readonly Timer parserTimer;
33    protected IDocument document;
[11700]34
35    protected CodeCompletionStrategy(CodeEditor codeEditor) {
36      this.codeEditor = codeEditor;
[12655]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      });
[11700]46    }
47
48    public virtual void DoCodeCompletion(bool controlSpace) {
49      var codeCompletionResult = GetCodeCompletionResult(controlSpace);
50      ApplyCodeCompletionData(codeCompletionResult);
51    }
52
[11937]53    public virtual void Initialize() {
[12655]54      parserTimer.Enabled = true;
[11800]55    }
56
[11700]57    protected abstract CodeCompletionResult GetCodeCompletionResult(bool controlSpace);
[11800]58    protected abstract void DoParseStep();
[11700]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;
[11800]79        var newData = codeCompletionResult.CompletionData.OrderBy(x => x.Text).ToArray();
80        foreach (var completion in newData)
[11700]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    }
[11800]100
[12655]101    private void codeEditor_TextEditorTextChanged(object sender, EventArgs e) {
102      var doc = codeEditor.TextEditor.Document;
103      document = new ReadOnlyDocument(doc, doc.FileName);
[11800]104    }
[11700]105  }
106}
Note: See TracBrowser for help on using the repository browser.