~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Ide.Gui.Content/CompletionTextEditorExtension.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
        public class CompletionTextEditorExtension: TextEditorExtension
43
43
        {
44
44
                CodeCompletionContext currentCompletionContext;
45
 
                ICompletionWidget completionWidget;
 
45
 
46
46
                bool autoHideCompletionWindow = true;
47
47
                bool enableCodeCompletion = false;
48
48
                bool enableParameterInsight = false;
49
49
                
50
 
                protected ICompletionWidget CompletionWidget {
51
 
                        get {
52
 
                                return completionWidget;
53
 
                        }
 
50
                public ICompletionWidget CompletionWidget {
 
51
                        get;
 
52
                        set;
54
53
                }
55
54
                
56
55
                public void ShowCompletion (ICompletionDataList completionList)
57
56
                {
58
 
                        completionWidget = Document.GetContent <ICompletionWidget> ();
59
 
                        currentCompletionContext = completionWidget.CreateCodeCompletionContext (Document.TextEditorData.Caret.Offset);
 
57
                        currentCompletionContext = CompletionWidget.CreateCodeCompletionContext (Document.Editor.Caret.Offset);
60
58
                        int cpos, wlen;
61
59
                        if (!GetCompletionCommandOffset (out cpos, out wlen)) {
62
 
                                cpos = Document.TextEditorData.Caret.Offset;
 
60
                                cpos = Document.Editor.Caret.Offset;
63
61
                                wlen = 0;
64
62
                        }
65
63
                        currentCompletionContext.TriggerOffset = cpos;
66
64
                        currentCompletionContext.TriggerWordLength = wlen;
67
65
                        
68
 
                        CompletionWindowManager.ShowWindow ('\0', completionList, completionWidget, currentCompletionContext, OnCompletionWindowClosed);
 
66
                        CompletionWindowManager.ShowWindow ('\0', completionList, CompletionWidget, currentCompletionContext, OnCompletionWindowClosed);
69
67
                }
70
68
 
71
69
                // When a key is pressed, and before the key is processed by the editor, this method will be invoked.
86
84
                        }
87
85
                        
88
86
                        if (ParameterInformationWindowManager.IsWindowVisible) {
89
 
                                if (ParameterInformationWindowManager.ProcessKeyEvent (key, modifier))
 
87
                                if (ParameterInformationWindowManager.ProcessKeyEvent (CompletionWidget, key, modifier))
90
88
                                        return false;
91
89
                                autoHideCompletionWindow = false;
92
90
                        }
103
101
                        
104
102
                        // Handle parameter completion
105
103
                        if (ParameterInformationWindowManager.IsWindowVisible) {
106
 
                                ParameterInformationWindowManager.CurrentCodeCompletionContext = Editor.CurrentCodeCompletionContext;
107
 
                                ParameterInformationWindowManager.PostProcessKeyEvent (key, modifier);
 
104
                                ParameterInformationWindowManager.PostProcessKeyEvent (CompletionWidget, key, modifier);
108
105
                        }
109
106
                        
110
107
                        if ((modifier & ignoreMods) != 0)
125
122
                        
126
123
                        // Handle code completion
127
124
 
128
 
                        if (keyChar != '\0' && completionWidget != null && currentCompletionContext == null) {
129
 
                                currentCompletionContext = completionWidget.CreateCodeCompletionContext (Editor.CursorPosition);
 
125
                        if (keyChar != '\0' && CompletionWidget != null && currentCompletionContext == null) {
 
126
                                currentCompletionContext = CompletionWidget.CurrentCodeCompletionContext;
130
127
                                
131
128
                                int triggerWordLength = currentCompletionContext.TriggerWordLength;
132
129
                                ICompletionDataList completionList = HandleCodeCompletion (currentCompletionContext, keyChar,
133
130
                                                                                           ref triggerWordLength);
134
131
                                
135
 
                                if (triggerWordLength > 0 && (triggerWordLength < Editor.CursorPosition
136
 
                                                              || (triggerWordLength == 1 && Editor.CursorPosition == 1)))
 
132
                                if (triggerWordLength > 0 && (triggerWordLength < Editor.Caret.Offset
 
133
                                                              || (triggerWordLength == 1 && Editor.Caret.Offset == 1)))
137
134
                                {
138
135
                                        currentCompletionContext
139
 
                                                = completionWidget.CreateCodeCompletionContext (Editor.CursorPosition - triggerWordLength);
 
136
                                                = CompletionWidget.CreateCodeCompletionContext (Editor.Caret.Offset - triggerWordLength);
140
137
                                        currentCompletionContext.TriggerWordLength = triggerWordLength;
141
138
                                }
142
139
                                
143
140
                                if (completionList != null) {
144
 
                                        if (!CompletionWindowManager.ShowWindow (keyChar, completionList, completionWidget, 
 
141
                                        if (!CompletionWindowManager.ShowWindow (keyChar, completionList, CompletionWidget, 
145
142
                                                                                 currentCompletionContext, OnCompletionWindowClosed))
146
143
                                                currentCompletionContext = null;
147
144
                                } else {
149
146
                                }
150
147
                        }
151
148
                        
152
 
                        if (enableParameterInsight && completionWidget != null) {
153
 
                                CodeCompletionContext ctx = completionWidget.CreateCodeCompletionContext (Editor.CursorPosition);
 
149
                        if (enableParameterInsight && CompletionWidget != null) {
 
150
                                CodeCompletionContext ctx = CompletionWidget.CurrentCodeCompletionContext;
154
151
                                IParameterDataProvider paramProvider = HandleParameterCompletion (ctx, keyChar);
155
152
                                if (paramProvider != null)
156
 
                                        ParameterInformationWindowManager.ShowWindow (ctx, paramProvider);
 
153
                                        ParameterInformationWindowManager.ShowWindow (CompletionWidget, ctx, paramProvider);
157
154
                        }
158
155
                        
159
156
                        autoHideCompletionWindow = true;
163
160
                
164
161
                protected void ShowCompletion (ICompletionDataList completionList, int triggerWordLength, char keyChar)
165
162
                {
166
 
                        if (completionWidget != null && currentCompletionContext == null) {
167
 
                                currentCompletionContext = completionWidget.CreateCodeCompletionContext (Editor.CursorPosition);
168
 
                                if (triggerWordLength > 0 && triggerWordLength < Editor.CursorPosition) {
 
163
                        if (CompletionWidget != null && currentCompletionContext == null) {
 
164
                                currentCompletionContext = CompletionWidget.CurrentCodeCompletionContext;
 
165
                                if (triggerWordLength > 0 && triggerWordLength < Editor.Caret.Offset) {
169
166
                                        currentCompletionContext =
170
 
                                                completionWidget.CreateCodeCompletionContext (Editor.CursorPosition - triggerWordLength);       
 
167
                                                CompletionWidget.CreateCodeCompletionContext (Editor.Caret.Offset - triggerWordLength); 
171
168
                                        currentCompletionContext.TriggerWordLength = triggerWordLength;
172
169
                                }
173
170
                                if (completionList != null)
174
 
                                        CompletionWindowManager.ShowWindow (keyChar, completionList, completionWidget, 
 
171
                                        CompletionWindowManager.ShowWindow (keyChar, completionList, CompletionWidget, 
175
172
                                                                            currentCompletionContext, OnCompletionWindowClosed);
176
173
                                else
177
174
                                        currentCompletionContext = null;
184
181
                        currentCompletionContext = null;
185
182
                }
186
183
                
187
 
                void OnCompletionContextChanged (object o, EventArgs a)
 
184
                protected void OnCompletionContextChanged (object o, EventArgs a)
188
185
                {
189
186
                        if (autoHideCompletionWindow) {
190
187
                                CompletionWindowManager.HideWindow ();
191
 
                                ParameterInformationWindowManager.HideWindow ();
 
188
                                ParameterInformationWindowManager.HideWindow (CompletionWidget);
192
189
                        }
193
190
                }
194
191
 
215
212
                        ICompletionDataList completionList = null;
216
213
                        int cpos, wlen;
217
214
                        if (!GetCompletionCommandOffset (out cpos, out wlen)) {
218
 
                                cpos = Editor.CursorPosition;
 
215
                                cpos = Editor.Caret.Offset;
219
216
                                wlen = 0;
220
217
                        }
221
 
                        currentCompletionContext = completionWidget.CreateCodeCompletionContext (cpos);
 
218
                        currentCompletionContext = CompletionWidget.CreateCodeCompletionContext (cpos);
222
219
                        currentCompletionContext.TriggerWordLength = wlen;
223
220
                        completionList = CodeCompletionCommand (currentCompletionContext);
224
221
                                
225
222
                        if (completionList != null)
226
 
                                CompletionWindowManager.ShowWindow ((char)0, completionList, completionWidget, 
 
223
                                CompletionWindowManager.ShowWindow ((char)0, completionList, CompletionWidget, 
227
224
                                                                    currentCompletionContext, OnCompletionWindowClosed);
228
225
                        else
229
226
                                currentCompletionContext = null;
235
232
                        ICompletionDataList completionList = null;
236
233
                        int cpos, wlen;
237
234
                        if (!GetCompletionCommandOffset (out cpos, out wlen)) {
238
 
                                cpos = Editor.CursorPosition;
 
235
                                cpos = Editor.Caret.Offset;
239
236
                                wlen = 0;
240
237
                        }
241
238
                        
242
 
                        currentCompletionContext = completionWidget.CreateCodeCompletionContext (cpos);
 
239
                        currentCompletionContext = CompletionWidget.CreateCodeCompletionContext (cpos);
243
240
                        currentCompletionContext.TriggerWordLength = wlen;
244
 
                        completionList = Document.TextEditor.SelectionStartPosition != Document.TextEditor.SelectionEndPosition ? ShowCodeSurroundingsCommand (currentCompletionContext) : ShowCodeTemplatesCommand (currentCompletionContext);
 
241
                        completionList = Document.Editor.IsSomethingSelected ? ShowCodeSurroundingsCommand (currentCompletionContext) : ShowCodeTemplatesCommand (currentCompletionContext);
245
242
                        
246
243
                        if (completionList != null)
247
 
                                CompletionWindowManager.ShowWindow ((char)0, completionList, completionWidget, currentCompletionContext, OnCompletionWindowClosed);
 
244
                                CompletionWindowManager.ShowWindow ((char)0, completionList, CompletionWidget, currentCompletionContext, OnCompletionWindowClosed);
248
245
                        else
249
246
                                currentCompletionContext = null;
250
247
                }
253
250
                internal void OnUpdateShowCodeTemplatesWindow (CommandInfo info)
254
251
                {
255
252
                        info.Bypass = !CanRunCompletionCommand ();
256
 
                        info.Text = Document.TextEditor.SelectionStartPosition != Document.TextEditor.SelectionEndPosition ? GettextCatalog.GetString ("_Surround With...") : GettextCatalog.GetString ("I_nsert Template...");
 
253
                        info.Text = Document.Editor.IsSomethingSelected ? GettextCatalog.GetString ("_Surround With...") : GettextCatalog.GetString ("I_nsert Template...");
257
254
                }
258
255
        
259
256
                
263
260
                        IParameterDataProvider cp = null;
264
261
                        int cpos;
265
262
                        if (!GetParameterCompletionCommandOffset (out cpos))
266
 
                                cpos = Editor.CursorPosition;
267
 
                        CodeCompletionContext ctx = completionWidget.CreateCodeCompletionContext (cpos);
 
263
                                cpos = Editor.Caret.Offset;
 
264
                        CodeCompletionContext ctx = CompletionWidget.CreateCodeCompletionContext (cpos);
268
265
                        cp = ParameterCompletionCommand (ctx);
269
 
 
270
266
                        if (cp != null) {
271
 
                                ParameterInformationWindowManager.ShowWindow (ctx, cp);
272
 
                                ParameterInformationWindowManager.PostProcessKeyEvent (Gdk.Key.F, Gdk.ModifierType.None);
 
267
                                ParameterInformationWindowManager.ShowWindow (CompletionWidget, ctx, cp);
 
268
                                ParameterInformationWindowManager.PostProcessKeyEvent (CompletionWidget, Gdk.Key.F, Gdk.ModifierType.None);
273
269
                        }
274
270
                }
275
271
                
276
272
                public virtual bool CanRunCompletionCommand ()
277
273
                {
278
 
                        return (completionWidget != null && currentCompletionContext == null);
 
274
                        return (CompletionWidget != null && currentCompletionContext == null);
279
275
                }
280
276
                
281
277
                public virtual bool CanRunParameterCompletionCommand ()
282
278
                {
283
 
                        return (completionWidget != null && !ParameterInformationWindowManager.IsWindowVisible);
 
279
                        return (CompletionWidget != null && !ParameterInformationWindowManager.IsWindowVisible);
284
280
                }
285
281
                
286
282
                
305
301
                public virtual bool GetCompletionCommandOffset (out int cpos, out int wlen)
306
302
                {
307
303
                        cpos = wlen = 0;
308
 
                        int pos = Editor.CursorPosition - 1;
 
304
                        int pos = Editor.Caret.Offset - 1;
309
305
                        while (pos >= 0) {
310
306
                                char c = Editor.GetCharAt (pos);
311
307
                                if (!char.IsLetterOrDigit (c) && c != '_')
317
313
                        
318
314
                        pos++;
319
315
                        cpos = pos;
320
 
                        int len = Editor.TextLength;
 
316
                        int len = Editor.Length;
321
317
                        
322
318
                        while (pos < len) {
323
319
                                char c = Editor.GetCharAt (pos);
371
367
                        // the char at the cursor position. If it returns a provider, just return it.
372
368
                        
373
369
                        int pos = completionContext.TriggerOffset;
374
 
                        string txt = Editor.GetText (pos - 1, pos);
 
370
                        string txt = Editor.GetTextBetween (pos - 1, pos);
375
371
                        if (txt.Length > 0) {
376
372
                                ICompletionDataList completionList = HandleCodeCompletion (completionContext, txt[0]);
377
373
                                if (completionList != null)
397
393
                        // the char at the cursor position. If it returns a provider, just return it.
398
394
                        
399
395
                        int pos = completionContext.TriggerOffset;
400
 
                        string txt = Editor.GetText (pos - 1, pos);
401
 
                        if (txt.Length > 0) {
402
 
                                IParameterDataProvider cp = HandleParameterCompletion (completionContext, txt[0]);
403
 
                                if (cp != null)
404
 
                                        return cp;
405
 
                        }
 
396
                        if (pos <= 0)
 
397
                                return null;
 
398
                        IParameterDataProvider cp = HandleParameterCompletion (completionContext, Editor.Document.GetCharAt (pos - 1));
 
399
                        if (cp != null)
 
400
                                return cp;
406
401
                        return null;
407
402
                }
408
403
                
414
409
                        enableParameterInsight = (bool)PropertyService.Get ("EnableParameterInsight", true);
415
410
                        
416
411
                        PropertyService.PropertyChanged += OnPropertyUpdated;
417
 
                        completionWidget = Document.GetContent <ICompletionWidget> ();
418
 
                        if (completionWidget != null)
419
 
                                completionWidget.CompletionContextChanged += OnCompletionContextChanged;
 
412
                        CompletionWidget = Document.GetContent <ICompletionWidget> ();
 
413
                        if (CompletionWidget != null)
 
414
                                CompletionWidget.CompletionContextChanged += OnCompletionContextChanged;
420
415
                }
421
416
 
422
417
                bool disposed = false;
425
420
                        if (!disposed) {
426
421
                                disposed = true;
427
422
                                PropertyService.PropertyChanged -= OnPropertyUpdated;
428
 
                                if (completionWidget != null)
429
 
                                        completionWidget.CompletionContextChanged -= OnCompletionContextChanged;
 
423
                                if (CompletionWidget != null)
 
424
                                        CompletionWidget.CompletionContextChanged -= OnCompletionContextChanged;
430
425
                        }
431
426
                        base.Dispose ();
432
427
                }