Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ download in the
These are the changes to each version that has been released
on the official Visual Studio extension gallery.

## 2.1
- [x] Ability to use a locally installed prettier version before using internal version
- [x] New "Format On Save" option in Tools->Options->Prettier

## 2.0
- [x] Updated to prettier@1.12.1
- [x] Ability to read and use the [prettier configuration](https://prettier.io/docs/en/configuration.html) specified in your project
Expand Down
5 changes: 4 additions & 1 deletion src/Commands/CommandRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ public void VsTextViewCreated(IVsTextView textViewAdapter)
ITextBufferUndoManager undoManager = UndoProvider.GetTextBufferUndoManager(view.TextBuffer);
NodeProcess node = view.Properties.GetOrCreateSingletonProperty(() => new NodeProcess());

AddCommandFilter(textViewAdapter, new PrettierCommand(view, undoManager, node, doc.Encoding, doc.FilePath));
var cmd = new PrettierCommand(view, undoManager, node, doc.Encoding, doc.FilePath);
view.Properties.AddProperty("prettierCommand", cmd);

AddCommandFilter(textViewAdapter, cmd);

if (!node.IsReadyToExecute())
{
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/PrettierCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public override int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt,
return Next.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
}

private async Task<bool> MakePrettierAsync()
public async Task<bool> MakePrettierAsync()
{
string input = _view.TextBuffer.CurrentSnapshot.GetText();
string output = await _node.ExecuteProcessAsync(input, _encoding, _filePath);
Expand Down
7 changes: 6 additions & 1 deletion src/JavaScriptPrettier.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<StartArguments>/rootsuffix Exp</StartArguments>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<TargetFrameworkProfile />
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand All @@ -22,7 +23,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>JavaScriptPrettier</RootNamespace>
<AssemblyName>JavaScriptPrettier</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<GeneratePkgDefFile>true</GeneratePkgDefFile>
<IncludeAssemblyInVSIXContainer>true</IncludeAssemblyInVSIXContainer>
<IncludeDebugSymbolsInVSIXContainer>true</IncludeDebugSymbolsInVSIXContainer>
Expand All @@ -38,6 +39,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>7.2</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -46,6 +48,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>7.2</LangVersion>
</PropertyGroup>
<ItemGroup>
<Compile Include="Commands\BaseCommand.cs" />
Expand All @@ -55,6 +58,7 @@
<Compile Include="NodeProcess.cs" />
<Compile Include="PrettierPackage.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RunningDocTableEventsHandler.cs" />
<Compile Include="source.extension.cs">
<DependentUpon>source.extension.vsixmanifest</DependentUpon>
</Compile>
Expand Down Expand Up @@ -99,6 +103,7 @@
<EmbedInteropTypes>False</EmbedInteropTypes>
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.ComponentModelHost, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Microsoft.VisualStudio.CoreUtility, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.VisualStudio.CoreUtility.14.3.25407\lib\net45\Microsoft.VisualStudio.CoreUtility.dll</HintPath>
<Private>False</Private>
Expand Down
35 changes: 34 additions & 1 deletion src/PrettierPackage.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,48 @@
using Microsoft.VisualStudio.Shell;
using EnvDTE80;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Threading;
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Threading;
using IServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;

namespace JavaScriptPrettier
{
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
[InstalledProductRegistration("#110", "#112", Vsix.Version, IconResourceID = 400)]
[Guid(PackageGuids.guidPrettierPackageString)]
[ProvideMenuResource("Menus.ctmenu", 1)]
[ProvideOptionPage(typeof(OptionPageGrid), "Prettier", "General", 0, 0, true)]
[ProvideAutoLoad(cmdUiContextGuid: VSConstants.UICONTEXT.NotBuildingAndNotDebugging_string, flags: PackageAutoLoadFlags.BackgroundLoad)]
public sealed class PrettierPackage : AsyncPackage
{
internal DTE2 _dte;
internal RunningDocumentTable _runningDocTable;
internal OptionPageGrid optionPage;
internal ServiceProvider _serviceProvider;

protected override async System.Threading.Tasks.Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
await JoinableTaskFactory.SwitchToMainThreadAsync();

_dte = ServiceProvider.GlobalProvider.GetService(typeof(EnvDTE.DTE)) as DTE2;
_serviceProvider = new ServiceProvider((IServiceProvider)_dte);
_runningDocTable = new RunningDocumentTable(_serviceProvider);
_runningDocTable.Advise(new RunningDocTableEventsHandler(this));

optionPage = (OptionPageGrid)GetDialogPage(typeof(OptionPageGrid));

await base.InitializeAsync(cancellationToken, progress);
}
}

public class OptionPageGrid : DialogPage
{
[Category("Prettier")]
[DisplayName("Format On Save")]
[Description("Run Pretter whenever a file is saved")]
public bool FormatOnSave { get; set; }
}
}
82 changes: 82 additions & 0 deletions src/RunningDocTableEventsHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Linq;
using EnvDTE;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.TextManager.Interop;

namespace JavaScriptPrettier
{
internal sealed class RunningDocTableEventsHandler : IVsRunningDocTableEvents3
{
private readonly PrettierPackage _package;

public RunningDocTableEventsHandler(PrettierPackage package)
{
_package = package;
}

public int OnAfterFirstDocumentLock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining) => VSConstants.S_OK;
public int OnBeforeLastDocumentUnlock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining) => VSConstants.S_OK;
public int OnAfterSave(uint docCookie) => VSConstants.S_OK;
public int OnAfterAttributeChange(uint docCookie, uint grfAttribs) => VSConstants.S_OK;
public int OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowFrame pFrame) => VSConstants.S_OK;
public int OnAfterDocumentWindowHide(uint docCookie, IVsWindowFrame pFrame) => VSConstants.S_OK;
public int OnAfterAttributeChangeEx(uint docCookie, uint grfAttribs, IVsHierarchy pHierOld, uint itemidOld, string pszMkDocumentOld, IVsHierarchy pHierNew, uint itemidNew, string pszMkDocumentNew) => VSConstants.S_OK;

