public ExpansionClient(ITextView textView, IVsEditorAdaptersFactoryService adapterFactory, IServiceProvider serviceProvider) {
     _textView = textView;
     _serviceProvider = serviceProvider;
     _view = adapterFactory.GetViewAdapter(textView);
     _lines = (IVsTextLines)adapterFactory.GetBufferAdapter(textView.TextBuffer);
     _expansion = _lines as IVsExpansion;
     if (_expansion == null) {
         throw new ArgumentException("TextBuffer does not support expansions");
     }
 }
예제 #2
0
        public virtual bool TryInsertExpansion(int startPositionInSubjectBuffer, int endPositionInSubjectBuffer)
        {
            var textViewModel = TextView.TextViewModel;

            if (textViewModel == null)
            {
                Debug.Assert(TextView.IsClosed);
                return(false);
            }

            // The expansion itself needs to be created in the data buffer, so map everything up
            if (!TryGetSpanOnHigherBuffer(
                    SubjectBuffer.CurrentSnapshot.GetSpan(startPositionInSubjectBuffer, endPositionInSubjectBuffer - startPositionInSubjectBuffer),
                    textViewModel.DataBuffer,
                    out var dataBufferSpan))
            {
                return(false);
            }

            var buffer = EditorAdaptersFactoryService.GetBufferAdapter(textViewModel.DataBuffer);

            if (buffer == null || !(buffer is IVsExpansion expansion))
            {
                return(false);
            }

            buffer.GetLineIndexOfPosition(dataBufferSpan.Start.Position, out var startLine, out var startIndex);
            buffer.GetLineIndexOfPosition(dataBufferSpan.End.Position, out var endLine, out var endIndex);

            var textSpan = new VsTextSpan
            {
                iStartLine  = startLine,
                iStartIndex = startIndex,
                iEndLine    = endLine,
                iEndIndex   = endIndex
            };

            return(expansion.InsertExpansion(textSpan, textSpan, this, LanguageServiceGuid, out ExpansionSession) == VSConstants.S_OK);
        }
예제 #3
0
 public ExpansionClient(ITextView textView, IVsEditorAdaptersFactoryService adapterFactory, IServiceProvider serviceProvider)
 {
     _textView        = textView;
     _serviceProvider = serviceProvider;
     _adapterFactory  = adapterFactory;
     _view            = adapterFactory.GetViewAdapter(textView);
     _lines           = (IVsTextLines)adapterFactory.GetBufferAdapter(textView.TextBuffer);
     _expansion       = _lines as IVsExpansion;
     if (_expansion == null)
     {
         throw new ArgumentException("TextBuffer does not support expansions");
     }
 }
예제 #4
0
파일: VsAdapter.cs 프로젝트: pckben/VsVim
        internal Result <IVsTextLines> GetTextLines(ITextBuffer textBuffer)
        {
            var lines = _editorAdaptersFactoryService.GetBufferAdapter(textBuffer) as IVsTextLines;

            return(lines != null
               ? Result.CreateSuccess(lines)
               : Result.Error);
        }
예제 #5
0
        void FixFormatMap()
        {
            if (_view == null || _view.IsClosed)
            {
                return;
            }

            var bufferAdapter = _adaptersService.GetBufferAdapter(_view.TextBuffer);

            if (bufferAdapter == null)
            {
                return;
            }

            Guid fontCategory = DefGuidList.guidTextEditorFontCategory;
            Guid languageService;

            if (0 != bufferAdapter.GetLanguageServiceID(out languageService))
            {
                return;
            }

            FontsAndColorsCategory category = new FontsAndColorsCategory(languageService, fontCategory, fontCategory);

            var info = _fcService.GetFontAndColorInformation(category);

            if (info == null)
            {
                return;
            }

            // This is pretty dirty. IVsFontsAndColorsInformation doesn't give you a count, and I don't really want
            // to go through the ugly of finding the eventual colorable items provider to ask for its count, so this nasty
            // little loop will go until an index past the count (at which point it returns null).
            HashSet <IClassificationType> types = new HashSet <IClassificationType>(_formatMap.CurrentPriorityOrder);

            for (int i = 1; i < 1000; i++)
            {
                var type = info.GetClassificationType(i);
                if (type == null)
                {
                    break;
                }

                types.Add(type);
            }

            FixFormatMap(types);
        }
예제 #6
0
        internal void SetReadOnly(DocumentId documentId, bool value)
        {
            AssertIsForeground();
            Debug.Assert(!_isDisposed);

            var textBuffer = GetTextBuffer(_workspace, documentId);

            if (textBuffer != null)
            {
                var vsBuffer = _adapters.GetBufferAdapter(textBuffer);
                if (vsBuffer != null)
                {
                    SetReadOnlyFlag(vsBuffer, value);
                }
            }
        }
