2022-02-22 04:40:14 +00:00
|
|
|
/**
|
|
|
|
* This module is a wrapper that facilitates manipulating the declaration data.
|
2022-02-22 07:01:14 +00:00
|
|
|
*
|
2022-02-22 04:40:14 +00:00
|
|
|
* Please see {@link DeclarationDataCenter} for more information.
|
|
|
|
*/
|
|
|
|
|
2022-02-22 15:20:20 +00:00
|
|
|
const CACHE_DB_NAME = "declaration-data";
|
|
|
|
const CACHE_DB_VERSION = 1;
|
2022-07-20 23:40:04 +00:00
|
|
|
const CACHE_DB_KEY = "DECLARATIONS_KEY";
|
|
|
|
|
2022-02-22 04:40:14 +00:00
|
|
|
/**
|
|
|
|
* The DeclarationDataCenter is used for declaration searching.
|
2022-02-22 07:01:14 +00:00
|
|
|
*
|
2022-02-22 04:40:14 +00:00
|
|
|
* For usage, see the {@link init} and {@link search} methods.
|
|
|
|
*/
|
|
|
|
export class DeclarationDataCenter {
|
|
|
|
/**
|
2022-02-22 07:01:14 +00:00
|
|
|
* The declaration data. Users should not interact directly with this field.
|
|
|
|
*
|
2022-02-22 04:40:14 +00:00
|
|
|
* *NOTE:* This is not made private to support legacy browsers.
|
|
|
|
*/
|
2022-02-22 07:01:14 +00:00
|
|
|
declarationData = null;
|
2022-02-22 04:40:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to implement the singleton, in case we need to fetch data mutiple times in the same page.
|
|
|
|
*/
|
2023-07-20 20:42:49 +00:00
|
|
|
static requestSingleton = null;
|
2022-02-22 04:40:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a DeclarationDataCenter with given data.
|
2022-02-22 07:01:14 +00:00
|
|
|
*
|
2022-02-22 04:40:14 +00:00
|
|
|
* Please use {@link DeclarationDataCenter.init} instead, which automates the data fetching process.
|
|
|
|
* @param {*} declarationData
|
|
|
|
*/
|
|
|
|
constructor(declarationData) {
|
|
|
|
this.declarationData = declarationData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The actual constructor of DeclarationDataCenter
|
|
|
|
* @returns {Promise<DeclarationDataCenter>}
|
|
|
|
*/
|
|
|
|
static async init() {
|
2023-07-20 20:55:09 +00:00
|
|
|
if (DeclarationDataCenter.requestSingleton === null) {
|
|
|
|
DeclarationDataCenter.requestSingleton = DeclarationDataCenter.getData();
|
2022-02-22 04:40:14 +00:00
|
|
|
}
|
2023-07-20 20:55:09 +00:00
|
|
|
return await DeclarationDataCenter.requestSingleton;
|
2022-02-22 04:40:14 +00:00
|
|
|
}
|
|
|
|
|
2023-07-20 20:42:49 +00:00
|
|
|
static async getData() {
|
|
|
|
const dataListUrl = new URL(
|
|
|
|
`${SITE_ROOT}/declarations/declaration-data.bmp`,
|
|
|
|
window.location
|
|
|
|
);
|
|
|
|
|
|
|
|
// try to use cache first
|
|
|
|
const data = await fetchCachedDeclarationData().catch(_e => null);
|
|
|
|
if (data) {
|
|
|
|
// if data is defined, use the cached one.
|
2023-07-20 20:55:09 +00:00
|
|
|
return new DeclarationDataCenter(data);
|
2023-07-20 20:42:49 +00:00
|
|
|
} else {
|
|
|
|
// undefined. then fetch the data from the server.
|
|
|
|
const dataListRes = await fetch(dataListUrl);
|
|
|
|
const data = await dataListRes.json();
|
2023-07-20 21:41:44 +00:00
|
|
|
// TODO https://github.com/leanprover/doc-gen4/issues/133
|
|
|
|
// await cacheDeclarationData(data);
|
2023-07-20 20:55:09 +00:00
|
|
|
return new DeclarationDataCenter(data);
|
2023-07-20 20:42:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 04:40:14 +00:00
|
|
|
/**
|
|
|
|
* Search for a declaration.
|
|
|
|
* @returns {Array<any>}
|
|
|
|
*/
|
2023-01-31 19:31:27 +00:00
|
|
|
search(pattern, strict = true, allowedKinds=undefined, maxResults=undefined) {
|
2022-02-22 04:40:14 +00:00
|
|
|
if (!pattern) {
|
|
|
|
return [];
|
|
|
|
}
|
2022-02-22 07:01:14 +00:00
|
|
|
if (strict) {
|
2022-07-22 12:48:36 +00:00
|
|
|
let decl = this.declarationData.declarations[pattern];
|
2022-02-22 07:01:14 +00:00
|
|
|
return decl ? [decl] : [];
|
|
|
|
} else {
|
2023-01-31 19:31:27 +00:00
|
|
|
return getMatches(this.declarationData.declarations, pattern, allowedKinds, maxResults);
|
2022-02-22 07:01:14 +00:00
|
|
|
}
|
2022-02-22 04:40:14 +00:00
|
|
|
}
|
2022-07-22 14:15:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for all instances of a certain typeclass
|
|
|
|
* @returns {Array<String>}
|
|
|
|
*/
|
|
|
|
instancesForClass(className) {
|
|
|
|
const instances = this.declarationData.instances[className];
|
|
|
|
if (!instances) {
|
|
|
|
return [];
|
|
|
|
} else {
|
|
|
|
return instances;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-23 13:40:08 +00:00
|
|
|
/**
|
|
|
|
* Search for all instances that involve a certain type
|
|
|
|
* @returns {Array<String>}
|
|
|
|
*/
|
|
|
|
instancesForType(typeName) {
|
|
|
|
const instances = this.declarationData.instancesFor[typeName];
|
|
|
|
if (!instances) {
|
|
|
|
return [];
|
|
|
|
} else {
|
|
|
|
return instances;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-22 14:15:37 +00:00
|
|
|
/**
|
|
|
|
* Analogous to Lean declNameToLink
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
|
|
|
declNameToLink(declName) {
|
|
|
|
return this.declarationData.declarations[declName].docLink;
|
|
|
|
}
|
2022-07-22 14:56:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all modules that imported the given one.
|
|
|
|
* @returns {Array<String>}
|
|
|
|
*/
|
|
|
|
moduleImportedBy(moduleName) {
|
|
|
|
return this.declarationData.importedBy[moduleName];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Analogous to Lean moduleNameToLink
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
|
|
|
moduleNameToLink(moduleName) {
|
|
|
|
return this.declarationData.modules[moduleName];
|
|
|
|
}
|
2022-02-22 04:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isSeparater(char) {
|
|
|
|
return char === "." || char === "_";
|
|
|
|
}
|
|
|
|
|
2022-02-22 07:01:14 +00:00
|
|
|
// HACK: the fuzzy matching is quite hacky
|
|
|
|
|
|
|
|
function matchCaseSensitive(declName, lowerDeclName, pattern) {
|
2022-02-22 04:40:14 +00:00
|
|
|
let i = 0,
|
|
|
|
j = 0,
|
|
|
|
err = 0,
|
|
|
|
lastMatch = 0;
|
|
|
|
while (i < declName.length && j < pattern.length) {
|
2022-02-22 07:01:14 +00:00
|
|
|
if (pattern[j] === declName[i] || pattern[j] === lowerDeclName[i]) {
|
2022-02-22 04:40:14 +00:00
|
|
|
err += (isSeparater(pattern[j]) ? 0.125 : 1) * (i - lastMatch);
|
|
|
|
if (pattern[j] !== declName[i]) err += 0.5;
|
|
|
|
lastMatch = i + 1;
|
|
|
|
j++;
|
|
|
|
} else if (isSeparater(declName[i])) {
|
|
|
|
err += 0.125 * (i + 1 - lastMatch);
|
|
|
|
lastMatch = i + 1;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
err += 0.125 * (declName.length - lastMatch);
|
|
|
|
if (j === pattern.length) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-31 19:31:27 +00:00
|
|
|
function getMatches(declarations, pattern, allowedKinds = undefined, maxResults = undefined) {
|
2022-02-22 04:40:14 +00:00
|
|
|
const lowerPats = pattern.toLowerCase().split(/\s/g);
|
|
|
|
const patNoSpaces = pattern.replace(/\s/g, "");
|
|
|
|
const results = [];
|
2023-09-10 10:39:37 +00:00
|
|
|
for (const [name, {
|
2023-01-31 19:31:27 +00:00
|
|
|
kind,
|
2022-02-22 20:26:20 +00:00
|
|
|
docLink,
|
2022-07-22 12:48:36 +00:00
|
|
|
}] of Object.entries(declarations)) {
|
2023-01-31 19:31:27 +00:00
|
|
|
// Apply "kind" filter
|
|
|
|
if (allowedKinds !== undefined) {
|
|
|
|
if (!allowedKinds.has(kind)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2022-07-22 12:48:36 +00:00
|
|
|
const lowerName = name.toLowerCase();
|
2022-02-22 07:01:14 +00:00
|
|
|
let err = matchCaseSensitive(name, lowerName, patNoSpaces);
|
2022-02-22 04:40:14 +00:00
|
|
|
if (err !== undefined) {
|
2022-02-22 21:32:37 +00:00
|
|
|
results.push({
|
|
|
|
name,
|
2023-01-31 19:31:27 +00:00
|
|
|
kind,
|
2022-02-22 21:32:37 +00:00
|
|
|
err,
|
|
|
|
lowerName,
|
|
|
|
docLink,
|
|
|
|
});
|
2022-02-22 04:40:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return results.sort(({ err: a }, { err: b }) => a - b).slice(0, maxResults);
|
|
|
|
}
|
2022-02-22 15:20:20 +00:00
|
|
|
|
|
|
|
// TODO: refactor the indexedDB part to be more robust
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the indexedDB database, automatically initialized.
|
2022-02-22 21:32:37 +00:00
|
|
|
* @returns {Promise<IDBDatabase>}
|
2022-02-22 15:20:20 +00:00
|
|
|
*/
|
2022-02-22 21:32:37 +00:00
|
|
|
async function getDeclarationDatabase() {
|
2022-02-22 15:20:20 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const request = indexedDB.open(CACHE_DB_NAME, CACHE_DB_VERSION);
|
2022-02-22 21:32:37 +00:00
|
|
|
|
2022-02-22 15:20:20 +00:00
|
|
|
request.onerror = function (event) {
|
|
|
|
reject(
|
|
|
|
new Error(
|
|
|
|
`fail to open indexedDB ${CACHE_DB_NAME} of version ${CACHE_DB_VERSION}`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
request.onupgradeneeded = function (event) {
|
|
|
|
let db = event.target.result;
|
|
|
|
// We only need to store one object, so no key path or increment is needed.
|
2022-02-22 21:32:37 +00:00
|
|
|
db.createObjectStore("declaration");
|
2022-02-22 15:20:20 +00:00
|
|
|
};
|
|
|
|
request.onsuccess = function (event) {
|
2022-02-22 21:32:37 +00:00
|
|
|
resolve(event.target.result);
|
2022-02-22 15:20:20 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store data in indexedDB object store.
|
|
|
|
* @param {Map<string, any>} data
|
|
|
|
*/
|
2022-07-20 23:40:04 +00:00
|
|
|
async function cacheDeclarationData(data) {
|
2022-02-22 21:32:37 +00:00
|
|
|
let db = await getDeclarationDatabase();
|
|
|
|
let store = db
|
|
|
|
.transaction("declaration", "readwrite")
|
|
|
|
.objectStore("declaration");
|
2022-02-22 15:20:20 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let clearRequest = store.clear();
|
2022-07-20 23:40:04 +00:00
|
|
|
let addRequest = store.add(data, CACHE_DB_KEY);
|
2022-02-22 21:32:37 +00:00
|
|
|
|
|
|
|
addRequest.onsuccess = function (event) {
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
addRequest.onerror = function (event) {
|
|
|
|
reject(new Error(`fail to store declaration data`));
|
2022-02-22 15:20:20 +00:00
|
|
|
};
|
|
|
|
clearRequest.onerror = function (event) {
|
|
|
|
reject(new Error("fail to clear object store"));
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve data from indexedDB database.
|
|
|
|
* @returns {Promise<Map<string, any>|undefined>}
|
|
|
|
*/
|
2022-07-20 23:40:04 +00:00
|
|
|
async function fetchCachedDeclarationData() {
|
2022-02-22 21:32:37 +00:00
|
|
|
let db = await getDeclarationDatabase();
|
|
|
|
let store = db
|
|
|
|
.transaction("declaration", "readonly")
|
|
|
|
.objectStore("declaration");
|
2022-02-22 15:20:20 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-07-20 23:40:04 +00:00
|
|
|
let transactionRequest = store.get(CACHE_DB_KEY);
|
2022-02-22 15:20:20 +00:00
|
|
|
transactionRequest.onsuccess = function (event) {
|
2023-07-20 21:41:44 +00:00
|
|
|
// TODO: This API is not thought 100% through. If we have a DB cached
|
|
|
|
// already it will not even ask the remote for a new one so we end up
|
|
|
|
// with outdated declaration-data. This has to have some form of cache
|
|
|
|
// invalidation: https://github.com/leanprover/doc-gen4/issues/133
|
|
|
|
//resolve(event.target.result);
|
|
|
|
resolve(undefined);
|
2022-02-22 15:20:20 +00:00
|
|
|
};
|
|
|
|
transactionRequest.onerror = function (event) {
|
|
|
|
reject(new Error(`fail to store declaration data`));
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|