Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.CodeEditor/3.4/LanguageFeatures/LanguageFeatures.cs @ 18242

Last change on this file since 18242 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Windows.Input;
24
25namespace HeuristicLab.CodeEditor {
26  internal abstract class LanguageFeatures : ILanguageFeatures {
27    protected readonly CodeEditor codeEditor;
28    protected readonly CommandBinding commandBinding;
29
30    private readonly ICodeFoldingStrategy codeFoldingStrategy;
31    public ICodeFoldingStrategy CodeFoldingStrategy { get { return codeFoldingStrategy; } }
32
33    private readonly ICodeCompletionStrategy codeCompletionStrategy;
34    public ICodeCompletionStrategy CodeCompletionStrategy { get { return codeCompletionStrategy; } }
35
36    protected LanguageFeatures(CodeEditor codeEditor, ICodeFoldingStrategy codeFoldingStrategy, ICodeCompletionStrategy codeCompletionStrategy) {
37      this.codeEditor = codeEditor;
38      this.codeFoldingStrategy = codeFoldingStrategy;
39      this.codeCompletionStrategy = codeCompletionStrategy;
40
41      commandBinding = new CommandBinding(codeEditor.CompletionCommand, CodeCompletionRequestedHandler);
42      if (codeFoldingStrategy != null) AddCodeFoldingStrategy();
43      if (codeCompletionStrategy != null) AddCodeComplectionStrategy();
44    }
45
46    #region CodeFoldingStrategy
47    protected virtual void AddCodeFoldingStrategy() {
48      codeEditor.TextEditor.TextChanged += CodeFoldingHandler;
49    }
50
51    private void CodeFoldingHandler(object sender, EventArgs e) {
52      codeFoldingStrategy.DoCodeFolding();
53    }
54    #endregion
55
56    #region CodeCompletionStrategy
57    protected virtual void AddCodeComplectionStrategy() {
58      codeEditor.Load += LoadedHandler;
59      codeEditor.TextEditor.TextArea.TextEntered += CodeCompletionHandler;
60      codeEditor.TextEditor.TextArea.TextEntering += InsertionRequestedHandler;
61      codeEditor.TextEditor.CommandBindings.Add(commandBinding);
62    }
63
64    private void LoadedHandler(object sender, EventArgs e) {
65      codeCompletionStrategy.Initialize();
66    }
67
68    private void CodeCompletionRequestedHandler(object sender, EventArgs e) {
69      var readOnlySectionProvider = codeEditor.TextEditor.TextArea.ReadOnlySectionProvider;
70      if (readOnlySectionProvider.CanInsert(codeEditor.TextEditor.CaretOffset))
71        codeCompletionStrategy.DoCodeCompletion(true);
72    }
73
74    private void CodeCompletionHandler(object sender, EventArgs e) {
75      var readOnlySectionProvider = codeEditor.TextEditor.TextArea.ReadOnlySectionProvider;
76      if (readOnlySectionProvider.CanInsert(codeEditor.TextEditor.CaretOffset))
77        codeCompletionStrategy.DoCodeCompletion(false);
78    }
79
80    private void InsertionRequestedHandler(object sender, TextCompositionEventArgs e) {
81      var cw = codeEditor.CompletionWindow;
82      if (cw != null && e.Text.Length > 0 && !char.IsLetterOrDigit(e.Text[0])) {
83        cw.CompletionList.RequestInsertion(e);
84      }
85    }
86    #endregion
87  }
88}
Note: See TracBrowser for help on using the repository browser.