Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GPDL/HeuristicLab.Problems.GPDL.Views/3.4/ColorizeErrors.cs @ 9674

Last change on this file since 9674 was 9674, checked in by gkronber, 11 years ago

#2026: Worked on integration of ICSharpCode.AvalonEdit control for GPDL syntax-highlighting and error reporting.

File size: 4.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows;
6using System.Windows.Media;
7using System.Windows.Shapes;
8using ICSharpCode.AvalonEdit.Document;
9using ICSharpCode.AvalonEdit.Rendering;
10
11namespace HeuristicLab.Problems.GPDL.Views {
12  public class ColorizeErrors : DocumentColorizingTransformer {
13    private Dictionary<int, string> synErrors = new Dictionary<int, string>();
14    private Dictionary<int, string> lexErrors = new Dictionary<int, string>();
15
16    protected override void ColorizeLine(DocumentLine line) {
17      if (lexErrors.ContainsKey(line.LineNumber)) {
18        ChangeLinePart(line.Offset, line.Offset + line.Length, ChangeLexError);
19      } else if (synErrors.ContainsKey(line.LineNumber)) {
20        ChangeLinePart(line.Offset, line.Offset + line.Length, ChangeSynError);
21      }
22      //int lineStartOffset = line.Offset;
23      //string text = CurrentContext.Document.GetText(line);
24      //int start = 0;
25      //int index;
26      //while ((index = text.IndexOf("AvalonEdit", start)) >= 0) {
27      //  base.ChangeLinePart(
28      //    lineStartOffset + index, // startOffset
29      //    lineStartOffset + index + 10, // endOffset
30      //    (VisualLineElement element) => {
31      //      // This lambda gets called once for every VisualLineElement
32      //      // between the specified offsets.
33      //      Typeface tf = element.TextRunProperties.Typeface;
34      //      // Replace the typeface with a modified version of
35      //      // the same typeface
36      //      element.TextRunProperties.SetTypeface(new Typeface(
37      //        tf.FontFamily,
38      //        FontStyles.Italic,
39      //        FontWeights.Bold,
40      //        tf.Stretch
41      //      ));
42      //    });
43      //  start = index + 1; // search for next occurrence
44      //}
45    }
46
47    private void ChangeSynError(VisualLineElement element) {
48
49
50      var wigglePath = new Path();
51      wigglePath.Data = Geometry.Parse("M 0,1 C 1,0 2,2 3,1");
52      wigglePath.Stroke = Brushes.Red;
53      wigglePath.StrokeThickness = 0.2;
54      wigglePath.StrokeEndLineCap = PenLineCap.Square;
55      wigglePath.StrokeStartLineCap = PenLineCap.Square;
56
57      var wigglyBrush = new VisualBrush();
58      wigglyBrush.Viewbox = new Rect(0, 0, 3, 2);
59      wigglyBrush.ViewboxUnits = BrushMappingMode.Absolute;
60      wigglyBrush.Viewport = new Rect(0, 0.8, 6, 4);
61      wigglyBrush.ViewportUnits = BrushMappingMode.Absolute;
62      wigglyBrush.TileMode = TileMode.Tile;
63      wigglyBrush.Visual = wigglePath;
64
65
66      var wigglyPen = new Pen(wigglyBrush, 6.0);
67      var decorations = new TextDecorationCollection();
68      decorations.Add(new TextDecoration(0, wigglyPen, 0, TextDecorationUnit.Pixel, TextDecorationUnit.Pixel));
69      element.TextRunProperties.SetTextDecorations(decorations);
70      //element.TextRunProperties.SetTypeface(new Typeface(
71      //  tf.FontFamily,
72      //  FontStyles.Italic,
73      //  FontWeights.Bold,
74      //  tf.Stretch
75      //));
76    }
77
78    private void ChangeLexError(VisualLineElement element) {
79
80      var wigglePath = new Path();
81      wigglePath.Data = Geometry.Parse("M 0,1 C 1,0 2,2 3,1");
82      wigglePath.Stroke = Brushes.Red;
83      wigglePath.StrokeThickness = 0.2;
84      wigglePath.StrokeEndLineCap = PenLineCap.Square;
85      wigglePath.StrokeStartLineCap = PenLineCap.Square;
86
87      var wigglyBrush = new VisualBrush();
88      wigglyBrush.Viewbox = new Rect(0, 0, 3, 2);
89      wigglyBrush.ViewboxUnits = BrushMappingMode.Absolute;
90      wigglyBrush.Viewport = new Rect(0, 0.8, 6, 4);
91      wigglyBrush.ViewportUnits = BrushMappingMode.Absolute;
92      wigglyBrush.TileMode = TileMode.Tile;
93      wigglyBrush.Visual = wigglePath;
94
95
96      var wigglyPen = new Pen(wigglyBrush, 6.0);
97      var decorations = new TextDecorationCollection();
98      decorations.Add(new TextDecoration(0, wigglyPen, 0, TextDecorationUnit.Pixel, TextDecorationUnit.Pixel));
99      element.TextRunProperties.SetTextDecorations(decorations);
100
101      //element.TextRunProperties.SetTypeface(new Typeface(
102      //  tf.FontFamily,
103      //  FontStyles.Italic,
104      //  FontWeights.Bold,
105      //  tf.Stretch
106      //));
107    }
108
109    internal void MarkSynError(string msg, int line, int col) {
110      if (!synErrors.ContainsKey(line)) {
111        synErrors.Add(line, msg);
112      }
113    }
114
115    internal void MarkLexError(string msg, int line, int col) {
116      if (!lexErrors.ContainsKey(line)) {
117        lexErrors.Add(line, msg);
118      }
119    }
120
121    internal void ClearErrors() {
122      synErrors.Clear();
123      lexErrors.Clear();
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.