Beispiel #1
0
 protected void ShowCompletion(ICompletionDataList completionList, int triggerWordLength, char keyChar)
 {
     if (Editor.SelectionMode == SelectionMode.Block)
     {
         return;
     }
     if (CompletionWidget != null && CurrentCompletionContext == null)
     {
         CurrentCompletionContext = CompletionWidget.CurrentCodeCompletionContext;
         if (triggerWordLength > 0 && triggerWordLength < Editor.CaretOffset)
         {
             CurrentCompletionContext =
                 CompletionWidget.CreateCodeCompletionContext(Editor.CaretOffset - triggerWordLength);
             CurrentCompletionContext.TriggerWordLength = triggerWordLength;
         }
         if (completionList != null)
         {
             CompletionWindowManager.ShowWindow(this, keyChar, completionList, CompletionWidget, CurrentCompletionContext);
         }
         else
         {
             CurrentCompletionContext = null;
         }
     }
     autoHideCompletionWindow = autoHideParameterWindow = true;
 }
        public virtual async void RunCompletionCommand()
        {
            if (Editor.SelectionMode == SelectionMode.Block)
            {
                return;
            }

            if (CompletionWindowManager.IsVisible)
            {
                CompletionWindowManager.Wnd.ToggleCategoryMode();
                return;
            }
            ICompletionDataList completionList = null;
            int cpos, wlen;

            if (!GetCompletionCommandOffset(out cpos, out wlen))
            {
                cpos = Editor.CaretOffset;
                wlen = 0;
            }
            CurrentCompletionContext = CompletionWidget.CreateCodeCompletionContext(cpos);
            CurrentCompletionContext.TriggerWordLength = wlen;
            completionList = await CodeCompletionCommand(CurrentCompletionContext);

            if (completionList == null || !CompletionWindowManager.ShowWindow(this, (char)0, completionList, CompletionWidget, CurrentCompletionContext))
            {
                CurrentCompletionContext = null;
            }
        }
        public virtual void RunShowCodeTemplatesWindow()
        {
            Editor.EnsureCaretIsNotVirtual();
            ICompletionDataList completionList = null;
            int cpos, wlen;

            if (!GetCompletionCommandOffset(out cpos, out wlen))
            {
                cpos = Editor.CaretOffset;
                wlen = 0;
            }

            var ctx = CompletionWidget.CreateCodeCompletionContext(cpos);

            ctx.TriggerWordLength = wlen;
            completionList        = Editor.IsSomethingSelected ? ShowCodeSurroundingsCommand(ctx) : ShowCodeTemplatesCommand(ctx);
            if (completionList == null)
            {
                return;
            }
            var wnd = CompletionListWindow.CreateAsDialog();

            wnd.Extension = this;
            wnd.ShowListWindow((char)0, completionList, CompletionWidget, ctx);
        }
        public virtual async Task TriggerCompletion(CompletionTriggerReason reason)
        {
            if (Editor.SelectionMode == SelectionMode.Block)
            {
                return;
            }

            if (CompletionWindowManager.IsVisible)
            {
                CompletionWindowManager.Wnd.ToggleCategoryMode();
                return;
            }
            Editor.EnsureCaretIsNotVirtual();
            ICompletionDataList completionList = null;
            int cpos, wlen;

            if (!GetCompletionCommandOffset(out cpos, out wlen))
            {
                cpos = Editor.CaretOffset;
                wlen = 0;
            }
            CurrentCompletionContext = CompletionWidget.CreateCodeCompletionContext(cpos);
            CurrentCompletionContext.TriggerWordLength = wlen;
            completionList = await HandleCodeCompletionAsync(CurrentCompletionContext, new CompletionTriggerInfo (reason));

            if (completionList != null && completionList.TriggerWordStart >= 0)
            {
                CurrentCompletionContext.TriggerOffset     = completionList.TriggerWordStart;
                CurrentCompletionContext.TriggerWordLength = completionList.TriggerWordLength;
            }
            if (completionList == null || !CompletionWindowManager.ShowWindow(this, (char)0, completionList, CompletionWidget, CurrentCompletionContext))
            {
                CurrentCompletionContext = null;
            }
        }
        public virtual void RunShowCodeTemplatesWindow()
        {
            ICompletionDataList completionList = null;
            int cpos, wlen;

            if (!GetCompletionCommandOffset(out cpos, out wlen))
            {
                cpos = Editor.CaretOffset;
                wlen = 0;
            }

            var ctx = CompletionWidget.CreateCodeCompletionContext(cpos);

            ctx.TriggerWordLength = wlen;
            completionList        = Editor.IsSomethingSelected ? ShowCodeSurroundingsCommand(ctx) : ShowCodeTemplatesCommand(ctx);
            if (completionList == null)
            {
                return;
            }
            var wnd = new CompletionListWindow(Gtk.WindowType.Toplevel);

            wnd.TypeHint        = Gdk.WindowTypeHint.Dialog;
            wnd.SkipPagerHint   = true;
            wnd.SkipTaskbarHint = true;
            wnd.Decorated       = false;
            wnd.Extension       = this;
            wnd.ShowListWindow((char)0, completionList, CompletionWidget, ctx);
        }
