@eagleoutice/flowr
Version:
Static Dataflow Analyzer and Program Slicer for the R Programming Language
130 lines • 5.84 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FilesQueryDefinition = void 0;
const ansi_1 = require("../../../util/text/ansi");
const joi_1 = __importDefault(require("joi"));
const files_query_executor_1 = require("./files-query-executor");
const flowr_file_1 = require("../../../project/context/flowr-file");
const json_1 = require("../../../util/json");
const retriever_1 = require("../../../r-bridge/retriever");
function summarizeObjectWithLimit(obj, limitChars = 500, limitLines = 10) {
const str = JSON.stringify(obj, json_1.jsonReplacer, 2);
if (str.split('\n').length > limitLines) {
const lines = str.split('\n').slice(0, limitLines);
return lines.join('\n') + '\n... (truncated)';
}
else if (str.length > limitChars) {
return str.slice(0, limitChars) + '... (truncated)';
}
else {
return str;
}
}
function rolesFromInput(rolesPart) {
return rolesPart
.reduce((acc, roleName) => {
roleName = roleName.trim();
// check if it is one of the values
if (Object.values(flowr_file_1.FileRole).includes(roleName)) {
acc.valid.push(roleName);
}
else {
acc.invalid.push(roleName);
}
return acc;
}, { valid: [], invalid: [] });
}
const rolePrefix = 'role:';
function filesQueryLineParser(output, line, _config) {
let roles = undefined;
let input = undefined;
if (line.length > 0 && line[0].startsWith(rolePrefix)) {
const rolesPart = line[0].slice(rolePrefix.length).split(',');
const parseResult = rolesFromInput(rolesPart);
if (parseResult.invalid.length > 0) {
output.stderr(`Invalid roles: ${parseResult.invalid.map(r => (0, ansi_1.bold)(r, output.formatter)).join(', ')}`
+ `\nValid roles are: ${Object.values(flowr_file_1.FileRole).map(r => (0, ansi_1.bold)(r, output.formatter)).join(', ')}`);
}
roles = parseResult.valid;
input = line[1];
}
else if (line.length > 0) {
input = line[0];
}
return { query: [{ type: 'files', roles }], rCode: input };
}
function filesQueryCompleter(line, startingNewArg, _config) {
const rolesPrefixNotPresent = line.length == 0 || (line.length == 1 && line[0].length < rolePrefix.length);
const rolesNotFinished = line.length == 1 && line[0].startsWith(rolePrefix) && !startingNewArg;
const endOfRoles = line.length == 1 && startingNewArg || line.length == 2;
if (rolesPrefixNotPresent) {
return { completions: [`${rolePrefix}`] };
}
else if (endOfRoles) {
return { completions: [retriever_1.fileProtocol] };
}
else if (rolesNotFinished) {
const rolesWithoutPrefix = line[0].slice(rolePrefix.length);
const usedRoles = rolesWithoutPrefix.split(',').map(r => r.trim());
const allRoles = Object.values(flowr_file_1.FileRole);
const unusedRoles = allRoles.filter(r => !usedRoles.includes(r));
const lastRole = usedRoles[usedRoles.length - 1];
const lastRoleIsUnfinished = !allRoles.includes(lastRole);
if (lastRoleIsUnfinished) {
return { completions: unusedRoles, argumentPart: lastRole };
}
else if (unusedRoles.length > 0) {
return { completions: [','], argumentPart: '' };
}
else {
return { completions: [' '], argumentPart: '' };
}
}
return { completions: [] };
}
function guessProto(obj) {
if (obj === null || obj === undefined) {
return undefined;
}
else if (typeof obj !== 'object') {
return typeof obj;
}
else if ('prototype' in obj && obj.prototype && typeof obj.prototype === 'object') {
const proto = obj.prototype;
if (proto.constructor && typeof proto.constructor.name === 'string') {
return proto.constructor.name;
}
}
return undefined;
}
exports.FilesQueryDefinition = {
executor: files_query_executor_1.executeFileQuery,
asciiSummarizer: (formatter, _analyzer, queryResults, result) => {
const out = queryResults;
result.push(`Query: ${(0, ansi_1.bold)('files', formatter)} (${out['.meta'].timing.toFixed(0)}ms)`);
result.push(` ╰ Found ${out.files.length} file${out.files.length === 1 ? '' : 's'}`);
for (const f of out.files) {
const nam = guessProto(f.content);
result.push(` ╰ ${(0, ansi_1.bold)(f.path, formatter)}${nam ? ' ' + formatter.format(nam, { effect: ansi_1.ColorEffect.Foreground, color: 7 /* Colors.White */ }) : ''}${f.roles ? ` [role: ${f.roles.join(', ')}]` : ''}:`);
const summary = summarizeObjectWithLimit(f.content, 250);
for (const line of summary.split('\n')) {
result.push(` ${line}`);
}
}
return true;
},
completer: filesQueryCompleter,
fromLine: filesQueryLineParser,
schema: joi_1.default.object({
type: joi_1.default.string().valid('files').required().description('The type of the query.'),
roles: joi_1.default.array().optional().items(joi_1.default.string().valid(...Object.values(flowr_file_1.FileRole))).description('Optional roles of the files to query. If not provided, all roles are considered.'),
matchesPathRegex: joi_1.default.string().optional().description('An optional regular expression to match the file paths against.')
}).description('The file query finds files in the project based on their roles and path patterns.'),
flattenInvolvedNodes: (_) => {
return [];
}
};
//# sourceMappingURL=files-query-format.js.map