Exemplo n.º 1
0
        // Spell Check

        private void SpellCheckSuggestions(ContextMenu contextMenu)
        {
            if (SpellCheckProvider != null)
            {
                int offset;
                if (_appsKeyDown || IsAlternateAppsKeyShortcut)
                {
                    _appsKeyDown = false;
                    offset       = EditBox.SelectionStart;
                }
                else
                {
                    var editorPosition = EditBox.GetPositionFromPoint(Mouse.GetPosition(EditBox));
                    if (!editorPosition.HasValue)
                    {
                        return;
                    }
                    offset = EditBox.Document.GetOffset(editorPosition.Value.Line, editorPosition.Value.Column);
                }

                var errorSegments     = SpellCheckProvider.GetSpellCheckErrors();
                var misspelledSegment = errorSegments.FirstOrDefault(segment => segment.StartOffset <= offset && segment.EndOffset >= offset);
                if (misspelledSegment == null)
                {
                    return;
                }

                // check if the clicked offset is the beginning or end of line to prevent snapping to it
                // (like in text selection) with GetPositionFromPoint
                // in practice makes context menu not show when clicking on the first character of a line
                var currentLine = EditBox.Document.GetLineByOffset(offset);
                if (offset == currentLine.Offset || offset == currentLine.EndOffset)
                {
                    return;
                }

                var misspelledText = EditBox.Document.GetText(misspelledSegment);
                var suggestions    = SpellCheckProvider.GetSpellCheckSuggestions(misspelledText);
                foreach (var item in suggestions)
                {
                    contextMenu.Items.Add(SpellSuggestMenuItem(item, misspelledSegment));
                }
                contextMenu.Items.Add(new MenuItem
                {
                    Header           = "Add to Dictionary",
                    Command          = EditingCommands.IgnoreSpellingError,
                    CommandParameter = misspelledText
                });
                contextMenu.Items.Add(new Separator());
            }
        }
Exemplo n.º 2
0
        void EditorContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            // don't show menu if we bail out with return
            e.Handled = true;

            if (SpellCheckProvider == null)
            {
                return;
            }

            // Bail out if the mouse isn't over the markdownEditor
            var editorPosition = Editor.GetPositionFromPoint(Mouse.GetPosition(Editor));

            if (!editorPosition.HasValue)
            {
                return;
            }

            var offset            = Editor.Document.GetOffset(editorPosition.Value.Line, editorPosition.Value.Column);
            var errorSegments     = SpellCheckProvider.GetSpellCheckErrors();
            var misspelledSegment = errorSegments.FirstOrDefault(segment => segment.StartOffset <= offset && segment.EndOffset >= offset);

            if (misspelledSegment == null)
            {
                return;
            }

            // check if the clicked offset is the beginning or end of line to prevent snapping to it (like in text selection) with GetPositionFromPoint
            // in practice makes context menu not show when clicking on the first character of a line
            var currentLine = Document.GetLineByOffset(offset);

            if (offset == currentLine.Offset || offset == currentLine.EndOffset)
            {
                return;
            }

            var misspelledWord   = editor.Document.GetText(misspelledSegment);
            var contextMenuItems = new List <object>(SpellCheckProvider.GetSpellcheckSuggestions(misspelledWord));

            contextMenuItems.Add(new Separator());
            contextMenuItems.Add(new DelegateCommand("Add to dictionary", obj =>
            {
                SpellCheckProvider.AddWordToCustomDictionary(misspelledWord);
                Editor.TextArea.TextView.Redraw();
            }));

            EditorContextMenu.ItemsSource = contextMenuItems;
            EditorContextMenu.Tag         = misspelledSegment;
            e.Handled = false;
        }