Beispiel #6
0
        public virtual async Task TriggerCompletion(CompletionTriggerReason reason)
        {
            if (Editor.SelectionMode == SelectionMode.Block)
            {
                return;
            }

            if (CompletionWindowManager.IsVisible)
            {
                CompletionWindowManager.ToggleCategoryMode();
                return;
            }
            Editor.EnsureCaretIsNotVirtual();
            ICompletionDataList completionList = null;
            int cpos, wlen;

            if (!GetCompletionCommandOffset(out cpos, out wlen))
            {
                cpos = Editor.CaretOffset;
                wlen = 0;
            }

            completionTokenSrc.Cancel();
            completionTokenSrc = new CancellationTokenSource();
            var token = completionTokenSrc.Token;

            CurrentCompletionContext = CompletionWidget.CreateCodeCompletionContext(cpos);
            CurrentCompletionContext.TriggerWordLength = wlen;

            var  timer = CurrentCompletionContext.BeginTiming();
            bool failure = false;

            try {
                completionList = await DoHandleCodeCompletionAsync(CurrentCompletionContext, new CompletionTriggerInfo (reason), token);

                if (completionList != null && completionList.TriggerWordStart >= 0)
                {
                    CurrentCompletionContext.TriggerOffset     = completionList.TriggerWordStart;
                    CurrentCompletionContext.TriggerWordLength = completionList.TriggerWordLength;
                }
                if (completionList == null || !CompletionWindowManager.ShowWindow(this, (char)0, completionList, CompletionWidget, CurrentCompletionContext))
                {
                    CurrentCompletionContext = null;
                }
            } catch (Exception) {
                failure = true;
                throw;
            } finally {
                timer.End();
                if (failure)
                {
                    completionStats.OnFailure(timer.Duration);
                }
                else
                {
                    completionStats.OnSuccess(timer.Duration);
                }
            }
        }
        public virtual async Task TriggerCompletion(CompletionTriggerReason reason)
        {
            if (Editor.SelectionMode == SelectionMode.Block)
            {
                return;
            }

            if (CompletionWindowManager.IsVisible)
            {
                CompletionWindowManager.ToggleCategoryMode();
                return;
            }
            Editor.EnsureCaretIsNotVirtual();
            ICompletionDataList completionList = null;
            int cpos, wlen;

            if (!GetCompletionCommandOffset(out cpos, out wlen))
            {
                cpos = Editor.CaretOffset;
                wlen = 0;
            }

            completionTokenSrc.Cancel();
            completionTokenSrc = new CancellationTokenSource();
            var token = completionTokenSrc.Token;

            CurrentCompletionContext = CompletionWidget.CreateCodeCompletionContext(cpos);
            CurrentCompletionContext.TriggerWordLength = wlen;

            var metadata = new Dictionary <string, string> ();

            metadata ["Result"] = "Success";
            try {
                Counters.ProcessCodeCompletion.BeginTiming(metadata);
                completionList = await DoHandleCodeCompletionAsync(CurrentCompletionContext, new CompletionTriggerInfo (reason), token);

                if (completionList != null && completionList.TriggerWordStart >= 0)
                {
                    CurrentCompletionContext.TriggerOffset     = completionList.TriggerWordStart;
                    CurrentCompletionContext.TriggerWordLength = completionList.TriggerWordLength;
                }
                if (completionList == null || !CompletionWindowManager.ShowWindow(this, (char)0, completionList, CompletionWidget, CurrentCompletionContext))
                {
                    CurrentCompletionContext = null;
                }
            } catch (Exception) {
                metadata ["Result"] = "Failure";
                throw;
            } finally {
                Counters.ProcessCodeCompletion.EndTiming();
            }
        }
        public void ShowCompletion(ICompletionDataList completionList)
        {
            CurrentCompletionContext = CompletionWidget.CreateCodeCompletionContext(Editor.CaretOffset);
            int cpos, wlen;

            if (!GetCompletionCommandOffset(out cpos, out wlen))
            {
                cpos = Editor.CaretOffset;
                wlen = 0;
            }
            CurrentCompletionContext.TriggerOffset     = cpos;
            CurrentCompletionContext.TriggerWordLength = wlen;

            CompletionWindowManager.ShowWindow(this, '\0', completionList, CompletionWidget, CurrentCompletionContext);
        }
        public virtual async void RunParameterCompletionCommand()
        {
            if (Editor.SelectionMode == SelectionMode.Block || CompletionWidget == null)
            {
                return;
            }
            ParameterHintingResult cp = null;
            int cpos = Editor.CaretOffset;
            CodeCompletionContext ctx = CompletionWidget.CreateCodeCompletionContext(cpos);

            cp = await ParameterCompletionCommand(ctx);

            if (cp != null)
            {
                ParameterInformationWindowManager.ShowWindow(this, CompletionWidget, ctx, cp);
                ParameterInformationWindowManager.PostProcessKeyEvent(this, CompletionWidget, KeyDescriptor.FromGtk(Gdk.Key.F, 'f', Gdk.ModifierType.None));
            }
        }
