Skip to content

Commit

Permalink
Merge branch 'master' into multitarget
Browse files Browse the repository at this point in the history
  • Loading branch information
grochocki authored Dec 27, 2023
2 parents acee86d + ad7aa2e commit 01bc672
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/XamlStyler.Console/XamlStyler.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<Version>13.0.3</Version>
</PackageReference>
<PackageReference Include="NuGet.CommandLine">
<Version>6.6.1</Version>
<Version>6.8.0</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,29 @@ public static bool IsAvaloniaXaml(this Document document)
&& document.FullName.EndsWith(Constants.AxamlFileExtension, StringComparison.OrdinalIgnoreCase);
}

public static bool IsReadOnly(this Document document)
{
ThreadHelper.ThrowIfNotOnUIThread();

try
{
return document.ReadOnly;
}
catch
{
// EnvDTE.Document.get_ReadOnly() *may* throw an Exception for some document types.
}

return false;
}

public static XamlLanguageOptions GetXamlLanguageOptions(this Document document)
{
ThreadHelper.ThrowIfNotOnUIThread();

var options = new XamlLanguageOptions();

if (document == null || document.ReadOnly)
if (document == null || document.IsReadOnly())
{
return options;
}
Expand Down
34 changes: 28 additions & 6 deletions src/XamlStyler.Extension.Windows.Shared/StylerPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private void AddCommandHandlers()

// Format XAML File Command
this.menuCommandService.AddCommand(new MenuCommand(
(s,e) => this.FormatFileEventHandler(),
(s, e) => this.FormatFileEventHandler(),
new CommandID(Guids.GuidXamlStylerMenuSet, (int)Constants.CommandIDFormatXamlFile)));

// Format All XAML Command
Expand All @@ -101,7 +101,10 @@ private void FormatFileEventHandler()
{
ThreadHelper.ThrowIfNotOnUIThread();
this.uiShell.SetWaitCursor();
this.FormatDocument(this.IDE2.ActiveDocument);
if (TryGetActiveDocument(out Document document))
{
this.FormatDocument(document);
}
}

private void FormatSolutionEventHandler()
Expand Down Expand Up @@ -135,11 +138,13 @@ private void OnFileSave(string guid, int id, object customIn, object customOut,
return;
}

Document document = this.IDE2.ActiveDocument;
IStylerOptions options = this.optionsHelper.GetDocumentStylerOptions(document);
if (options.FormatOnSave)
if (TryGetActiveDocument(out Document document))
{
this.FormatDocument(document, options);
IStylerOptions options = this.optionsHelper.GetDocumentStylerOptions(document);
if (options.FormatOnSave)
{
this.FormatDocument(document, options);
}
}
}

Expand Down Expand Up @@ -180,6 +185,23 @@ private void OnFileSaveAll(string guid, int id, object customIn, object customOu
}
}

private bool TryGetActiveDocument(out Document document)
{
ThreadHelper.ThrowIfNotOnUIThread();
try
{
document = this.IDE2.ActiveDocument;
return document != null;
}
catch
{
// EnvDTE80.DTE2.get_ActiveDocument() *may* throw an ArgumentNullException for some documents.
}

document = null;
return false;
}

private void ShowMessageBox(Exception ex)
{
ThreadHelper.ThrowIfNotOnUIThread();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,18 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="CompareNETObjects">
<Version>4.79.0</Version>
<Version>4.83.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers">
<Version>7.0.3</Version>
<Version>8.0.0</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.SDK">
<Version>17.4.33103.184</Version>
</PackageReference>
<PackageReference Include="Microsoft.VSSDK.BuildTools">
<Version>17.5.4074</Version>
<Version>17.8.2365</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,18 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="CompareNETObjects">
<Version>4.79.0</Version>
<Version>4.83.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers">
<Version>7.0.3</Version>
<Version>8.0.0</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.SDK">
<Version>17.5.33428.388</Version>
<Version>17.8.37222</Version>
</PackageReference>
<PackageReference Include="Microsoft.VSSDK.BuildTools">
<Version>17.5.4074</Version>
<Version>17.8.2365</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
6 changes: 3 additions & 3 deletions src/XamlStyler.UnitTests/TestConfigurations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void TestConfigurationDefault()
var stylerOptions = new StylerOptions(config: Tests.GetConfiguration(@"TestConfigurations/Default.json"));
this.TestConfig(stylerOptions, @"TestConfigurations/SerializedDefault.json");

Assert.IsTrue(stylerOptions.UseVisualStudioIndentWithTabs);
Assert.That(stylerOptions.UseVisualStudioIndentWithTabs);
}

[Test]
Expand Down Expand Up @@ -58,8 +58,8 @@ public void TestConfigurationIndentUsingTabs()
var stylerOptions = new StylerOptions(config: Tests.GetConfiguration(@"TestConfigurations/IndentWithTabsOverride.json"));
this.TestConfig(stylerOptions, @"TestConfigurations/IndentWithTabsOverride.json");

Assert.IsFalse(stylerOptions.UseVisualStudioIndentWithTabs); // IndentWithTabs is true
Assert.IsFalse(stylerOptions.UseVisualStudioIndentSize); // IndentSize is set
Assert.That(!stylerOptions.UseVisualStudioIndentWithTabs); // IndentWithTabs is true
Assert.That(!stylerOptions.UseVisualStudioIndentSize); // IndentSize is set
}

private void TestConfig(StylerOptions stylerOptions, string expectedConfiguration)
Expand Down
6 changes: 3 additions & 3 deletions src/XamlStyler.UnitTests/XamlStyler.UnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
<OutputPath>..\bin\Release\UnitTest</OutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CompareNETObjects" Version="4.79.0">
<PackageReference Include="CompareNETObjects" Version="4.83.0">
</PackageReference>
<PackageReference Include="Irony" Version="1.2.0">
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3">
</PackageReference>
<PackageReference Include="NUnit" Version="3.13.3">
<PackageReference Include="NUnit" Version="4.0.1">
</PackageReference>
<PackageReference Include="NUnit.ConsoleRunner" Version="3.16.3">
</PackageReference>
Expand Down

0 comments on commit 01bc672

Please sign in to comment.