public override VisualStudioDocumentTracker Create(ITextBuffer textBuffer)
        {
            if (textBuffer == null)
            {
                throw new ArgumentNullException(nameof(textBuffer));
            }

            if (!_textDocumentFactory.TryGetTextDocument(textBuffer, out var textDocument))
            {
                Debug.Fail("Text document should be available from the text buffer.");
                return(null);
            }

            var filePath = textDocument.FilePath;
            var project  = _projectService.GetHostProject(textBuffer);

            if (project == null)
            {
                Debug.Fail("Text buffer should belong to a project.");
                return(null);
            }

            var projectPath = _projectService.GetProjectPath(project);

            var tracker = new DefaultVisualStudioDocumentTracker(filePath, projectPath, _projectManager, _editorSettingsManager, _workspace, textBuffer, _importDocumentManager);

            return(tracker);
        }
Esempio n. 2
0
        public void Subscribe()
        {
            // Fundamentally we have a Razor half of the world as as soon as the document is open - and then later
            // the C# half of the world will be initialized. This code is in general pretty tolerant of
            // unexpected /impossible states.
            //
            // We also want to successfully shut down if the buffer is something other than .cshtml.
            IVsHierarchy hierarchy          = null;
            string       projectPath        = null;
            var          isSupportedProject = false;

            if (_textBuffer.ContentType.IsOfType(RazorLanguage.ContentType) &&

                // We expect the document to have a hierarchy even if it's not a real 'project'.
                // However the hierarchy can be null when the document is in the process of closing.
                (hierarchy = _projectService.GetHierarchy(_textBuffer)) != null)
            {
                projectPath        = _projectService.GetProjectPath(hierarchy);
                isSupportedProject = _projectService.IsSupportedProject(hierarchy);
            }

            if (!isSupportedProject || projectPath == null)
            {
                return;
            }

            _isSupportedProject      = isSupportedProject;
            _projectPath             = projectPath;
            _project                 = _projectManager.GetProjectWithFilePath(projectPath);
            _projectManager.Changed += ProjectManager_Changed;

            OnContextChanged(_project);
        }