Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hk: cleanup and refactoring with roslyn lsp #81

Merged
merged 7 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions .gitpod.yml

This file was deleted.

24 changes: 13 additions & 11 deletions Clippit.Tests/PowerPoint/PresentationBuilderSlidePublishingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ namespace Clippit.Tests.PowerPoint
{
public class PresentationBuilderSlidePublishingTests : TestsBase
{
public static string SourceDirectory = "../../../../TestFiles/PublishSlides/";
public static string TargetDirectory = "../../../../TestFiles/PublishSlides/output";
private const string SourceDirectory = "../../../../TestFiles/PublishSlides/";
private const string TargetDirectory = "../../../../TestFiles/PublishSlides/output";

public static IEnumerable<object[]> GetData()
private class PublishingTestData : TheoryData<string>
{
var files = Directory.GetFiles(SourceDirectory, "*.pptx", SearchOption.TopDirectoryOnly);
return files.OrderBy(x => x).Select(path => new[] { path });
public PublishingTestData()
{
var files = Directory.GetFiles(SourceDirectory, "*.pptx", SearchOption.TopDirectoryOnly);
foreach (var file in files.OrderBy(x => x))
Add(file);
}
}

public PresentationBuilderSlidePublishingTests(ITestOutputHelper log)
Expand All @@ -27,7 +31,7 @@ public PresentationBuilderSlidePublishingTests(ITestOutputHelper log)
}

[Theory]
[MemberData(nameof(GetData))]
[ClassData(typeof(PublishingTestData))]
public void PublishUsingPublishSlides(string sourcePath)
{
var targetDir = Path.Combine(TargetDirectory, Path.GetFileNameWithoutExtension(sourcePath));
Expand Down Expand Up @@ -66,7 +70,7 @@ public void ExtractOneSlide(string fileName, int slideNumber)
var document = new PmlDocument(file);

var source = new SlideSource(document, slideNumber - 1, 1, true);
var slide = PresentationBuilder.BuildPresentation(new List<SlideSource> { source });
var slide = PresentationBuilder.BuildPresentation([source]);

slide.FileName = document.FileName.Replace(".pptx", $"_{slideNumber:000}.pptx");
slide.SaveAs(Path.Combine(TargetDirectory, Path.GetFileName(slide.FileName)));
Expand Down Expand Up @@ -126,7 +130,7 @@ public void ExtractMasters(string fileName)
numberOfMasters = doc1.PresentationPart.SlideMasterParts.Count();
}

var onlyMaster = PresentationBuilder.BuildPresentation(new List<SlideSource> { new(source, 0, 0, true) });
var onlyMaster = PresentationBuilder.BuildPresentation([new(source, 0, 0, true)]);

onlyMaster.FileName = fileName.Replace(".pptx", "_masterOnly.pptx");
onlyMaster.SaveAs(Path.Combine(TargetDirectory, onlyMaster.FileName));
Expand All @@ -146,9 +150,7 @@ public void ReassemblePresentationWithMaster(string fileName)
var presentation = new PmlDocument(file);

// generate presentation with all masters
var onlyMaster = PresentationBuilder.BuildPresentation(
new List<SlideSource> { new(presentation, 0, 0, true) }
);
var onlyMaster = PresentationBuilder.BuildPresentation([new(presentation, 0, 0, true)]);

// publish slides with one-layout masters
var slides = PresentationBuilder.PublishSlides(presentation);
Expand Down
5 changes: 1 addition & 4 deletions Clippit.Tests/PowerPoint/PresentationBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@

namespace Clippit.Tests.PowerPoint
{
public class PresentationBuilderTests : TestsBase
public class PresentationBuilderTests(ITestOutputHelper log) : TestsBase(log)
{
public PresentationBuilderTests(ITestOutputHelper log)
: base(log) { }

[Fact]
public void PB001_Formatting()
{
Expand Down
6 changes: 3 additions & 3 deletions Clippit.Tests/PowerPoint/PtUtilTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public void PU001(string name)
var sourceMht = new FileInfo(Path.Combine(sourceDir.FullName, name));
var src = File.ReadAllText(sourceMht.FullName);
var p = MhtParser.Parse(src);
Assert.True(p.ContentType != null);
Assert.True(p.MimeVersion != null);
Assert.True(p.Parts.Length != 0);
Assert.NotNull(p.ContentType);
Assert.NotNull(p.MimeVersion);
Assert.NotEmpty(p.Parts);
Assert.DoesNotContain(p.Parts, part => part.ContentType == null || part.ContentLocation == null);
}
}
Expand Down
7 changes: 5 additions & 2 deletions Clippit/Internal/Relationships.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ namespace Clippit.Internal;

internal static class Relationships
{
internal static string GetNewRelationshipId() =>
"rcId" + Guid.NewGuid().ToString().Replace("-", "").Substring(0, 16);
internal static string GetNewRelationshipId()
{
var uid = Guid.NewGuid().ToString().Replace("-", "").AsSpan(0, 16);
return string.Concat("rcId", uid);
}
}
38 changes: 20 additions & 18 deletions Clippit/Internal/TextReplacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,9 @@ namespace Clippit.Internal
{
public class TextReplacer
{
private class MatchSemaphore
private class MatchSemaphore(int matchId)
{
public int MatchId { get; }

public MatchSemaphore(int matchId)
{
MatchId = matchId;
}
public int MatchId { get; } = matchId;
}

private static XObject CloneWithAnnotation(XNode node)
Expand All @@ -44,7 +39,10 @@ private static object WmlSearchAndReplaceTransform(XNode node, string search, st
if (element.Name == W.p)
{
var contents = element.Descendants(W.t).Select(t => (string)t).StringConcatenate();
if (contents.Contains(search) || (!matchCase && contents.ToUpper().Contains(search.ToUpper())))
if (
contents.Contains(search)
|| (!matchCase && contents.Contains(search, System.StringComparison.CurrentCultureIgnoreCase))
)
{
var paragraphWithSplitRuns = new XElement(
W.p,
Expand Down Expand Up @@ -82,9 +80,10 @@ var pc in subRunArray
if (matchCase)
b = z.ParagraphChildProjection.Value != z.CharacterToCompare.ToString();
else
b =
z.ParagraphChildProjection.Value.ToUpper()
!= z.CharacterToCompare.ToString().ToUpper();
b = !z.ParagraphChildProjection.Value.Equals(
z.CharacterToCompare.ToString(),
System.StringComparison.CurrentCultureIgnoreCase
);
return b;
});
var match = !dontMatch;
Expand Down Expand Up @@ -141,7 +140,7 @@ var pc in subRunArray
return (object)g;
var textValue = g.Select(r => r.Element(W.t).Value).StringConcatenate();
XAttribute xs = null;
if (textValue[0] == ' ' || textValue[textValue.Length - 1] == ' ')
if (textValue[0] == ' ' || textValue[^1] == ' ')
xs = new XAttribute(XNamespace.Xml + "space", "preserve");
return new XElement(W.r, g.First().Elements(W.rPr), new XElement(W.t, xs, textValue));
})
Expand Down Expand Up @@ -259,13 +258,15 @@ bool matchCase

private static object PmlReplaceTextTransform(XNode node, string search, string replace, bool matchCase)
{
var element = node as XElement;
if (element != null)
if (node is XElement element)
{
if (element.Name == A.p)
{
var contents = element.Descendants(A.t).Select(t => (string)t).StringConcatenate();
if (contents.Contains(search) || (!matchCase && contents.ToUpper().Contains(search.ToUpper())))
if (
contents.Contains(search)
|| (!matchCase && contents.Contains(search, System.StringComparison.CurrentCultureIgnoreCase))
)
{
var paragraphWithSplitRuns = new XElement(
A.p,
Expand Down Expand Up @@ -303,9 +304,10 @@ var pc in subRunArray
if (matchCase)
b = z.ParagraphChildProjection.Value != z.CharacterToCompare.ToString();
else
b =
z.ParagraphChildProjection.Value.ToUpper()
!= z.CharacterToCompare.ToString().ToUpper();
b = !z.ParagraphChildProjection.Value.Equals(
z.CharacterToCompare.ToString(),
System.StringComparison.CurrentCultureIgnoreCase
);
return b;
});
var match = !dontMatch;
Expand Down
Loading
Loading