Skip to content

Commit

Permalink
Eagerly get offline doc path
Browse files Browse the repository at this point in the history
  • Loading branch information
Folyd committed Jun 15, 2024
1 parent 68d011e commit 77a9679
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions extension/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export default class RustSearchOmnibox {
// All dynamic setting items. Those items will been updated
// in chrome.storage.onchange listener callback.
let isOfflineMode = await settings.isOfflineMode;
let offlineDocPath = await settings.offlineDocPath;
let defaultSearch = await settings.defaultSearch;
let crateRegistry = await settings.crateRegistry;

Expand Down Expand Up @@ -54,7 +53,7 @@ export default class RustSearchOmnibox {
];
}

omnibox.bootstrap({
const docsSearchMixins = {
onSearch: (query) => {
return stdSearcher.search(query);
},
Expand All @@ -65,6 +64,10 @@ export default class RustSearchOmnibox {
description: `Search Rust docs <match>${query}</match> on ${isOfflineMode ? "offline mode" : stdSearcher.getRootPath()}`,
}];
},
};

omnibox.bootstrap({
...docsSearchMixins,
onEmptyNavigate: (content, disposition) => {
commandManager.handleCommandEnterEvent(content, disposition);
},
Expand Down Expand Up @@ -96,16 +99,7 @@ export default class RustSearchOmnibox {

omnibox.addRegexQueryEvent(/^s(?:rc)?:/i, {
name: "Source code",
onSearch: (query) => {
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()}`,
}];
},
...docsSearchMixins,
});

// Nightly std docs search
Expand Down Expand Up @@ -272,15 +266,15 @@ export default class RustSearchOmnibox {

omnibox.addRegexQueryEvent(/^`?e\d{2,4}`?$/i, {
name: "Error code",
onSearch: (query) => {
onSearch: async (query) => {
query = query.replace("`", "");
let baseIndex = parseInt(query.slice(1).padEnd(4, '0'));
let result = [];
for (let i = 0; i < 10; i++) {
let errorIndex = 'E' + String(baseIndex++).padStart(4, "0").toUpperCase();
result.push(errorIndex);
}
let baseUrl = isOfflineMode ? offlineDocPath : 'https://doc.rust-lang.org/';
let baseUrl = await settings.isOfflineMode ? await settings.offlineDocPath : 'https://doc.rust-lang.org/';
return result.map(errorCode => {
return {
content: `${baseUrl}error_codes/${errorCode}.html`,
Expand Down

0 comments on commit 77a9679

Please sign in to comment.