From ca47896204482bf4a6979e3838bf7f09f61cebeb Mon Sep 17 00:00:00 2001
From: giy <giy@omp-system.ru>
Date: Fri, 02 Sep 2022 14:16:56 +0300
Subject: [PATCH] Обновление до версии 2.9.0

---
 QtVsTools.Package/Package/QtHelp.cs |  113 ++++++++++++++++++++++++++------------------------------
 1 files changed, 52 insertions(+), 61 deletions(-)

diff --git a/QtVsTools.Package/Package/QtHelp.cs b/QtVsTools.Package/Package/QtHelp.cs
index e36cb83..12e7b19 100644
--- a/QtVsTools.Package/Package/QtHelp.cs
+++ b/QtVsTools.Package/Package/QtHelp.cs
@@ -1,6 +1,6 @@
 /****************************************************************************
 **
-** Copyright (C) 2016 The Qt Company Ltd.
+** Copyright (C) 2022 The Qt Company Ltd.
 ** Contact: https://www.qt.io/licensing/
 **
 ** This file is part of the Qt VS Tools.
@@ -26,13 +26,6 @@
 **
 ****************************************************************************/
 
-using EnvDTE;
-using Microsoft.VisualStudio.Settings;
-using Microsoft.VisualStudio.Shell;
-using Microsoft.VisualStudio.Shell.Interop;
-using Microsoft.VisualStudio.Shell.Settings;
-using QtVsTools.Core;
-using QtVsTools.VisualStudio;
 using System;
 using System.Collections.Generic;
 using System.ComponentModel.Design;
@@ -40,37 +33,36 @@
 using System.Data.SQLite;
 using System.IO;
 using System.Linq;
