aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.Package/Package/QtHelp.cs
blob: 65d54f81427d8d2acb30fb44d63f177155fb2801 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using EnvDTE;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;

using Tasks = System.Threading.Tasks;

namespace QtVsTools
{
    using Core;
    using Core.Options;
    using VisualStudio;

    public class QtHelp
    {
        private static QtHelp Instance
        {
            get;
            set;
        }

        public static void Initialize()
        {
            Instance = new QtHelp();
        }

        private QtHelp()
        {
            var commandService = VsServiceProvider
                .GetService<IMenuCommandService, OleMenuCommandService>();
            commandService?.AddCommand(new MenuCommand(F1QtHelpEventHandler,
                new CommandID(QtMenus.Package.Guid, QtMenus.Package.F1QtHelp)));
        }

        private static bool IsSuperfluousCharacter(string text)
        {
            switch (text) {
            case " ":
            case ";":
            case ".":
            case "<":
            case ">":
            case "{":
            case "}":
            case "(":
            case ")":
            case ":":
            case ",":
            case "/":
            case "\\":
            case "^":
            case "%":
            case "+":
            case "-":
            case "*":
            case "\t":
            case "&":
            case "\"":
            case "!":
            case "[":
            case "]":
            case "|":
            case "'":
            case "~":
            case "#":
            case "=":
                return true; // nothing we are interested in
            }
            return false;
        }

        private static string GetString(IDataRecord reader, int index)
        {
            return reader.IsDBNull(index) ? "" : reader.GetString(index);
        }

        private static void F1QtHelpEventHandler(object sender, EventArgs args)
        {
            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 ShowEditorContextHelp()
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            try {
                var dte = VsServiceProvider.GetService<SDTE, DTE>();
                if (dte?.ActiveDocument?.Object() is not TextDocument objTextDocument)
                    return false;

                string keyword;
                var selection = objTextDocument.Selection;
                if (selection.IsEmpty) { // no selection inside the document
                    var line = selection.ActivePoint.Line; // current line
                    var offset = selection.ActivePoint.LineCharOffset; // current char offset

                    selection.CharLeft(true); // try the character before the cursor
                    if (!selection.IsEmpty) {
                        keyword = selection.Text; // something in front of the cursor
                        selection.CharRight(true); // reset to origin
                        if (!IsSuperfluousCharacter(keyword)) {
                            // move the selection to the start of the word
                            selection.WordLeft(true);
                            selection.MoveToPoint(selection.TopPoint);
                        }
                    }
                    selection.WordRight(true);  // select the word
                    keyword = selection.Text;  // get the selected text
                    selection.MoveToLineAndOffset(line, offset); // reset
                } else {
                    keyword = selection.Text;
                }

                keyword = keyword.Trim();
                if (keyword.Length <= 1 || IsSuperfluousCharacter(keyword))
                    return false; // suppress single character, operators etc...

                var qtVersion = "$(DefaultQtVersion)";
                if (HelperFunctions.GetSelectedQtProject(dte) is { } project)
                    qtVersion = project.QtVersion;

                var info = VersionInformation.GetOrAddByName(qtVersion);
                var docPath = info?.QtInstallDocs;
                if (string.IsNullOrEmpty(docPath) || !Directory.Exists(docPath))
                    return false;

                var qchFiles = Directory.GetFiles(docPath, "*?.qch");
                if (qchFiles.Length == 0)
                    return TryShowGenericSearchResultsOnline(keyword, info.Major);

                var linksForKeyword = "SELECT d.Title, f.Name, e.Name, "
                    + "d.Name, a.Anchor FROM IndexTable a, FileNameTable d, FolderTable e, "
                    + "NamespaceTable f WHERE a.FileId=d.FileId AND d.FolderId=e.Id AND "
                    + $"a.NamespaceId=f.Id AND a.Name='{keyword}'";

                var links = new Dictionary<string, string>();
                ProcessQchFiles(qchFiles, linksForKeyword, keyword, docPath, info, links);

                string uri;
                switch (links.Values.Count) {
                case 0:
                    return TryShowGenericSearchResultsOnline(keyword, info.Major);
                case 1:
                    uri = links.First().Value;
                    break;
                default:
                    var dialog = new QtHelpLinkChooser
                    {
                        Links = links,
                        Keyword = keyword,
                        ShowInTaskbar = false
                    };
                    if (!dialog.ShowModal().GetValueOrDefault())
                        return false;
                    uri = dialog.Link;
                    break;
                }

                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 {
                    VsShellUtilities.OpenSystemBrowser(uri);
                }
            } catch (Exception exception) {
                exception.Log();
            }
            return true;
        }

        private static bool TryShowGenericSearchResultsOnline(string keyword, uint version)
        {
            if (QtOptionsPage.HelpPreference != QtOptionsPage.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;
        }

        private static void ProcessQchFiles(IEnumerable<string> qchFiles, string linksForKeyword,
            string keyword, string docPath, VersionInformation info, IDictionary<string, string> links)
        {
            ThreadHelper.JoinableTaskFactory.Run(async () => await
                ProcessQchFilesAsync(qchFiles, linksForKeyword, keyword, docPath, info, links));
        }

        private static async Tasks.Task ProcessQchFilesAsync(IEnumerable<string> qchFiles,
            string linksForKeyword, string keyword, string docPath, VersionInformation info,
            IDictionary<string, string> links)
        {
            var builder = new SQLiteConnectionStringBuilder
            {
                ReadOnly = true
            };

            foreach (var file in qchFiles) {
                builder.DataSource = file;

                using var connection = new SQLiteConnection(builder.ToString());
                await connection.OpenAsync();

                using var command = new SQLiteCommand(linksForKeyword, connection);
                using var reader = await command.ExecuteReaderAsync();
                while (await reader.ReadAsync()) {
                    var title = GetString(reader, 0);
                    if (string.IsNullOrWhiteSpace(title))
                        title = keyword + ':' + GetString(reader, 3);
                    string path;
                    if (QtOptionsPage.HelpPreference == QtOptionsPage.SourcePreference.Offline) {
                        path = "file:///" + Path.Combine(docPath,
                            GetString(reader, 2), GetString(reader, 3));
                    } else {
                        path = "https://" + Path.Combine("doc.qt.io",
                            $"qt-{info.Major}", GetString(reader, 3));
                    }
                    if (!string.IsNullOrWhiteSpace(GetString(reader, 4)))
                        path += "#" + GetString(reader, 4);
                    links.Add(title, path);
                }
            }
        }
    }
}