Repository navigation
Expand file tree
/
Copy pathPrettierCommand.cs
More file actions
134 lines (109 loc) · 4.97 KB
/
Copy pathPrettierCommand.cs
File metadata and controls
134 lines (109 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Operations;
namespace JavaScriptPrettier
{
internal sealed class PrettierCommand : BaseCommand
{
private Guid _commandGroup = PackageGuids.guidPrettierPackageCmdSet;
private const uint _commandId = PackageIds.PrettierCommandId;
private IWpfTextView _view;
private ITextBufferUndoManager _undoManager;
private NodeProcess _node;
private readonly Encoding _encoding;
private readonly string _filePath;
public PrettierCommand(IWpfTextView view, ITextBufferUndoManager undoManager, Encoding encoding, string filePath)
{
_view = view;
_undoManager = undoManager;
_encoding = encoding;
_filePath = filePath;
_node = PrettierPackage._node;
}
public override int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
{
ThreadHelper.ThrowIfNotOnUIThread();
if (pguidCmdGroup == _commandGroup && nCmdID == _commandId)
{
if (_node != null && _node.IsReadyToExecute())
{
ThreadHelper.JoinableTaskFactory.RunAsync(MakePrettierAsync);
}
return VSConstants.S_OK;
}
return Next.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
}
public async Task<bool> MakePrettierAsync()
{
string input = _view.TextBuffer.CurrentSnapshot.GetText();
string output = await _node.ExecuteProcessAsync(input, _encoding, _filePath);
VirtualSnapshotPoint snapshotPoint = _view.Selection.ActivePoint;
if (string.IsNullOrEmpty(output) || input == output)
return false;
using (ITextEdit edit = _view.TextBuffer.CreateEdit())
using (ITextUndoTransaction undo = _undoManager.TextBufferUndoHistory.CreateTransaction("Make Prettier"))
{
edit.Replace(0, _view.TextBuffer.CurrentSnapshot.Length, output);
edit.Apply();
undo.Complete();
}
var currSnapShot = _view.TextBuffer.CurrentSnapshot;
var newSnapshotPoint = new SnapshotPoint(currSnapShot, Math.Min(snapshotPoint.Position.Position, currSnapShot.Length));
_view.Caret.MoveTo(newSnapshotPoint);
_view.ViewScroller.EnsureSpanVisible(new SnapshotSpan(newSnapshotPoint, 0));
return true;
}
public override int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText)
{
ThreadHelper.ThrowIfNotOnUIThread();
if (pguidCmdGroup == _commandGroup && prgCmds[0].cmdID == _commandId)
{
if (_node != null)
{
if (_node.IsReadyToExecute())
{
SetText(pCmdText, "Make Prettier");
prgCmds[0].cmdf = (uint)OLECMDF.OLECMDF_ENABLED | (uint)OLECMDF.OLECMDF_SUPPORTED;
}
else
{
SetText(pCmdText, "Make Prettier (installing npm modules...)");
prgCmds[0].cmdf = (uint)OLECMDF.OLECMDF_SUPPORTED;
}
}
return VSConstants.S_OK;
}
return Next.QueryStatus(pguidCmdGroup, cCmds, prgCmds, pCmdText);
}
private static void SetText(IntPtr pCmdTextInt, string text)
{
try
{
var pCmdText = (OLECMDTEXT)Marshal.PtrToStructure(pCmdTextInt, typeof(OLECMDTEXT));
char[] menuText = text.ToCharArray();
// Get the offset to the rgsz param. This is where we will stuff our text
IntPtr offset = Marshal.OffsetOf(typeof(OLECMDTEXT), "rgwz");
IntPtr offsetToCwActual = Marshal.OffsetOf(typeof(OLECMDTEXT), "cwActual");
// The max chars we copy is our string, or one less than the buffer size,
// since we need a null at the end.
int maxChars = Math.Min((int)pCmdText.cwBuf - 1, menuText.Length);
Marshal.Copy(menuText, 0, (IntPtr)((long)pCmdTextInt + (long)offset), maxChars);
// append a null character
Marshal.WriteInt16((IntPtr)((long)pCmdTextInt + (long)offset + maxChars * 2), 0);
// write out the length +1 for the null char
Marshal.WriteInt32((IntPtr)((long)pCmdTextInt + (long)offsetToCwActual), maxChars + 1);
}
catch (Exception ex)
{
Logger.Log(ex);
}
}
}
}