Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10083 was 8140, checked in by ascheibe, 12 years ago

#1722 fixed compiler warning

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.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        try {
95          ArrayList completionData = rr.GetCompletionData(codeEditor.projectContent);
96          if (completionData != null) {
97            AddCompletionData(resultList, completionData);
98          }
99        }
100        catch (NullReferenceException) { }
101      }
102      return resultList.ToArray();
103    }
104
105    /// <summary>
106    /// Find the expression the cursor is at.
107    /// Also determines the context (using statement, "new"-expression etc.) the
108    /// cursor is at.
109    /// </summary>
110    Dom.ExpressionResult FindExpression(TextArea textArea) {
111      Dom.IExpressionFinder finder;
112      finder = new Dom.CSharp.CSharpExpressionFinder(codeEditor.parseInformation);
113      Dom.ExpressionResult expression = finder.FindExpression(textArea.Document.TextContent, textArea.Caret.Offset);
114      if (expression.Region.IsEmpty) {
115        expression.Region = new Dom.DomRegion(textArea.Caret.Line + 1, textArea.Caret.Column + 1);
116      }
117      return expression;
118    }
119
120    void AddCompletionData(List<ICompletionData> resultList, ArrayList completionData) {
121      // used to store the method names for grouping overloads
122      Dictionary<string, CodeCompletionData> nameDictionary = new Dictionary<string, CodeCompletionData>();
123
124      // Add the completion data as returned by SharpDevelop.Dom to the
125      // list for the text editor
126      foreach (object obj in completionData) {
127        if (obj is string) {
128          // namespace names are returned as string
129          resultList.Add(new DefaultCompletionData((string)obj, "namespace " + obj, 5));
130        } else if (obj is Dom.IClass) {
131          Dom.IClass c = (Dom.IClass)obj;
132          resultList.Add(new CodeCompletionData(c));
133        } else if (obj is Dom.IMember) {
134          Dom.IMember m = (Dom.IMember)obj;
135          if (m is Dom.IMethod && ((m as Dom.IMethod).IsConstructor)) {
136            // Skip constructors
137            continue;
138          }
139          // Group results by name and add "(x Overloads)" to the
140          // description if there are multiple results with the same name.
141
142          CodeCompletionData data;
143          if (nameDictionary.TryGetValue(m.Name, out data)) {
144            data.AddOverload(m);
145          } else {
146            nameDictionary[m.Name] = data = new CodeCompletionData(m);
147            resultList.Add(data);
148          }
149        } else {
150          // Current ICSharpCode.SharpDevelop.Dom should never return anything else
151          throw new NotSupportedException();
152        }
153      }
154    }
155  }
156}
Note: See TracBrowser for help on using the repository browser.