Skip to content

Commit 339db97

Browse files
Merge pull request #170 from icsharpcode/feature/fixes
Add more font-styling options to highlighting, fix current line highlighting, update .editorconfig
2 parents 3320ce1 + 4db42e1 commit 339db97

14 files changed

Lines changed: 276 additions & 10 deletions

ICSharpCode.AvalonEdit/CodeCompletion/CompletionList.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3-
xmlns:cc="clr-namespace:ICSharpCode.AvalonEdit.CodeCompletion">
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:cc="clr-namespace:ICSharpCode.AvalonEdit.CodeCompletion">
44
<Style TargetType="{x:Type ListBoxItem}" x:Key="CompletionListBoxItem">
55
<Setter Property="Template">
66
<Setter.Value>

ICSharpCode.AvalonEdit/Highlighting/HighlightingColor.cs

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
using System;
2020
using System.Globalization;
21+
using System.Linq;
2122
using System.Runtime.Serialization;
2223
using System.Text;
2324
using System.Windows;
@@ -36,9 +37,12 @@ public class HighlightingColor : ISerializable, IFreezable, ICloneable, IEquatab
3637
internal static readonly HighlightingColor Empty = FreezableHelper.FreezeAndReturn(new HighlightingColor());
3738

3839
string name;
40+
FontFamily fontFamily = null;
41+
int? fontSize;
3942
FontWeight? fontWeight;
4043
FontStyle? fontStyle;
4144
bool? underline;
45+
bool? strikethrough;
4246
HighlightingBrush foreground;
4347
HighlightingBrush background;
4448
bool frozen;
@@ -57,6 +61,34 @@ public string Name {
5761
}
5862
}
5963

64+
/// <summary>
65+
/// Gets/sets the font family. Null if the highlighting color does not change the font style.
66+
/// </summary>
67+
public FontFamily FontFamily {
68+
get {
69+
return fontFamily;
70+
}
71+
set {
72+
if (frozen)
73+
throw new InvalidOperationException();
74+
fontFamily = value;
75+
}
76+
}
77+
78+
/// <summary>
79+
/// Gets/sets the font size. Null if the highlighting color does not change the font style.
80+
/// </summary>
81+
public int? FontSize {
82+
get {
83+
return fontSize;
84+
}
85+
set {
86+
if (frozen)
87+
throw new InvalidOperationException();
88+
fontSize = value;
89+
}
90+
}
91+
6092
/// <summary>
6193
/// Gets/sets the font weight. Null if the highlighting color does not change the font weight.
6294
/// </summary>
@@ -99,6 +131,20 @@ public bool? Underline {
99131
}
100132
}
101133

134+
/// <summary>
135+
/// Gets/sets the strikethrough flag. Null if the strikethrough status does not change the font style.
136+
/// </summary>
137+
public bool? Strikethrough {
138+
get {
139+
return strikethrough;
140+
}
141+
set {
142+
if (frozen)
143+
throw new InvalidOperationException();
144+
strikethrough = value;
145+
}
146+
}
147+
102148
/// <summary>
103149
/// Gets/sets the foreground color applied by the highlighting.
104150
/// </summary>
@@ -148,8 +194,14 @@ protected HighlightingColor(SerializationInfo info, StreamingContext context)
148194
this.FontStyle = (FontStyle?)new FontStyleConverter().ConvertFromInvariantString(info.GetString("Style"));
149195
if (info.GetBoolean("HasUnderline"))
150196
this.Underline = info.GetBoolean("Underline");
197+
if (info.GetBoolean("HasStrikethrough"))
198+
this.Strikethrough = info.GetBoolean("Strikethrough");
151199
this.Foreground = (HighlightingBrush)info.GetValue("Foreground", typeof(HighlightingBrush));
152200
this.Background = (HighlightingBrush)info.GetValue("Background", typeof(HighlightingBrush));
201+
if (info.GetBoolean("HasFamily"))
202+
this.FontFamily = new FontFamily(info.GetString("Family"));
203+
if (info.GetBoolean("HasSize"))
204+
this.FontSize = info.GetInt32("Size");
153205
}
154206

