示例#1
0
        public BookmarksSimpleTagger(ISharedServiceProvider serviceProvider, ITextBuffer buffer) : base(buffer)
        {
            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }

            _serviceProvider              = serviceProvider;
            _serviceProvider.Initialized += ServiceProvider_Initialized;

            _buffer          = buffer;
            _buffer.Changed += Buffer_Changed;

            DoChanged(0, BookmarkType.Any);
        }
示例#2
0
        public void Dispose()
        {
            if (_buffer != null)
            {
                if ((_bookmarkSpans.Count > 0) && (this.BookmarksService != null))
                {
                    var fileName = this.BookmarksService.GetCallbackFileName(this);                     // ITextDocument file name retrieval isn't possible at this stage
                    if (!string.IsNullOrEmpty(fileName))
                    {
                        _bookmarkSpans?.Keys.ForEach(b => this.BookmarksService?.UpdateBookmark(fileName, b.Number, b.Type));                         // report updated line numbers back
                    }
                    _bookmarkSpans.Clear();
                }

                if (this.BookmarksService != null)
                {
                    this.BookmarksService.Changed -= BookmarksService_Changed;
                    if (this.CallbackRegistered)
                    {
                        this.BookmarksService.UnregisterCallback(this);
                        this.CallbackRegistered = false;
                    }
                    this.BookmarksService = null;
                }

                if (_serviceProvider != null)
                {
                    _serviceProvider.Initialized -= ServiceProvider_Initialized;
                    _serviceProvider              = null;
                }

                if (_buffer != null)
                {
                    _buffer.Changed -= Buffer_Changed;
                    _buffer          = null;
                }

                _log = null;
            }
        }
