Exemplo n.º 1
0
        public static ITextBuffer GetBufferForDocument(string filename, bool openDocument, string contentType)
        {
            if (openDocument)
            {
                IVsTextView    viewAdapter;
                IVsWindowFrame frame;
                OpenDocument(filename, out viewAdapter, out frame);

                IVsTextLines lines;
                ErrorHandler.ThrowOnFailure(viewAdapter.GetBuffer(out lines));

                var adapter    = ComponentModel.GetService <IVsEditorAdaptersFactoryService>();
                var textBuffer = adapter.GetDocumentBuffer(lines);
                if (!BufferDictionary.ContainsKey(filename))
                {
                    BufferDictionary.Add(filename, new TextBufferOpenStatus {
                        Buffer = textBuffer, IsOpen = true
                    });
                }
                else
                {
                    BufferDictionary[filename].IsOpen = true;
                }
                return(textBuffer);
            }
            else
            {
                TextBufferOpenStatus bufferOpen;
                if (!BufferDictionary.TryGetValue(filename, out bufferOpen))
                {
                    ITextDocumentFactoryService documentFactory = ComponentModel.GetService <ITextDocumentFactoryService>();
                    IContentTypeRegistryService contentRegistry = ComponentModel.GetService <IContentTypeRegistryService>();
                    ITextDocument textDoc = documentFactory.CreateAndLoadTextDocument(filename, contentRegistry.GetContentType(contentType));
                    if (!BufferDictionary.ContainsKey(filename))
                    {
                        BufferDictionary.Add(filename, new TextBufferOpenStatus {
                            Buffer = textDoc.TextBuffer, IsOpen = false
                        });
                    }
                    return(textDoc.TextBuffer);
                }
                return(bufferOpen.Buffer);
            }
        }
Exemplo n.º 2
0
        public IDocument GetOrCreateDocument(string path, bool observe)
        {
            if (_documents.TryGetValue(path, out var document))
            {
                return(document.Value);
            }

            if (!System.IO.File.Exists(path))
            {
                return(null);
            }

            var contentType = _contentTypeManager.Value.DetermineContentType(path);

            if (contentType == null)
            {
                return(null);
            }

            var textDocument = _textDocumentFactoryService.CreateAndLoadTextDocument(path, contentType);

            return(GetOrCreateDocument(textDocument, observe));
        }