예제 #7
0
        public ExpansionClient(ITextView textView, IVsEditorAdaptersFactoryService editorAdaptersFactoryService)
        {
            if (editorAdaptersFactoryService == null)
            {
                throw new ArgumentNullException(nameof(editorAdaptersFactoryService));
            }

            _textView  = textView ?? throw new ArgumentNullException(nameof(textView));
            _view      = editorAdaptersFactoryService.GetViewAdapter(_textView);
            _lines     = (IVsTextLines)editorAdaptersFactoryService.GetBufferAdapter(_textView.TextBuffer);
            _expansion = _lines as IVsExpansion;
            if (_expansion == null)
            {
                throw new ArgumentException("TextBuffer does not support expansions");
            }
        }
예제 #8
0
        private void OnBeforeSaveWorker(uint docCookie)
        {
            // We want to raise a save event for this document. First let's try to get the docData
            uint         flags;
            uint         readLocks;
            uint         writeLocks;
            string       moniker;
            IVsHierarchy hierarchy;
            uint         itemid;
            var          docData = IntPtr.Zero;

            try
            {
                Marshal.ThrowExceptionForHR(_runningDocumentTable.GetDocumentInfo(docCookie, out flags, out readLocks, out writeLocks, out moniker, out hierarchy, out itemid, out docData));

                var textBuffer = TryGetTextBufferFromDocData(docData);

                // Do a quick check that this is a Roslyn file at all before we go do more expensive things
                if (textBuffer != null && textBuffer.ContentType.IsOfType(ContentTypeNames.RoslynContentType))
                {
                    var textBufferAdapter = _editorAdaptersFactoryService.GetBufferAdapter(textBuffer);

                    if (textBufferAdapter != null)
                    {
                        // OK, we want to go and raise a save event. Currently, CommandArgs demands that we have a view, so let's try to go and find one.
                        IVsEnumTextViews enumTextViews;
                        _textManager.EnumViews(textBufferAdapter, out enumTextViews);
                        IVsTextView[] views   = new IVsTextView[1];
                        uint          fetched = 0;

                        if (ErrorHandler.Succeeded(enumTextViews.Next(1, views, ref fetched)) && fetched == 1)
                        {
                            var view = _editorAdaptersFactoryService.GetWpfTextView(views[0]);
                            var commandHandlerService = _commandHandlerServiceFactory.GetService(textBuffer);
                            commandHandlerService.Execute(textBuffer.ContentType, new SaveCommandArgs(view, textBuffer));
                        }
                    }
                }
            }
            finally
            {
                if (docData != IntPtr.Zero)
                {
                    Marshal.Release(docData);
                }
            }
        }
        public static IOleUndoManager TryGetUndoManager(
            this IVsEditorAdaptersFactoryService editorAdaptersFactoryService, ITextBuffer subjectBuffer)
        {
            if (subjectBuffer != null)
            {
                var adapter = editorAdaptersFactoryService?.GetBufferAdapter(subjectBuffer);
                if (adapter != null)
                {
                    if (ErrorHandler.Succeeded(adapter.GetUndoManager(out var manager)))
                    {
                        return(manager);
                    }
                }
            }

            return(null);
        }
예제 #10
0
        internal SnippetHandler(IVsTextView textViewAdapter, SnippetHandlerProvider provider, IVsEditorAdaptersFactoryService adaptersFactory, ITextView textView)
        {
            _adapterFactory = adaptersFactory;
            _textView       = textView;
            _view           = textViewAdapter;
            _mProvider      = provider;
            //get the text manager from the service provider
            var textManager = (IVsTextManager2)_mProvider.ServiceProvider.GetService(typeof(SVsTextManager));

            textManager.GetExpansionManager(out _mExManager);
            _session = null;

            //add the command to the command chain
            textViewAdapter.AddCommandFilter(this, out _mNextCommandHandler);

            _lines = (IVsTextLines)_adapterFactory.GetBufferAdapter(_textView.TextBuffer);
        }
        /*private static Project GetCurrentProject(ITextBuffer buffer, IVsEditorAdaptersFactoryService adaptersFactory)
        {
            IExtensibleObject bufferAdapter = adaptersFactory.GetBufferAdapter(buffer) as IExtensibleObject;
            if (bufferAdapter == null)
                return null;
            object ppDisp;
            bufferAdapter.GetAutomationObject("Document", null, out ppDisp);
            Document document = ppDisp as Document;
            if (document == null)
                return null;

            return document.ProjectItem.ContainingProject;
        }*/

        private static Project GetCurrentProject(ITextBuffer buffer, IVsEditorAdaptersFactoryService adaptersFactory, SVsServiceProvider serviceProvider)
        {
            IPersistFileFormat persistFileFormat = adaptersFactory.GetBufferAdapter(buffer) as IPersistFileFormat;
            if (persistFileFormat == null)
                return null;

            string ppzsFilename;
            uint iii;
            persistFileFormat.GetCurFile(out ppzsFilename, out iii);

            if (string.IsNullOrWhiteSpace(ppzsFilename))
                return null;

            DTE dte = (DTE)serviceProvider.GetService(typeof(DTE));

            ProjectItem prjItem = dte.Solution.FindProjectItem(ppzsFilename);

            return prjItem.ContainingProject;
        }
