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