public int OnBeforeSave(uint docCookie)
{
if (_package.optionPage.FormatOnSave)
{
RunningDocumentInfo docInfo = _package._runningDocTable.GetDocumentInfo(docCookie);
Document doc = _package._dte.Documents.OfType<Document>().SingleOrDefault(x => x.FullName == docInfo.Moniker);

if (doc != null)
{
IVsTextView vsTextView = GetIVsTextView(doc.FullName);
if (vsTextView == null)
{
return VSConstants.S_OK;
}

IWpfTextView wpfTextView = GetWpfTextView(vsTextView);
if (wpfTextView == null)
{
return VSConstants.S_OK;
}

PrettierCommand cmd = wpfTextView.Properties.GetProperty<PrettierCommand>("prettierCommand");
if (cmd != null)
{
ThreadHelper.JoinableTaskFactory.Run(() => cmd.MakePrettierAsync());
}
}
}
return VSConstants.S_OK;
}

private IVsTextView GetIVsTextView(string filePath)
{
return VsShellUtilities.IsDocumentOpen(_package._serviceProvider, filePath, Guid.Empty, out var uiHierarchy, out uint itemId, out var windowFrame)
? VsShellUtilities.GetTextView(windowFrame) : null;
}

private static IWpfTextView GetWpfTextView(IVsTextView vTextView)
{
IWpfTextView view = null;
IVsUserData userData = (IVsUserData)vTextView;

if (userData != null)
{
Guid guidViewHost = Microsoft.VisualStudio.Editor.DefGuidList.guidIWpfTextViewHost;
userData.GetData(ref guidViewHost, out var holder);
IWpfTextViewHost viewHost = (IWpfTextViewHost)holder;
view = viewHost.TextView;
}

return view;
}
}
}
4 changes: 2 additions & 2 deletions src/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?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="J1da7ad9e-85b3-4a0c-8e45-b2ae59a575a7" Version="2.0" Language="en-US" Publisher="Mads Kristensen" />
<Identity Id="J1da7ad9e-85b3-4a0c-8e45-b2ae59a575a7" Version="2.1" Language="en-US" Publisher="Mads Kristensen" />
<DisplayName>JavaScript Prettier</DisplayName>
<Description xml:space="preserve">Prettier is an opinionated JavaScript formatter inspired by refmt with advanced support for language features from ES2017, JSX, and Flow. It removes all original styling and ensures that all outputted JavaScript conforms to a consistent style.</Description>
<MoreInfo>https://github.com/madskristensen/JavaScriptPrettier</MoreInfo>
<License>Resources\LICENSE</License>
<ReleaseNotes>https://github.com/madskristensen/JavaScriptPrettier/blob/master/CHANGELOG.md</ReleaseNotes>
<Icon>Resources\Icon.png</Icon>
<PreviewImage>Resources\Icon.png</PreviewImage>
<Tags>JavaScript, JSX, EcmaScript, formatting</Tags>
<Tags>JavaScript, JSX, TypeScript, TSX, EcmaScript, formatting</Tags>
</Metadata>
<Installation>
<InstallationTarget Id="Microsoft.VisualStudio.Community" Version="[14.0,17.0)" />
Expand Down