-using System.Threading.Tasks;
-using Task = System.Threading.Tasks.Task;
+using EnvDTE;
+using Microsoft.VisualStudio.Shell;
+using Microsoft.VisualStudio.Shell.Interop;
 
 namespace QtVsTools
 {
+    using Core;
+    using VisualStudio;
+
     public class QtHelp
     {
         public enum SourcePreference { Online, Offline }
 
-        public static QtHelp Instance
+        private static QtHelp Instance
         {
             get;
-            private set;
+            set;
         }
 
-        public static void Initialize(Package package)
+        public static void Initialize()
         {
-            Instance = new QtHelp(package);
+            Instance = new QtHelp();
         }
 
         const int F1QtHelpId = 0x0502;
 
-        readonly Package package;
-        public static readonly Guid MainMenuGuid = new Guid("58f83fff-d39d-4c66-810b-2702e1f04e73");
+        private static readonly Guid MainMenuGuid = new Guid("58f83fff-d39d-4c66-810b-2702e1f04e73");
 
-        QtHelp(Package pkg)
+        private QtHelp()
         {
-            if (pkg == null)
-                throw new ArgumentNullException("package");
-            package = pkg;
-
             var commandService = VsServiceProvider
                 .GetService<IMenuCommandService, OleMenuCommandService>();
             if (commandService == null)
@@ -79,12 +71,6 @@
             var menuCommandID = new CommandID(MainMenuGuid, F1QtHelpId);
             commandService.AddCommand(new MenuCommand(F1QtHelpEventHandler, menuCommandID));
         }
-
-        IServiceProvider ServiceProvider
-        {
-            get { return package; }
-        }
-
         static bool IsSuperfluousCharacter(string text)
         {
             switch (text) {
@@ -131,11 +117,17 @@
 
         void F1QtHelpEventHandler(object sender, EventArgs args)
         {
-            QueryEditorContextHelp(true);
+            ThreadHelper.ThrowIfNotOnUIThread();
+            if (!ShowEditorContextHelp()) {
+                Messages.Print("No help match was found. You can still try to search online at "
+                    + "https://doc.qt.io" + ".", false, true);
+            }
         }
 
-        public static bool QueryEditorContextHelp(bool defaultTryOnline = false)
+        public static bool ShowEditorContextHelp()
         {
+            ThreadHelper.ThrowIfNotOnUIThread();
+
             try {
                 var dte = VsServiceProvider.GetService<SDTE, DTE>();
                 var objTextDocument = dte?.ActiveDocument?.Object() as TextDocument;
@@ -173,7 +165,7 @@
                 var project = HelperFunctions.GetSelectedQtProject(dte);
                 if (project == null) {
                     project = HelperFunctions.GetSelectedProject(dte);
-                    if (project != null && HelperFunctions.IsQMakeProject(project)) {
+                    if (project != null && HelperFunctions.IsQtProject(project)) {
                         var qmakeQtDir = HelperFunctions.GetQtDirFromQMakeProject(project);
                         qtVersion = QtVersionManager.The().GetQtVersionFromInstallDir(qmakeQtDir);
                     }
@@ -188,7 +180,7 @@
 
                 var qchFiles = Directory.GetFiles(docPath, "*?.qch");
                 if (qchFiles.Length == 0)
-                    return false;
+                    return TryShowGenericSearchResultsOnline(keyword, info.qtMajor);
 
                 var offline = QtVsToolsPackage.Instance.Options.HelpPreference == SourcePreference.Offline;
 
@@ -207,8 +199,9 @@
                     using (var connection = new SQLiteConnection(builder.ToString())) {
                         connection.Open();
                         using (var command = new SQLiteCommand(linksForKeyword, connection)) {
-                            using (var reader =
-                                Task.Run(async () => await command.ExecuteReaderAsync()).Result) {
+                            var reader = QtVsToolsPackage.Instance.JoinableTaskFactory
+                                .Run(async () => await command.ExecuteReaderAsync());
+                            using (reader) {
                                 while (reader.Read()) {
                                     var title = GetString(reader, 0);
                                     if (string.IsNullOrWhiteSpace(title))
@@ -233,15 +226,7 @@
                 var uri = string.Empty;
                 switch (links.Values.Count) {
                 case 0:
-                    if (!offline && defaultTryOnline) {
-                        uri = new UriBuilder($"https://doc.qt.io/qt-{info.qtMajor}/search-results.html")
-                        {
-                            Query = "q=" + keyword
-                        }.ToString();
-                    } else {
-                        return false;
-                    }
-                    break;
+                    return TryShowGenericSearchResultsOnline(keyword, info.qtMajor);
                 case 1:
                     uri = links.First().Value;
                     break;
@@ -254,34 +239,40 @@
                     };
                     if (!dialog.ShowModal().GetValueOrDefault())
                         return false;
-                    uri = dialog.Link
-                        .Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
+                    uri = dialog.Link;
                     break;
                 }
 
-                if (string.IsNullOrEmpty(uri)) { // offline mode without a single search hit
-                    VsShellUtilities.ShowMessageBox(Instance.ServiceProvider,
-                        "Your search - " + keyword + " - did not match any documents.",
+                uri = HelperFunctions.FromNativeSeparators(uri);
+                var helpUri = new Uri(uri);
+                if (helpUri.IsFile && !File.Exists(helpUri.LocalPath)) {
+                    VsShellUtilities.ShowMessageBox(QtVsToolsPackage.Instance,
+                        "Your search - " + keyword + " - did match a document, but it could "
+                        + "not be found on disk. To use the online help, select: "
+                        + "Tools | Options | Qt | Preferred source | Online",
                         string.Empty, OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK,
                         OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
                 } else {
-                    var helpUri = new Uri(uri.Replace('\\', '/'));
-                    if (helpUri.IsFile && !File.Exists(helpUri.LocalPath)) {
-                        VsShellUtilities.ShowMessageBox(Instance.ServiceProvider,
-                            "Your search - " + keyword + " - did match a document, but it could "
-                            + "not be found on disk. To use the online help, select: "
-                            + "Help | Set Qt Help Preference | Use Online Documentation",
-                            string.Empty, OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK,
-                            OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
-                    } else {
-                        VsShellUtilities.OpenSystemBrowser(HelperFunctions.ChangePathFormat(uri));
-                    }
+                    VsShellUtilities.OpenSystemBrowser(uri);
                 }
-            } catch (Exception e) {
-                Messages.Print(
-                    e.Message + "\r\n\r\nStacktrace:\r\n" + e.StackTrace);
+            } catch (Exception exception) {
+                exception.Log();
             }
             return true;
         }
+
+        private static bool TryShowGenericSearchResultsOnline(string keyword, uint version)
+        {
+            if (QtVsToolsPackage.Instance.Options.HelpPreference != SourcePreference.Online)
+                return false;
+
+            VsShellUtilities.OpenSystemBrowser(HelperFunctions.FromNativeSeparators(
+                new UriBuilder($"https://doc.qt.io/qt-{version}/search-results.html")
+                {
+                    Query = "q=" + keyword
+                }.ToString())
+            );
+            return true;
+        }
     }
 }

--
Gitblit v1.9.1