Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.CodeEditor/3.4/LanguageFeatures/CodeCompletion/CSharp/CSharpOverloadProvider.cs @ 18242

Last change on this file since 18242 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 6.2 KB
Line 
1#region License Information
2// CShell, A Simple C# Scripting IDE
3// Copyright (C) 2013  Arnova Asset Management Ltd., Lukas Buhler
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18// This file is based on code from the SharpDevelop project:
19//   Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \Doc\sharpdevelop-copyright.txt)
20//   This code is distributed under the GNU LGPL (for details please see \Doc\COPYING.LESSER.txt)
21
22/* HeuristicLab
23 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
24 *
25 * This file is part of HeuristicLab.
26 *
27 * HeuristicLab is free software: you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation, either version 3 of the License, or
30 * (at your option) any later version.
31 *
32 * HeuristicLab is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35 * GNU General Public License for more details.
36 *
37 * You should have received a copy of the GNU General Public License
38 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
39 */
40#endregion
41
42using System;
43using System.Collections.Generic;
44using System.ComponentModel;
45using System.Diagnostics;
46using System.Linq;
47using ICSharpCode.NRefactory.Completion;
48using ICSharpCode.NRefactory.CSharp.Completion;
49using ICSharpCode.NRefactory.Editor;
50
51namespace HeuristicLab.CodeEditor {
52  internal class CSharpOverloadProvider : IUpdatableOverloadProvider, IParameterDataProvider {
53    private readonly CSharpCodeCompletionContext context;
54    private readonly int startOffset;
55    internal readonly IList<CSharpInsightItem> items;
56    private int selectedIndex;
57
58    public CSharpOverloadProvider(CSharpCodeCompletionContext context, int startOffset, IEnumerable<CSharpInsightItem> items) {
59      Debug.Assert(items != null);
60      this.context = context;
61      this.startOffset = startOffset;
62      this.selectedIndex = 0;
63      this.items = items.ToList();
64
65      Update(context);
66    }
67
68    public bool RequestClose { get; set; }
69
70    public int Count {
71      get { return items.Count; }
72    }
73
74    public object CurrentContent {
75      get { return items[selectedIndex].Content; }
76    }
77
78    public object CurrentHeader {
79      get { return items[selectedIndex].Header; }
80    }
81
82    public string CurrentIndexText {
83      get { return (selectedIndex + 1).ToString() + " of " + this.Count.ToString(); }
84    }
85
86    public int SelectedIndex {
87      get { return selectedIndex; }
88      set {
89        selectedIndex = value;
90        if (selectedIndex >= items.Count)
91          selectedIndex = items.Count - 1;
92        if (selectedIndex < 0)
93          selectedIndex = 0;
94        OnPropertyChanged("SelectedIndex");
95        OnPropertyChanged("CurrentIndexText");
96        OnPropertyChanged("CurrentHeader");
97        OnPropertyChanged("CurrentContent");
98      }
99    }
100
101    public void Update(IDocument document, int offset) {
102      var completionContext = new CSharpCodeCompletionContext(document, offset, context.ProjectContent);
103      Update(completionContext);
104    }
105
106    public void Update(CSharpCodeCompletionContext completionContext) {
107      var completionFactory = new CSharpCodeCompletionDataFactory(completionContext);
108      var pce = new CSharpParameterCompletionEngine(
109          completionContext.Document,
110          completionContext.CompletionContextProvider,
111          completionFactory,
112          completionContext.ProjectContent,
113          completionContext.TypeResolveContextAtCaret
114      );
115
116      int parameterIndex = pce.GetCurrentParameterIndex(startOffset, completionContext.Offset);
117      if (parameterIndex < 0 || !items.Any()) {
118        RequestClose = true;
119        return;
120      } else {
121        if (parameterIndex > items[selectedIndex].Method.Parameters.Count) {
122          var newItem = items.FirstOrDefault(i => parameterIndex <= i.Method.Parameters.Count);
123          SelectedIndex = items.IndexOf(newItem);
124        }
125        if (parameterIndex > 0)
126          parameterIndex--; // NR returns 1-based parameter index
127        foreach (var item in items) {
128          item.HighlightParameter(parameterIndex);
129        }
130      }
131    }
132
133    #region IParameterDataProvider implementation
134    int IParameterDataProvider.StartOffset {
135      get { return startOffset; }
136    }
137
138    string IParameterDataProvider.GetHeading(int overload, string[] parameterDescription, int currentParameter) {
139      throw new NotImplementedException();
140    }
141
142    string IParameterDataProvider.GetDescription(int overload, int currentParameter) {
143      throw new NotImplementedException();
144    }
145
146    string IParameterDataProvider.GetParameterDescription(int overload, int paramIndex) {
147      throw new NotImplementedException();
148    }
149
150    string IParameterDataProvider.GetParameterName(int overload, int currentParameter) {
151      throw new NotImplementedException();
152    }
153
154    int IParameterDataProvider.GetParameterCount(int overload) {
155      throw new NotImplementedException();
156    }
157
158    bool IParameterDataProvider.AllowParameterList(int overload) {
159      throw new NotImplementedException();
160    }
161    #endregion
162
163    public event PropertyChangedEventHandler PropertyChanged;
164    private void OnPropertyChanged(string propertyName) {
165      var args = new PropertyChangedEventArgs(propertyName);
166      if (PropertyChanged != null)
167        PropertyChanged(this, args);
168    }
169  }
170}
Note: See TracBrowser for help on using the repository browser.