Skip to content

Commit

Permalink
Merge branch 'main' into rider-freeze
Browse files Browse the repository at this point in the history
  • Loading branch information
shocklateboy92 authored Sep 16, 2023
2 parents ae90bcf + 8504fa0 commit 2bb3a0f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="83d6b6a0-9e25-4034-80f3-38445d8a8837" Version="1.4.5" Language="en-US" Publisher="CSharpier" />
<Identity Id="83d6b6a0-9e25-4034-80f3-38445d8a8837" Version="1.4.7" Language="en-US" Publisher="CSharpier" />
<DisplayName>CSharpier</DisplayName>
<Description xml:space="preserve">CSharpier is an opinionated code formatter for c#. It uses Roslyn to parse your code and re-prints it using its own rules.</Description>
<MoreInfo>https://github.com/belav/csharpier</MoreInfo>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MinimumVisualStudioVersion>16.0</MinimumVisualStudioVersion>
Expand Down Expand Up @@ -42,7 +42,7 @@
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE;DEV16</DefineConstants>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Linq;
using System;
using EnvDTE;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
Expand All @@ -8,10 +8,6 @@
namespace CSharpier.VisualStudio
{
public class ReformatWithCSharpierOnSave : IVsRunningDocTableEvents3
#if !DEV16
, IVsRunningDocTableEvents7
#endif

{
private readonly DTE dte;
private readonly RunningDocumentTable runningDocumentTable;
Expand All @@ -35,20 +31,9 @@ public static async Task InitializeAsync(CSharpierPackage package)
new ReformatWithCSharpierOnSave(package, dte!);
}

public IVsTask? OnBeforeSaveAsync(uint cookie, uint flags, IVsTask? saveTask)
{
return ThreadHelper.JoinableTaskFactory.RunAsyncAsVsTask(
VsTaskRunContext.UIThreadNormalPriority,
async _ =>
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
return this.OnBeforeSave(cookie);
}
);
}

public int OnBeforeSave(uint docCookie)
{
Logger.Instance.Debug("OnBeforeSave");
var runOnSave =
CSharpierOptions.Instance.SolutionRunOnSave is true
|| (
Expand All @@ -58,18 +43,21 @@ CSharpierOptions.Instance.SolutionRunOnSave is null

if (!runOnSave)
{
Logger.Instance.Debug("No RunOnSave");
return VSConstants.S_OK;
}

var document = this.FindDocument(docCookie);

if (document == null)
{
Logger.Instance.Debug("No Document");
return VSConstants.S_OK;
}

Logger.Instance.Debug("Before format");
this.formattingService.Format(document);

Logger.Instance.Debug("Done Format");
return VSConstants.S_OK;
}

Expand All @@ -85,16 +73,26 @@ public int OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowF
return VSConstants.S_OK;
}

private Document? FindDocument(uint docCookie)
private Document FindDocument(uint docCookie)
{
ThreadHelper.ThrowIfNotOnUIThread();
try
{
ThreadHelper.ThrowIfNotOnUIThread();
var documentInfo = this.runningDocumentTable.GetDocumentInfo(docCookie);
var documentPath = documentInfo.Moniker;

var documentInfo = this.runningDocumentTable.GetDocumentInfo(docCookie);
var documentPath = documentInfo.Moniker;
if (this.dte.ActiveDocument.FullName == documentPath)
{
return this.dte.ActiveDocument;
}

return this.dte.Documents
.Cast<Document>()
.FirstOrDefault(o => o.FullName == documentPath);
return this.dte.Documents.Item(documentPath);
}
catch (Exception ex)
{
Logger.Instance.Error(ex);
return null;
}
}

public int OnAfterFirstDocumentLock(
Expand Down Expand Up @@ -122,11 +120,6 @@ public int OnAfterSave(uint docCookie)
return VSConstants.S_OK;
}

public IVsTask? OnAfterSaveAsync(uint cookie, uint flags)
{
return null;
}

public int OnAfterAttributeChange(uint docCookie, uint grfAttribs)
{
return VSConstants.S_OK;
Expand Down
8 changes: 7 additions & 1 deletion Src/CSharpier.VisualStudio/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
## [1.4.5]
## [1.4.7]
- Log exception when finding document fails but don't throw it.

## [1.4.6]
- Possible fix for issue with VS2022 not reformatting on save

## [1.4.5]
- Handle error on startup in VS 17.8.0

## [1.4.4]
Expand Down

0 comments on commit 2bb3a0f

Please sign in to comment.