Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8055 was 7983, checked in by epitzer, 12 years ago

#1826 catch exceptions from sharp develop code completion

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