Skip to content

Commit d449c2c

Browse files
author
drewmiller
committed
added XML-doc comments and a Sandcastle Help File Builder project file;
fixed the x64 perf by disabling Regex compilation; removed the unused Unload methods for the language repository; added a Formatters accessor class
1 parent 0ea5faf commit d449c2c

17 files changed

+347
-191
lines changed

ColorCode.Facts/Common/LanguageRepositoryFacts.cs

-116
Original file line numberDiff line numberDiff line change
@@ -166,121 +166,5 @@ public void Will_throw_when__the_language_identifier_is_empty()
166166
Assert.Equal("language", ((ArgumentException) ex).ParamName);
167167
}
168168
}
169-
170-
public class The_Unload_method
171-
{
172-
[Fact]
173-
public void Will_throw_when_the_language_is_null()
174-
{
175-
var languageRepository = new LanguageRepository(new Dictionary<string, ILanguage>());
176-
177-
Exception ex = Record.Exception(() => languageRepository.Unload((ILanguage) null));
178-
179-
Assert.IsType<ArgumentNullException>(ex);
180-
Assert.Equal("language", ((ArgumentNullException) ex).ParamName);
181-
}
182-
183-
[Fact]
184-
public void Will_throw_when_the_language_identifier_is_null()
185-
{
186-
var languageRepository = new LanguageRepository(new Dictionary<string, ILanguage>());
187-
var language = new StubLanguage();
188-
language.id__getValue = null;
189-
190-
Exception ex = Record.Exception(() => languageRepository.Unload(language));
191-
192-
Assert.IsType<ArgumentException>(ex);
193-
Assert.Contains("The language identifier must not be null or empty.", ex.Message);
194-
Assert.Equal("language", ((ArgumentException) ex).ParamName);
195-
}
196-
197-
[Fact]
198-
public void Will_throw_when__the_language_identifier_is_empty()
199-
{
200-
var languageRepository = new LanguageRepository(new Dictionary<string, ILanguage>());
201-
var language = new StubLanguage();
202-
language.id__getValue = string.Empty;
203-
204-
Exception ex = Record.Exception(() => languageRepository.Unload(language));
205-
206-
Assert.IsType<ArgumentException>(ex);
207-
Assert.Contains("The language identifier must not be null or empty.", ex.Message);
208-
Assert.Equal("language", ((ArgumentException) ex).ParamName);
209-
}
210-
211-
[Fact]
212-
public void Will_throw_when_the_language_identifier_is_null_for_identifier()
213-
{
214-
var languageRepository = new LanguageRepository(new Dictionary<string, ILanguage>());
215-
216-
Exception ex = Record.Exception(() => languageRepository.Unload((string) null));
217-
218-
Assert.IsType<ArgumentException>(ex);
219-
Assert.Contains("The language identifier must not be null or empty.", ex.Message);
220-
Assert.Equal("language", ((ArgumentException)ex).ParamName);
221-
}
222-
223-
[Fact]
224-
public void Will_throw_when__the_language_identifier_is_empty_for_identifier()
225-
{
226-
var languageRepository = new LanguageRepository(new Dictionary<string, ILanguage>());
227-
228-
Exception ex = Record.Exception(() => languageRepository.Unload(string.Empty));
229-
230-
Assert.IsType<ArgumentException>(ex);
231-
Assert.Contains("The language identifier must not be null or empty.", ex.Message);
232-
Assert.Equal("language", ((ArgumentException)ex).ParamName);
233-
}
234-
235-
[Fact]
236-
public void Will_remove_the_language_from_the_loaded_languages()
237-
{
238-
var stubLoadedLanguages = new Dictionary<string, ILanguage>();
239-
var languageRepository = new LanguageRepository(stubLoadedLanguages);
240-
var stubLanguage = new StubLanguage {id__getValue = "fnord"};
241-
languageRepository.Load(stubLanguage);
242-
243-
languageRepository.Unload(stubLanguage);
244-
245-
Assert.DoesNotContain(stubLanguage, stubLoadedLanguages.Values);
246-
}
247-
248-
[Fact]
249-
public void Will_not_remove_the_language_from_the_loaded_languages_if_not_present()
250-
{
251-
var stubLoadedLanguages = new Dictionary<string, ILanguage>();
252-
var languageRepository = new LanguageRepository(stubLoadedLanguages);
253-
var stubLanguage = new StubLanguage { id__getValue = "fnord" };
254-
255-
languageRepository.Unload(stubLanguage);
256-
257-
Assert.DoesNotContain(stubLanguage, stubLoadedLanguages.Values);
258-
}
259-
260-
[Fact]
261-
public void Will_remove_the_language_from_the_loaded_languages_for_identifier()
262-
{
263-
var stubLoadedLanguages = new Dictionary<string, ILanguage>();
264-
var languageRepository = new LanguageRepository(stubLoadedLanguages);
265-
var stubLanguage = new StubLanguage { id__getValue = "fnord" };
266-
languageRepository.Load(stubLanguage);
267-
268-
languageRepository.Unload("fnord");
269-
270-
Assert.DoesNotContain(stubLanguage, stubLoadedLanguages.Values);
271-
}
272-
273-
[Fact]
274-
public void Will_not_remove_the_language_from_the_loaded_languages_if_not_present_for_identifier()
275-
{
276-
var stubLoadedLanguages = new Dictionary<string, ILanguage>();
277-
var languageRepository = new LanguageRepository(stubLoadedLanguages);
278-
var stubLanguage = new StubLanguage { id__getValue = "fnord" };
279-
280-
languageRepository.Unload("fnord");
281-
282-
Assert.DoesNotContain(stubLanguage, stubLoadedLanguages.Values);
283-
}
284-
}
285169
}
286170
}