예제 #12
0
        private IDocumentMarkup TryGetDocumentMarkup()
        {
            VSIVsTextBuffer bufferAdapter = _editorAdaptersFactoryService.GetBufferAdapter(_textBuffer);

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

            IDocument document = new JetIVsTextBuffer(bufferAdapter).JetDocument.Value;

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

            IDocumentMarkup documentMarkup = DocumentMarkupManagerBase.TryGetMarkupModel(document);

            return(documentMarkup is IVsDocumentMarkup ? documentMarkup : null);
        }
예제 #13
0
        /// <summary>
        /// Gets a classifier for the given text buffer.
        /// </summary>
        /// <param name="buffer">The <see cref="ITextBuffer"/> to classify.</param>
        /// <returns>A classifier for the text buffer, or null if the provider cannot do so in its current state.</returns>
        public IClassifier GetClassifier(ITextBuffer buffer)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            Parser.Project project       = null;
            var            bufferAdapter = AdapterFactory.GetBufferAdapter(buffer);
            var            textManager   = ServiceProvider.GetService(typeof(SVsTextManager)) as IVsTextManager;

            if (bufferAdapter != null)
            {
                IVsEnumTextViews enumerator;
                textManager.EnumViews(bufferAdapter, out enumerator);

                uint count = 0;
                enumerator.GetCount(ref count);
                if (count > 0)
                {
                    IVsTextView[] viewarray  = new IVsTextView[count];
                    uint          fetchCount = 0;
                    if (enumerator.Next(count, viewarray, ref fetchCount) == VSConstants.S_OK)
                    {
                        foreach (var view in viewarray)
                        {
                            var viewAdapter = AdapterFactory.GetWpfTextView(view);
                            viewAdapter.Properties.TryGetProperty <Parser.Project>(typeof(Parser.Project), out project);
                            if (project != null)
                            {
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                // This is done for highlighted tooltips
                buffer.Properties.TryGetProperty(typeof(Parser.Project), out project);
            }

            return(buffer.Properties.GetOrCreateSingletonProperty <EpochClassifier>(creator: () => new EpochClassifier(this.classificationRegistry, project)));
        }
예제 #14
0
        private void SetReadOnlyFlag(ITextBuffer buffer, bool value)
        {
            var vsBuffer = _adapters.GetBufferAdapter(buffer);

            uint oldFlags;
            uint newFlags;

            vsBuffer.GetStateFlags(out oldFlags);
            if (value)
            {
                newFlags = oldFlags | (uint)BUFFERSTATEFLAGS.BSF_USER_READONLY;
            }
            else
            {
                newFlags = oldFlags & ~(uint)BUFFERSTATEFLAGS.BSF_USER_READONLY;
            }

            if (oldFlags != newFlags)
            {
                vsBuffer.SetStateFlags(newFlags);
            }
        }
        private bool NavigateTo(ITextBuffer textBuffer, VsTextSpan vsTextSpan)
        {
            using (Logger.LogBlock(FunctionId.NavigationService_VSDocumentNavigationService_NavigateTo, CancellationToken.None))
            {
                var vsTextBuffer = _editorAdaptersFactoryService.GetBufferAdapter(textBuffer);
                if (vsTextBuffer == null)
                {
                    Debug.Fail("Could not get IVsTextBuffer for document!");
                    return(false);
                }

                var textManager = (IVsTextManager2)_serviceProvider.GetService(typeof(SVsTextManager));
                if (textManager == null)
                {
                    Debug.Fail("Could not get IVsTextManager service!");
                    return(false);
                }

                return(ErrorHandler.Succeeded(
                           textManager.NavigateToLineAndColumn2(
                               vsTextBuffer, VSConstants.LOGVIEWID.TextView_guid, vsTextSpan.iStartLine, vsTextSpan.iStartIndex, vsTextSpan.iEndLine, vsTextSpan.iEndIndex, (uint)_VIEWFRAMETYPE.vftCodeWindow)));
            }
        }
예제 #16
0
 public static IVsTextBuffer GetTextBuffer(IVsEditorAdaptersFactoryService vsEditorAdaptersFactory, ITextBuffer textBuffer)
 {
     return vsEditorAdaptersFactory.GetBufferAdapter(textBuffer);
 }
예제 #17
0
 public static IVsTextBuffer GetTextBuffer(IVsEditorAdaptersFactoryService vsEditorAdaptersFactory, ITextBuffer textBuffer)
 {
     return(vsEditorAdaptersFactory.GetBufferAdapter(textBuffer));
 }