public void Show(ITextEditor editor, NSTextView text, string label, Item[] items, string stem, bool isInstance, bool isStatic)
        {
            var wind = (CompletionsWindow) window();
            NSPoint loc = editor.GetBoundingBox(text.selectedRange()).origin;
            wind.SetLoc(loc);

            m_label.Value.setStringValue(NSString.Create(label));
            m_table.Value.Open(editor, text, items, stem, m_label.Value, label);
            Log.WriteLine("AutoComplete", "took {0:0.000} secs to open the window", AutoComplete.Watch.ElapsedMilliseconds/1000.0);

            if (items.Length != 1)
                NSApplication.sharedApplication().beginSheet_modalForWindow_modalDelegate_didEndSelector_contextInfo(
                    wind, text.window(), null, null, IntPtr.Zero);
        }
        public void Populate(NSMenu menu, NSTextView view, Boss window)
        {
            // ITextContextCommands expect that the main window is the one the user
            // is working with.
            view.window().makeKeyAndOrderFront(view);

            // We don't extend the default menu because it has tons of stuff that we
            // don't really want. But we should add the services...
            //			NSMenu menu = SuperCall("menuForEvent:", evt).To<NSMenu>();
            //			menu.addItem(NSMenuItem.separatorItem());

            try
            {
                // Get the selection.
                m_view = view;
                m_range = view.selectedRange();
                m_selection = null;
                if (m_range.length > 0)
                    view.string_().getCharacters_range(m_range, out m_selection);

                // Get the language.
                string language = null;
                if (window.Has<ITextEditor>())
                {
                    var editor = window.Get<ITextEditor>();
                    language = editor.Language;
                }

                // Get the commands.
                var watch = new Stopwatch();
                watch.Start();
                m_entries.Clear();
                if (window != null)
                    DoGetEntries(view, m_selection, language, window);
                DoGetEntries(view, m_selection, language, m_boss);

                if (m_entries.Count == 0)
                {
                    if (window != null)
                        DoGetEntries(view, null, language, window);
                    DoGetEntries(view, null, language, m_boss);
                }
                Log.WriteLine("ContextMenu", "took {0:0.000} secs to open the menu", watch.ElapsedMilliseconds/1000.0);

                m_entries.Sort(this.DoCompareEntry);

                // Remove duplicate separators and any at the start or end.
                for (int i = m_entries.Count - 1; i > 0; --i)
                {
                    if (m_entries[i].Command.Name == null && m_entries[i - 1].Command.Name == null)
                        m_entries.RemoveAt(i);
                }
                while (m_entries.Count > 0 && m_entries[0].Command.Name == null)
                    m_entries.RemoveAt(0);
                while (m_entries.Count > 0 && m_entries[m_entries.Count - 1].Command.Name == null)
                    m_entries.RemoveAt(m_entries.Count - 1);

                // Build the menu.
                menu.removeAllItems();
                for (int i = 0; i < m_entries.Count; ++i)
                {
                    NSMenuItem item = null;

                    if (m_entries[i].Command.Name != null)
                    {
                        if (m_entries[i].Command.Name != Constants.Ellipsis)
                        {
                            item = NSMenuItem.Create(m_entries[i].Command.Name, "dispatchTextContextMenu:");

                            if (m_entries[i].Command.Title != null)
                                item.setAttributedTitle(m_entries[i].Command.Title);
                        }
                        else
                            item = NSMenuItem.Create(Constants.Ellipsis);
                        item.setTag(i);
                    }
                    else
                    {
                        Contract.Assert(m_entries[i].Command.Handler == null, "names is null, but handlers is not");
                        item = NSMenuItem.separatorItem();
                    }

                    if (item != null)
                        menu.addItem(item);
                }
            }
            catch (DatabaseLockedException)
            {
                NSString title = NSString.Create("Database was locked.");
                NSString message = NSString.Create("Try again.");
                Unused.Value = Functions.NSRunAlertPanel(title, message);
            }
            catch (Exception e)
            {
                Log.WriteLine(TraceLevel.Error, "App", "Error building context menu:");
                Log.WriteLine(TraceLevel.Error, "App", "{0}", e);

                NSString title = NSString.Create("Error building the menu.");
                NSString message = NSString.Create(e.Message);
                Unused.Value = Functions.NSRunAlertPanel(title, message);
            }
        }
        public void ExtendSelection(NSTextView view, NSEvent evt)
        {
            int index = DoMouseEventToIndex(view, evt);

            NSRange range = view.selectedRange();
            if (range.length == 0 && index < view.string_().length() && view.string_()[index] == '\n')
            {
                // don't extend the selection if the user clicked off to the right side of a line
            }
            else if (index >= view.string_().length())
            {
                // don't extend the selection if the user clicked below the last line of text
                view.setSelectedRange(NSRange.Empty);
            }
            else
            {
                // Extend the selection so that it contains the entire word the user right-clicked on.
                if (range.length == 0 || !range.Intersects(index))
                {
                    range = new NSRange(index, 1);
                    range = view.selectionRangeForProposedRange_granularity(range, Enums.NSSelectByWord);
                    view.setSelectedRange(range);
                }
            }
        }
Beispiel #4
0
        public bool HandleKey(NSTextView view, NSEvent evt)
        {
            bool handled = false;

            try
            {
                if (m_database != null)
                {
                    NSRange range = view.selectedRange();

                    NSString chars = evt.characters();
                    if (range.length == 0 && chars.length() == 1 && chars[0] == '.')
                    {
                        view.insertText(NSString.Create('.'));
                        Unused.Value = DoComplete(this.DoCompleteDot, view, range);
                        handled = true;
                    }
                    else if (range.length == 0 && chars.length() == 1 && chars[0] == '\t')
                    {
                        handled = DoCompleteTab(view, evt, range);
                    }

                    if (!handled)
                    {
                        var annotation = m_boss.Get<IArgsAnnotation>();
                        handled = annotation.HandleKey(view, evt);
                    }
                }
            }
            catch (Exception e)
            {
                Log.WriteLine(TraceLevel.Error, "Errors", "Autocomplete failed:");
                Log.WriteLine(TraceLevel.Error, "Errors", e.ToString());

                NSString title = NSString.Create("Auto-complete failed.");
                NSString message = NSString.Create(e.Message);
                Unused.Value = Functions.NSRunAlertPanel(title, message);
            }

            return handled;
        }