Skip to content

Commit

Permalink
Merge pull request #1694 from glopesdev/issue-1692
Browse files Browse the repository at this point in the history
Improve detection of word boundaries in node names
  • Loading branch information
glopesdev authored Feb 29, 2024
2 parents 4a729f3 + fab9aa0 commit 4fd86c5
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Bonsai.Editor.Tests/Bonsai.Editor.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<IsPackable>false</IsPackable>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<TargetFramework>net472</TargetFramework>
<VersionPrefix>2.8.0</VersionPrefix>
<VersionPrefix>2.8.1</VersionPrefix>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="**\*.bonsai" />
Expand Down
34 changes: 34 additions & 0 deletions Bonsai.Editor.Tests/WordSeparationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Bonsai.Editor.Tests
{
[TestClass]
public class WordSeparationTests
{
[DataTestMethod]
[DataRow("")]
[DataRow("Point.X", "Point.", "X")]
[DataRow("Source/1", "Source/", "1")]
[DataRow("Source/Path", "Source/", "Path")]
[DataRow("State Space", "State ", "Space")]
[DataRow("TimeStep.ElapsedTime", "Time", "Step.", "Elapsed", "Time")]
[DataRow("UpdateVRState", "Update", "VR", "State")]
[DataRow("Get-DisplayLED", "Get-", "Display", "LED")]
[DataRow("A_LARGE_NAME", "A_", "LARGE_", "NAME")]
[DataRow("Get-LatestI2CReading", "Get-", "Latest", "I2C", "Reading")]
[DataRow("ObserveOnTaskPool", "Observe", "On", "Task", "Pool")]
public void SplitOnWordBoundaries_ExpectedWordSequence(string text, params string[] expectedWords)
{
var words = text.SplitOnWordBoundaries();
CollectionAssert.AreEqual(expectedWords, words);
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void SplitOnWordBoundaries_NullString_ThrowsArgumentNullException()
{
StringExtensions.SplitOnWordBoundaries(null);
}
}
}
2 changes: 1 addition & 1 deletion Bonsai.Editor/Bonsai.Editor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<UseWindowsForms>true</UseWindowsForms>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<TargetFramework>net472</TargetFramework>
<VersionPrefix>2.8.0</VersionPrefix>
<VersionPrefix>2.8.1</VersionPrefix>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="**\*.svg" />
Expand Down
23 changes: 1 addition & 22 deletions Bonsai.Editor/GraphView/GraphViewControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -988,31 +988,10 @@ private void SetCursor(GraphNode node)
EnsureVisible(CursorNode);
}

private static string[] GetWords(string text)
{
var wordCount = 0;
var words = new string[text.Length];
var builder = new StringBuilder(text.Length);
foreach (var c in text)
{
if (builder.Length > 0 && (Char.IsUpper(c) || Char.IsWhiteSpace(c)))
{
words[wordCount++] = builder.ToString();
builder.Clear();
}

builder.Append(c);
}

if (builder.Length > 0) words[wordCount++] = builder.ToString();
Array.Resize(ref words, wordCount);
return words;
}

private static IEnumerable<string> WordWrap(Graphics graphics, string text, Font font, float lineWidth)
{
var trimStart = true;
var words = GetWords(text);
var words = text.SplitOnWordBoundaries();
var lineBreak = words.Length <= 1 ? 0 : 2;
var result = new StringBuilder(text.Length);
foreach (var word in words)
Expand Down
58 changes: 58 additions & 0 deletions Bonsai.Editor/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Text;

namespace Bonsai.Editor
{
internal static class StringExtensions
{
static bool IsLowerToUpperCase(char a, char b)
{
return char.IsLower(a) && char.IsUpper(b);
}

static bool IsUpperToLowerCase(char a, char b)
{
return char.IsUpper(a) && char.IsLower(b);
}

static bool IsWordSeparator(char c)
{
return char.IsSeparator(c) || char.IsPunctuation(c);
}

static bool IsWordBoundary(string text, int index, char current)
{
var previous = text[index - 1];
return IsLowerToUpperCase(previous, current)
|| IsWordSeparator(previous)
|| index < text.Length - 1 && IsUpperToLowerCase(current, text[index + 1]);
}

public static string[] SplitOnWordBoundaries(this string text)
{
if (text == null)
{
throw new ArgumentNullException(nameof(text));
}

var wordCount = 0;
var words = new string[text.Length];
var builder = new StringBuilder(text.Length);
for (int i = 0; i < text.Length; i++)
{
var c = text[i];
if (i > 0 && IsWordBoundary(text, i, c))
{
words[wordCount++] = builder.ToString();
builder.Clear();
}

builder.Append(c);
}

if (builder.Length > 0) words[wordCount++] = builder.ToString();
Array.Resize(ref words, wordCount);
return words;
}
}
}

0 comments on commit 4fd86c5

Please sign in to comment.