aboutsummaryrefslogtreecommitdiffstats
path: root/qt-core/src/online-docs.ts
blob: fc3730db49211210448bd7ef6316f165fc4f0319 (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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only

import * as vscode from 'vscode';

import { EXTENSION_ID } from '@/constants';
import { telemetry, createLogger, isError, fetchWithAbort } from 'qt-lib';

const logger = createLogger('online-docs');
interface SearchItem {
  link: string;
  snippet: string;
  title: string;
}

interface SearchResponse {
  items?: SearchItem[];
  searchInformation: {
    formattedTotalResults: string;
    totalResults: string;
  };
  queries: {
    nextPage: {
      startIndex: number;
    };
  };
}

function getCurrentWord(): string {
  const editor = vscode.window.activeTextEditor;
  if (!editor) {
    return '';
  }
  const range = editor.selection.isEmpty
    ? editor.document.getWordRangeAtPosition(editor.selection.active)
    : editor.selection;
  if (range) {
    const word = editor.document.getText(range);
    return word;
  }
  return '';
}

async function tryToOpenDocumentationFor(
  word: string,
  token?: vscode.CancellationToken
) {
  if (!word) {
    return false;
  }
  const link = `https://doc.qt.io/qt-6/${word.toLowerCase()}.html`;

  if (token?.isCancellationRequested) {
    return false;
  }
  const controller = new AbortController();
  token?.onCancellationRequested(() => {
    controller.abort();
  });
  const response = await fetchWithAbort(link, {
    controller: controller,
    timeout: 5000
  });

  if (response?.ok) {
    openInBrowser(link);
    return true;
  }
  return false;
}

function openInBrowser(url: string) {
  const openInExternalBrowserCommand = vscode.workspace
    .getConfiguration(EXTENSION_ID)
    .get<boolean>('openOnlineDocumentationInExternalBrowser');
  if (openInExternalBrowserCommand) {
    void vscode.env.openExternal(vscode.Uri.parse(url));
    return;
  }
  const simpleBrowserCommand = 'simpleBrowser.api.open';
  void vscode.commands.executeCommand(simpleBrowserCommand, url, {
    viewColumn: vscode.ViewColumn.Beside
  });
}

async function search() {
  telemetry.sendAction('documentationSearchManually');
  const hintWord = getCurrentWord();
  const value = await vscode.window.showInputBox({
    value: hintWord,
    placeHolder: 'Search for...',
    prompt: 'Enter a term to search for in the Qt Documentation'
  });
  if (value === undefined || value === '') {
    return;
  }

  searchAndAskforResult(value);
}

async function searchWithEngine(
  value: string,
  token?: vscode.CancellationToken
) {
  const link = 'https://d24zn9cw9ofw9u.cloudfront.net?q=';
  const controller = new AbortController();
  token?.onCancellationRequested(() => {
    controller.abort();
  });
  const response = await fetchWithAbort(link + value, {
    controller: controller,
    timeout: 5000
  });

  if (token?.isCancellationRequested) {
    return;
  }
  if (!response?.ok) {
    throw new Error('Network response: ' + response?.status);
  }

  return (await response.json()) as SearchResponse;
}

function searchAndAskforResult(value: string) {
  let quickPickItems: {
    label: string;
    link: string;
    detail: string;
  }[] = [];
  const task = async (
    _: vscode.Progress<{ message?: string; increment?: number }>,
    token: vscode.CancellationToken
  ) => {
    try {
      if (await tryToOpenDocumentationFor(value, token)) {
        return;
      }
      const searchResponseJson = await searchWithEngine(value, token);
      if (token.isCancellationRequested) {
        return;
      }
      if (!searchResponseJson?.items) {
        void vscode.window.showInformationMessage('No search results found.');
        return;
      }
      quickPickItems = searchResponseJson.items.map((item) => ({
        label: item.title,
        link: item.link,
        detail: item.snippet
      }));
    } catch (error) {
      const err = isError(error) ? error.message : String(error);
      logger.error(err);
      void vscode.window.showErrorMessage(`Error: "${err}"`);
    }
  };
  const options = {
    location: vscode.ProgressLocation.Notification,
    title: 'Searching...',
    cancellable: true
  };
  void vscode.window.withProgress(options, task).then(async () => {
    if (quickPickItems.length === 0) {
      return;
    }
    const selected = await vscode.window.showQuickPick(quickPickItems, {
      placeHolder: 'Select a search result'
    });
    if (selected) {
      openInBrowser(selected.link);
    }
  });
}

function openHomepage() {
  telemetry.sendAction('documentationHomepage');
  openInBrowser('https://doc.qt.io');
}

function searchForCurrentWord() {
  telemetry.sendAction('documentationSearchForCurrentWord');
  const word = getCurrentWord();
  if (word === '') {
    void vscode.window.showInformationMessage('No word found at the cursor.');
    return;
  }

  searchAndAskforResult(word);
}

export function registerDocumentationCommands() {
  const homepageCommand = vscode.commands.registerCommand(
    `${EXTENSION_ID}.documentationHomepage`,
    openHomepage
  );
  const searchManuallyCommand = vscode.commands.registerCommand(
    `${EXTENSION_ID}.documentationSearchManually`,
    search
  );
  const searchForCurrentWordCommand = vscode.commands.registerCommand(
    `${EXTENSION_ID}.documentationSearchForCurrentWord`,
    searchForCurrentWord
  );
  return [homepageCommand, searchManuallyCommand, searchForCurrentWordCommand];
}