Repository navigation
Expand file tree
/
Copy pathCommandRegistration.cs
More file actions
69 lines (55 loc) · 2.5 KB
/
Copy pathCommandRegistration.cs
File metadata and controls
69 lines (55 loc) · 2.5 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
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Operations;
using Microsoft.VisualStudio.TextManager.Interop;
using Microsoft.VisualStudio.Utilities;
namespace JavaScriptPrettier
{
[Export(typeof(IVsTextViewCreationListener))]
[ContentType("TypeScript")]
[ContentType("JavaScript")]
[TextViewRole(PredefinedTextViewRoles.PrimaryDocument)]
internal sealed class CommandRegistration : IVsTextViewCreationListener
{
[Import]
private IVsEditorAdaptersFactoryService AdaptersFactory { get; set; }
[Import]
private ITextDocumentFactoryService DocumentService { get; set; }
[Import]
private ITextBufferUndoManagerProvider UndoProvider { get; set; }
public async void VsTextViewCreated(IVsTextView textViewAdapter)
{
IWpfTextView view = AdaptersFactory.GetWpfTextView(textViewAdapter);
if (!DocumentService.TryGetTextDocument(view.TextBuffer, out ITextDocument doc))
return;
ITextBufferUndoManager undoManager = UndoProvider.GetTextBufferUndoManager(view.TextBuffer);
NodeProcess node = view.Properties.GetOrCreateSingletonProperty(() => new NodeProcess());
AddCommandFilter(textViewAdapter, new PrettierCommand(view, undoManager, node, doc.Encoding, doc.FilePath));
if (!node.IsReadyToExecute())
{
await Install(node);
}
}
private static async System.Threading.Tasks.Task Install(NodeProcess node)
{
var statusbar = (IVsStatusbar)ServiceProvider.GlobalProvider.GetService(typeof(SVsStatusbar));
statusbar.FreezeOutput(0);
statusbar.SetText($"Installing {NodeProcess.Packages} npm module...");
statusbar.FreezeOutput(1);
bool success = await node.EnsurePackageInstalled();
string status = success ? "Done" : "Failed";
statusbar.FreezeOutput(0);
statusbar.SetText($"Installing {NodeProcess.Packages} npm module... {status}");
statusbar.FreezeOutput(1);
}
private void AddCommandFilter(IVsTextView textViewAdapter, BaseCommand command)
{
textViewAdapter.AddCommandFilter(command, out var next);
command.Next = next;
}
}
}