Skip to content

Commit

Permalink
Better handle internal VS exceptions relating to .csproj files (#453)
Browse files Browse the repository at this point in the history
Co-authored-by: Dave Grochocki <[email protected]>
  • Loading branch information
mrlacey and grochocki authored Dec 27, 2023
1 parent fc1107c commit ad7aa2e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
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

0 comments on commit ad7aa2e

Please sign in to comment.