Beispiel #10
0
        internal void OnUpdateShowCodeTemplatesWindow(CommandInfo info)
        {
            ICompletionDataList completionList = null;
            int cpos, wlen;

            if (!GetCompletionCommandOffset(out cpos, out wlen))
            {
                cpos = Editor.Caret.Offset;
                wlen = 0;
            }

            var ctx = CompletionWidget.CreateCodeCompletionContext(cpos);

            ctx.TriggerWordLength = wlen;
            completionList        = Document.Editor.IsSomethingSelected ? ShowCodeSurroundingsCommand(ctx) : ShowCodeTemplatesCommand(ctx);

            info.Bypass = completionList == null;
            info.Text   = Document.Editor.IsSomethingSelected ? GettextCatalog.GetString("_Surround With...") : GettextCatalog.GetString("I_nsert Template...");
        }
Beispiel #11
0
        public virtual void RunParameterCompletionCommand()
        {
            if (Document.Editor.SelectionMode == Mono.TextEditor.SelectionMode.Block || CompletionWidget == null)
            {
                return;
            }
            ParameterDataProvider cp = null;
            int cpos;

            if (!GetParameterCompletionCommandOffset(out cpos))
            {
                cpos = Editor.Caret.Offset;
            }
            CodeCompletionContext ctx = CompletionWidget.CreateCodeCompletionContext(cpos);

            cp = ParameterCompletionCommand(ctx);
            if (cp != null)
            {
                ParameterInformationWindowManager.ShowWindow(this, CompletionWidget, ctx, cp);
                ParameterInformationWindowManager.PostProcessKeyEvent(this, CompletionWidget, Gdk.Key.F, Gdk.ModifierType.None);
            }
        }
Beispiel #12
0
        internal void OnUpdateShowCodeTemplatesWindow(CommandInfo info)
        {
            ICompletionDataList completionList = null;
            int cpos, wlen;

            if (!GetCompletionCommandOffset(out cpos, out wlen))
            {
                cpos = Editor.CaretOffset;
                wlen = 0;
            }
            try {
                var ctx = CompletionWidget.CreateCodeCompletionContext(cpos);
                ctx.TriggerWordLength = wlen;
                completionList        = Editor.IsSomethingSelected ? ShowCodeSurroundingsCommand(ctx) : ShowCodeTemplatesCommand(ctx);

                info.Bypass = completionList == null;
                info.Text   = Editor.IsSomethingSelected ? GettextCatalog.GetString("_Surround With...") : GettextCatalog.GetString("I_nsert Template...");
            } catch (Exception e) {
                LoggingService.LogError("Error while update show code templates window", e);
                info.Bypass = true;
            }
        }
        internal void OnUpdateSelectionSurroundWith(CommandInfo info)
        {
            info.Enabled = Editor.IsSomethingSelected && !string.IsNullOrWhiteSpace(Editor.SelectedText);
            info.Bypass  = !IsActiveExtension() || !info.Enabled;
            if (info.Enabled)
            {
                if (!GetCompletionCommandOffset(out var cpos, out var wlen))
                {
                    cpos = Editor.CaretOffset;
                    wlen = 0;
                }
                try {
                    var ctx = CompletionWidget.CreateCodeCompletionContext(cpos);
                    ctx.TriggerWordLength = wlen;

                    info.Bypass = ShowCodeSurroundingsCommand(ctx) == null;
                } catch (Exception e) {
                    LoggingService.LogError("Error while update show code surroundings window", e);
                    info.Bypass = true;
                }
            }
        }
        internal void OnUpdateShowCodeTemplatesWindow(CommandInfo info)
        {
            info.Enabled = !Editor.IsSomethingSelected;
            info.Bypass  = !IsActiveExtension() || !info.Enabled;
            if (info.Enabled)
            {
                int cpos, wlen;
                if (!GetCompletionCommandOffset(out cpos, out wlen))
                {
                    cpos = Editor.CaretOffset;
                    wlen = 0;
                }
                try {
                    var ctx = CompletionWidget.CreateCodeCompletionContext(cpos);
                    ctx.TriggerWordLength = wlen;

                    info.Bypass = ShowCodeTemplatesCommand(ctx) == null;
                } catch (Exception e) {
                    LoggingService.LogError("Error while update show code templates window", e);
                    info.Bypass = true;
                }
            }
        }
        async void ImportSymbolCommand()
        {
            if (Editor.SelectionMode == SelectionMode.Block)
            {
                return;
            }
            var analysisDocument = DocumentContext.AnalysisDocument;

            if (analysisDocument == null)
            {
                return;
            }
            var offset = Editor.CaretOffset;

            int cpos, wlen;

            if (!GetCompletionCommandOffset(out cpos, out wlen))
            {
                cpos = Editor.CaretOffset;
                wlen = 0;
            }
            CurrentCompletionContext = CompletionWidget.CreateCodeCompletionContext(cpos);
            CurrentCompletionContext.TriggerWordLength = wlen;

            int  triggerWordLength = 0;
            char ch             = CurrentCompletionContext.TriggerOffset > 0 ? Editor.GetCharAt(CurrentCompletionContext.TriggerOffset - 1) : '\0';
            var  completionList = await InternalHandleCodeCompletion(CurrentCompletionContext, new CompletionTriggerInfo (CompletionTriggerReason.CompletionCommand, ch), triggerWordLength, default(CancellationToken), true);

            if (completionList != null)
            {
                CompletionWindowManager.ShowWindow(this, (char)0, completionList, CompletionWidget, CurrentCompletionContext);
            }
            else
            {
                CurrentCompletionContext = null;
            }
        }
