1: #region Syntax Highlighting
2: //================================================
3: private string AddOutput(string output)
4: { 5: if (output != "")
6: { 7: AddRun(output, Colors.Black);
8: output = "";
9: }
10:
11: return output;
12: }
13:
14: //================================================
15: private void AddRun(string txt, Color color)
16: { 17: Run run = new Run();
18: run.Text = txt;
19: run.Foreground = new SolidColorBrush(color);
20: txtOutput.Inlines.Add(run);
21: }
22:
23: //================================================
24: private void ApplyHighlighting(string txt)
25: { 26: string output = "";
27: bool commentMode = false;
28:
29: // Clean line spacing
30: txt = txt.Replace("\t", " "); 31: txt = txt.Replace(" ", " "); 32:
33: // Clear UI Elements
34: txtOutput.Inlines.Clear();
35: txtOutput.Text = "";
36:
37: // Parse file contents into lines
38: List<string> lines = txt.Split('\n').ToList(); 39:
40: foreach (string line in lines)
41: { 42: // Blank line (append to current output)
43: if (line.Trim() == "")
44: { 45: output = String.Format("{0}\n", output); 46: continue;
47: }
48:
49: // Check for comments if we are not in a comment
50: if (!commentMode)
51: { 52: // Visual Studio Region coloring
53: if (line.Contains("#region") line.Contains("#endregion")) 54: { 55: int index = line.IndexOf("#"); 56:
57: // Add words before the comment
58: output = String.Format("{0}{1}", output, line.Substring(0, index)); 59: output = AddOutput(output);
60:
61: // Add the comment
62: AddRun(String.Format("{0}\n", line.Substring(index)), Colors.Blue); 63: continue;
64: }
65:
66: // Single line comments (// and # symbols)
67: if ((line.Contains("//") && !line.Contains("http://")) (line.Contains("#") && !line.Contains("\"#") && !line.Contains("#\""))) 68: { 69: int index = 0;
70:
71: if (line.Contains("//")) 72: index = line.IndexOf("//"); 73: else
74: index = line.IndexOf("#"); 75:
76: // Add words before the comment
77: output = String.Format("{0}{1}", output, line.Substring(0, index)); 78: output = AddOutput(output);
79:
80: // Add the comment
81: AddRun(String.Format("{0}\n", line.Substring(index)), Colors.Green); 82: continue;
83: }
84:
85: // Apply string highlighting
86: if (line.Contains("\"")) 87: { 88: output = ApplyStringHighlighting(output, line);
89: continue;
90: }
91: }
92:
93: // Comments Highlighting
94: if (line.Contains("/*")) 95: { 96: if (line.Contains("*/")) 97: { 98: try
99: { 100: int index = line.IndexOf("/*"); 101:
102: if (index > 0)
103: output = String.Format("{0}{1}", output, line.Substring(0, index)); 104: else
105: index = 0;
106:
107: // Add words before the comment
108: output = AddOutput(output);
109:
110: // Add the comment
111: int commentIndex = line.IndexOf("*/"); 112: AddRun(line.Substring(index, commentIndex + 2 - index), Colors.Green);
113:
114: // Add the words after the comment
115: output = AddOutput(String.Format("{0}\n", line.Substring(commentIndex + 2))); 116: }
117: catch { } 118:
119: continue;
120: }
121: else
122: { 123: // Append to the output data (not the UI)
124: output = AddOutput(output);
125: commentMode = true;
126: }
127: }
128:
129: // Append string
130: output = String.Format("{0}{1}\n", output, line); 131:
132: // Close comments (if applicable)
133: if (line.Contains("*/") && commentMode) 134: { 135: AddRun(output, Colors.Green);
136: output = "";
137: commentMode = false;
138: }
139: }
140:
141: // Add remainder output
142: output = AddOutput(output);
143:
144: // Update scroll viewer to its top position
145: scrollViewer.ScrollToVerticalOffset(0);
146: }
147:
148: //================================================
149: private string ApplyStringHighlighting(string output, string line)
150: { 151: string curLine = line;
152: bool openString = false;
153: int index = 0;
154:
155: output = AddOutput(output);
156:
157: while (curLine.Length > 1 && curLine.Substring(1).IndexOf('\"') != -1) 158: { 159: index = curLine.IndexOf('\"'); 160:
161: if (openString)
162: { 163: AddRun(String.Format("\"{0}", curLine.Substring(0, index + 1)), Colors.Red); 164: openString = false;
165: }
166: else
167: { 168: output = AddOutput(curLine.Substring(0, index));
169: openString = true;
170: }
171:
172: curLine = curLine.Substring(index + 1);
173: }
174:
175: return AddOutput(String.Format("{0}\n", curLine)); 176: }
177: #endregion Syntax Highlighting