Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CodeEditor/3.3/CodeCompletionProvider.cs @ 4034

Last change on this file since 4034 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: 6.3 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;
30using System.Collections.Generic;
31using System.Text;
32using System.Windows.Forms;
33using ICSharpCode.TextEditor;
34using ICSharpCode.TextEditor.Gui.CompletionWindow;
35
36using Dom = ICSharpCode.SharpDevelop.Dom;
37using NRefactoryResolver = ICSharpCode.SharpDevelop.Dom.NRefactoryResolver.NRefactoryResolver;
38
39namespace HeuristicLab.CodeEditor {
40  class CodeCompletionProvider : ICompletionDataProvider {
41    CodeEditor codeEditor;
42
43    public CodeCompletionProvider(CodeEditor codeEditor) {
44      this.codeEditor = codeEditor;
45    }
46
47    public ImageList ImageList {
48      get {
49        return codeEditor.imageList1;
50      }
51    }
52
53    public string PreSelection {
54      get {
55        return null;
56      }
57    }
58
59    public int DefaultIndex {
60      get {
61        return -1;
62      }
63    }
64
65    public CompletionDataProviderKeyResult ProcessKey(char key) {
66      if (char.IsLetterOrDigit(key) || key == '_') {
67        return CompletionDataProviderKeyResult.NormalKey;
68      } else {
69        // key triggers insertion of selected items
70        return CompletionDataProviderKeyResult.InsertionKey;
71      }
72    }
73
74    /// <summary>
75    /// Called when entry should be inserted. Forward to the insertion action of the completion data.
76    /// </summary>
77    public bool InsertAction(ICompletionData data, TextArea textArea, int insertionOffset, char key) {
78      textArea.Caret.Position = textArea.Document.OffsetToPosition(insertionOffset);
79      return data.InsertAction(textArea, key);
80    }
81
82    public ICompletionData[] GenerateCompletionData(string fileName, TextArea textArea, char charTyped) {
83      // We can return code-completion items like this:
84
85      //return new ICompletionData[] {
86      //  new DefaultCompletionData("Text", "Description", 1)
87      //};
88
89      NRefactoryResolver resolver = new NRefactoryResolver(codeEditor.projectContent.Language);
90      Dom.ResolveResult rr = resolver.Resolve(FindExpression(textArea),
91                                              codeEditor.parseInformation,
92                                              textArea.MotherTextEditorControl.Text);
93      List<ICompletionData> resultList = new List<ICompletionData>();
94      if (rr != null) {
95        ArrayList completionData = rr.GetCompletionData(codeEditor.projectContent);
96        if (completionData != null) {
97          AddCompletionData(resultList, completionData);
98        }
99      }
100      return resultList.ToArray();
101    }
102
103    /// <summary>
104    /// Find the expression the cursor is at.
105    /// Also determines the context (using statement, "new"-expression etc.) the
106    /// cursor is at.
107    /// </summary>
108    Dom.ExpressionResult FindExpression(TextArea textArea) {
109      Dom.IExpressionFinder finder;
110      finder = new Dom.CSharp.CSharpExpressionFinder(codeEditor.parseInformation);
111      Dom.ExpressionResult expression = finder.FindExpression(textArea.Document.TextContent, textArea.Caret.Offset);
112      if (expression.Region.IsEmpty) {
113        expression.Region = new Dom.DomRegion(textArea.Caret.Line + 1, textArea.Caret.Column + 1);
114      }
115      return expression;
116    }
117
118    void AddCompletionData(List<ICompletionData> resultList, ArrayList completionData) {
119      // used to store the method names for grouping overloads
120      Dictionary<string, CodeCompletionData> nameDictionary = new Dictionary<string, CodeCompletionData>();
121
122      // Add the completion data as returned by SharpDevelop.Dom to the
123      // list for the text editor
124      foreach (object obj in completionData) {
125        if (obj is string) {
126          // namespace names are returned as string
127          resultList.Add(new DefaultCompletionData((string)obj, "namespace " + obj, 5));
128        } else if (obj is Dom.IClass) {
129          Dom.IClass c = (Dom.IClass)obj;
130          resultList.Add(new CodeCompletionData(c));
131        } else if (obj is Dom.IMember) {
132          Dom.IMember m = (Dom.IMember)obj;
133          if (m is Dom.IMethod && ((m as Dom.IMethod).IsConstructor)) {
134            // Skip constructors
135            continue;
136          }
137          // Group results by name and add "(x Overloads)" to the
138          // description if there are multiple results with the same name.
139
140          CodeCompletionData data;
141          if (nameDictionary.TryGetValue(m.Name, out data)) {
142            data.AddOverload(m);
143          } else {
144            nameDictionary[m.Name] = data = new CodeCompletionData(m);
145            resultList.Add(data);
146          }
147        } else {
148          // Current ICSharpCode.SharpDevelop.Dom should never return anything else
149          throw new NotSupportedException();
150        }
151      }
152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.