Beispiel #16
0
        // When a key is pressed, and before the key is processed by the editor, this method will be invoked.
        // Return true if the key press should be processed by the editor.
        public override bool KeyPress(KeyDescriptor descriptor)
        {
            bool res;

            if (CurrentCompletionContext != null)
            {
                if (CompletionWindowManager.PreProcessKeyEvent(descriptor))
                {
                    CompletionWindowManager.PostProcessKeyEvent(descriptor);
                    autoHideCompletionWindow = true;
                    // in named parameter case leave the parameter window open.
                    autoHideParameterWindow = descriptor.KeyChar != ':';
                    if (!autoHideParameterWindow && ParameterInformationWindowManager.IsWindowVisible)
                    {
                        ParameterInformationWindowManager.PostProcessKeyEvent(this, CompletionWidget, descriptor);
                    }

                    return(false);
                }
                autoHideCompletionWindow = autoHideParameterWindow = false;
            }

            if (ParameterInformationWindowManager.IsWindowVisible)
            {
                if (ParameterInformationWindowManager.ProcessKeyEvent(this, CompletionWidget, descriptor))
                {
                    return(false);
                }
                autoHideCompletionWindow = autoHideParameterWindow = false;
            }

            //			int oldPos = Editor.CursorPosition;
            //			int oldLen = Editor.TextLength;
            char deleteOrBackspaceTriggerChar = '\0';

            if (descriptor.SpecialKey == SpecialKey.Delete && Editor.CaretOffset < Editor.Length)
            {
                deleteOrBackspaceTriggerChar = Editor.GetCharAt(Editor.CaretOffset);
            }
            if (descriptor.SpecialKey == SpecialKey.BackSpace && Editor.CaretOffset > 0)
            {
                deleteOrBackspaceTriggerChar = Editor.GetCharAt(Editor.CaretOffset - 1);
            }

            res = base.KeyPress(descriptor);

            CompletionWindowManager.PostProcessKeyEvent(descriptor);

            var ignoreMods = ModifierKeys.Control | ModifierKeys.Alt
                             | ModifierKeys.Command;

            // Handle parameter completion
            if (ParameterInformationWindowManager.IsWindowVisible)
            {
                ParameterInformationWindowManager.PostProcessKeyEvent(this, CompletionWidget, descriptor);
            }

            if ((descriptor.ModifierKeys & ignoreMods) != 0)
            {
                return(res);
            }

            // don't complete on block selection
            if (/*!EnableCodeCompletion ||*/ Editor.SelectionMode == MonoDevelop.Ide.Editor.SelectionMode.Block)
            {
                return(res);
            }
            // Handle code completion
            if (descriptor.KeyChar != '\0' && CompletionWidget != null && !CompletionWindowManager.IsVisible)
            {
                CurrentCompletionContext = CompletionWidget.CurrentCodeCompletionContext;
                completionTokenSrc.Cancel();
                completionTokenSrc = new CancellationTokenSource();
                var caretOffset = Editor.CaretOffset;
                var token       = completionTokenSrc.Token;
                try {
                    var task = HandleCodeCompletionAsync(CurrentCompletionContext, descriptor.KeyChar, token);
                    if (task != null)
                    {
                        // Show the completion window in two steps. The call to PrepareShowWindow creates the window but
                        // it doesn't show it. It is used only to process the keys while the completion data is being retrieved.
                        CompletionWindowManager.PrepareShowWindow(this, descriptor.KeyChar, CompletionWidget, CurrentCompletionContext);
                        EventHandler windowClosed = delegate(object o, EventArgs a) {
                            completionTokenSrc.Cancel();
                        };
                        CompletionWindowManager.WindowClosed += windowClosed;

                        task.ContinueWith(t => {
                            CompletionWindowManager.WindowClosed -= windowClosed;
                            if (token.IsCancellationRequested)
                            {
                                return;
                            }
                            var result = t.Result;
                            if (result != null)
                            {
                                int triggerWordLength = result.TriggerWordLength + (Editor.CaretOffset - caretOffset);

                                if (triggerWordLength > 0 && (triggerWordLength < Editor.CaretOffset ||
                                                              (triggerWordLength == 1 && Editor.CaretOffset == 1)))
                                {
                                    CurrentCompletionContext = CompletionWidget.CreateCodeCompletionContext(Editor.CaretOffset - triggerWordLength);
                                    CurrentCompletionContext.TriggerWordLength = triggerWordLength;
                                }
                                // Now show the window for real.
                                if (!CompletionWindowManager.ShowWindow(result, CurrentCompletionContext))
                                {
                                    CurrentCompletionContext = null;
                                }
                            }
                            else
                            {
                                CompletionWindowManager.HideWindow();
                                CurrentCompletionContext = null;
                            }
                        }, Runtime.MainTaskScheduler);
                    }
                    else
                    {
                        CurrentCompletionContext = null;
                    }
                } catch (TaskCanceledException) {
                    CurrentCompletionContext = null;
                } catch (AggregateException) {
                    CurrentCompletionContext = null;
                }
            }

            if ((descriptor.SpecialKey == SpecialKey.Delete || descriptor.SpecialKey == SpecialKey.BackSpace) && CompletionWidget != null && !CompletionWindowManager.IsVisible)
            {
                CurrentCompletionContext = CompletionWidget.CurrentCodeCompletionContext;

                int cpos, wlen;
                if (!GetCompletionCommandOffset(out cpos, out wlen))
                {
                    cpos = Editor.CaretOffset;
                    wlen = 0;
                }
                CurrentCompletionContext.TriggerOffset     = cpos;
                CurrentCompletionContext.TriggerWordLength = wlen;

                completionTokenSrc.Cancel();
                completionTokenSrc = new CancellationTokenSource();
                var caretOffset = Editor.CaretOffset;
                var token       = completionTokenSrc.Token;
                try {
                    var task = HandleBackspaceOrDeleteCodeCompletionAsync(CurrentCompletionContext, descriptor.SpecialKey, deleteOrBackspaceTriggerChar, token);
                    if (task != null)
                    {
                        // Show the completion window in two steps. The call to PrepareShowWindow creates the window but
                        // it doesn't show it. It is used only to process the keys while the completion data is being retrieved.
                        CompletionWindowManager.PrepareShowWindow(this, descriptor.KeyChar, CompletionWidget, CurrentCompletionContext);
                        EventHandler windowClosed = delegate(object o, EventArgs a) {
                            completionTokenSrc.Cancel();
                        };
                        CompletionWindowManager.WindowClosed += windowClosed;

                        task.ContinueWith(t => {
                            CompletionWindowManager.WindowClosed -= windowClosed;
                            if (token.IsCancellationRequested)
                            {
                                return;
                            }
                            var result = t.Result;
                            if (result != null)
                            {
                                int triggerWordLength = result.TriggerWordLength + (Editor.CaretOffset - caretOffset);

                                if (triggerWordLength > 0 && (triggerWordLength < Editor.CaretOffset ||
                                                              (triggerWordLength == 1 && Editor.CaretOffset == 1)))
                                {
                                    CurrentCompletionContext = CompletionWidget.CreateCodeCompletionContext(Editor.CaretOffset - triggerWordLength);
                                    CurrentCompletionContext.TriggerWordLength = triggerWordLength;
                                }
                                // Now show the window for real.
                                if (!CompletionWindowManager.ShowWindow(result, CurrentCompletionContext))
                                {
                                    CurrentCompletionContext = null;
                                }
                                else
                                {
                                    CompletionWindowManager.Wnd.StartOffset = CurrentCompletionContext.TriggerOffset;
                                    CompletionWindowManager.Wnd.EndOffset   = Editor.CaretOffset;
                                }
                            }
                            else
                            {
                                CompletionWindowManager.HideWindow();
                                CurrentCompletionContext = null;
                            }
                        }, Runtime.MainTaskScheduler);
                    }
                    else
                    {
                        CurrentCompletionContext = null;
                    }
                } catch (TaskCanceledException) {
                    CurrentCompletionContext = null;
                } catch (AggregateException) {
                    CurrentCompletionContext = null;
                }
            }

            if (CompletionWidget != null)
            {
                CodeCompletionContext ctx = CompletionWidget.CurrentCodeCompletionContext;
                parameterHintingSrc.Cancel();
                parameterHintingSrc = new CancellationTokenSource();
                var token = parameterHintingSrc.Token;
                try {
                    var task = HandleParameterCompletionAsync(ctx, descriptor.KeyChar, token);
                    if (task != null)
                    {
                        task.ContinueWith(t => {
                            if (!token.IsCancellationRequested && t.Result != null)
                            {
                                ParameterInformationWindowManager.ShowWindow(this, CompletionWidget, ctx, t.Result);
                            }
                        }, Runtime.MainTaskScheduler);
                    }
                } catch (TaskCanceledException) {
                } catch (AggregateException) {
                }
            }

            /*			autoHideCompletionWindow = true;
             *                      autoHideParameterWindow = keyChar != ':';*/
            return(res);
        }
        // When a key is pressed, and before the key is processed by the editor, this method will be invoked.
        // Return true if the key press should be processed by the editor.
        public override bool KeyPress(KeyDescriptor descriptor)
        {
            if (!IsActiveExtension())
            {
                return(base.KeyPress(descriptor));
            }
            bool res;

            if (CurrentCompletionContext != null)
            {
                if (CompletionWindowManager.PreProcessKeyEvent(descriptor))
                {
                    CompletionWindowManager.PostProcessKeyEvent(descriptor);
                    autoHideCompletionWindow = true;
                    // in named parameter case leave the parameter window open.
                    autoHideParameterWindow = descriptor.KeyChar != ':';
                    if (!autoHideParameterWindow && ParameterInformationWindowManager.IsWindowVisible)
                    {
                        ParameterInformationWindowManager.PostProcessKeyEvent(this, CompletionWidget, descriptor);
                    }

                    return(false);
                }
                autoHideCompletionWindow = autoHideParameterWindow = false;
            }

            if (ParameterInformationWindowManager.IsWindowVisible)
            {
                if (ParameterInformationWindowManager.ProcessKeyEvent(this, CompletionWidget, descriptor))
                {
                    return(false);
                }
                autoHideCompletionWindow = autoHideParameterWindow = false;
            }

            //			int oldPos = Editor.CursorPosition;
            //			int oldLen = Editor.TextLength;
            char deleteOrBackspaceTriggerChar = '\0';

            if (descriptor.SpecialKey == SpecialKey.Delete && Editor.CaretOffset < Editor.Length)
            {
                deleteOrBackspaceTriggerChar = Editor.GetCharAt(Editor.CaretOffset);
            }
            if (descriptor.SpecialKey == SpecialKey.BackSpace && Editor.CaretOffset > 0)
            {
                deleteOrBackspaceTriggerChar = Editor.GetCharAt(Editor.CaretOffset - 1);
            }

            res = base.KeyPress(descriptor);
            if (Editor.EditMode == EditMode.TextLink && Editor.TextLinkPurpose == TextLinkPurpose.Rename)
            {
                return(res);
            }
            if (descriptor.KeyChar == (char)16 || descriptor.KeyChar == (char)17)
            {
                return(res);
            }

            CompletionWindowManager.PostProcessKeyEvent(descriptor);

            var ignoreMods = ModifierKeys.Control | ModifierKeys.Alt
                             | ModifierKeys.Command;

            // Handle parameter completion
            if (ParameterInformationWindowManager.IsWindowVisible)
            {
                ParameterInformationWindowManager.PostProcessKeyEvent(this, CompletionWidget, descriptor);
            }

            if ((descriptor.ModifierKeys & ignoreMods) != 0)
            {
                return(res);
            }

            // don't complete on block selection
            if (/*!EnableCodeCompletion ||*/ Editor.SelectionMode == MonoDevelop.Ide.Editor.SelectionMode.Block)
            {
                return(res);
            }
            // Handle code completion
            if (descriptor.KeyChar != '\0' && CompletionWidget != null && !CompletionWindowManager.IsVisible)
            {
                completionTokenSrc.Cancel();
                CurrentCompletionContext = CompletionWidget.CurrentCodeCompletionContext;
                completionTokenSrc       = new CancellationTokenSource();
                var caretOffset = Editor.CaretOffset;
                var token       = completionTokenSrc.Token;
                try {
                    var task = HandleCodeCompletionAsync(CurrentCompletionContext, new CompletionTriggerInfo(CompletionTriggerReason.CharTyped, descriptor.KeyChar), token);
                    if (task != null)
                    {
                        // Show the completion window in two steps. The call to PrepareShowWindow creates the window but
                        // it doesn't show it. It is used only to process the keys while the completion data is being retrieved.
                        CompletionWindowManager.PrepareShowWindow(this, descriptor.KeyChar, CompletionWidget, CurrentCompletionContext);
                        EventHandler windowClosed = delegate(object o, EventArgs a) {
                            completionTokenSrc.Cancel();
                        };
                        CompletionWindowManager.WindowClosed += windowClosed;
                        task.ContinueWith(t => {
                            CompletionWindowManager.WindowClosed -= windowClosed;
                            if (token.IsCancellationRequested)
                            {
                                return;
                            }
                            var result = t.Result;
                            if (result != null)
                            {
                                int triggerWordLength = result.TriggerWordLength + (Editor.CaretOffset - caretOffset);
                                if (triggerWordLength > 0 && (triggerWordLength < Editor.CaretOffset ||
                                                              (triggerWordLength == 1 && Editor.CaretOffset == 1)))
                                {
                                    CurrentCompletionContext = CompletionWidget.CreateCodeCompletionContext(Editor.CaretOffset - triggerWordLength);
                                    if (result.TriggerWordStart >= 0)
                                    {
                                        CurrentCompletionContext.TriggerOffset = result.TriggerWordStart;
                                    }
                                    CurrentCompletionContext.TriggerWordLength = triggerWordLength;
                                }
                                // Now show the window for real.
                                if (!CompletionWindowManager.ShowWindow(result, CurrentCompletionContext))
                                {
                                    CurrentCompletionContext = null;
                                }
                            }
                            else
                            {
                                CompletionWindowManager.HideWindow();
                                CurrentCompletionContext = null;
                            }
                        }, Runtime.MainTaskScheduler);
                    }
                    else
                    {
                        CurrentCompletionContext = null;
                    }
                } catch (TaskCanceledException) {
                } catch (AggregateException) {
                }
            }

            if ((descriptor.SpecialKey == SpecialKey.Delete || descriptor.SpecialKey == SpecialKey.BackSpace) && CompletionWidget != null && !CompletionWindowManager.IsVisible)
            {
                if (!char.IsLetterOrDigit(deleteOrBackspaceTriggerChar) && deleteOrBackspaceTriggerChar != '_')
                {
                    return(res);
                }
                CurrentCompletionContext = CompletionWidget.CurrentCodeCompletionContext;

                int cpos, wlen;
                if (!GetCompletionCommandOffset(out cpos, out wlen))
                {
                    cpos = Editor.CaretOffset;
                    wlen = 0;
                }

                CurrentCompletionContext.TriggerOffset     = cpos;
                CurrentCompletionContext.TriggerWordLength = wlen;

                completionTokenSrc.Cancel();
                completionTokenSrc = new CancellationTokenSource();
                var caretOffset = Editor.CaretOffset;
                var token       = completionTokenSrc.Token;
                try {
                    var task = HandleCodeCompletionAsync(CurrentCompletionContext, new CompletionTriggerInfo(CompletionTriggerReason.BackspaceOrDeleteCommand, deleteOrBackspaceTriggerChar), token);
                    if (task != null)
                    {
                        // Show the completion window in two steps. The call to PrepareShowWindow creates the window but
                        // it doesn't show it. It is used only to process the keys while the completion data is being retrieved.
                        CompletionWindowManager.PrepareShowWindow(this, descriptor.KeyChar, CompletionWidget, CurrentCompletionContext);
                        EventHandler windowClosed = delegate(object o, EventArgs a) {
                            completionTokenSrc.Cancel();
                        };
                        CompletionWindowManager.WindowClosed += windowClosed;

                        task.ContinueWith(t => {
                            CompletionWindowManager.WindowClosed -= windowClosed;
                            if (token.IsCancellationRequested)
                            {
                                return;
                            }
                            var result = t.Result;
                            if (result != null)
                            {
                                int triggerWordLength = result.TriggerWordLength + (Editor.CaretOffset - caretOffset);

                                if (triggerWordLength > 0 && (triggerWordLength < Editor.CaretOffset ||
                                                              (triggerWordLength == 1 && Editor.CaretOffset == 1)))
                                {
                                    CurrentCompletionContext = CompletionWidget.CreateCodeCompletionContext(Editor.CaretOffset - triggerWordLength);
                                    if (result.TriggerWordStart >= 0)
                                    {
                                        CurrentCompletionContext.TriggerOffset = result.TriggerWordStart;
                                    }
                                    CurrentCompletionContext.TriggerWordLength = triggerWordLength;
                                }
                                // Now show the window for real.
                                if (!CompletionWindowManager.ShowWindow(result, CurrentCompletionContext))
                                {
                                    CurrentCompletionContext = null;
                                }
                                else
                                {
                                    CompletionWindowManager.Wnd.StartOffset = CurrentCompletionContext.TriggerOffset;
                                }
                            }
                            else
                            {
                                CompletionWindowManager.HideWindow();
                                CurrentCompletionContext = null;
                            }
                        }, Runtime.MainTaskScheduler);
                    }
                    else
                    {
                        CurrentCompletionContext = null;
                    }
                } catch (TaskCanceledException) {
                    CurrentCompletionContext = null;
                } catch (AggregateException) {
                    CurrentCompletionContext = null;
                }
            }

            if (CompletionWidget != null)
            {
                CodeCompletionContext ctx  = CompletionWidget.CurrentCodeCompletionContext;
                var newparameterHintingSrc = new CancellationTokenSource();
                var token = newparameterHintingSrc.Token;
                try {
                    var task = HandleParameterCompletionAsync(ctx, descriptor.KeyChar, token);
                    if (task != null)
                    {
                        parameterHintingSrc.Cancel();
                        parameterHintingSrc = newparameterHintingSrc;
                        parameterHingtingCursorPositionChanged = false;
                        task.ContinueWith(t => {
                            if (!token.IsCancellationRequested && t.Result != null)
                            {
                                ParameterInformationWindowManager.ShowWindow(this, CompletionWidget, ctx, t.Result);
                                if (parameterHingtingCursorPositionChanged)
                                {
                                    ParameterInformationWindowManager.UpdateCursorPosition(this, CompletionWidget);
                                }
                            }
                        }, token, TaskContinuationOptions.None, Runtime.MainTaskScheduler);
                    }
                    else
                    {
                        //Key was typed that was filtered out, no heavy processing will be performed(task==null)
                        //but we still want to update ParameterInfo window to avoid displaying it outside method call
                        parameterHingtingCursorPositionChanged = true;
                    }
                } catch (TaskCanceledException) {
                } catch (AggregateException) {
                }
            }
            return(res);
        }