示例#3
0
文件: Package.cs 项目: codecopy/dpack
        /// <summary>
        /// Configures global service container.
        /// </summary>
        private async Task <bool> ConfigureServicesAsync(CancellationToken cancellationToken)
        {
            await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

            _container = new ServiceContainer();
            try
            {
                // Here's where MEF and 3rd party DI are linked
                if (_sharedServiceProvider == null)
                {
                    this.SatisfyImportsOnce();
                }
                if (_sharedServiceProvider == null)
                {
                    Trace.TraceError("Shared service provider could not be resolved");
                    _sharedServiceProvider = new SharedServiceProvider();                     // DI setup is gonna fail without it
                }

                var menuCommandService = await this.GetServiceAsync <SystemIMenuCommandService>();

                this.AddService(typeof(IServiceProvider), async(container, token, serviceType) =>
                {
                    await this.JoinableTaskFactory.SwitchToMainThreadAsync(token);
                    return(_container);
                });                 // expose DI factory itself
                this.AddService(typeof(IPackageService), async(container, token, serviceType) =>
                {
                    await this.JoinableTaskFactory.SwitchToMainThreadAsync(token);
                    return(_container.TryGetInstance <IPackageService>());
                }, true);                                                                                                // promote our own service
                // Global services
                _container.Register <IAsyncServiceProvider>((factory) => this, new PerContainerLifetime());              // used for shell services resolution
                _container.Register <IServiceProvider>((factory) => _sharedServiceProvider, new PerContainerLifetime()); // used for shell services resolution
                _container.Register <IPackageService, PackageService>(new PerContainerLifetime());
                _container.Register <ILog, Log>(new PerContainerLifetime());
                _container.Register <SystemIMenuCommandService>((factory) => menuCommandService, new PerContainerLifetime());
                _container.Register <ILanguageRegistrationService, LanguageRegistrationService>(new PerContainerLifetime());
                _container.Register <ILanguageService, LanguageService>(new PerContainerLifetime());
                _container.Register <ShellService>(new PerContainerLifetime());
                _container.Register <IShellHelperService>((factory) => factory.GetInstance <ShellService>(), new PerContainerLifetime());
                _container.Register <IShellStatusBarService>((factory) => factory.GetInstance <ShellService>(), new PerContainerLifetime());
                _container.Register <IShellReferenceService>((factory) => factory.GetInstance <ShellService>(), new PerContainerLifetime());
                _container.Register <IShellSelectionService>((factory) => factory.GetInstance <ShellService>(), new PerContainerLifetime());
                _container.Register <IShellProjectService>((factory) => factory.GetInstance <ShellService>(), new PerContainerLifetime());
                _container.Register <IShellCodeModelService>((factory) => factory.GetInstance <ShellService>(), new PerContainerLifetime());
                _container.Register <IShellEventsService, ShellEventsService>(new PerContainerLifetime());
                _container.Register <IShellImageService, ShellImageService>(new PerContainerLifetime());
                _container.Register <IShellInfoBarService, ShellInfoBarService>(new PerContainerLifetime());
                _container.Register <IImageService, ImageService>(new PerContainerLifetime());
                _container.Register <IMessageService, MessageService>(new PerContainerLifetime());
                _container.Register <JoinableTaskFactory>((factory) => this.JoinableTaskFactory, new PerContainerLifetime());
                _container.Register <IAsyncTaskService, AsyncTaskService>(new PerContainerLifetime());
                _container.Register <IOptionsService, OptionsService>(new PerContainerLifetime());
                _container.Register <IOptionsPersistenceService, OptionsPersistenceService>(new PerContainerLifetime());
                _container.Register <IKeyboardService, KeyboardService>(new PerContainerLifetime());
                // Per request services
                _container.Register <IFeatureCommand, FeatureCommand>();
                _container.Register <IFeatureCommandFactory, FeatureCommandFactory>();
                _container.Register <IFeatureFactory, FeatureFactory>();
                _container.Register <IUtilsService, UtilsService>();
                _container.Register <IFileTypeResolver, FileTypeResolver>();
                _container.Register <ISolutionProcessor, SolutionProcessor>();
                _container.Register <IProjectProcessor, ProjectProcessor>();
                _container.Register <IFileProcessor, FileProcessor>();
                _container.Register <IModalDialogService, ModalDialogService>();
                _container.Register <ISearchMatchService, SearchMatchService>();
                _container.Register <IWildcardMatch, WildcardMatch>();
                // Features
                _container.Register <Features.SupportOptions.SupportOptionsFeature>(new PerContainerLifetime());
                _container.Register <Features.Miscellaneous.MiscellaneousFeature>(new PerContainerLifetime());
                // File Browser feature
                _container.Register <Features.FileBrowser.FileBrowserFeature>(new PerContainerLifetime());
                _container.Register <Features.FileBrowser.FileBrowserViewModel>();
                // Code Browser feature
                _container.Register <Features.CodeBrowser.CodeBrowserFeature>(new PerContainerLifetime());
                _container.Register <Features.CodeBrowser.CodeBrowserViewModel>();
                // Bookmarks feature
                _container.Register <BookmarksFeature>(new PerContainerLifetime());
                _container.Register <IBookmarksService, BookmarksService>(new PerContainerLifetime());
                // Surround With feature
                _container.Register <SurroundWithFeature>(new PerContainerLifetime());

                _container.Compile();
                _sharedServiceProvider?.Initialize(this, _container);
                return(true);
            }
            catch (Exception ex)
            {
                // Failing this early we can't even use existing services
                Trace.TraceError($"Failed to initialize:\r\n{ex}");

                // Can't rely on any of our own services here

                var resourceManager = new ResourceManager("VSPackage", this.GetType().Assembly);
                var product         = resourceManager?.GetString(IDs.PRODUCT.ToString());

                VsShellUtilities.ShowMessageBox(this, $"Failed to initialize: {ex.Message}", product,
                                                OLEMSGICON.OLEMSGICON_CRITICAL, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);

                return(false);
            }
        }
 /// <summary>
 /// Returns test instance.
 /// </summary>
 private IBookmarksSimpleTagger GetTagger(ISharedServiceProvider serviceProvider, ITextBuffer textBuffer)
 {
     return(new BookmarksSimpleTagger(serviceProvider, textBuffer));
 }