2 using Microsoft
.VisualStudio
.Editor
;
3 using Microsoft
.VisualStudio
.Shell
;
4 using Microsoft
.VisualStudio
.Shell
.Interop
;
5 using Microsoft
.VisualStudio
.Text
;
6 using Microsoft
.VisualStudio
.Text
.Editor
;
7 using Microsoft
.VisualStudio
.TextManager
.Interop
;
11 namespace LLVM
.ClangFormat
13 internal sealed class Vsix
16 /// Returns the currently active view if it is a IWpfTextView.
18 public static IWpfTextView
GetCurrentView()
20 // The SVsTextManager is a service through which we can get the active view.
21 var textManager
= (IVsTextManager
)Package
.GetGlobalService(typeof(SVsTextManager
));
23 textManager
.GetActiveView(1, null, out textView
);
25 // Now we have the active view as IVsTextView, but the text interfaces we need
26 // are in the IWpfTextView.
27 return VsToWpfTextView(textView
);
30 public static bool IsDocumentDirty(Document document
)
32 var textView
= GetDocumentView(document
);
33 var textDocument
= GetTextDocument(textView
);
34 return textDocument
?.IsDirty
== true;
37 public static IWpfTextView
GetDocumentView(Document document
)
39 var textView
= GetVsTextViewFrompPath(document
.FullName
);
40 return VsToWpfTextView(textView
);
43 public static IWpfTextView
VsToWpfTextView(IVsTextView textView
)
45 var userData
= (IVsUserData
)textView
;
48 Guid guidWpfViewHost
= DefGuidList
.guidIWpfTextViewHost
;
50 userData
.GetData(ref guidWpfViewHost
, out host
);
51 return ((IWpfTextViewHost
)host
).TextView
;
54 public static IVsTextView
GetVsTextViewFrompPath(string filePath
)
56 // From http://stackoverflow.com/a/2427368/4039972
57 var dte2
= (EnvDTE80
.DTE2
)Package
.GetGlobalService(typeof(SDTE
));
58 var sp
= (Microsoft
.VisualStudio
.OLE
.Interop
.IServiceProvider
)dte2
;
59 var serviceProvider
= new Microsoft
.VisualStudio
.Shell
.ServiceProvider(sp
);
61 IVsUIHierarchy uiHierarchy
;
63 IVsWindowFrame windowFrame
;
64 if (VsShellUtilities
.IsDocumentOpen(serviceProvider
, filePath
, Guid
.Empty
,
65 out uiHierarchy
, out itemID
, out windowFrame
))
67 // Get the IVsTextView from the windowFrame.
68 return VsShellUtilities
.GetTextView(windowFrame
);
73 public static ITextDocument
GetTextDocument(IWpfTextView view
)
75 ITextDocument document
;
76 if (view
!= null && view
.TextBuffer
.Properties
.TryGetProperty(typeof(ITextDocument
), out document
))
81 public static string GetDocumentParent(IWpfTextView view
)
83 ITextDocument document
= GetTextDocument(view
);
86 return Directory
.GetParent(document
.FilePath
).ToString();
91 public static string GetDocumentPath(IWpfTextView view
)
93 return GetTextDocument(view
)?.FilePath
;