ColorCode.shfbproj

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
2+
<PropertyGroup>
3+
<!-- The configuration and platform will be used to determine which
4+
assemblies to include from solution and project documentation
5+
sources -->
6+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
7+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
8+
<SchemaVersion>2.0</SchemaVersion>
9+
<ProjectGuid>{7691855c-93b9-44e0-8311-85ca9599e0a9}</ProjectGuid>
10+
<SHFBSchemaVersion>1.8.0.0</SHFBSchemaVersion>
11+
<!-- AssemblyName, Name, and RootNamespace are not used by SHFB but Visual
12+
Studio adds them anyway -->
13+
<AssemblyName>Documentation</AssemblyName>
14+
<RootNamespace>Documentation</RootNamespace>
15+
<Name>Documentation</Name>
16+
<!-- SHFB properties -->
17+
<OutputPath>.\Help\</OutputPath>
18+
<HtmlHelpName>Documentation</HtmlHelpName>
19+
<DocumentationSources>
20+
<DocumentationSource sourceFile="ColorCode\bin\Debug\ColorCode.dll" />
21+
<DocumentationSource sourceFile="ColorCode\bin\Debug\ColorCode.XML" /></DocumentationSources>
22+
<NamespaceSummaries>
23+
<NamespaceSummaryItem name="ColorCode.Common" isDocumented="False" />
24+
<NamespaceSummaryItem name="ColorCode.Compilation" isDocumented="False" />
25+
<NamespaceSummaryItem name="ColorCode.Compilation.Languages" isDocumented="False" />
26+
<NamespaceSummaryItem name="ColorCode.Formatting" isDocumented="False" />
27+
<NamespaceSummaryItem name="ColorCode.Parsing" isDocumented="False" />
28+
<NamespaceSummaryItem name="ColorCode.Styling" isDocumented="False" />
29+
<NamespaceSummaryItem name="ColorCode.Styling.StyleSheets" isDocumented="False" />
30+
<NamespaceSummaryItem name="ColorCode" isDocumented="True">The types in the root ColorCode namespace are the supported, public API.</NamespaceSummaryItem></NamespaceSummaries>
31+
<HelpTitle>ColorCode - Source Code Colorization for .NET</HelpTitle>
32+
</PropertyGroup>
33+
<!-- There are no properties for these two groups but they need to appear in
34+
order for Visual Studio to perform the build. -->
35+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
36+
</PropertyGroup>
37+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
38+
</PropertyGroup>
39+
<!-- Import the SHFB build targets -->
40+
<Import Project="$(SHFBROOT)\SandcastleHelpFileBuilder.targets" />
41+
</Project>

