Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4614 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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