Eagerly get offline doc path

This commit is contained in:
Folyd 2024-06-15 15:11:51 -07:00
parent 68d011e310
commit 7fe32096a5

View file

@ -24,7 +24,6 @@ export default class RustSearchOmnibox {
// All dynamic setting items. Those items will been updated // All dynamic setting items. Those items will been updated
// in chrome.storage.onchange listener callback. // in chrome.storage.onchange listener callback.
let isOfflineMode = await settings.isOfflineMode; let isOfflineMode = await settings.isOfflineMode;
let offlineDocPath = await settings.offlineDocPath;
let defaultSearch = await settings.defaultSearch; let defaultSearch = await settings.defaultSearch;
let crateRegistry = await settings.crateRegistry; let crateRegistry = await settings.crateRegistry;
@ -54,7 +53,7 @@ export default class RustSearchOmnibox {
]; ];
} }
omnibox.bootstrap({ const docsSearchMixins = {
onSearch: (query) => { onSearch: (query) => {
return stdSearcher.search(query); return stdSearcher.search(query);
}, },
@ -65,6 +64,10 @@ export default class RustSearchOmnibox {
description: `Search Rust docs <match>${query}</match> on ${isOfflineMode ? "offline mode" : stdSearcher.getRootPath()}`, description: `Search Rust docs <match>${query}</match> on ${isOfflineMode ? "offline mode" : stdSearcher.getRootPath()}`,
}]; }];
}, },
};
omnibox.bootstrap({
...docsSearchMixins,
onEmptyNavigate: (content, disposition) => { onEmptyNavigate: (content, disposition) => {
commandManager.handleCommandEnterEvent(content, disposition); commandManager.handleCommandEnterEvent(content, disposition);
}, },
@ -96,16 +99,7 @@ export default class RustSearchOmnibox {
omnibox.addRegexQueryEvent(/^s(?:rc)?:/i, { omnibox.addRegexQueryEvent(/^s(?:rc)?:/i, {
name: "Source code", name: "Source code",
onSearch: (query) => { ...docsSearchMixins,
return stdSearcher.search(query);
},
onFormat: formatDoc,
onAppend: (query) => {
return [{
content: stdSearcher.getSearchUrl(query),
description: `Search Rust docs <match>${query}</match> on ${isOfflineMode ? "offline mode" : stdSearcher.getRootPath()}`,
}];
},
}); });
// Nightly std docs search // Nightly std docs search
@ -272,7 +266,7 @@ export default class RustSearchOmnibox {
omnibox.addRegexQueryEvent(/^`?e\d{2,4}`?$/i, { omnibox.addRegexQueryEvent(/^`?e\d{2,4}`?$/i, {
name: "Error code", name: "Error code",
onSearch: (query) => { onSearch: async (query) => {
query = query.replace("`", ""); query = query.replace("`", "");
let baseIndex = parseInt(query.slice(1).padEnd(4, '0')); let baseIndex = parseInt(query.slice(1).padEnd(4, '0'));
let result = []; let result = [];
@ -280,7 +274,9 @@ export default class RustSearchOmnibox {
let errorIndex = 'E' + String(baseIndex++).padStart(4, "0").toUpperCase(); let errorIndex = 'E' + String(baseIndex++).padStart(4, "0").toUpperCase();
result.push(errorIndex); result.push(errorIndex);
} }
let baseUrl = isOfflineMode ? offlineDocPath : 'https://doc.rust-lang.org/';
let isOfflineMode = await settings.isOfflineMode;
let baseUrl = isOfflineMode ? await settings.offlineDocPath : 'https://doc.rust-lang.org/';
return result.map(errorCode => { return result.map(errorCode => {
return { return {
content: `${baseUrl}error_codes/${errorCode}.html`, content: `${baseUrl}error_codes/${errorCode}.html`,