|
| 1 | +using System; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | +using Microsoft.Internal.VisualStudio.Shell.Interop; |
| 4 | +using Microsoft.VisualStudio.ComponentModelHost; |
| 5 | +using Microsoft.VisualStudio.Editor; |
| 6 | +using Microsoft.VisualStudio.OLE.Interop; |
| 7 | +using Microsoft.VisualStudio.Package; |
| 8 | +using Microsoft.VisualStudio.Shell; |
| 9 | +using Microsoft.VisualStudio.Shell.Interop; |
| 10 | +using Microsoft.VisualStudio.TextManager.Interop; |
| 11 | +using Microsoft.VisualStudio.Utilities; |
| 12 | + |
| 13 | +namespace Microsoft.VisualStudio.LanguageServerClient.Razor |
| 14 | +{ |
| 15 | + [Guid(EditorFactoryGuidString)] |
| 16 | + public class RazorEditorFactory : EditorFactory |
| 17 | + { |
| 18 | + private const string EditorFactoryGuidString = "3dfdce9e-1799-4372-8aa6-d8e65182fdfc"; |
| 19 | + private const string RazorLSPEditorFeatureFlag = "Razor.LSP.Editor"; |
| 20 | + private readonly Lazy<IVsEditorAdaptersFactoryService> _adaptersFactory; |
| 21 | + private readonly Lazy<IContentType> _razorLSPContentType; |
| 22 | + private readonly Lazy<IVsFeatureFlags> _featureFlags; |
| 23 | + |
| 24 | + public RazorEditorFactory(AsyncPackage package) : base(package) |
| 25 | + { |
| 26 | + _adaptersFactory = new Lazy<IVsEditorAdaptersFactoryService>(() => |
| 27 | + { |
| 28 | + var componentModel = (IComponentModel)AsyncPackage.GetGlobalService(typeof(SComponentModel)); |
| 29 | + var adaptersFactory = componentModel.GetService<IVsEditorAdaptersFactoryService>(); |
| 30 | + return adaptersFactory; |
| 31 | + }); |
| 32 | + |
| 33 | + _razorLSPContentType = new Lazy<IContentType>(() => |
| 34 | + { |
| 35 | + var componentModel = (IComponentModel)AsyncPackage.GetGlobalService(typeof(SComponentModel)); |
| 36 | + var contentTypeService = componentModel.GetService<IContentTypeRegistryService>(); |
| 37 | + var contentType = contentTypeService.GetContentType(RazorLSPContentTypeDefinition.Name); |
| 38 | + return contentType; |
| 39 | + }); |
| 40 | + |
| 41 | + _featureFlags = new Lazy<IVsFeatureFlags>(() => |
| 42 | + { |
| 43 | + var featureFlags = (IVsFeatureFlags)AsyncPackage.GetGlobalService(typeof(SVsFeatureFlags)); |
| 44 | + return featureFlags; |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + private bool IsLSPRazorEditorEnabled |
| 49 | + { |
| 50 | + get |
| 51 | + { |
| 52 | + var lspRazorEnabledString = Environment.GetEnvironmentVariable(RazorLSPEditorFeatureFlag); |
| 53 | + bool.TryParse(lspRazorEnabledString, out var enabled); |
| 54 | + if (enabled) |
| 55 | + { |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + enabled = _featureFlags.Value.IsFeatureEnabled(RazorLSPEditorFeatureFlag, defaultValue: false); |
| 60 | + return enabled; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public override int CreateEditorInstance( |
| 65 | + uint createDocFlags, |
| 66 | + string moniker, |
| 67 | + string physicalView, |
| 68 | + IVsHierarchy pHier, |
| 69 | + uint itemid, |
| 70 | + IntPtr existingDocData, |
| 71 | + out IntPtr docView, |
| 72 | + out IntPtr docData, |
| 73 | + out string editorCaption, |
| 74 | + out Guid cmdUI, |
| 75 | + out int cancelled) |
| 76 | + { |
| 77 | + if (!IsLSPRazorEditorEnabled) |
| 78 | + { |
| 79 | + docView = default; |
| 80 | + docData = default; |
| 81 | + editorCaption = null; |
| 82 | + cmdUI = default; |
| 83 | + cancelled = 0; |
| 84 | + |
| 85 | + // Razor LSP is not enabled, allow another editor to handle this document |
| 86 | + return VSConstants.VS_E_UNSUPPORTEDFORMAT; |
| 87 | + } |
| 88 | + |
| 89 | + var editorInstance = base.CreateEditorInstance(createDocFlags, moniker, physicalView, pHier, itemid, existingDocData, out docView, out docData, out editorCaption, out cmdUI, out cancelled); |
| 90 | + var textLines = (IVsTextLines)Marshal.GetObjectForIUnknown(docData); |
| 91 | + |
| 92 | + SetTextBufferContentType(textLines); |
| 93 | + |
| 94 | + return editorInstance; |
| 95 | + } |
| 96 | + |
| 97 | + private void SetTextBufferContentType(IVsTextLines textLines) |
| 98 | + { |
| 99 | + var textBufferDataEventsGuid = typeof(IVsTextBufferDataEvents).GUID; |
| 100 | + var connectionPointContainer = textLines as IConnectionPointContainer; |
| 101 | + connectionPointContainer.FindConnectionPoint(textBufferDataEventsGuid, out var connectionPoint); |
| 102 | + var contentTypeSetter = new TextBufferContentTypeSetter( |
| 103 | + textLines, |
| 104 | + _adaptersFactory.Value, |
| 105 | + _razorLSPContentType.Value); |
| 106 | + contentTypeSetter.Attach(connectionPoint); |
| 107 | + |
| 108 | + // Next, the editor typically resets the ContentType after TextBuffer creation. We need to let them know |
| 109 | + // to not update the content type. |
| 110 | + var userData = textLines as IVsUserData; |
| 111 | + var hresult = userData.SetData(VSConstants.VsTextBufferUserDataGuid.VsBufferDetectLangSID_guid, false); |
| 112 | + |
| 113 | + ErrorHandler.ThrowOnFailure(hresult); |
| 114 | + } |
| 115 | + |
| 116 | + private class TextBufferContentTypeSetter : IVsTextBufferDataEvents |
| 117 | + { |
| 118 | + private readonly IVsTextLines _textLines; |
| 119 | + private readonly IVsEditorAdaptersFactoryService _adaptersFactory; |
| 120 | + private readonly IContentType _razorLSPContentType; |
| 121 | + private IConnectionPoint _connectionPoint; |
| 122 | + private uint _connectionPointCookie; |
| 123 | + |
| 124 | + public TextBufferContentTypeSetter( |
| 125 | + IVsTextLines textLines, |
| 126 | + IVsEditorAdaptersFactoryService adaptersFactory, |
| 127 | + IContentType razorLSPContentType) |
| 128 | + { |
| 129 | + _textLines = textLines; |
| 130 | + _adaptersFactory = adaptersFactory; |
| 131 | + _razorLSPContentType = razorLSPContentType; |
| 132 | + } |
| 133 | + |
| 134 | + public void Attach(IConnectionPoint connectionPoint) |
| 135 | + { |
| 136 | + if (connectionPoint is null) |
| 137 | + { |
| 138 | + throw new ArgumentNullException(nameof(connectionPoint)); |
| 139 | + } |
| 140 | + |
| 141 | + _connectionPoint = connectionPoint; |
| 142 | + |
| 143 | + connectionPoint.Advise(this, out _connectionPointCookie); |
| 144 | + } |
| 145 | + |
| 146 | + public void OnFileChanged(uint grfChange, uint dwFileAttrs) |
| 147 | + { |
| 148 | + } |
| 149 | + |
| 150 | + public int OnLoadCompleted(int fReload) |
| 151 | + { |
| 152 | + try |
| 153 | + { |
| 154 | + var diskBuffer = _adaptersFactory.GetDocumentBuffer(_textLines); |
| 155 | + diskBuffer.ChangeContentType(_razorLSPContentType, editTag: null); |
| 156 | + } |
| 157 | + finally |
| 158 | + { |
| 159 | + _connectionPoint.Unadvise(_connectionPointCookie); |
| 160 | + } |
| 161 | + |
| 162 | + return VSConstants.S_OK; |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments