public void Draw(TextView textView, DrawingContext drawingContext) { if (SynchronizedSegments != null) { if (MasterSegment.HasValue) { DrawRectangle(textView, drawingContext, MasterSegment.Value, Brushes.Transparent, MasterEdgePen); } foreach (var segment in SynchronizedSegments) { DrawRectangle(textView, drawingContext, segment, Brushes.Transparent, SynchronizedEdgePen); } } if (_activeSnippet != null && _activeSnippet.ActiveAnchorsValid) { DrawRectangle(textView, drawingContext, SourcePosition.Create(_activeSnippet.ActiveAnchors.Item1.Offset, _activeSnippet.ActiveAnchors.Item2.Offset), Brushes.Transparent, MasterEdgePen); foreach (var anchorGroup in _activeSnippet.FollowingAnchorGroups) { foreach (var anchors in anchorGroup) { if (anchors.Item1.IsDeleted || anchors.Item2.IsDeleted) { continue; } DrawRectangle(textView, drawingContext, SourcePosition.Create(anchors.Item1.Offset, anchors.Item2.Offset), Brushes.Transparent, SynchronizedEdgePen); } } } foreach (var highlightSegmentGroup in _highlightSegments) { foreach (var highlightSegment in highlightSegmentGroup) { var brush = highlightSegment.Segment.DisplayOptions == DisplayOptions.Definition ? HighlightDefinitionBrush : HighlightUsageBrush; if (highlightSegment.HighlightStartAnchor.IsDeleted || highlightSegment.HighlightEndAnchor.IsDeleted) { continue; } var indexStart = highlightSegment.HighlightStartAnchor.Offset; var indexEnd = highlightSegment.HighlightEndAnchor.Offset; if (indexEnd > indexStart) { DrawRectangle(textView, drawingContext, SourcePosition.Create(indexStart, indexEnd), brush, NullPen); } } } }
protected override SourcePosition BuildSourcePosition() { var indexStart = -1; var indexEnd = -1; if (Type == NodeType.Terminal) { indexStart = Token.Index; indexEnd = Token.Index + Token.Value.Length - 1; } else if (LastTerminalNode != null) { indexStart = FirstTerminalNode.Token.Index; var lastTerminal = LastTerminalNode.Token; indexEnd = lastTerminal.Index + lastTerminal.Value.Length - 1; } return(SourcePosition.Create(indexStart, indexEnd)); }
protected override void ColorizeLine(DocumentLine line) { if (_statements == null) { return; } if (_lineTerminals.TryGetValue(line, out ICollection <StatementGrammarNode> lineTerminals)) { foreach (var terminal in lineTerminals) { SolidColorBrush brush = null; if (_unrecognizedTerminals.Contains(terminal)) { brush = ErrorBrush; } else if (_redundantTerminals.Contains(terminal)) { brush = RedundantBrush; } else if (terminal.IsReservedWord) { brush = KeywordBrush; } else if (_parser.IsLiteral(terminal.Id)) { brush = LiteralBrush; } else if (_parser.IsAlias(terminal.Id)) { brush = AliasBrush; } else if (_recognizedProgramTerminals.Contains(terminal)) { brush = ProgramBrush; } if (brush == null) { continue; } ProcessSegmentAtLine(line, terminal.SourcePosition, element => element.TextRunProperties.SetForegroundBrush(brush)); } } ProcessNodeCollectionAtLine(line, _lineNodesWithSemanticErrorsOrInvalidGrammar, element => element.TextRunProperties.SetTextDecorations(Resources.WaveErrorUnderline)); ProcessNodeCollectionAtLine(line, _lineNodesWithSuggestion, element => element.TextRunProperties.SetTextDecorations(Resources.WaveWarningUnderline)); ProcessNodeCollectionAtLine(line, _lineComments, element => { element.TextRunProperties.SetForegroundBrush(CommentBrush); element.BackgroundBrush = null; }); var statementsAtLine = _statements.Where(s => s.SourcePosition.IndexStart <= line.EndOffset && s.SourcePosition.IndexEnd >= line.Offset); var colorizeBackground = ColorizeBackground; foreach (var statement in statementsAtLine) { if (statement.FirstUnparsedToken != null) { ProcessSegmentAtLine(line, SourcePosition.Create(statement.FirstUnparsedToken.Index, statement.FirstUnparsedToken.Index + statement.FirstUnparsedToken.Value.Length - 1), element => element.TextRunProperties.SetTextDecorations(Resources.WaveErrorUnderline)); } Brush backgroundBrush; if (statement == ActiveStatement) { backgroundBrush = statement.ParseStatus == ParseStatus.Success ? ValidActiveStatementBackgroundBrush : InvalidActiveStatementBackgroundBrush; } else { backgroundBrush = statement.ParseStatus == ParseStatus.Success ? ValidStatementBackgroundBrush : InvalidStatementBackgroundBrush; } var colorStartOffset = Math.Max(line.Offset, statement.SourcePosition.IndexStart); var colorEndOffset = Math.Min(line.EndOffset, statement.SourcePosition.IndexEnd + 1); SetCorrespondingTerminalsBrush(line); if (colorizeBackground) { ChangeLinePart( colorStartOffset, colorEndOffset, element => { element.BackgroundBrush = backgroundBrush; /* * // This lambda gets called once for every VisualLineElement * // between the specified offsets. * var tf = element.TextRunProperties.Typeface; * // Replace the typeface with a modified version of * // the same typeface * element.TextRunProperties.SetTypeface(new Typeface( * tf.FontFamily, * FontStyles.Italic, * FontWeights.Bold, * tf.Stretch * ));*/ }); } } SetCorrespondingTerminalsBrush(line); }
protected override SourcePosition BuildSourcePosition() { return(SourcePosition.Create(Token.Index, Token.Index + Token.Value.Length - 1)); }