Beispiel #18
0
        // When a key is pressed, and before the key is processed by the editor, this method will be invoked.
        // Return true if the key press should be processed by the editor.
        public override bool KeyPress(Gdk.Key key, char keyChar, Gdk.ModifierType modifier)
        {
            bool res;

            if (currentCompletionContext != null)
            {
                if (CompletionWindowManager.PreProcessKeyEvent(key, keyChar, modifier))
                {
                    CompletionWindowManager.PostProcessKeyEvent(key, keyChar, modifier);
                    autoHideCompletionWindow = true;
                    // in named parameter case leave the parameter window open.
                    autoHideParameterWindow = keyChar != ':';
                    if (!autoHideParameterWindow && ParameterInformationWindowManager.IsWindowVisible)
                    {
                        ParameterInformationWindowManager.PostProcessKeyEvent(this, CompletionWidget, key, modifier);
                    }

                    return(false);
                }
                autoHideCompletionWindow = autoHideParameterWindow = false;
            }

            if (ParameterInformationWindowManager.IsWindowVisible)
            {
                if (ParameterInformationWindowManager.ProcessKeyEvent(this, CompletionWidget, key, modifier))
                {
                    return(false);
                }
                autoHideCompletionWindow = autoHideParameterWindow = false;
            }

            //			int oldPos = Editor.CursorPosition;
            //			int oldLen = Editor.TextLength;
            res = base.KeyPress(key, keyChar, modifier);

            CompletionWindowManager.PostProcessKeyEvent(key, keyChar, modifier);

            var ignoreMods = Gdk.ModifierType.ControlMask | Gdk.ModifierType.MetaMask
                             | Gdk.ModifierType.Mod1Mask | Gdk.ModifierType.SuperMask;

            // Handle parameter completion
            if (ParameterInformationWindowManager.IsWindowVisible)
            {
                ParameterInformationWindowManager.PostProcessKeyEvent(this, CompletionWidget, key, modifier);
            }

            if ((modifier & ignoreMods) != 0)
            {
                return(res);
            }

            // don't complete on block selection
            if (/*!EnableCodeCompletion ||*/ Document.Editor.SelectionMode == Mono.TextEditor.SelectionMode.Block)
            {
                return(res);
            }

            // Handle code completion
            if (keyChar != '\0' && CompletionWidget != null && !CompletionWindowManager.IsVisible)
            {
                currentCompletionContext = CompletionWidget.CurrentCodeCompletionContext;
                int triggerWordLength = currentCompletionContext.TriggerWordLength;
                ICompletionDataList completionList = HandleCodeCompletion(currentCompletionContext, keyChar,
                                                                          ref triggerWordLength);
                if (triggerWordLength > 0 && (triggerWordLength < Editor.Caret.Offset ||
                                              (triggerWordLength == 1 && Editor.Caret.Offset == 1)))
                {
                    currentCompletionContext
                        = CompletionWidget.CreateCodeCompletionContext(Editor.Caret.Offset - triggerWordLength);
                    currentCompletionContext.TriggerWordLength = triggerWordLength;
                }
                if (completionList != null)
                {
                    if (!CompletionWindowManager.ShowWindow(this, keyChar, completionList, CompletionWidget, currentCompletionContext))
                    {
                        currentCompletionContext = null;
                    }
                }
                else
                {
                    currentCompletionContext = null;
                }
            }

            if (/*EnableParameterInsight &&*/ CompletionWidget != null)
            {
                CodeCompletionContext ctx = CompletionWidget.CurrentCodeCompletionContext;
                var paramProvider         = HandleParameterCompletion(ctx, keyChar);
                if (paramProvider != null)
                {
                    ParameterInformationWindowManager.ShowWindow(this, CompletionWidget, ctx, paramProvider);
                }
            }

/*			autoHideCompletionWindow = true;
 *                      autoHideParameterWindow = keyChar != ':';*/
            return(res);
        }