|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using EnvDTE; |
| 4 | +using Microsoft.VisualStudio; |
| 5 | +using Microsoft.VisualStudio.Shell; |
| 6 | +using Microsoft.VisualStudio.Shell.Interop; |
| 7 | +using Microsoft.VisualStudio.Text.Editor; |
| 8 | +using Microsoft.VisualStudio.TextManager.Interop; |
| 9 | + |
| 10 | +namespace JavaScriptPrettier |
| 11 | +{ |
| 12 | + internal sealed class RunningDocTableEventsHandler : IVsRunningDocTableEvents3 |
| 13 | + { |
| 14 | + private readonly PrettierPackage _package; |
| 15 | + |
| 16 | + public RunningDocTableEventsHandler(PrettierPackage package) |
| 17 | + { |
| 18 | + _package = package; |
| 19 | + } |
| 20 | + |
| 21 | + public int OnAfterFirstDocumentLock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining) => VSConstants.S_OK; |
| 22 | + public int OnBeforeLastDocumentUnlock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining) => VSConstants.S_OK; |
| 23 | + public int OnAfterSave(uint docCookie) => VSConstants.S_OK; |
| 24 | + public int OnAfterAttributeChange(uint docCookie, uint grfAttribs) => VSConstants.S_OK; |
| 25 | + public int OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowFrame pFrame) => VSConstants.S_OK; |
| 26 | + public int OnAfterDocumentWindowHide(uint docCookie, IVsWindowFrame pFrame) => VSConstants.S_OK; |
| 27 | + public int OnAfterAttributeChangeEx(uint docCookie, uint grfAttribs, IVsHierarchy pHierOld, uint itemidOld, string pszMkDocumentOld, IVsHierarchy pHierNew, uint itemidNew, string pszMkDocumentNew) => VSConstants.S_OK; |
| 28 | + |
| 29 | + public int OnBeforeSave(uint docCookie) |
| 30 | + { |
| 31 | + if (_package.optionPage.FormatOnSave) |
| 32 | + { |
| 33 | + RunningDocumentInfo docInfo = _package._runningDocTable.GetDocumentInfo(docCookie); |
| 34 | + Document doc = _package._dte.Documents.OfType<Document>().SingleOrDefault(x => x.FullName == docInfo.Moniker); |
| 35 | + |
| 36 | + if (doc != null) |
| 37 | + { |
| 38 | + IVsTextView vsTextView = GetIVsTextView(doc.FullName); |
| 39 | + if (vsTextView == null) |
| 40 | + { |
| 41 | + return VSConstants.S_OK; |
| 42 | + } |
| 43 | + |
| 44 | + IWpfTextView wpfTextView = GetWpfTextView(vsTextView); |
| 45 | + if (wpfTextView == null) |
| 46 | + { |
| 47 | + return VSConstants.S_OK; |
| 48 | + } |
| 49 | + |
| 50 | + PrettierCommand cmd = wpfTextView.Properties.GetProperty<PrettierCommand>("prettierCommand"); |
| 51 | + if (cmd != null) |
| 52 | + { |
| 53 | + ThreadHelper.JoinableTaskFactory.Run(() => cmd.MakePrettierAsync()); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + return VSConstants.S_OK; |
| 58 | + } |
| 59 | + |
| 60 | + private IVsTextView GetIVsTextView(string filePath) |
| 61 | + { |
| 62 | + return VsShellUtilities.IsDocumentOpen(_package._serviceProvider, filePath, Guid.Empty, out var uiHierarchy, out uint itemId, out var windowFrame) |
| 63 | + ? VsShellUtilities.GetTextView(windowFrame) : null; |
| 64 | + } |
| 65 | + |
| 66 | + private static IWpfTextView GetWpfTextView(IVsTextView vTextView) |
| 67 | + { |
| 68 | + IWpfTextView view = null; |
| 69 | + IVsUserData userData = (IVsUserData)vTextView; |
| 70 | + |
| 71 | + if (userData != null) |
| 72 | + { |
| 73 | + Guid guidViewHost = Microsoft.VisualStudio.Editor.DefGuidList.guidIWpfTextViewHost; |
| 74 | + userData.GetData(ref guidViewHost, out var holder); |
| 75 | + IWpfTextViewHost viewHost = (IWpfTextViewHost)holder; |
| 76 | + view = viewHost.TextView; |
| 77 | + } |
| 78 | + |
| 79 | + return view; |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments