Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CodeEditor/3.2/CodeCompletionKeyHandler.cs @ 2659

Last change on this file since 2659 was 2659, checked in by epitzer, 15 years ago

Add new Plugin that provides a code editor with syntax highlighting and code completion (#842)

File size: 4.2 KB
Line 
1// CSharp Editor Example with Code Completion
2// Copyright (c) 2006, Daniel Grunwald
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without modification, are
6// permitted provided that the following conditions are met:
7//
8// - Redistributions of source code must retain the above copyright notice, this list
9//   of conditions and the following disclaimer.
10//
11// - Redistributions in binary form must reproduce the above copyright notice, this list
12//   of conditions and the following disclaimer in the documentation and/or other materials
13//   provided with the distribution.
14//
15// - Neither the name of the ICSharpCode team nor the names of its contributors may be used to
16//   endorse or promote products derived from this software without specific prior written
17//   permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &AS IS& AND ANY EXPRESS
20// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28using System;
29using System.Collections.Generic;
30using System.Drawing;
31using System.Windows.Forms;
32using System.IO;
33using System.Threading;
34
35using ICSharpCode.TextEditor;
36using ICSharpCode.TextEditor.Gui;
37using ICSharpCode.TextEditor.Gui.CompletionWindow;
38
39namespace HeuristicLab.CodeEditor {
40  class CodeCompletionKeyHandler {
41    CodeEditor codeEditor;
42    TextEditorControl editor;
43    CodeCompletionWindow codeCompletionWindow;
44
45    private CodeCompletionKeyHandler(CodeEditor codeEditor, TextEditorControl editor) {
46      this.codeEditor = codeEditor;
47      this.editor = editor;
48    }
49
50    public static CodeCompletionKeyHandler Attach(CodeEditor codeEditor, TextEditorControl editor) {
51      CodeCompletionKeyHandler h = new CodeCompletionKeyHandler(codeEditor, editor);
52
53      editor.ActiveTextAreaControl.TextArea.KeyEventHandler += h.TextAreaKeyEventHandler;
54
55      // When the editor is disposed, close the code completion window
56      editor.Disposed += h.CloseCodeCompletionWindow;
57
58      return h;
59    }
60
61    /// <summary>
62    /// Return true to handle the keypress, return false to let the text area handle the keypress
63    /// </summary>
64    bool TextAreaKeyEventHandler(char key) {
65      if (codeCompletionWindow != null) {
66        // If completion window is open and wants to handle the key, don't let the text area
67        // handle it
68        if (codeCompletionWindow.ProcessKeyEvent(key))
69          return true;
70      }
71      if (key == '.') {
72        ICompletionDataProvider completionDataProvider = new CodeCompletionProvider(codeEditor);
73
74        codeCompletionWindow = CodeCompletionWindow.ShowCompletionWindow(
75          codeEditor.ParentForm,      // The parent window for the completion window
76          editor,                   // The text editor to show the window for
77          CodeEditor.DummyFileName,    // Filename - will be passed back to the provider
78          completionDataProvider,    // Provider to get the list of possible completions
79          key                        // Key pressed - will be passed to the provider
80        );
81        if (codeCompletionWindow != null) {
82          // ShowCompletionWindow can return null when the provider returns an empty list
83          codeCompletionWindow.Closed += new EventHandler(CloseCodeCompletionWindow);
84        }
85      }
86      return false;
87    }
88
89    void CloseCodeCompletionWindow(object sender, EventArgs e) {
90      if (codeCompletionWindow != null) {
91        codeCompletionWindow.Closed -= new EventHandler(CloseCodeCompletionWindow);
92        codeCompletionWindow.Dispose();
93        codeCompletionWindow = null;
94      }
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.