155207
/// <summary>
@@ -170,8 +222,16 @@ public virtual void GetObjectData(SerializationInfo info, StreamingContext conte
170222
info.AddValue("HasUnderline", this.Underline.HasValue);
171223
if (this.Underline.HasValue)
172224
info.AddValue("Underline", this.Underline.Value);
225+
if (this.Strikethrough.HasValue)
226+
info.AddValue("Strikethrough", this.Strikethrough.Value);
173227
info.AddValue("Foreground", this.Foreground);
174228
info.AddValue("Background", this.Background);
229+
info.AddValue("HasFamily", this.FontFamily != null);
230+
if (this.FontFamily != null)
231+
info.AddValue("Family", this.FontFamily.FamilyNames.FirstOrDefault());
232+
info.AddValue("HasSize", this.FontSize.HasValue);
233+
if (this.FontSize.HasValue)
234+
info.AddValue("Size", this.FontSize.Value.ToString());
175235
}
176236

177237
/// <summary>
@@ -202,6 +262,14 @@ public virtual string ToCss()
202262
b.Append(Underline.Value ? "underline" : "none");
203263
b.Append("; ");
204264
}
265+
if (Strikethrough != null) {
266+
if (Underline == null)
267+
b.Append("text-decoration: ");
268+
269+
b.Remove(b.Length - 1, 1);
270+
b.Append(Strikethrough.Value ? " line-through" : " none");
271+
b.Append("; ");
272+
}
205273
return b.ToString();
206274
}
207275

@@ -254,8 +322,9 @@ public virtual bool Equals(HighlightingColor other)
254322
if (other == null)
255323
return false;
256324
return this.name == other.name && this.fontWeight == other.fontWeight
257-
&& this.fontStyle == other.fontStyle && this.underline == other.underline
258-
&& object.Equals(this.foreground, other.foreground) && object.Equals(this.background, other.background);
325+
&& this.fontStyle == other.fontStyle && this.underline == other.underline && this.strikethrough == other.strikethrough
326+
&& object.Equals(this.foreground, other.foreground) && object.Equals(this.background, other.background)
327+
&& object.Equals(this.fontFamily, other.fontFamily) && object.Equals(this.FontSize, other.FontSize);
259328
}
260329

261330
/// <inheritdoc/>
@@ -271,6 +340,10 @@ public override int GetHashCode()
271340
hashCode += 1000000033 * foreground.GetHashCode();
272341
if (background != null)
273342
hashCode += 1000000087 * background.GetHashCode();
343+
if (fontFamily != null)
344+
hashCode += 1000000123 * fontFamily.GetHashCode();
345+
if (fontSize != null)
346+
hashCode += 1000000167 * fontSize.GetHashCode();
274347
}
275348
return hashCode;
276349
}
@@ -292,12 +365,19 @@ public void MergeWith(HighlightingColor color)
292365
this.background = color.background;
293366
if (color.underline != null)
294367
this.underline = color.underline;
368+
if (color.strikethrough != null)
369+
this.strikethrough = color.strikethrough;
370+
if (color.fontFamily != null)
371+
this.fontFamily = color.fontFamily;
372+
if (color.fontSize != null)
373+
this.fontSize = color.fontSize;
295374
}
296375

297376
internal bool IsEmptyForMerge {
298377
get {
299378
return fontWeight == null && fontStyle == null && underline == null
300-
&& foreground == null && background == null;
379+
&& strikethrough == null && foreground == null && background == null
380+
&& fontFamily == null && fontSize == null;
301381
}
302382
}
303383
}

ICSharpCode.AvalonEdit/Highlighting/HighlightingColorizer.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ internal static bool IsEmptyColor(HighlightingColor color)
234234
return true;
235235
return color.Background == null && color.Foreground == null
236236
&& color.FontStyle == null && color.FontWeight == null
237-
&& color.Underline == null;
237+
&& color.Underline == null && color.Strikethrough == null;
238238
}
239239

240240
/// <summary>
@@ -257,17 +257,21 @@ internal static void ApplyColorToElement(VisualLineElement element, Highlighting
257257
if (b != null)
258258
element.BackgroundBrush = b;
259259
}
260-
if (color.FontStyle != null || color.FontWeight != null) {
260+
if (color.FontStyle != null || color.FontWeight != null || color.FontFamily != null) {
261261
Typeface tf = element.TextRunProperties.Typeface;
262262
element.TextRunProperties.SetTypeface(new Typeface(
263-
tf.FontFamily,
263+
color.FontFamily ?? tf.FontFamily,
264264
color.FontStyle ?? tf.Style,
265265
color.FontWeight ?? tf.Weight,
266266
tf.Stretch
267267
));
268268
}
269269
if (color.Underline ?? false)
270270
element.TextRunProperties.SetTextDecorations(TextDecorations.Underline);
271+
if (color.Strikethrough ?? false)
272+
element.TextRunProperties.SetTextDecorations(TextDecorations.Strikethrough);
273+
if (color.FontSize.HasValue)
274+
element.TextRunProperties.SetFontRenderingEmSize(color.FontSize.Value);
271275
}
272276

