public override bool TryCreateFor(ITextBuffer hostDocumentBuffer, out VirtualDocument virtualDocument)
        {
            if (hostDocumentBuffer is null)
            {
                throw new ArgumentNullException(nameof(hostDocumentBuffer));
            }

            if (!hostDocumentBuffer.ContentType.IsOfType(HostDocumentContentTypeName))
            {
                // Another content type we don't care about.
                virtualDocument = null;
                return(false);
            }

            var hostDocumentUri = _fileUriProvider.GetOrCreate(hostDocumentBuffer);

            // E.g. Index.cshtml => Index.cshtml__virtual.html (for html), similar for other languages
            var virtualLanguageFilePath = hostDocumentUri.GetAbsoluteOrUNCPath() + LanguageFileNameSuffix;
            var virtualLanguageUri      = new Uri(virtualLanguageFilePath);

            var languageBuffer = _textBufferFactory.CreateTextBuffer();

            _fileUriProvider.AddOrUpdate(languageBuffer, virtualLanguageUri);

            // This ensures that LiveShare does not serialize this virtual document to disk in LiveShare & Codespaces scenarios.
            languageBuffer.Properties.AddProperty(ContainedLanguageMarker, true);

            if (!(LanguageBufferProperties is null))
            {
                foreach (var keyValuePair in LanguageBufferProperties)
                {
                    languageBuffer.Properties.AddProperty(keyValuePair.Key, keyValuePair.Value);
                }
            }

            // Create a text document to trigger language server initialization for the contained language.
            _textDocumentFactory.CreateTextDocument(languageBuffer, virtualLanguageFilePath);

            languageBuffer.ChangeContentType(LanguageContentType, editTag: null);

            virtualDocument = CreateVirtualDocument(virtualLanguageUri, languageBuffer);
            return(true);
        }
Ejemplo n.º 2
0
 public abstract bool TryCreateFor(ITextBuffer hostDocumentBuffer, out VirtualDocument virtualDocument);