private void OnCodeEditorToolTipOpened(object sender, RoutedEventArgs e)
 {
     if (CurrentSymbolAtPointerPosition is null && CurrentDiagnosticsAtPointerPosition.Count() == 0)
     {
         CodeEditorToolTip.IsOpen = false;
     }
 }
 private void OnCodeEditorToolTipClosed(object sender, RoutedEventArgs e)
 {
     if (IsLoaded && (CurrentSymbolAtPointerPosition != null || CurrentDiagnosticsAtPointerPosition.Count() > 0))
     {
         CodeEditorToolTip.IsOpen = true;
     }
 }
        private async Task ShowDiagnostics(Point point)
        {
            ScrollViewer scrollViewer = CodeEditor.FindDescendant <ScrollViewer>();

            Point      position     = new Point(point.X + scrollViewer.HorizontalOffset - 12, point.Y + scrollViewer.VerticalOffset - 12);
            ITextRange pointerRange = CodeEditor.Document.GetRangeFromPoint(position, PointOptions.ClientCoordinates);
            TextSpan   caretSpan    = GetAdjustedTextSpan(TextSpan.FromBounds(pointerRange.StartPosition, pointerRange.EndPosition), ViewModel.CurrentText, ViewModel.NewLineMode, true);

            SyntaxNode?node = !char.IsWhiteSpace(pointerRange.Character) ? await ViewModel.GetSyntaxNodeAsync(caretSpan) : null;

            var diagnostics = await ViewModel.GetDiagnosticsAsync();

            diagnostics = diagnostics.Where(diagnostic =>
            {
                TextSpan span = GetAdjustedTextSpan(diagnostic.Location.SourceSpan, ViewModel.CurrentText, ViewModel.NewLineMode);
                ITextRange diagnosticRange = CodeEditor.Document.GetRange(span.Start, span.End);

                if (diagnosticRange.Length == 0)
                {
                    diagnosticRange.MoveStart(TextRangeUnit.Word, -1);
                }

                return(TextSpan.FromBounds(diagnosticRange.StartPosition, diagnosticRange.EndPosition).Contains(pointerRange.StartPosition));
            });

            if (node != CurrentSyntaxNodeAtPointerPosition || !CurrentDiagnosticsAtPointerPosition.SequenceEqual(diagnostics, DiagnosticsEqualityComparer.Default))
            {
                CurrentSyntaxNodeAtPointerPosition  = node;
                CurrentDiagnosticsAtPointerPosition = diagnostics;

                SemanticModel?semanticModel = null;
                ISymbol?      symbol        = null;

                if (node != null)
                {
                    semanticModel = await ViewModel.GetSemanticModelAsync();

                    if (semanticModel != null)
                    {
                        SymbolInfo symbolInfo = semanticModel.GetSymbolInfo(node);
                        symbol = symbolInfo.Symbol;
                    }
                }

                CurrentSymbolAtPointerPosition = symbol;

                bool hasDiagnostics = diagnostics.Count() > 0;

                if (symbol != null || hasDiagnostics)
                {
                    StringBuilder stringBuilder = new StringBuilder();

                    if (symbol != null)
                    {
                        stringBuilder.Append(symbol.ToDisplayString());
                    }

                    if (hasDiagnostics)
                    {
                        if (symbol != null)
                        {
                            stringBuilder.AppendLine();
                            stringBuilder.AppendLine();
                        }

                        stringBuilder.Append(diagnostics.First().GetMessage());

                        foreach (Diagnostic diagnostic in diagnostics.Skip(1))
                        {
                            stringBuilder.AppendLine();
                            stringBuilder.AppendLine();
                            stringBuilder.Append(diagnostic.GetMessage());
                        }
                    }

                    CodeEditorToolTip.Content = stringBuilder.ToString();

                    Point toolTipPosition = GetAdjustedPointFromDocument(pointerRange);
                    Rect  exclusionRect   = new Rect(toolTipPosition, new Size(12, 12));

                    CodeEditorToolTip.PlacementRect = exclusionRect;
                    CodeEditorToolTip.IsOpen        = true;
                }
                else
                {
                    CodeEditorToolTip.IsOpen = false;
                }
            }
        }