Exemplo n.º 1
0
        public override bool TryCreateFor(ITextBuffer hostDocumentBuffer, out VirtualDocument virtualDocument)
        {
            if (hostDocumentBuffer is null)
            {
                throw new ArgumentNullException(nameof(hostDocumentBuffer));
            }

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

            var hostDocumentUri = _fileUriProvider.GetOrCreate(hostDocumentBuffer);

            // Index.cshtml => Index.cshtml__virtual.html
            var virtualHtmlFilePath = hostDocumentUri.GetAbsoluteOrUNCPath() + VirtualHtmlFileNameSuffix;
            var virtualHtmlUri      = new Uri(virtualHtmlFilePath);

            var htmlBuffer = _textBufferFactory.CreateTextBuffer();

            _fileUriProvider.AddOrUpdate(htmlBuffer, virtualHtmlUri);
            htmlBuffer.Properties.AddProperty(ContainedLanguageMarker, true);

            // Create a text document to trigger the Html language server initialization.
            _textDocumentFactory.CreateTextDocument(htmlBuffer, virtualHtmlFilePath);

            htmlBuffer.ChangeContentType(HtmlLSPContentType, editTag: null);

            virtualDocument = new HtmlVirtualDocument(virtualHtmlUri, htmlBuffer);
            return(true);
        }
Exemplo n.º 2
0
        private void CreateTextViewHost(string text, string filePath)
        {
            ITextDataModel textDataModel;

            text = text ?? string.Empty;

            var diskBuffer = _textBufferFactoryService.CreateTextBuffer(text, ContentType);
            var cs         = _services.GetService <ICompositionService>();

            _editorViewModel = EditorViewModelFactory.CreateEditorViewModel(diskBuffer, _services);

            if (_editorViewModel != null)
            {
                textDataModel = new TextDataModel(diskBuffer, _editorViewModel.ViewBuffer.As <ITextBuffer>());
            }
            else
            {
                textDataModel = new TextDataModel(diskBuffer, diskBuffer);
            }

            var textBuffer = textDataModel.DocumentBuffer;

            TextDocument = _textDocumentFactoryService.CreateTextDocument(textBuffer, filePath);

            SetGlobalEditorOptions();
            var textView = _textEditorFactoryService.CreateTextView(textDataModel,
                                                                    new DefaultTextViewRoleSet(),
                                                                    GlobalOptions);

            _wpftextViewHost = _textEditorFactoryService.CreateTextViewHost(textView, true);

            ApplyDefaultSettings();
            Control.Content = _wpftextViewHost.HostControl;

            var baseController = new BaseController();

            BaseController = baseController;

            if (_editorViewModel != null)
            {
                CommandTarget = _editorViewModel.GetCommandTarget(textView.ToEditorView());
                var controller = CommandTarget as Common.Core.UI.Commands.Controller;
                Debug.Assert(controller != null);
                controller.ChainedController = baseController;
            }
            else
            {
                CommandTarget = baseController;
            }

            baseController.Initialize(textView, EditorOperations, UndoManager, _services);
        }
Exemplo n.º 3
0
        private void CreateTextViewHost(string text, string filePath)
        {
            text = text ?? string.Empty;

            var diskBuffer = _textBufferFactoryService.CreateTextBuffer(text, ContentType);
            var cs         = _coreShell.GetService <ICompositionService>();

            _editorIntance = EditorInstanceFactory.CreateEditorInstance(diskBuffer, cs);

            ITextDataModel textDataModel;

            if (_editorIntance != null)
            {
                textDataModel = new TextDataModel(diskBuffer, _editorIntance.ViewBuffer);
            }
            else
            {
                textDataModel = new TextDataModel(diskBuffer, diskBuffer);
            }

            var textBuffer = textDataModel.DocumentBuffer;

            TextDocument = _textDocumentFactoryService.CreateTextDocument(textBuffer, filePath);

            SetGlobalEditorOptions();
            var textView = _textEditorFactoryService.CreateTextView(textDataModel,
                                                                    new DefaultTextViewRoleSet(),
                                                                    GlobalOptions);

            _wpftextViewHost = _textEditorFactoryService.CreateTextViewHost(textView, true);

            ApplyDefaultSettings();
            Control.Content = _wpftextViewHost.HostControl;

            var baseController = new BaseController();

            BaseController = baseController;

            if (_editorIntance != null)
            {
                CommandTarget = _editorIntance.GetCommandTarget(textView);
                var controller = CommandTarget as Microsoft.Languages.Editor.Controller.Controller;
                controller.ChainedController = baseController;
            }
            else
            {
                CommandTarget = baseController;
            }

            baseController.Initialize(textView, EditorOperations, UndoManager, _coreShell);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Create an IVsTextBuffer instance and populate it with the existing ITextBuffer value
        /// </summary>
        private IVsTextBuffer CreateVsTextBuffer(ITextBuffer textBuffer, string name)
        {
            // Wrap the result's ITextBuffer so that it can be used with the old VS APIs
            var vsTextBuffer = _vsEditorAdaptersFactoryService.CreateVsTextBufferAdapter(_oleServiceProvider, textBuffer.ContentType);
            var textDocument = _textDocumentFactoryService.CreateTextDocument(textBuffer, name);

            // HACK OF EVIL HACKS: Since there is currently no way to create a text buffer shim
            // for an ITextBuffer that we have already created, we have to create an empty shim,
            // and initialize its underlying ITextBuffer ourselves via reflection
            var vsTextBufferType = vsTextBuffer.GetType();

            vsTextBufferType.GetField("_documentTextBuffer", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(vsTextBuffer, textBuffer);
            vsTextBufferType.GetField("_surfaceTextBuffer", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(vsTextBuffer, textBuffer);
            vsTextBufferType.GetField("_textDocument", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(vsTextBuffer, textDocument);
            vsTextBufferType.GetMethod("InitializeDocumentTextBuffer", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(vsTextBuffer, null);

            return(vsTextBuffer);
        }
        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);
        }