273277
/// <summary>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<!-- syntaxdefinition for Json by alek kowalczyk -->
3+
<SyntaxDefinition name="Json" extensions=".json" xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
4+
<Color name="Digits" foreground="#8700FF" exampleText="3.14" />
5+
<Color name="Value" foreground="#000CFF" />
6+
<Color name="ParamName" foreground="#057500" />
7+
<RuleSet ignoreCase="false">
8+
<Keywords color="Digits" >
9+
<Word>true</Word>
10+
<Word>false</Word>
11+
</Keywords>
12+
<Span color="ParamName">
13+
<Begin>"</Begin>
14+
<End>"\w*(?=:,)</End>
15+
</Span>
16+
<Span color="ParamName">
17+
<Begin>'</Begin>
18+
<End>'\w*(?=:,)</End>
19+
</Span>
20+
<Span color="Value">
21+
<Begin>
22+
(?&lt;=:)\040"[^"]*
23+
</Begin>
24+
<End>"</End>
25+
</Span>
26+
<Span color="Value">
27+
<Begin>
28+
(?&lt;=:)\040'[^']*
29+
</Begin>
30+
<End>'</End>
31+
</Span>
32+
<Rule color="Digits">\b0[xX][0-9a-fA-F]+|(\b\d+(\.[0-9]+)?|\.[0-9]+)([eE][+-]?[0-9]+)?</Rule>
33+
</RuleSet>
34+
</SyntaxDefinition>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0"?>
2+
<SyntaxDefinition name="MarkDownWithFontSize" extensions=".md" xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
3+
<Color name="Heading1" foreground="Maroon" fontSize="30" exampleText="# Title #" />
4+
<Color name="Heading2" foreground="Maroon" fontSize="27" exampleText="# Title #" />
5+
<Color name="Heading3" foreground="Maroon" fontSize="24" exampleText="# Title #" />
6+
<Color name="Heading4" foreground="Maroon" fontSize="21" exampleText="# Title #" />
7+
<Color name="Heading5" foreground="Maroon" fontSize="18" exampleText="# Title #" />
8+
<Color name="Heading6" foreground="Maroon" fontSize="15" exampleText="# Title #" />
9+
<Color name="Emphasis" fontStyle="italic" exampleText="*this* is important!" />
10+
<Color name="StrongEmphasis" fontWeight="bold" exampleText="**this** is more important!" />
11+
<Color name="Code" fontFamily="Footlight MT Light" exampleText="this is `int.GetHashCode()`" />
12+
<Color name="BlockQuote" foreground="DarkBlue" exampleText="&gt; This is a\r\n&gt; quote." />
13+
<Color name="Link" foreground="Blue" exampleText="[text](http://example.com)" />
14+
<Color name="Image" foreground="Green" exampleText="[text][http://example.com/test.png]" />
15+
<Color name="LineBreak" background="LightGray" exampleText="end of line \r\n2nd line " />
16+
17+
<RuleSet ignoreCase="true">
18+
<Rule color="Heading1">
19+
^[#]{1}[ ]{1}.*
20+
</Rule>
21+
<Rule color="Heading2">
22+
^[#]{2}[ ]{1}.*
23+
</Rule>
24+
<Rule color="Heading3">
25+
^[#]{3}[ ]{1}.*
26+
</Rule>
27+
<Rule color="Heading4">
28+
^[#]{4}[ ]{1}.*
29+
</Rule>
30+
<Rule color="Heading5">
31+
^[#]{5}[ ]{1}.*
32+
</Rule>
33+
<Rule color="Heading6">
34+
^[#]{6}[ ]{1}.*
35+
</Rule>
36+
<Rule color="StrongEmphasis">
37+
\*\*.*\*\*
38+
</Rule>
39+
<Rule color="StrongEmphasis">
40+
__.*__
41+
</Rule>
42+
<Rule color="Emphasis">
43+
\*(?![ ]).*\*
44+
</Rule>
45+
<Rule color="Emphasis">
46+
_.*_
47+
</Rule>
48+
<Rule color="Code">
49+
`.*`
50+
</Rule>
51+
<Span color="Code" ruleSet="C#/" multiline="true">
52+
<Begin>^\t</Begin>
53+
<End>^(?!\t)</End>
54+
</Span>
55+
<Span color="Code" ruleSet="C#/" multiline="true">
56+
<Begin>^[ ]{4}</Begin>
57+
<End>^(?![ ]{4})</End>
58+
</Span>
59+
<Span color="BlockQuote" multiline="true">
60+
<Begin>^&gt;</Begin>
61+
<End>^(?!&gt;)</End>
62+
</Span>
63+
<Rule color="Image">
64+
\!\[.*\]\[.*\]
65+
</Rule>
66+
<Rule color="Link">
67+
\[.*\]\(.*\)
68+
</Rule>
69+
<Rule color="Link">
70+
\[.*\]\[.*\]
71+
</Rule>
72+
<Rule color="LineBreak">
73+
[ ]{2}$
74+
</Rule>
75+
</RuleSet>
76+
</SyntaxDefinition>

ICSharpCode.AvalonEdit/Highlighting/Resources/ModeV2.xsd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@
3535
<xsd:attribute name="background" type="xsd:string" use="optional" />
3636
<xsd:attribute name="fontWeight" type="FontWeight" use="optional" />
3737
<xsd:attribute name="fontStyle" type="FontStyle" use="optional" />
38+
<xsd:attribute name="fontFamily" type="xsd:string" use="optional" />
39+
<xsd:attribute name="fontSize" type="xsd:integer" use="optional" />
3840
<xsd:attribute name="underline" type="xsd:boolean" use="optional" />
41+
<xsd:attribute name="strikethrough" type="xsd:boolean" use="optional" />
3942
<xsd:anyAttribute namespace="##other" processContents="lax" />
4043
</xsd:attributeGroup>
4144

ICSharpCode.AvalonEdit/Highlighting/Resources/Resources.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ internal static void RegisterBuiltInHighlightings(HighlightingManager.DefaultHig
5959
".xft;.map;.wsdl;.disco;.ps1xml;.nuspec").Split(';'),
6060
"XML-Mode.xshd");
6161
hlm.RegisterHighlighting("MarkDown", new[] { ".md" }, "MarkDown-Mode.xshd");
62+
hlm.RegisterHighlighting("MarkDownWithFontSize", new[] { ".md" }, "MarkDownWithFontSize-Mode.xshd");
63+
hlm.RegisterHighlighting("Json", new[] { ".json" }, "Json.xshd");
6264
}
6365
}
6466
}

ICSharpCode.AvalonEdit/Highlighting/Xshd/V2Loader.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ static XshdColor ParseColorAttributes(XmlReader reader)
298298
color.FontWeight = ParseFontWeight(reader.GetAttribute("fontWeight"));
299299
color.FontStyle = ParseFontStyle(reader.GetAttribute("fontStyle"));
300300
color.Underline = reader.GetBoolAttribute("underline");
301+
color.Strikethrough = reader.GetBoolAttribute("strikethrough");
302+
color.FontFamily = ParseFontFamily(position, reader.GetAttribute("fontFamily"));
303+
color.FontSize = ParseFontSize(position, reader.GetAttribute("fontSize"));
301304
return color;
302305
}
303306

@@ -315,6 +318,23 @@ static HighlightingBrush ParseColor(IXmlLineInfo lineInfo, string color)
315318
return FixedColorHighlightingBrush((Color?)ColorConverter.ConvertFromInvariantString(color));
316319
}
317320

321+
static int? ParseFontSize(IXmlLineInfo lineInfo, string size)
322+
{
323+
int value;
324+
return int.TryParse(size, out value)
325+
? value
326+
: (int?)null;
327+
}
328+
329+
static FontFamily ParseFontFamily(IXmlLineInfo lineInfo, string family)
330+
{
331+
if (!string.IsNullOrEmpty(family)) {
332+
return new FontFamily(family);
333+
} else {
334+
return null;
335+
}
336+
}
337+
318338
internal static SystemColorHighlightingBrush GetSystemColorBrush(IXmlLineInfo lineInfo, string name)
319339
{
320340
Debug.Assert(name.StartsWith("SystemColors.", StringComparison.Ordinal));

ICSharpCode.AvalonEdit/Highlighting/Xshd/XmlHighlightingDefinition.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,11 @@ public object VisitColor(XshdColor color)
206206
c.Foreground = color.Foreground;
207207
c.Background = color.Background;
208208
c.Underline = color.Underline;
209+
c.Strikethrough = color.Strikethrough;
209210
c.FontStyle = color.FontStyle;
210211
c.FontWeight = color.FontWeight;
212+
c.FontFamily = color.FontFamily;
213+
c.FontSize = color.FontSize;
211214
return c;
212215
}
213216

0 commit comments

Comments
 (0)