private void SetDialogPosition(IWpfTextView _view, QuickLookDialog dialog)
        {
            SnapshotSpan span = _view.Selection.SelectedSpans[0];

            var textViewOrigin = (_view as System.Windows.UIElement).PointToScreen(new Point(0, 0));

            var caretPos = _view.Selection.Start.Position;
            var charBounds = _view
                .GetTextViewLineContainingBufferPosition(caretPos)
                .GetCharacterBounds(caretPos);
            double textBottom = charBounds.Bottom;
            double textX = charBounds.Left;

            double newLeft = textViewOrigin.X + textX - _view.ViewportLeft;
            double newTop = textViewOrigin.Y + textBottom - _view.ViewportTop;

            dialog.Left = newLeft;
            dialog.Top = newTop;
        }
        /// <summary>
        /// This function is the callback used to execute a command when the a menu item is clicked.
        /// See the Initialize method to see how the menu item is associated to this function using
        /// the OleMenuCommandService service and the MenuCommand class.
        /// </summary>
        private void GenerateMenu_Clicked(object sender, EventArgs e)
        {
            var props = Properties.Settings.Default;
            IWpfTextView view = GetActiveTextView();
            
            ITextSelection selection = view.Selection;
            if (selection != null)
            {
                // Get extension of active document.
                String fileType = "_default_";
                var textDoc = GetTextDocument(view.TextBuffer);
                if (textDoc != null)
                {
                    fileType = Path.GetExtension(textDoc.FilePath).ToLower();
                }

                LetterCase letterCase = LetterCase.ValueOf(props.LetterCaseConvention[fileType],
                    LetterCase.Enumulate()[0]);

                // Get selection in view.
                SnapshotSpan span = view.Selection.SelectedSpans[0];
                var selectedText = span.GetText();
                //if (string.IsNullOrEmpty(selectedText))
                //{
                //ITextSnapshotLine line = span.Start.GetContainingLine();
                //selectedText = span.Start.GetContainingLine().GetText();
                //view.Selection.Select(new SnapshotSpan(line.Start, line.End), false);
                //}

                ITrackingSpan trackingSpan = span.Snapshot.CreateTrackingSpan(span, SpanTrackingMode.EdgeExclusive);
                QuickLookDialog dialog = new QuickLookDialog();
                SetDialogPosition(view, dialog);
                dialog.SetText(selectedText);
                dialog.Selected += (sender2, args) => {
                    _dispatcher.BeginInvoke(new Action(() => {
                        var buffer = trackingSpan.TextBuffer;
                        Span sp = trackingSpan.GetSpan(buffer.CurrentSnapshot);
                        buffer.Replace(sp, (string)args.Selection);
                    }));
                    
                };
                dialog.LetterCaseChanged += (sender2, args) => {
                    if (props.LetterCaseConvention.ContainsKey(fileType))
                        props.LetterCaseConvention.Remove(fileType);
                    if (args.Selection != null)
                    {
                        props.LetterCaseConvention.Add(fileType, args.Selection.Id);
                        props.Save();
                    }
                };
                dialog.ShowModeless(letterCase);
            }
        }