1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Text.RegularExpressions;
using Tango.Scripting.Editors.Utils;
namespace Tango.Scripting.Editors.Rendering
{
// This class is public because it can be used as a base class for custom links.
/// <summary>
/// Detects hyperlinks and makes them clickable.
/// </summary>
/// <remarks>
/// This element generator can be easily enabled and configured using the
/// <see cref="TextEditorOptions"/>.
/// </remarks>
public class LinkElementGenerator : VisualLineElementGenerator, IBuiltinElementGenerator
{
// a link starts with a protocol (or just with www), followed by 0 or more 'link characters', followed by a link end character
// (this allows accepting punctuation inside links but not at the end)
internal readonly static Regex defaultLinkRegex = new Regex(@"\b(https?://|ftp://|www\.)[\w\d\._/\-~%@()+:?&=#!]*[\w\d/]");
// try to detect email addresses
internal readonly static Regex defaultMailRegex = new Regex(@"\b[\w\d\.\-]+\@[\w\d\.\-]+\.[a-z]{2,6}\b");
readonly Regex linkRegex;
/// <summary>
/// Gets/Sets whether the user needs to press Control to click the link.
/// The default value is true.
/// </summary>
public bool RequireControlModifierForClick { get; set; }
/// <summary>
/// Creates a new LinkElementGenerator.
/// </summary>
public LinkElementGenerator()
{
this.linkRegex = defaultLinkRegex;
this.RequireControlModifierForClick = true;
}
/// <summary>
/// Creates a new LinkElementGenerator using the specified regex.
/// </summary>
protected LinkElementGenerator(Regex regex) : this()
{
if (regex == null)
throw new ArgumentNullException("regex");
this.linkRegex = regex;
}
void IBuiltinElementGenerator.FetchOptions(TextEditorOptions options)
{
this.RequireControlModifierForClick = options.RequireControlModifierForHyperlinkClick;
}
Match GetMatch(int startOffset, out int matchOffset)
{
int endOffset = CurrentContext.VisualLine.LastDocumentLine.EndOffset;
StringSegment relevantText = CurrentContext.GetText(startOffset, endOffset - startOffset);
Match m = linkRegex.Match(relevantText.Text, relevantText.Offset, relevantText.Count);
matchOffset = m.Success ? m.Index - relevantText.Offset + startOffset : -1;
return m;
}
/// <inheritdoc/>
public override int GetFirstInterestedOffset(int startOffset)
{
int matchOffset;
GetMatch(startOffset, out matchOffset);
return matchOffset;
}
/// <inheritdoc/>
public override VisualLineElement ConstructElement(int offset)
{
int matchOffset;
Match m = GetMatch(offset, out matchOffset);
if (m.Success && matchOffset == offset) {
return ConstructElementFromMatch(m);
} else {
return null;
}
}
/// <summary>
/// Constructs a VisualLineElement that replaces the matched text.
/// The default implementation will create a <see cref="VisualLineLinkText"/>
/// based on the URI provided by <see cref="GetUriFromMatch"/>.
/// </summary>
protected virtual VisualLineElement ConstructElementFromMatch(Match m)
{
Uri uri = GetUriFromMatch(m);
if (uri == null)
return null;
VisualLineLinkText linkText = new VisualLineLinkText(CurrentContext.VisualLine, m.Length);
linkText.NavigateUri = uri;
linkText.RequireControlModifierForClick = this.RequireControlModifierForClick;
return linkText;
}
/// <summary>
/// Fetches the URI from the regex match. Returns null if the URI format is invalid.
/// </summary>
protected virtual Uri GetUriFromMatch(Match match)
{
string targetUrl = match.Value;
if (targetUrl.StartsWith("www.", StringComparison.Ordinal))
targetUrl = "http://" + targetUrl;
if (Uri.IsWellFormedUriString(targetUrl, UriKind.Absolute))
return new Uri(targetUrl);
return null;
}
}
// This class is internal because it does not need to be accessed by the user - it can be configured using TextEditorOptions.
/// <summary>
/// Detects e-mail addresses and makes them clickable.
/// </summary>
/// <remarks>
/// This element generator can be easily enabled and configured using the
/// <see cref="TextEditorOptions"/>.
/// </remarks>
sealed class MailLinkElementGenerator : LinkElementGenerator
{
/// <summary>
/// Creates a new MailLinkElementGenerator.
/// </summary>
public MailLinkElementGenerator()
: base(defaultMailRegex)
{
}
protected override Uri GetUriFromMatch(Match match)
{
return new Uri("mailto:" + match.Value);
}
}
}
|