Esempio n. 1
0
        internal void ProjectSnapshotManager_Changed(object sender, ProjectChangeEventArgs args)
        {
            if (!_lspEditorFeatureDetector.IsLSPEditorAvailable(args.ProjectFilePath, hierarchy: null))
            {
                return;
            }

            switch (args.Kind)
            {
            case ProjectChangeKind.DocumentRemoved:
            case ProjectChangeKind.DocumentAdded:
            case ProjectChangeKind.DocumentChanged:
            case ProjectChangeKind.ProjectChanged:
                // These changes can come in bursts so we don't want to overload the publishing system. Therefore,
                // we enqueue publishes and then publish the latest project after a delay.

                EnqueuePublish(args.Newer);
                break;

            case ProjectChangeKind.ProjectAdded:
                Publish(args.Newer);
                break;

            case ProjectChangeKind.ProjectRemoved:
                RemovePublishingData(args.Older);
                break;
            }
        }
        public bool TryGetContentTypeForFilePath(string filePath, [NotNullWhen(true)] out IContentType?contentType)
        {
            if (_lspEditorFeatureDetector.IsLSPEditorAvailable(filePath, hierarchy: null))
            {
                contentType = _contentTypeRegistryService.GetContentType(RazorConstants.RazorLSPContentTypeName);
                return(true);
            }

            contentType = null;
            return(false);
        }
        // Internal for testing
        internal bool IsRazorLSPTextDocument(ITextDocument textDocument)
        {
            var filePath = textDocument.FilePath;

            if (filePath == null)
            {
                return(false);
            }

            if (!IsRazorFilePath(filePath))
            {
                return(false);
            }

            // We pass a `null` hierarchy so we don't eagerly lookup hierarchy information before it's needed.
            if (!_lspEditorFeatureDetector.IsLSPEditorAvailable(textDocument.FilePath, hierarchy: null))
            {
                return(false);
            }

            return(true);
        }
Esempio n. 4
0
        internal void ProjectSnapshotManager_Changed(object sender, ProjectChangeEventArgs args)
        {
            if (!_lspEditorFeatureDetector.IsLSPEditorAvailable(args.ProjectFilePath, hierarchy: null))
            {
                return;
            }

            // All the below Publish's (except ProjectRemoved) wait until our project has been initialized (ProjectWorkspaceState != null)
            // so that we don't publish half-finished projects, which can cause things like Semantic coloring to "flash"
            // when they update repeatedly as they load.
            switch (args.Kind)
            {
            case ProjectChangeKind.DocumentRemoved:
            case ProjectChangeKind.DocumentAdded:
            case ProjectChangeKind.ProjectChanged:

                if (args.Newer.ProjectWorkspaceState != null)
                {
                    // These changes can come in bursts so we don't want to overload the publishing system. Therefore,
                    // we enqueue publishes and then publish the latest project after a delay.
                    EnqueuePublish(args.Newer);
                }
                break;

            case ProjectChangeKind.ProjectAdded:

                if (args.Newer.ProjectWorkspaceState != null)
                {
                    Publish(args.Newer);
                }
                break;

            case ProjectChangeKind.ProjectRemoved:
                RemovePublishingData(args.Older);
                break;
            }
        }
        public override int CreateEditorInstance(
            uint createDocFlags,
            string moniker,
            string physicalView,
            IVsHierarchy hierarchy,
            uint itemid,
            IntPtr existingDocData,
            out IntPtr docView,
            out IntPtr docData,
            out string editorCaption,
            out Guid cmdUI,
            out int cancelled)
        {
            if (!_lspEditorFeatureDetector.IsLSPEditorAvailable(moniker, hierarchy))
            {
                docView       = default;
                docData       = default;
                editorCaption = null;
                cmdUI         = default;
                cancelled     = 0;

                // Razor LSP is not enabled, allow another editor to handle this document
                return(VSConstants.VS_E_UNSUPPORTEDFORMAT);
            }

            var editorInstance = base.CreateEditorInstance(createDocFlags, moniker, physicalView, hierarchy, itemid, existingDocData, out docView, out docData, out editorCaption, out cmdUI, out cancelled);
            var textLines      = (IVsTextLines)Marshal.GetObjectForIUnknown(docData);

            // Next, the editor typically resets the ContentType after TextBuffer creation. We need to let them know
            // to not update the content type because we'll be taking care of the ContentType changing lifecycle.
            var userData = textLines as IVsUserData;
            var hresult  = userData.SetData(VSConstants.VsTextBufferUserDataGuid.VsBufferDetectLangSID_guid, false);

            ErrorHandler.ThrowOnFailure(hresult);

            return(editorInstance);
        }
Esempio n. 6
0
        internal void ProjectSnapshotManager_Changed(object sender, ProjectChangeEventArgs args)
        {
            if (!_lspEditorFeatureDetector.IsLSPEditorAvailable(args.ProjectFilePath, hierarchy: null))
            {
                return;
            }

            // Prior to doing any sort of project state serialization/publishing we avoid any excess work as long as there aren't any "open" Razor files.
            // However, once a Razor file is opened we turn the firehose on and start doing work.
            // By taking this lazy approach we ensure that we don't do any excess Razor work (serialization is expensive) in non-Razor scenarios.

            if (!_active)
            {
                // Not currently active, we need to decide if we should become active or if we should no-op.

                if (_projectSnapshotManager.OpenDocuments.Count > 0)
                {
                    // A Razor document was just opened, we should become "active" which means we'll constantly be monitoring project state.
                    _active = true;

                    if (args.Newer?.ProjectWorkspaceState != null)
                    {
                        // Typically document open events don't result in us re-processing project state; however, given this is the first time a user opened a Razor document we should.
                        // Don't enqueue, just publish to get the most immediate result.
                        Publish(args.Newer);
                        return;
                    }
                }
                else
                {
                    // No open documents and not active. No-op.
                    return;
                }
            }

            // All the below Publish's (except ProjectRemoved) wait until our project has been initialized (ProjectWorkspaceState != null)
            // so that we don't publish half-finished projects, which can cause things like Semantic coloring to "flash"
            // when they update repeatedly as they load.
            switch (args.Kind)
            {
            case ProjectChangeKind.DocumentRemoved:
            case ProjectChangeKind.DocumentAdded:
            case ProjectChangeKind.ProjectChanged:

                if (args.Newer.ProjectWorkspaceState != null)
                {
                    // These changes can come in bursts so we don't want to overload the publishing system. Therefore,
                    // we enqueue publishes and then publish the latest project after a delay.
                    EnqueuePublish(args.Newer);
                }
                break;

            case ProjectChangeKind.ProjectAdded:

                if (args.Newer.ProjectWorkspaceState != null)
                {
                    Publish(args.Newer);
                }
                break;

            case ProjectChangeKind.ProjectRemoved:
                RemovePublishingData(args.Older);
                break;
            }
        }