Overwrite registered highlightings

This commit is contained in:
Lucas Trzesniewski 2023-02-25 19:20:40 +01:00
Родитель d12a78257b
Коммит 1c833b8f73
3 изменённых файлов: 45 добавлений и 1 удалений

3
.gitignore поставляемый
Просмотреть файл

@ -45,6 +45,9 @@ bld/
# Visual Studio 2017 auto generated files
Generated\ Files/
# JetBrains Rider
.idea/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*

Просмотреть файл

@ -0,0 +1,39 @@
using System;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Highlighting.Xshd;
using NUnit.Framework;
namespace ICSharpCode.AvalonEdit.Tests.Highlighting
{
[TestFixture]
public class HighlightingManagerTests
{
[Test]
public void OverwriteHighlightingDefinitionWithSameName()
{
var highlightingManager = new HighlightingManager();
var definitionA = CreateDefinition("TestDefinition");
var definitionB = CreateDefinition("TestDefinition");
var definitionC = CreateDefinition("DifferentName");
Assert.That(highlightingManager.HighlightingDefinitions, Is.Empty);
highlightingManager.RegisterHighlighting(definitionA.Name, Array.Empty<string>(), definitionA);
Assert.That(highlightingManager.HighlightingDefinitions, Is.EqualTo(new[] { definitionA }));
highlightingManager.RegisterHighlighting(definitionB.Name, Array.Empty<string>(), definitionB);
Assert.That(highlightingManager.HighlightingDefinitions, Is.EqualTo(new[] { definitionB }));
highlightingManager.RegisterHighlighting(definitionC.Name, Array.Empty<string>(), definitionC);
Assert.That(highlightingManager.HighlightingDefinitions, Is.EqualTo(new[] { definitionB, definitionC }));
XmlHighlightingDefinition CreateDefinition(string name)
{
return new XmlHighlightingDefinition(new XshdSyntaxDefinition { Name = name, Elements = { new XshdRuleSet() } }, highlightingManager);
}
}
}
}

Просмотреть файл

@ -185,8 +185,9 @@ namespace ICSharpCode.AvalonEdit.Highlighting
throw new ArgumentNullException("highlighting");
lock (lockObj) {
allHighlightings.Add(highlighting);
if (name != null) {
if (highlightingsByName.TryGetValue(name, out var existingDefinition))
allHighlightings.Remove(existingDefinition);
highlightingsByName[name] = highlighting;
}
if (extensions != null) {
@ -194,6 +195,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting
highlightingsByExtension[ext] = highlighting;
}
}
allHighlightings.Add(highlighting);
}
}