ColorCode/CodeColorizer.cs

+31-3
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,38 @@
99

1010
namespace ColorCode
1111
{
12+
/// <summary>
13+
/// Colorizes source code.
14+
/// </summary>
1215
public class CodeColorizer : ICodeColorizer
1316
{
1417
private readonly ILanguageParser languageParser;
1518

19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="CodeColorizer"/> class.
21+
/// </summary>
1622
public CodeColorizer()
1723
{
1824
languageParser = new LanguageParser(new LanguageCompiler(Languages.CompiledLanguages), Languages.LanguageRepository);
1925
}
2026

27+
/// <summary>
28+
/// Initializes a new instance of the <see cref="CodeColorizer"/> class.
29+
/// </summary>
30+
/// <param name="languageParser">The language parser that the <see cref="CodeColorizer"/> instance will use for its lifetime.</param>
2131
public CodeColorizer(ILanguageParser languageParser)
2232
{
2333
Guard.ArgNotNull(languageParser, "languageParser");
2434

2535
this.languageParser = languageParser;
2636
}
2737

38+
/// <summary>
39+
/// Colorizes source code using the specified language, the default formatter, and the default style sheet.
40+
/// </summary>
41+
/// <param name="sourceCode">The source code to colorize.</param>
42+
/// <param name="language">The language to use to colorize the source code.</param>
43+
/// <returns>The colorized source code.</returns>
2844
public string Colorize(string sourceCode, ILanguage language)
2945
{
3046
var buffer = new StringBuilder(sourceCode.Length * 2);
@@ -39,13 +55,25 @@ public string Colorize(string sourceCode, ILanguage language)
3955
return buffer.ToString();
4056
}
4157

58+
/// <summary>
59+
/// Colorizes source code using the specified language, the default formatter, and the default style sheet.
60+
/// </summary>
61+
/// <param name="sourceCode">The source code to colorize.</param>
62+
/// <param name="language">The language to use to colorize the source code.</param>
63+
/// <param name="textWriter">The text writer to which the colorized source code will be written.</param>
4264
public void Colorize(string sourceCode, ILanguage language, TextWriter textWriter)
4365
{
44-
IFormatter formatter = new HtmlFormatter();
45-
IStyleSheet styleSheet = StyleSheets.Default;
46-
Colorize(sourceCode, language, formatter, styleSheet, textWriter);
66+
Colorize(sourceCode, language, Formatters.Default, StyleSheets.Default, textWriter);
4767
}
4868

69+
/// <summary>
70+
/// Colorizes source code using the specified language, formatter, and style sheet.
71+
/// </summary>
72+
/// <param name="sourceCode">The source code to colorize.</param>
73+
/// <param name="language">The language to use to colorize the source code.</param>
74+
/// <param name="formatter">The formatter to use to colorize the source code.</param>
75+
/// <param name="styleSheet">The style sheet to use to colorize the source code.</param>
76+
/// <param name="textWriter">The text writer to which the colorized source code will be written.</param>
4977
public void Colorize(string sourceCode,
5078
ILanguage language,
5179
IFormatter formatter,

ColorCode/ColorCode.csproj

+5-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<DefineConstants>DEBUG;TRACE</DefineConstants>
2222
<ErrorReport>prompt</ErrorReport>
2323
<WarningLevel>4</WarningLevel>
24+
<DocumentationFile>bin\Debug\ColorCode.XML</DocumentationFile>
2425
</PropertyGroup>
2526
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2627
<DebugType>pdbonly</DebugType>
@@ -73,6 +74,7 @@
7374
<Compile Include="Compilation\Languages\Xml.cs">
7475
<SubType>Code</SubType>
7576
</Compile>
77+
<Compile Include="Formatters.cs" />
7678
<Compile Include="Formatting\HtmlFormatter.cs" />
7779
<Compile Include="Parsing\Scope.cs" />
7880
<Compile Include="IFormatter.cs" />
@@ -88,7 +90,7 @@
8890
<Compile Include="Compilation\Languages\Ashx.cs">
8991
<SubType>Code</SubType>
9092
</Compile>
91-
<Compile Include="Compilation\LanguageRule.cs" />
93+
<Compile Include="LanguageRule.cs" />
9294
<Compile Include="ICodeColorizer.cs" />
9395
<Compile Include="CodeColorizer.cs" />
9496
<Compile Include="Common\ExtensionMethods.cs" />
@@ -98,8 +100,8 @@
98100
<Compile Include="..\GlobalAssemblyInfo.cs">
99101
<Link>Properties\GlobalAssemblyInfo.cs</Link>
100102
</Compile>
101-
<Compile Include="Styling\Style.cs" />
102-
<Compile Include="Styling\StyleDictionary.cs">
103+
<Compile Include="Style.cs" />
104+
<Compile Include="StyleDictionary.cs">
103105
<SubType>Code</SubType>
104106
</Compile>
105107
<Compile Include="Formatting\TextInsertion.cs" />

ColorCode/Common/ILanguageRepository.cs

-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,5 @@ public interface ILanguageRepository
99
IEnumerable<ILanguage> All { get; }
1010
ILanguage FindById(string languageId);
1111
void Load(ILanguage language);
12-
void Unload(ILanguage language);
13-
void Unload(string languageId);
1412
}
1513
}

ColorCode/Common/LanguageRepository.cs

-25
Original file line numberDiff line numberDiff line change
@@ -61,30 +61,5 @@ public void Load(ILanguage language)
6161
loadLock.ExitWriteLock();
6262
}
6363
}
64-
65-
public void Unload(ILanguage language)
66-
{
67-
Guard.ArgNotNull(language, "language");
68-
69-
Unload(language.Id);
70-
}
71-
72-
public void Unload(string languageId)
73-
{
74-
if (string.IsNullOrEmpty(languageId))
75-
throw new ArgumentException("The language identifier must not be null or empty.", "language");
76-
77-
loadLock.EnterWriteLock();
78-
79-
try
80-
{
81-
if (loadedLanguages.ContainsKey(languageId))
82-
loadedLanguages.Remove(languageId);
83-
}
84-
finally
85-
{
86-
loadLock.ExitWriteLock();
87-
}
88-
}
8964
}
9065
}

ColorCode/Compilation/LanguageCompiler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private static void CompileRules(IList<LanguageRule> rules,
107107
for (int i = 1; i < rules.Count; i++)
108108
CompileRule(rules[i], regexBuilder, captures, false);
109109

110-
regex = new Regex(regexBuilder.ToString(), RegexOptions.Compiled);
110+
regex = new Regex(regexBuilder.ToString());
111111
}
112112

113113

ColorCode/Compilation/LanguageRule.cs

-23
This file was deleted.

ColorCode/Formatters.cs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using ColorCode.Formatting;
6+
7+
namespace ColorCode
8+
{
9+
/// <summary>
10+
/// Provides easy access to ColorCode's built-in formatters.
11+
/// </summary>
12+
public static class Formatters
13+
{
14+
/// <summary>
15+
/// Gets the default formatter.
16+
/// </summary>
17+
/// <remarks>
18+
/// The default formatter produces HTML with inline styles.
19+
/// </remarks>
20+
public static IFormatter Default { get { return new HtmlFormatter(); } }
21+
}
22+
}

0 commit comments

Comments
 (0)