Esempio n. 1
0
        /// <summary>
        /// Initialization of the package; this method is called right after the package is sited, so this is the place
        /// where you can put all the initialization code that rely on services provided by VisualStudio.
        /// </summary>
        /// <param name="cancellationToken">A cancellation token to monitor for initialization cancellation, which can occur when VS is shutting down.</param>
        /// <param name="progress">A provider for progress updates.</param>
        /// <returns>A task representing the async work of package initialization, or an already completed task if there is none. Do not return null from this method.</returns>
        protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress <ServiceProgressData> progress)
        {
            // When initialized asynchronously, the current thread may be a background thread at this point.
            // Do any initialization that requires the UI thread after switching to the UI thread.
            await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

            // Since this package might not be initialized until after a solution has finished loading,
            // we need to check if a solution has already been loaded and then handle it.
            bool isSolutionLoaded = await this.IsSolutionLoadedAsync(cancellationToken);

            if (isSolutionLoaded)
            {
                await this.HandleOpenSolutionAsync(cancellationToken);
            }

            // Listen for subsequent solution events
            SolutionEvents.OnAfterOpenSolution  += this.HandleOpenSolution;
            SolutionEvents.OnAfterCloseSolution += this.HandleCloseSolution;

            await this.SetUpRunningDocumentTableEventsAsync(cancellationToken).ConfigureAwait(false);

            var componentModel = GetGlobalService(typeof(SComponentModel)) as IComponentModel;
            await ConstFinder.TryParseSolutionAsync(componentModel);

            await this.LoadSystemTextSettingsAsync(cancellationToken);

            VSColorTheme.ThemeChanged += (e) => this.LoadSystemTextSettingsAsync(CancellationToken.None).LogAndForget(nameof(StringResourceVisualizer));

            await SponsorRequestHelper.CheckIfNeedToShowAsync();

            Instance = this;
        }
Esempio n. 2
0
        private async Task HandleOpenSolutionAsync(CancellationToken cancellationToken)
        {
            await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

            await OutputPane.Instance?.WriteAsync("If you have problems, or suggestions for improvement, report them at https://github.com/mrlacey/StringResourceVisualizer/issues/new ");

            await OutputPane.Instance?.WriteAsync("If you like this extension please leave a review at https://marketplace.visualstudio.com/items?itemName=MattLaceyLtd.StringResourceVisualizer#review-details ");

            await OutputPane.Instance?.WriteAsync(string.Empty);

            // Get all resource files from the solution
            // Do this now, rather than in adornment manager for performance and to avoid thread issues
            if (await this.GetServiceAsync(typeof(DTE)) is DTE dte)
            {
                var    fileName = dte.Solution.FileName;
                string rootDir  = null;

                if (!string.IsNullOrWhiteSpace(fileName) && File.Exists(fileName))
                {
                    rootDir = Path.GetDirectoryName(fileName);
                }

                if (string.IsNullOrWhiteSpace(rootDir))
                {
                    await OutputPane.Instance?.WriteAsync("No solution file found so attempting to load resources for project file.");

                    fileName = ((dte.ActiveSolutionProjects as Array).GetValue(0) as EnvDTE.Project).FileName;

                    rootDir = Path.GetDirectoryName(fileName);
                }

                if (!string.IsNullOrWhiteSpace(rootDir) && Directory.Exists(rootDir))
                {
                    await this.SetOrUpdateListOfResxFilesAsync(rootDir);

#pragma warning disable VSTHRD101 // Avoid unsupported async delegates
                    Messenger.ReloadResources += async() =>
                    {
                        try
                        {
                            await this.SetOrUpdateListOfResxFilesAsync(rootDir);
                        }
                        catch (Exception exc)
                        {
                            await OutputPane.Instance?.WriteAsync("Unexpected error when reloading resources.");

                            await OutputPane.Instance?.WriteAsync(exc.Message);

                            await OutputPane.Instance?.WriteAsync(exc.StackTrace);
                        }
                    };
#pragma warning restore VSTHRD101 // Avoid unsupported async delegates

                    this.WatchForSolutionOrProjectChanges(fileName);
                }

                if (ResourceAdornmentManager.ResourceFiles.Any())
                {
                    var plural = ResourceAdornmentManager.ResourceFiles.Count > 1 ? "s" : string.Empty;
                    await OutputPane.Instance?.WriteAsync($"String Resource Visualizer initialized with {ResourceAdornmentManager.ResourceFiles.Count} resource file{plural}.");

                    foreach (var resourceFile in ResourceAdornmentManager.ResourceFiles)
                    {
                        await OutputPane.Instance?.WriteAsync(resourceFile);
                    }
                }
                else
                {
                    await OutputPane.Instance?.WriteAsync("String Resource Visualizer could not find any resource files to load.");
                }
            }

            if (!ConstFinder.HasParsedSolution)
            {
                await ConstFinder.TryParseSolutionAsync();
            }
        }