zulip/static/js/search_suggestion.js

855 lines
28 KiB
JavaScript
Raw Normal View History

exports.max_num_of_search_results = 12;
function stream_matches_query(stream_name, q) {
return common.phrase_match(q, stream_name);
}
function make_person_highlighter(query) {
const hilite = typeahead_helper.make_query_highlighter(query);
return function (person) {
if (settings_org.show_email()) {
return hilite(person.full_name) + " <" + hilite(person.email) + ">";
}
return hilite(person.full_name);
};
}
function match_criteria(operators, criteria) {
const filter = new Filter(operators);
return _.any(criteria, function (cr) {
if (_.has(cr, 'operand')) {
return filter.has_operand(cr.operator, cr.operand);
}
return filter.has_operator(cr.operator);
});
}
function check_validity(last, operators, valid, invalid) {
// valid: list of strings valid for the last operator
// invalid: list of operators invalid for any previous operators except last.
js: Convert a.indexOf(…) !== -1 to a.includes(…). Babel polyfills this for us for Internet Explorer. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import K from "ast-types/gen/kinds"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; recast.visit(ast, { visitBinaryExpression(path) { const { operator, left, right } = path.node; if ( n.CallExpression.check(left) && n.MemberExpression.check(left.callee) && !left.callee.computed && n.Identifier.check(left.callee.property) && left.callee.property.name === "indexOf" && left.arguments.length === 1 && checkExpression(left.arguments[0]) && ((["===", "!==", "==", "!=", ">", "<="].includes(operator) && n.UnaryExpression.check(right) && right.operator == "-" && n.Literal.check(right.argument) && right.argument.value === 1) || ([">=", "<"].includes(operator) && n.Literal.check(right) && right.value === 0)) ) { const test = b.callExpression( b.memberExpression(left.callee.object, b.identifier("includes")), [left.arguments[0]] ); path.replace( ["!==", "!=", ">", ">="].includes(operator) ? test : b.unaryExpression("!", test) ); changed = true; } this.traverse(path); }, }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-08 04:55:06 +01:00
if (!valid.includes(last.operator)) {
return false;
}
if (match_criteria(operators, invalid)) {
return false;
}
return true;
}
function format_as_suggestion(terms) {
return {
description: Filter.describe(terms),
search_string: Filter.unparse(terms),
};
}
function compare_by_huddle(huddle) {
js: Convert _.map(a, …) to a.map(…). And convert the corresponding function expressions to arrow style while we’re here. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import K from "ast-types/gen/kinds"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; recast.visit(ast, { visitCallExpression(path) { const { callee, arguments: args } = path.node; if ( n.MemberExpression.check(callee) && !callee.computed && n.Identifier.check(callee.object) && callee.object.name === "_" && n.Identifier.check(callee.property) && callee.property.name === "map" && args.length === 2 && checkExpression(args[0]) && checkExpression(args[1]) ) { const [arr, fn] = args; path.replace( b.callExpression(b.memberExpression(arr, b.identifier("map")), [ n.FunctionExpression.check(fn) || n.ArrowFunctionExpression.check(fn) ? b.arrowFunctionExpression( fn.params, n.BlockStatement.check(fn.body) && fn.body.body.length === 1 && n.ReturnStatement.check(fn.body.body[0]) ? fn.body.body[0].argument || b.identifier("undefined") : fn.body ) : fn, ]) ); changed = true; } this.traverse(path); }, }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-08 02:43:49 +01:00
huddle = huddle.slice(0, -1).map(person => {
person = people.get_by_email(person);
js: Convert _.map(a, …) to a.map(…). And convert the corresponding function expressions to arrow style while we’re here. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import K from "ast-types/gen/kinds"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; recast.visit(ast, { visitCallExpression(path) { const { callee, arguments: args } = path.node; if ( n.MemberExpression.check(callee) && !callee.computed && n.Identifier.check(callee.object) && callee.object.name === "_" && n.Identifier.check(callee.property) && callee.property.name === "map" && args.length === 2 && checkExpression(args[0]) && checkExpression(args[1]) ) { const [arr, fn] = args; path.replace( b.callExpression(b.memberExpression(arr, b.identifier("map")), [ n.FunctionExpression.check(fn) || n.ArrowFunctionExpression.check(fn) ? b.arrowFunctionExpression( fn.params, n.BlockStatement.check(fn.body) && fn.body.body.length === 1 && n.ReturnStatement.check(fn.body.body[0]) ? fn.body.body[0].argument || b.identifier("undefined") : fn.body ) : fn, ]) ); changed = true; } this.traverse(path); }, }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-08 02:43:49 +01:00
return person && person.user_id;
});
// Construct dict for all huddles, so we can lookup each's recency
const huddles = activity.get_huddles();
const huddle_dict = {};
for (let i = 0; i < huddles.length; i += 1) {
huddle_dict[huddles[i]] = i + 1;
}
return function (person1, person2) {
const huddle1 = huddle.concat(person1.user_id).sort().join(',');
const huddle2 = huddle.concat(person2.user_id).sort().join(',');
// If not in the dict, assign an arbitrarily high index
const score1 = huddle_dict[huddle1] || 100;
const score2 = huddle_dict[huddle2] || 100;
const diff = score1 - score2;
if (diff !== 0) {
return diff;
}
return typeahead_helper.compare_by_pms(person1, person2);
};
}
function get_stream_suggestions(last, operators) {
const valid = ['stream', 'search', ''];
const invalid = [
{operator: 'stream'},
{operator: 'streams'},
{operator: 'is', operand: 'private'},
{operator: 'pm-with'},
];
if (!check_validity(last, operators, valid, invalid)) {
return [];
}
const query = last.operand;
let streams = stream_data.subscribed_streams();
streams = _.filter(streams, function (stream) {
return stream_matches_query(stream, query);
});
streams = typeahead_helper.sorter(query, streams);
const regex = typeahead_helper.build_highlight_regex(query);
const hilite = typeahead_helper.highlight_with_escaping_and_regex;
js: Convert _.map(a, …) to a.map(…). And convert the corresponding function expressions to arrow style while we’re here. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import K from "ast-types/gen/kinds"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; recast.visit(ast, { visitCallExpression(path) { const { callee, arguments: args } = path.node; if ( n.MemberExpression.check(callee) && !callee.computed && n.Identifier.check(callee.object) && callee.object.name === "_" && n.Identifier.check(callee.property) && callee.property.name === "map" && args.length === 2 && checkExpression(args[0]) && checkExpression(args[1]) ) { const [arr, fn] = args; path.replace( b.callExpression(b.memberExpression(arr, b.identifier("map")), [ n.FunctionExpression.check(fn) || n.ArrowFunctionExpression.check(fn) ? b.arrowFunctionExpression( fn.params, n.BlockStatement.check(fn.body) && fn.body.body.length === 1 && n.ReturnStatement.check(fn.body.body[0]) ? fn.body.body[0].argument || b.identifier("undefined") : fn.body ) : fn, ]) ); changed = true; } this.traverse(path); }, }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-08 02:43:49 +01:00
const objs = streams.map(stream => {
const prefix = 'stream';
const highlighted_stream = hilite(regex, stream);
const verb = last.negated ? 'exclude ' : '';
const description = verb + prefix + ' ' + highlighted_stream;
const term = {
operator: 'stream',
operand: stream,
negated: last.negated,
};
const search_string = Filter.unparse([term]);
return {description: description, search_string: search_string};
});
return objs;
}
function get_group_suggestions(last, operators) {
if (!check_validity(last, operators, ['pm-with'], [{operator: 'stream'}])) {
return [];
}
const operand = last.operand;
const negated = last.negated;
// The operand has the form "part1,part2,pa", where all but the last part
// are emails, and the last part is an arbitrary query.
//
// We only generate group suggestions when there's more than one part, and
// we only use the last part to generate suggestions.
const last_comma_index = operand.lastIndexOf(',');
if (last_comma_index < 0) {
return [];
}
// Neither all_but_last_part nor last_part include the final comma.
const all_but_last_part = operand.slice(0, last_comma_index);
const last_part = operand.slice(last_comma_index + 1);
// We don't suggest a person if their email is already present in the
// operand (not including the last part).
const parts = all_but_last_part.split(',').concat(people.my_current_email());
const person_matcher = people.build_person_matcher(last_part);
let persons = people.filter_all_persons(function (person) {
if (_.contains(parts, person.email)) {
return false;
}
return last_part === '' || person_matcher(person);
});
persons.sort(compare_by_huddle(parts));
// Take top 15 persons, since they're ordered by pm_recipient_count.
persons = persons.slice(0, 15);
const prefix = Filter.operator_to_prefix('pm-with', negated);
const highlight_person = make_person_highlighter(last_part);
js: Convert _.map(a, …) to a.map(…). And convert the corresponding function expressions to arrow style while we’re here. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import K from "ast-types/gen/kinds"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; recast.visit(ast, { visitCallExpression(path) { const { callee, arguments: args } = path.node; if ( n.MemberExpression.check(callee) && !callee.computed && n.Identifier.check(callee.object) && callee.object.name === "_" && n.Identifier.check(callee.property) && callee.property.name === "map" && args.length === 2 && checkExpression(args[0]) && checkExpression(args[1]) ) { const [arr, fn] = args; path.replace( b.callExpression(b.memberExpression(arr, b.identifier("map")), [ n.FunctionExpression.check(fn) || n.ArrowFunctionExpression.check(fn) ? b.arrowFunctionExpression( fn.params, n.BlockStatement.check(fn.body) && fn.body.body.length === 1 && n.ReturnStatement.check(fn.body.body[0]) ? fn.body.body[0].argument || b.identifier("undefined") : fn.body ) : fn, ]) ); changed = true; } this.traverse(path); }, }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-08 02:43:49 +01:00
const suggestions = persons.map(person => {
const term = {
operator: 'pm-with',
operand: all_but_last_part + ',' + person.email,
negated: negated,
};
const name = highlight_person(person);
const description = prefix + ' ' + Handlebars.Utils.escapeExpression(all_but_last_part) + ',' + name;
let terms = [term];
if (negated) {
terms = [{operator: 'is', operand: 'private'}, term];
}
const search_string = Filter.unparse(terms);
return {description: description, search_string: search_string};
});
return suggestions;
}
function make_people_getter(last) {
let persons;
/* The next function will be called between 0 and 4
times for each keystroke in a search, but we will
only do real work one time.
*/
return function () {
if (persons !== undefined) {
return persons;
}
let query;
// This next block is designed to match the behavior of the
// `is:private` block in get_person_suggestions
if (last.operator === "is" && last.operand === "private") {
query = '';
} else {
query = last.operand;
}
persons = people.get_people_for_search_bar(query);
persons.sort(typeahead_helper.compare_by_pms);
return persons;
};
}
// Possible args for autocomplete_operator: pm-with, sender, from
function get_person_suggestions(people_getter, last, operators, autocomplete_operator) {
if (last.operator === "is" && last.operand === "private") {
// Interpret 'is:private' as equivalent to 'pm-with:'
last = {operator: "pm-with", operand: "", negated: false};
}
const query = last.operand;
// Be especially strict about the less common "from" operator.
if (autocomplete_operator === 'from' && last.operator !== 'from') {
return [];
}
const valid = ['search', autocomplete_operator];
let invalid;
if (autocomplete_operator === 'pm-with') {
invalid = [{operator: 'pm-with'}, {operator: 'stream'}];
} else {
// If not pm-with, then this must either be 'sender' or 'from'
invalid = [{operator: 'sender'}, {operator: 'from'}];
}
if (!check_validity(last, operators, valid, invalid)) {
return [];
}
const persons = people_getter();
const prefix = Filter.operator_to_prefix(autocomplete_operator, last.negated);
const highlight_person = make_person_highlighter(query);
js: Convert _.map(a, …) to a.map(…). And convert the corresponding function expressions to arrow style while we’re here. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import K from "ast-types/gen/kinds"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; recast.visit(ast, { visitCallExpression(path) { const { callee, arguments: args } = path.node; if ( n.MemberExpression.check(callee) && !callee.computed && n.Identifier.check(callee.object) && callee.object.name === "_" && n.Identifier.check(callee.property) && callee.property.name === "map" && args.length === 2 && checkExpression(args[0]) && checkExpression(args[1]) ) { const [arr, fn] = args; path.replace( b.callExpression(b.memberExpression(arr, b.identifier("map")), [ n.FunctionExpression.check(fn) || n.ArrowFunctionExpression.check(fn) ? b.arrowFunctionExpression( fn.params, n.BlockStatement.check(fn.body) && fn.body.body.length === 1 && n.ReturnStatement.check(fn.body.body[0]) ? fn.body.body[0].argument || b.identifier("undefined") : fn.body ) : fn, ]) ); changed = true; } this.traverse(path); }, }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-08 02:43:49 +01:00
const objs = persons.map(person => {
const name = highlight_person(person);
const description = prefix + ' ' + name;
const terms = [{
operator: autocomplete_operator,
operand: person.email,
negated: last.negated,
}];
if (autocomplete_operator === 'pm-with' && last.negated) {
// In the special case of '-pm-with', add 'is:private' before it
// because we assume the user still wants to narrow to PMs
terms.unshift({operator: 'is', operand: 'private'});
}
const search_string = Filter.unparse(terms);
return {description: description, search_string: search_string};
});
return objs;
}
function get_default_suggestion(operators) {
// Here we return the canonical suggestion for the last query that the
// user typed.
if (operators !== undefined && operators.length > 0) {
return format_as_suggestion(operators);
}
return false;
}
function get_default_suggestion_legacy(operators) {
// Here we return the canonical suggestion for the full query that the
// user typed. (The caller passes us the parsed query as "operators".)
if (operators.length === 0) {
return {description: '', search_string: ''};
}
return format_as_suggestion(operators);
}
function get_topic_suggestions(last, operators) {
const invalid = [
{operator: 'pm-with'},
{operator: 'is', operand: 'private'},
{operator: 'topic'},
];
if (!check_validity(last, operators, ['stream', 'topic', 'search'], invalid)) {
return [];
}
const operator = Filter.canonicalize_operator(last.operator);
const operand = last.operand;
const negated = operator === 'topic' && last.negated;
let stream;
let guess;
const filter = new Filter(operators);
const suggest_operators = [];
// stream:Rome -> show all Rome topics
// stream:Rome topic: -> show all Rome topics
// stream:Rome f -> show all Rome topics with a word starting in f
// stream:Rome topic:f -> show all Rome topics with a word starting in f
// stream:Rome topic:f -> show all Rome topics with a word starting in f
// When narrowed to a stream:
// topic: -> show all topics in current stream
// foo -> show all topics in current stream with words starting with foo
// If somebody explicitly types search:, then we might
// not want to suggest topics, but I feel this is a very
// minor issue, and Filter.parse() is currently lossy
// in terms of telling us whether they provided the operator,
// i.e. "foo" and "search:foo" both become [{operator: 'search', operand: 'foo'}].
switch (operator) {
case 'stream':
guess = '';
stream = operand;
suggest_operators.push(last);
break;
case 'topic':
case 'search':
guess = operand;
if (filter.has_operator('stream')) {
stream = filter.operands('stream')[0];
} else {
stream = narrow_state.stream();
suggest_operators.push({operator: 'stream', operand: stream});
}
break;
}
if (!stream) {
return [];
}
const stream_id = stream_data.get_stream_id(stream);
if (!stream_id) {
return [];
}
let topics = topic_data.get_recent_names(stream_id);
if (!topics || !topics.length) {
return [];
}
// Be defensive here in case stream_data.get_recent_topics gets
// super huge, but still slice off enough topics to find matches.
topics = topics.slice(0, 300);
if (guess !== '') {
topics = _.filter(topics, function (topic) {
return common.phrase_match(guess, topic);
});
}
topics = topics.slice(0, 10);
// Just use alphabetical order. While recency and read/unreadness of
// topics do matter in some contexts, you can get that from the left sidebar,
// and I'm leaning toward high scannability for autocompletion. I also don't
// care about case.
topics.sort();
js: Convert _.map(a, …) to a.map(…). And convert the corresponding function expressions to arrow style while we’re here. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import K from "ast-types/gen/kinds"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; recast.visit(ast, { visitCallExpression(path) { const { callee, arguments: args } = path.node; if ( n.MemberExpression.check(callee) && !callee.computed && n.Identifier.check(callee.object) && callee.object.name === "_" && n.Identifier.check(callee.property) && callee.property.name === "map" && args.length === 2 && checkExpression(args[0]) && checkExpression(args[1]) ) { const [arr, fn] = args; path.replace( b.callExpression(b.memberExpression(arr, b.identifier("map")), [ n.FunctionExpression.check(fn) || n.ArrowFunctionExpression.check(fn) ? b.arrowFunctionExpression( fn.params, n.BlockStatement.check(fn.body) && fn.body.body.length === 1 && n.ReturnStatement.check(fn.body.body[0]) ? fn.body.body[0].argument || b.identifier("undefined") : fn.body ) : fn, ]) ); changed = true; } this.traverse(path); }, }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-08 02:43:49 +01:00
return topics.map(topic => {
const topic_term = {operator: 'topic', operand: topic, negated: negated};
const operators = suggest_operators.concat([topic_term]);
return format_as_suggestion(operators);
});
}
function get_operator_subset_suggestions(operators) {
// For stream:a topic:b search:c, suggest:
// stream:a topic:b
// stream:a
if (operators.length < 1) {
return [];
}
let i;
const suggestions = [];
for (i = operators.length - 1; i >= 1; i -= 1) {
const subset = operators.slice(0, i);
suggestions.push(format_as_suggestion(subset));
}
return suggestions;
}
function get_special_filter_suggestions(last, operators, suggestions) {
const is_search_operand_negated = last.operator === 'search' && last.operand[0] === '-';
// Negating suggestions on is_search_operand_negated is required for
// suggesting negated operators.
if (last.negated || is_search_operand_negated) {
js: Convert _.map(a, …) to a.map(…). And convert the corresponding function expressions to arrow style while we’re here. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import K from "ast-types/gen/kinds"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; recast.visit(ast, { visitCallExpression(path) { const { callee, arguments: args } = path.node; if ( n.MemberExpression.check(callee) && !callee.computed && n.Identifier.check(callee.object) && callee.object.name === "_" && n.Identifier.check(callee.property) && callee.property.name === "map" && args.length === 2 && checkExpression(args[0]) && checkExpression(args[1]) ) { const [arr, fn] = args; path.replace( b.callExpression(b.memberExpression(arr, b.identifier("map")), [ n.FunctionExpression.check(fn) || n.ArrowFunctionExpression.check(fn) ? b.arrowFunctionExpression( fn.params, n.BlockStatement.check(fn.body) && fn.body.body.length === 1 && n.ReturnStatement.check(fn.body.body[0]) ? fn.body.body[0].argument || b.identifier("undefined") : fn.body ) : fn, ]) ); changed = true; } this.traverse(path); }, }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-08 02:43:49 +01:00
suggestions = suggestions.map(suggestion => ({
search_string: '-' + suggestion.search_string,
description: 'exclude ' + suggestion.description,
invalid: suggestion.invalid,
}));
}
const last_string = Filter.unparse([last]).toLowerCase();
suggestions = _.filter(suggestions, function (s) {
if (match_criteria(operators, s.invalid)) {
return false;
}
if (last_string === '') {
return true;
}
// returns the substring after the ":" symbol.
const suggestion_operand = s.search_string.substring(s.search_string.indexOf(":") + 1);
// e.g for `att` search query, `has:attachment` should be suggested.
const show_operator_suggestions = last.operator === 'search' &&
suggestion_operand.toLowerCase().startsWith(last_string);
return s.search_string.toLowerCase().startsWith(last_string) ||
show_operator_suggestions ||
s.description.toLowerCase().startsWith(last_string);
});
// Only show home if there's an empty bar
if (operators.length === 0 && last_string === '') {
suggestions.unshift({search_string: '', description: 'All messages'});
}
return suggestions;
}
function get_streams_filter_suggestions(last, operators) {
const suggestions = [
{
search_string: 'streams:public',
description: 'All public streams in organization',
invalid: [
{operator: 'is', operand: 'private'},
{operator: 'stream'},
{operator: 'group-pm-with'},
{operator: 'pm-with'},
{operator: 'in'},
{operator: 'streams'},
],
},
];
return get_special_filter_suggestions(last, operators, suggestions);
}
function get_is_filter_suggestions(last, operators) {
const suggestions = [
{
search_string: 'is:private',
description: 'private messages',
invalid: [
{operator: 'is', operand: 'private'},
{operator: 'stream'},
{operator: 'pm-with'},
{operator: 'in'},
],
},
{
search_string: 'is:starred',
description: 'starred messages',
invalid: [
{operator: 'is', operand: 'starred'},
],
},
{
search_string: 'is:mentioned',
description: '@-mentions',
invalid: [
{operator: 'is', operand: 'mentioned'},
],
},
{
search_string: 'is:alerted',
description: 'alerted messages',
invalid: [
{operator: 'is', operand: 'alerted'},
],
},
{
search_string: 'is:unread',
description: 'unread messages',
invalid: [
{operator: 'is', operand: 'unread'},
],
},
];
return get_special_filter_suggestions(last, operators, suggestions);
}
function get_has_filter_suggestions(last, operators) {
const suggestions = [
{
search_string: 'has:link',
description: 'messages with one or more link',
invalid: [
{operator: 'has', operand: 'link'},
],
},
{
search_string: 'has:image',
description: 'messages with one or more image',
invalid: [
{operator: 'has', operand: 'image'},
],
},
{
search_string: 'has:attachment',
description: 'messages with one or more attachment',
invalid: [
{operator: 'has', operand: 'attachment'},
],
},
];
return get_special_filter_suggestions(last, operators, suggestions);
}
function get_sent_by_me_suggestions(last, operators) {
const last_string = Filter.unparse([last]).toLowerCase();
const negated = last.negated || last.operator === 'search' && last.operand[0] === '-';
const negated_symbol = negated ? '-' : '';
const verb = negated ? 'exclude ' : '';
const sender_query = negated_symbol + 'sender:' + people.my_current_email();
const from_query = negated_symbol + 'from:' + people.my_current_email();
const sender_me_query = negated_symbol + 'sender:me';
const from_me_query = negated_symbol + 'from:me';
const sent_string = negated_symbol + 'sent';
const description = verb + 'sent by me';
const invalid = [
{operator: 'sender'},
{operator: 'from'},
];
if (match_criteria(operators, invalid)) {
return [];
}
if (last.operator === '' ||
sender_query.startsWith(last_string) ||
sender_me_query.startsWith(last_string) ||
last_string === sent_string
) {
return [
{
search_string: sender_query,
description: description,
},
];
} else if (from_query.startsWith(last_string) ||
from_me_query.startsWith(last_string)
) {
return [
{
search_string: from_query,
description: description,
},
];
}
return [];
}
function get_operator_suggestions(last) {
if (!(last.operator === 'search')) {
return [];
}
let last_operand = last.operand;
let negated = false;
if (last_operand.startsWith("-")) {
negated = true;
last_operand = last_operand.slice(1);
}
let choices = ['stream', 'topic', 'pm-with', 'sender', 'near', 'from', 'group-pm-with'];
choices = _.filter(choices, function (choice) {
return common.phrase_match(last_operand, choice);
});
js: Convert _.map(a, …) to a.map(…). And convert the corresponding function expressions to arrow style while we’re here. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import K from "ast-types/gen/kinds"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; recast.visit(ast, { visitCallExpression(path) { const { callee, arguments: args } = path.node; if ( n.MemberExpression.check(callee) && !callee.computed && n.Identifier.check(callee.object) && callee.object.name === "_" && n.Identifier.check(callee.property) && callee.property.name === "map" && args.length === 2 && checkExpression(args[0]) && checkExpression(args[1]) ) { const [arr, fn] = args; path.replace( b.callExpression(b.memberExpression(arr, b.identifier("map")), [ n.FunctionExpression.check(fn) || n.ArrowFunctionExpression.check(fn) ? b.arrowFunctionExpression( fn.params, n.BlockStatement.check(fn.body) && fn.body.body.length === 1 && n.ReturnStatement.check(fn.body.body[0]) ? fn.body.body[0].argument || b.identifier("undefined") : fn.body ) : fn, ]) ); changed = true; } this.traverse(path); }, }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-08 02:43:49 +01:00
return choices.map(choice => {
const op = [{operator: choice, operand: '', negated: negated}];
return format_as_suggestion(op);
});
}
function make_attacher(base) {
const self = {};
self.result = [];
const prev = {};
function prepend_base(suggestion) {
if (base && base.description.length > 0) {
suggestion.search_string = base.search_string + " " + suggestion.search_string;
suggestion.description = base.description + ", " + suggestion.description;
}
}
self.push = function (suggestion) {
if (!prev[suggestion.search_string]) {
prev[suggestion.search_string] = suggestion;
self.result.push(suggestion);
}
};
self.concat = function (suggestions) {
_.each(suggestions, self.push);
};
self.attach_many = function (suggestions) {
js: Automatically convert _.each to for…of. This commit was automatically generated by the following script, followed by lint --fix and a few small manual lint-related cleanups. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import { Context } from "ast-types/lib/path-visitor"; import K from "ast-types/gen/kinds"; import { NodePath } from "ast-types/lib/node-path"; import assert from "assert"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); const checkStatement = (node: n.Node): node is K.StatementKind => n.Statement.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; let inLoop = false; let replaceReturn = false; const visitLoop = (...args: string[]) => function(this: Context, path: NodePath) { for (const arg of args) { this.visit(path.get(arg)); } const old = { inLoop }; inLoop = true; this.visit(path.get("body")); inLoop = old.inLoop; return false; }; recast.visit(ast, { visitDoWhileStatement: visitLoop("test"), visitExpressionStatement(path) { const { expression, comments } = path.node; let valueOnly; if ( n.CallExpression.check(expression) && n.MemberExpression.check(expression.callee) && !expression.callee.computed && n.Identifier.check(expression.callee.object) && expression.callee.object.name === "_" && n.Identifier.check(expression.callee.property) && ["each", "forEach"].includes(expression.callee.property.name) && [2, 3].includes(expression.arguments.length) && checkExpression(expression.arguments[0]) && (n.FunctionExpression.check(expression.arguments[1]) || n.ArrowFunctionExpression.check(expression.arguments[1])) && [1, 2].includes(expression.arguments[1].params.length) && n.Identifier.check(expression.arguments[1].params[0]) && ((valueOnly = expression.arguments[1].params[1] === undefined) || n.Identifier.check(expression.arguments[1].params[1])) && (expression.arguments[2] === undefined || n.ThisExpression.check(expression.arguments[2])) ) { const old = { inLoop, replaceReturn }; inLoop = false; replaceReturn = true; this.visit( path .get("expression") .get("arguments") .get(1) .get("body") ); inLoop = old.inLoop; replaceReturn = old.replaceReturn; const [right, { body, params }] = expression.arguments; const loop = b.forOfStatement( b.variableDeclaration("let", [ b.variableDeclarator( valueOnly ? params[0] : b.arrayPattern([params[1], params[0]]) ), ]), valueOnly ? right : b.callExpression( b.memberExpression(right, b.identifier("entries")), [] ), checkStatement(body) ? body : b.expressionStatement(body) ); loop.comments = comments; path.replace(loop); changed = true; } this.traverse(path); }, visitForStatement: visitLoop("init", "test", "update"), visitForInStatement: visitLoop("left", "right"), visitForOfStatement: visitLoop("left", "right"), visitFunction(path) { this.visit(path.get("params")); const old = { replaceReturn }; replaceReturn = false; this.visit(path.get("body")); replaceReturn = old.replaceReturn; return false; }, visitReturnStatement(path) { if (replaceReturn) { assert(!inLoop); // could use labeled continue if this ever fires const { argument, comments } = path.node; if (argument === null) { const s = b.continueStatement(); s.comments = comments; path.replace(s); } else { const s = b.expressionStatement(argument); s.comments = comments; path.replace(s, b.continueStatement()); } return false; } this.traverse(path); }, visitWhileStatement: visitLoop("test"), }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
for (const suggestion of suggestions) {
prepend_base(suggestion);
self.push(suggestion);
js: Automatically convert _.each to for…of. This commit was automatically generated by the following script, followed by lint --fix and a few small manual lint-related cleanups. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import { Context } from "ast-types/lib/path-visitor"; import K from "ast-types/gen/kinds"; import { NodePath } from "ast-types/lib/node-path"; import assert from "assert"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); const checkStatement = (node: n.Node): node is K.StatementKind => n.Statement.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; let inLoop = false; let replaceReturn = false; const visitLoop = (...args: string[]) => function(this: Context, path: NodePath) { for (const arg of args) { this.visit(path.get(arg)); } const old = { inLoop }; inLoop = true; this.visit(path.get("body")); inLoop = old.inLoop; return false; }; recast.visit(ast, { visitDoWhileStatement: visitLoop("test"), visitExpressionStatement(path) { const { expression, comments } = path.node; let valueOnly; if ( n.CallExpression.check(expression) && n.MemberExpression.check(expression.callee) && !expression.callee.computed && n.Identifier.check(expression.callee.object) && expression.callee.object.name === "_" && n.Identifier.check(expression.callee.property) && ["each", "forEach"].includes(expression.callee.property.name) && [2, 3].includes(expression.arguments.length) && checkExpression(expression.arguments[0]) && (n.FunctionExpression.check(expression.arguments[1]) || n.ArrowFunctionExpression.check(expression.arguments[1])) && [1, 2].includes(expression.arguments[1].params.length) && n.Identifier.check(expression.arguments[1].params[0]) && ((valueOnly = expression.arguments[1].params[1] === undefined) || n.Identifier.check(expression.arguments[1].params[1])) && (expression.arguments[2] === undefined || n.ThisExpression.check(expression.arguments[2])) ) { const old = { inLoop, replaceReturn }; inLoop = false; replaceReturn = true; this.visit( path .get("expression") .get("arguments") .get(1) .get("body") ); inLoop = old.inLoop; replaceReturn = old.replaceReturn; const [right, { body, params }] = expression.arguments; const loop = b.forOfStatement( b.variableDeclaration("let", [ b.variableDeclarator( valueOnly ? params[0] : b.arrayPattern([params[1], params[0]]) ), ]), valueOnly ? right : b.callExpression( b.memberExpression(right, b.identifier("entries")), [] ), checkStatement(body) ? body : b.expressionStatement(body) ); loop.comments = comments; path.replace(loop); changed = true; } this.traverse(path); }, visitForStatement: visitLoop("init", "test", "update"), visitForInStatement: visitLoop("left", "right"), visitForOfStatement: visitLoop("left", "right"), visitFunction(path) { this.visit(path.get("params")); const old = { replaceReturn }; replaceReturn = false; this.visit(path.get("body")); replaceReturn = old.replaceReturn; return false; }, visitReturnStatement(path) { if (replaceReturn) { assert(!inLoop); // could use labeled continue if this ever fires const { argument, comments } = path.node; if (argument === null) { const s = b.continueStatement(); s.comments = comments; path.replace(s); } else { const s = b.expressionStatement(argument); s.comments = comments; path.replace(s, b.continueStatement()); } return false; } this.traverse(path); }, visitWhileStatement: visitLoop("test"), }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
}
};
return self;
}
exports.get_search_result = function (base_query, query) {
let suggestion;
// base_query_operators correspond to the existing pills. query_operators correspond
// to the operators for the query in the input. This query may contain one or more
// operators. e.g if `is:starred stream:Ver` was typed without selecting the typeahead
// or pressing enter in between i.e search pill for is:starred has not yet been added,
// then `base` should be equal to the default suggestion for `is:starred`. Thus the
// description of `is:starred` will act as a prefix in every suggestion.
const base_query_operators = Filter.parse(base_query);
const query_operators = Filter.parse(query);
const operators = base_query_operators.concat(query_operators);
let last = {operator: '', operand: '', negated: false};
if (query_operators.length > 0) {
last = query_operators.slice(-1)[0];
} else {
// If query_operators = [] then last will remain
// {operator: '', operand: '', negated: false}; from above.
// `last` has not yet been added to operators/query_operators.
// The code below adds last to operators/query_operators
operators.push(last);
query_operators.push(last);
}
const person_suggestion_ops = ['sender', 'pm-with', 'from', 'group-pm'];
const operators_len = operators.length;
const query_operators_len = query_operators.length;
// Handle spaces in person name in new suggestions only. Checks if the last operator is
// 'search' and the second last operator in query_operators is one out of person_suggestion_ops.
// e.g for `sender:Ted sm`, initially last = {operator: 'search', operand: 'sm'....}
// and second last is {operator: 'sender', operand: 'sm'....}. If the second last operand
// is an email of a user, both of these operators remain unchanged. Otherwise search operator
// will be deleted and new last will become {operator:'sender', operand: 'Ted sm`....}.
if (query_operators_len > 1 &&
last.operator === 'search' &&
js: Convert a.indexOf(…) !== -1 to a.includes(…). Babel polyfills this for us for Internet Explorer. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import K from "ast-types/gen/kinds"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; recast.visit(ast, { visitBinaryExpression(path) { const { operator, left, right } = path.node; if ( n.CallExpression.check(left) && n.MemberExpression.check(left.callee) && !left.callee.computed && n.Identifier.check(left.callee.property) && left.callee.property.name === "indexOf" && left.arguments.length === 1 && checkExpression(left.arguments[0]) && ((["===", "!==", "==", "!=", ">", "<="].includes(operator) && n.UnaryExpression.check(right) && right.operator == "-" && n.Literal.check(right.argument) && right.argument.value === 1) || ([">=", "<"].includes(operator) && n.Literal.check(right) && right.value === 0)) ) { const test = b.callExpression( b.memberExpression(left.callee.object, b.identifier("includes")), [left.arguments[0]] ); path.replace( ["!==", "!=", ">", ">="].includes(operator) ? test : b.unaryExpression("!", test) ); changed = true; } this.traverse(path); }, }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-08 04:55:06 +01:00
person_suggestion_ops.includes(query_operators[query_operators_len - 2].operator)) {
const person_op = query_operators[query_operators_len - 2];
if (!people.reply_to_to_user_ids_string(person_op.operand)) {
last = {
operator: person_op.operator,
operand: person_op.operand + ' ' + last.operand,
negated: person_op.negated,
};
operators[operators_len - 2] = last;
operators.splice(-1, 1);
query_operators[query_operators_len - 2] = last;
query_operators.splice(-1, 1);
}
}
const base = get_default_suggestion(query_operators.slice(0, -1));
const attacher = make_attacher(base);
// Display the default first
// `has` and `is` operators work only on predefined categories. Default suggestion
// is not displayed in that case. e.g. `messages with one or more abc` as
// a suggestion for `has:abc`does not make sense.
if (last.operator !== '' && last.operator !== 'has' && last.operator !== 'is') {
suggestion = get_default_suggestion(query_operators);
if (suggestion) {
attacher.push(suggestion);
}
}
let base_operators = [];
if (operators.length > 1) {
base_operators = operators.slice(0, -1);
}
// only make one people_getter to avoid duplicate work
const people_getter = make_people_getter(last);
search: Retrofit recent changes to pills code. This change makes these two functions more alike: - get_search_result - get_search_result_legacy To test the UI modify zerver/views/home.py by replacing `settings.SEARCH_PILLS_ENABLED` with `True`. I only did a quick sanity check, since any bugs with the new system are more likely due to bitrot than any changes I have made here. The history is this: Tim cloned the code (before the smaller helpers were extracted): db4f6e278f5600e89a2da8d626b38943d3977c59 In 8b153f6452b178929d90fb6820e68a2f29c6e6da Shubham removed get_operator_subset_suggestions but accidentally left a `concat` statement in that got misapplied to the previous suggestions: - suggestions = get_operator_subset_suggestions(operators); result = result.concat(suggestions); The error there was carried over in some recent changes, but this commit fixes that strangeness. In 73e4f3b3fad4b20abc7a3d458950564094a81a2b Shubham made this change, which makes sense only for pills, and this code remains intact. - if (operators.length > 0) { - last = operators.slice(-1)[0]; + if (query_operators.length > 0) { + last = query_operators.slice(-1)[0]; + } else { + // If query_operators = [] then last will remain + // {operator: '', operand: '', negated: false}; from above. + // `last` has not yet been added to operators/query_operators. + // The code below adds last to operators/query_operators + operators.push(last); + query_operators.push(last); } Mohit made a couple changes to both old and new. Anders made a couple non-substantive changes related to the ES6 migration. Steve (me) made several structural changes to the code. For some of them I only changed the legacy code, not the pills code. I didn't fix Shubham's mistake until this change. Now the two functions should look similar except in the places where they are intentionally different. I also added a comment explaining the get_operator_subset_suggestions difference. Fixes #13609
2020-01-07 14:21:35 +01:00
function get_people(flavor) {
return function (last, base_operators) {
return get_person_suggestions(people_getter, last, base_operators, flavor);
};
}
search: Retrofit recent changes to pills code. This change makes these two functions more alike: - get_search_result - get_search_result_legacy To test the UI modify zerver/views/home.py by replacing `settings.SEARCH_PILLS_ENABLED` with `True`. I only did a quick sanity check, since any bugs with the new system are more likely due to bitrot than any changes I have made here. The history is this: Tim cloned the code (before the smaller helpers were extracted): db4f6e278f5600e89a2da8d626b38943d3977c59 In 8b153f6452b178929d90fb6820e68a2f29c6e6da Shubham removed get_operator_subset_suggestions but accidentally left a `concat` statement in that got misapplied to the previous suggestions: - suggestions = get_operator_subset_suggestions(operators); result = result.concat(suggestions); The error there was carried over in some recent changes, but this commit fixes that strangeness. In 73e4f3b3fad4b20abc7a3d458950564094a81a2b Shubham made this change, which makes sense only for pills, and this code remains intact. - if (operators.length > 0) { - last = operators.slice(-1)[0]; + if (query_operators.length > 0) { + last = query_operators.slice(-1)[0]; + } else { + // If query_operators = [] then last will remain + // {operator: '', operand: '', negated: false}; from above. + // `last` has not yet been added to operators/query_operators. + // The code below adds last to operators/query_operators + operators.push(last); + query_operators.push(last); } Mohit made a couple changes to both old and new. Anders made a couple non-substantive changes related to the ES6 migration. Steve (me) made several structural changes to the code. For some of them I only changed the legacy code, not the pills code. I didn't fix Shubham's mistake until this change. Now the two functions should look similar except in the places where they are intentionally different. I also added a comment explaining the get_operator_subset_suggestions difference. Fixes #13609
2020-01-07 14:21:35 +01:00
const filterers = [
get_streams_filter_suggestions,
get_is_filter_suggestions,
get_sent_by_me_suggestions,
get_stream_suggestions,
get_people('sender'),
get_people('pm-with'),
get_people('from'),
get_people('group-pm-with'),
get_group_suggestions,
get_topic_suggestions,
get_operator_suggestions,
get_has_filter_suggestions,
];
search: Retrofit recent changes to pills code. This change makes these two functions more alike: - get_search_result - get_search_result_legacy To test the UI modify zerver/views/home.py by replacing `settings.SEARCH_PILLS_ENABLED` with `True`. I only did a quick sanity check, since any bugs with the new system are more likely due to bitrot than any changes I have made here. The history is this: Tim cloned the code (before the smaller helpers were extracted): db4f6e278f5600e89a2da8d626b38943d3977c59 In 8b153f6452b178929d90fb6820e68a2f29c6e6da Shubham removed get_operator_subset_suggestions but accidentally left a `concat` statement in that got misapplied to the previous suggestions: - suggestions = get_operator_subset_suggestions(operators); result = result.concat(suggestions); The error there was carried over in some recent changes, but this commit fixes that strangeness. In 73e4f3b3fad4b20abc7a3d458950564094a81a2b Shubham made this change, which makes sense only for pills, and this code remains intact. - if (operators.length > 0) { - last = operators.slice(-1)[0]; + if (query_operators.length > 0) { + last = query_operators.slice(-1)[0]; + } else { + // If query_operators = [] then last will remain + // {operator: '', operand: '', negated: false}; from above. + // `last` has not yet been added to operators/query_operators. + // The code below adds last to operators/query_operators + operators.push(last); + query_operators.push(last); } Mohit made a couple changes to both old and new. Anders made a couple non-substantive changes related to the ES6 migration. Steve (me) made several structural changes to the code. For some of them I only changed the legacy code, not the pills code. I didn't fix Shubham's mistake until this change. Now the two functions should look similar except in the places where they are intentionally different. I also added a comment explaining the get_operator_subset_suggestions difference. Fixes #13609
2020-01-07 14:21:35 +01:00
const max_items = exports.max_num_of_search_results;
js: Automatically convert _.each to for…of. This commit was automatically generated by the following script, followed by lint --fix and a few small manual lint-related cleanups. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import { Context } from "ast-types/lib/path-visitor"; import K from "ast-types/gen/kinds"; import { NodePath } from "ast-types/lib/node-path"; import assert from "assert"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); const checkStatement = (node: n.Node): node is K.StatementKind => n.Statement.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; let inLoop = false; let replaceReturn = false; const visitLoop = (...args: string[]) => function(this: Context, path: NodePath) { for (const arg of args) { this.visit(path.get(arg)); } const old = { inLoop }; inLoop = true; this.visit(path.get("body")); inLoop = old.inLoop; return false; }; recast.visit(ast, { visitDoWhileStatement: visitLoop("test"), visitExpressionStatement(path) { const { expression, comments } = path.node; let valueOnly; if ( n.CallExpression.check(expression) && n.MemberExpression.check(expression.callee) && !expression.callee.computed && n.Identifier.check(expression.callee.object) && expression.callee.object.name === "_" && n.Identifier.check(expression.callee.property) && ["each", "forEach"].includes(expression.callee.property.name) && [2, 3].includes(expression.arguments.length) && checkExpression(expression.arguments[0]) && (n.FunctionExpression.check(expression.arguments[1]) || n.ArrowFunctionExpression.check(expression.arguments[1])) && [1, 2].includes(expression.arguments[1].params.length) && n.Identifier.check(expression.arguments[1].params[0]) && ((valueOnly = expression.arguments[1].params[1] === undefined) || n.Identifier.check(expression.arguments[1].params[1])) && (expression.arguments[2] === undefined || n.ThisExpression.check(expression.arguments[2])) ) { const old = { inLoop, replaceReturn }; inLoop = false; replaceReturn = true; this.visit( path .get("expression") .get("arguments") .get(1) .get("body") ); inLoop = old.inLoop; replaceReturn = old.replaceReturn; const [right, { body, params }] = expression.arguments; const loop = b.forOfStatement( b.variableDeclaration("let", [ b.variableDeclarator( valueOnly ? params[0] : b.arrayPattern([params[1], params[0]]) ), ]), valueOnly ? right : b.callExpression( b.memberExpression(right, b.identifier("entries")), [] ), checkStatement(body) ? body : b.expressionStatement(body) ); loop.comments = comments; path.replace(loop); changed = true; } this.traverse(path); }, visitForStatement: visitLoop("init", "test", "update"), visitForInStatement: visitLoop("left", "right"), visitForOfStatement: visitLoop("left", "right"), visitFunction(path) { this.visit(path.get("params")); const old = { replaceReturn }; replaceReturn = false; this.visit(path.get("body")); replaceReturn = old.replaceReturn; return false; }, visitReturnStatement(path) { if (replaceReturn) { assert(!inLoop); // could use labeled continue if this ever fires const { argument, comments } = path.node; if (argument === null) { const s = b.continueStatement(); s.comments = comments; path.replace(s); } else { const s = b.expressionStatement(argument); s.comments = comments; path.replace(s, b.continueStatement()); } return false; } this.traverse(path); }, visitWhileStatement: visitLoop("test"), }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
for (const filterer of filterers) {
search: Retrofit recent changes to pills code. This change makes these two functions more alike: - get_search_result - get_search_result_legacy To test the UI modify zerver/views/home.py by replacing `settings.SEARCH_PILLS_ENABLED` with `True`. I only did a quick sanity check, since any bugs with the new system are more likely due to bitrot than any changes I have made here. The history is this: Tim cloned the code (before the smaller helpers were extracted): db4f6e278f5600e89a2da8d626b38943d3977c59 In 8b153f6452b178929d90fb6820e68a2f29c6e6da Shubham removed get_operator_subset_suggestions but accidentally left a `concat` statement in that got misapplied to the previous suggestions: - suggestions = get_operator_subset_suggestions(operators); result = result.concat(suggestions); The error there was carried over in some recent changes, but this commit fixes that strangeness. In 73e4f3b3fad4b20abc7a3d458950564094a81a2b Shubham made this change, which makes sense only for pills, and this code remains intact. - if (operators.length > 0) { - last = operators.slice(-1)[0]; + if (query_operators.length > 0) { + last = query_operators.slice(-1)[0]; + } else { + // If query_operators = [] then last will remain + // {operator: '', operand: '', negated: false}; from above. + // `last` has not yet been added to operators/query_operators. + // The code below adds last to operators/query_operators + operators.push(last); + query_operators.push(last); } Mohit made a couple changes to both old and new. Anders made a couple non-substantive changes related to the ES6 migration. Steve (me) made several structural changes to the code. For some of them I only changed the legacy code, not the pills code. I didn't fix Shubham's mistake until this change. Now the two functions should look similar except in the places where they are intentionally different. I also added a comment explaining the get_operator_subset_suggestions difference. Fixes #13609
2020-01-07 14:21:35 +01:00
if (attacher.result.length < max_items) {
const suggestions = filterer(last, base_operators);
attacher.attach_many(suggestions);
}
js: Automatically convert _.each to for…of. This commit was automatically generated by the following script, followed by lint --fix and a few small manual lint-related cleanups. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import { Context } from "ast-types/lib/path-visitor"; import K from "ast-types/gen/kinds"; import { NodePath } from "ast-types/lib/node-path"; import assert from "assert"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); const checkStatement = (node: n.Node): node is K.StatementKind => n.Statement.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; let inLoop = false; let replaceReturn = false; const visitLoop = (...args: string[]) => function(this: Context, path: NodePath) { for (const arg of args) { this.visit(path.get(arg)); } const old = { inLoop }; inLoop = true; this.visit(path.get("body")); inLoop = old.inLoop; return false; }; recast.visit(ast, { visitDoWhileStatement: visitLoop("test"), visitExpressionStatement(path) { const { expression, comments } = path.node; let valueOnly; if ( n.CallExpression.check(expression) && n.MemberExpression.check(expression.callee) && !expression.callee.computed && n.Identifier.check(expression.callee.object) && expression.callee.object.name === "_" && n.Identifier.check(expression.callee.property) && ["each", "forEach"].includes(expression.callee.property.name) && [2, 3].includes(expression.arguments.length) && checkExpression(expression.arguments[0]) && (n.FunctionExpression.check(expression.arguments[1]) || n.ArrowFunctionExpression.check(expression.arguments[1])) && [1, 2].includes(expression.arguments[1].params.length) && n.Identifier.check(expression.arguments[1].params[0]) && ((valueOnly = expression.arguments[1].params[1] === undefined) || n.Identifier.check(expression.arguments[1].params[1])) && (expression.arguments[2] === undefined || n.ThisExpression.check(expression.arguments[2])) ) { const old = { inLoop, replaceReturn }; inLoop = false; replaceReturn = true; this.visit( path .get("expression") .get("arguments") .get(1) .get("body") ); inLoop = old.inLoop; replaceReturn = old.replaceReturn; const [right, { body, params }] = expression.arguments; const loop = b.forOfStatement( b.variableDeclaration("let", [ b.variableDeclarator( valueOnly ? params[0] : b.arrayPattern([params[1], params[0]]) ), ]), valueOnly ? right : b.callExpression( b.memberExpression(right, b.identifier("entries")), [] ), checkStatement(body) ? body : b.expressionStatement(body) ); loop.comments = comments; path.replace(loop); changed = true; } this.traverse(path); }, visitForStatement: visitLoop("init", "test", "update"), visitForInStatement: visitLoop("left", "right"), visitForOfStatement: visitLoop("left", "right"), visitFunction(path) { this.visit(path.get("params")); const old = { replaceReturn }; replaceReturn = false; this.visit(path.get("body")); replaceReturn = old.replaceReturn; return false; }, visitReturnStatement(path) { if (replaceReturn) { assert(!inLoop); // could use labeled continue if this ever fires const { argument, comments } = path.node; if (argument === null) { const s = b.continueStatement(); s.comments = comments; path.replace(s); } else { const s = b.expressionStatement(argument); s.comments = comments; path.replace(s, b.continueStatement()); } return false; } this.traverse(path); }, visitWhileStatement: visitLoop("test"), }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
}
search: Retrofit recent changes to pills code. This change makes these two functions more alike: - get_search_result - get_search_result_legacy To test the UI modify zerver/views/home.py by replacing `settings.SEARCH_PILLS_ENABLED` with `True`. I only did a quick sanity check, since any bugs with the new system are more likely due to bitrot than any changes I have made here. The history is this: Tim cloned the code (before the smaller helpers were extracted): db4f6e278f5600e89a2da8d626b38943d3977c59 In 8b153f6452b178929d90fb6820e68a2f29c6e6da Shubham removed get_operator_subset_suggestions but accidentally left a `concat` statement in that got misapplied to the previous suggestions: - suggestions = get_operator_subset_suggestions(operators); result = result.concat(suggestions); The error there was carried over in some recent changes, but this commit fixes that strangeness. In 73e4f3b3fad4b20abc7a3d458950564094a81a2b Shubham made this change, which makes sense only for pills, and this code remains intact. - if (operators.length > 0) { - last = operators.slice(-1)[0]; + if (query_operators.length > 0) { + last = query_operators.slice(-1)[0]; + } else { + // If query_operators = [] then last will remain + // {operator: '', operand: '', negated: false}; from above. + // `last` has not yet been added to operators/query_operators. + // The code below adds last to operators/query_operators + operators.push(last); + query_operators.push(last); } Mohit made a couple changes to both old and new. Anders made a couple non-substantive changes related to the ES6 migration. Steve (me) made several structural changes to the code. For some of them I only changed the legacy code, not the pills code. I didn't fix Shubham's mistake until this change. Now the two functions should look similar except in the places where they are intentionally different. I also added a comment explaining the get_operator_subset_suggestions difference. Fixes #13609
2020-01-07 14:21:35 +01:00
return attacher.result.slice(0, max_items);
};
exports.get_search_result_legacy = function (query) {
let suggestion;
// Add an entry for narrow by operators.
const operators = Filter.parse(query);
let last = {operator: '', operand: '', negated: false};
if (operators.length > 0) {
last = operators.slice(-1)[0];
}
const person_suggestion_ops = ['sender', 'pm-with', 'from', 'group-pm'];
const operators_len = operators.length;
// Handle spaces in person name in new suggestions only. Checks if the last operator is
// 'search' and the second last operator is one out of person_suggestion_ops.
// e.g for `sender:Ted sm`, initially last = {operator: 'search', operand: 'sm'....}
// and second last is {operator: 'sender', operand: 'sm'....}. If the second last operand
// is an email of a user, both of these operators remain unchanged. Otherwise search operator
// will be deleted and new last will become {operator:'sender', operand: 'Ted sm`....}.
if (operators_len > 1 &&
last.operator === 'search' &&
js: Convert a.indexOf(…) !== -1 to a.includes(…). Babel polyfills this for us for Internet Explorer. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import K from "ast-types/gen/kinds"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; recast.visit(ast, { visitBinaryExpression(path) { const { operator, left, right } = path.node; if ( n.CallExpression.check(left) && n.MemberExpression.check(left.callee) && !left.callee.computed && n.Identifier.check(left.callee.property) && left.callee.property.name === "indexOf" && left.arguments.length === 1 && checkExpression(left.arguments[0]) && ((["===", "!==", "==", "!=", ">", "<="].includes(operator) && n.UnaryExpression.check(right) && right.operator == "-" && n.Literal.check(right.argument) && right.argument.value === 1) || ([">=", "<"].includes(operator) && n.Literal.check(right) && right.value === 0)) ) { const test = b.callExpression( b.memberExpression(left.callee.object, b.identifier("includes")), [left.arguments[0]] ); path.replace( ["!==", "!=", ">", ">="].includes(operator) ? test : b.unaryExpression("!", test) ); changed = true; } this.traverse(path); }, }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-08 04:55:06 +01:00
person_suggestion_ops.includes(operators[operators_len - 2].operator)) {
const person_op = operators[operators_len - 2];
if (!people.reply_to_to_user_ids_string(person_op.operand)) {
last = {
operator: person_op.operator,
operand: person_op.operand + ' ' + last.operand,
negated: person_op.negated,
};
operators[operators_len - 2] = last;
operators.splice(-1, 1);
}
}
let base_operators = [];
if (operators.length > 1) {
base_operators = operators.slice(0, -1);
}
const base = get_default_suggestion_legacy(base_operators);
const attacher = make_attacher(base);
// Display the default first
// `has` and `is` operators work only on predefined categories. Default suggestion
// is not displayed in that case. e.g. `messages with one or more abc` as
// a suggestion for `has:abc`does not make sense.
if (last.operator !== '' && last.operator !== 'has' && last.operator !== 'is') {
suggestion = get_default_suggestion_legacy(operators);
attacher.push(suggestion);
}
// only make one people_getter to avoid duplicate work
const people_getter = make_people_getter(last);
function get_people(flavor) {
return function (last, base_operators) {
return get_person_suggestions(people_getter, last, base_operators, flavor);
};
}
const filterers = [
get_streams_filter_suggestions,
get_is_filter_suggestions,
get_sent_by_me_suggestions,
get_stream_suggestions,
get_people('sender'),
get_people('pm-with'),
get_people('from'),
get_people('group-pm-with'),
get_group_suggestions,
get_topic_suggestions,
get_operator_suggestions,
get_has_filter_suggestions,
];
const max_items = exports.max_num_of_search_results;
js: Automatically convert _.each to for…of. This commit was automatically generated by the following script, followed by lint --fix and a few small manual lint-related cleanups. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import { Context } from "ast-types/lib/path-visitor"; import K from "ast-types/gen/kinds"; import { NodePath } from "ast-types/lib/node-path"; import assert from "assert"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); const checkStatement = (node: n.Node): node is K.StatementKind => n.Statement.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; let inLoop = false; let replaceReturn = false; const visitLoop = (...args: string[]) => function(this: Context, path: NodePath) { for (const arg of args) { this.visit(path.get(arg)); } const old = { inLoop }; inLoop = true; this.visit(path.get("body")); inLoop = old.inLoop; return false; }; recast.visit(ast, { visitDoWhileStatement: visitLoop("test"), visitExpressionStatement(path) { const { expression, comments } = path.node; let valueOnly; if ( n.CallExpression.check(expression) && n.MemberExpression.check(expression.callee) && !expression.callee.computed && n.Identifier.check(expression.callee.object) && expression.callee.object.name === "_" && n.Identifier.check(expression.callee.property) && ["each", "forEach"].includes(expression.callee.property.name) && [2, 3].includes(expression.arguments.length) && checkExpression(expression.arguments[0]) && (n.FunctionExpression.check(expression.arguments[1]) || n.ArrowFunctionExpression.check(expression.arguments[1])) && [1, 2].includes(expression.arguments[1].params.length) && n.Identifier.check(expression.arguments[1].params[0]) && ((valueOnly = expression.arguments[1].params[1] === undefined) || n.Identifier.check(expression.arguments[1].params[1])) && (expression.arguments[2] === undefined || n.ThisExpression.check(expression.arguments[2])) ) { const old = { inLoop, replaceReturn }; inLoop = false; replaceReturn = true; this.visit( path .get("expression") .get("arguments") .get(1) .get("body") ); inLoop = old.inLoop; replaceReturn = old.replaceReturn; const [right, { body, params }] = expression.arguments; const loop = b.forOfStatement( b.variableDeclaration("let", [ b.variableDeclarator( valueOnly ? params[0] : b.arrayPattern([params[1], params[0]]) ), ]), valueOnly ? right : b.callExpression( b.memberExpression(right, b.identifier("entries")), [] ), checkStatement(body) ? body : b.expressionStatement(body) ); loop.comments = comments; path.replace(loop); changed = true; } this.traverse(path); }, visitForStatement: visitLoop("init", "test", "update"), visitForInStatement: visitLoop("left", "right"), visitForOfStatement: visitLoop("left", "right"), visitFunction(path) { this.visit(path.get("params")); const old = { replaceReturn }; replaceReturn = false; this.visit(path.get("body")); replaceReturn = old.replaceReturn; return false; }, visitReturnStatement(path) { if (replaceReturn) { assert(!inLoop); // could use labeled continue if this ever fires const { argument, comments } = path.node; if (argument === null) { const s = b.continueStatement(); s.comments = comments; path.replace(s); } else { const s = b.expressionStatement(argument); s.comments = comments; path.replace(s, b.continueStatement()); } return false; } this.traverse(path); }, visitWhileStatement: visitLoop("test"), }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
for (const filterer of filterers) {
if (attacher.result.length < max_items) {
const suggestions = filterer(last, base_operators);
attacher.attach_many(suggestions);
}
js: Automatically convert _.each to for…of. This commit was automatically generated by the following script, followed by lint --fix and a few small manual lint-related cleanups. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import { Context } from "ast-types/lib/path-visitor"; import K from "ast-types/gen/kinds"; import { NodePath } from "ast-types/lib/node-path"; import assert from "assert"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); const checkStatement = (node: n.Node): node is K.StatementKind => n.Statement.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; let inLoop = false; let replaceReturn = false; const visitLoop = (...args: string[]) => function(this: Context, path: NodePath) { for (const arg of args) { this.visit(path.get(arg)); } const old = { inLoop }; inLoop = true; this.visit(path.get("body")); inLoop = old.inLoop; return false; }; recast.visit(ast, { visitDoWhileStatement: visitLoop("test"), visitExpressionStatement(path) { const { expression, comments } = path.node; let valueOnly; if ( n.CallExpression.check(expression) && n.MemberExpression.check(expression.callee) && !expression.callee.computed && n.Identifier.check(expression.callee.object) && expression.callee.object.name === "_" && n.Identifier.check(expression.callee.property) && ["each", "forEach"].includes(expression.callee.property.name) && [2, 3].includes(expression.arguments.length) && checkExpression(expression.arguments[0]) && (n.FunctionExpression.check(expression.arguments[1]) || n.ArrowFunctionExpression.check(expression.arguments[1])) && [1, 2].includes(expression.arguments[1].params.length) && n.Identifier.check(expression.arguments[1].params[0]) && ((valueOnly = expression.arguments[1].params[1] === undefined) || n.Identifier.check(expression.arguments[1].params[1])) && (expression.arguments[2] === undefined || n.ThisExpression.check(expression.arguments[2])) ) { const old = { inLoop, replaceReturn }; inLoop = false; replaceReturn = true; this.visit( path .get("expression") .get("arguments") .get(1) .get("body") ); inLoop = old.inLoop; replaceReturn = old.replaceReturn; const [right, { body, params }] = expression.arguments; const loop = b.forOfStatement( b.variableDeclaration("let", [ b.variableDeclarator( valueOnly ? params[0] : b.arrayPattern([params[1], params[0]]) ), ]), valueOnly ? right : b.callExpression( b.memberExpression(right, b.identifier("entries")), [] ), checkStatement(body) ? body : b.expressionStatement(body) ); loop.comments = comments; path.replace(loop); changed = true; } this.traverse(path); }, visitForStatement: visitLoop("init", "test", "update"), visitForInStatement: visitLoop("left", "right"), visitForOfStatement: visitLoop("left", "right"), visitFunction(path) { this.visit(path.get("params")); const old = { replaceReturn }; replaceReturn = false; this.visit(path.get("body")); replaceReturn = old.replaceReturn; return false; }, visitReturnStatement(path) { if (replaceReturn) { assert(!inLoop); // could use labeled continue if this ever fires const { argument, comments } = path.node; if (argument === null) { const s = b.continueStatement(); s.comments = comments; path.replace(s); } else { const s = b.expressionStatement(argument); s.comments = comments; path.replace(s, b.continueStatement()); } return false; } this.traverse(path); }, visitWhileStatement: visitLoop("test"), }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
}
search: Retrofit recent changes to pills code. This change makes these two functions more alike: - get_search_result - get_search_result_legacy To test the UI modify zerver/views/home.py by replacing `settings.SEARCH_PILLS_ENABLED` with `True`. I only did a quick sanity check, since any bugs with the new system are more likely due to bitrot than any changes I have made here. The history is this: Tim cloned the code (before the smaller helpers were extracted): db4f6e278f5600e89a2da8d626b38943d3977c59 In 8b153f6452b178929d90fb6820e68a2f29c6e6da Shubham removed get_operator_subset_suggestions but accidentally left a `concat` statement in that got misapplied to the previous suggestions: - suggestions = get_operator_subset_suggestions(operators); result = result.concat(suggestions); The error there was carried over in some recent changes, but this commit fixes that strangeness. In 73e4f3b3fad4b20abc7a3d458950564094a81a2b Shubham made this change, which makes sense only for pills, and this code remains intact. - if (operators.length > 0) { - last = operators.slice(-1)[0]; + if (query_operators.length > 0) { + last = query_operators.slice(-1)[0]; + } else { + // If query_operators = [] then last will remain + // {operator: '', operand: '', negated: false}; from above. + // `last` has not yet been added to operators/query_operators. + // The code below adds last to operators/query_operators + operators.push(last); + query_operators.push(last); } Mohit made a couple changes to both old and new. Anders made a couple non-substantive changes related to the ES6 migration. Steve (me) made several structural changes to the code. For some of them I only changed the legacy code, not the pills code. I didn't fix Shubham's mistake until this change. Now the two functions should look similar except in the places where they are intentionally different. I also added a comment explaining the get_operator_subset_suggestions difference. Fixes #13609
2020-01-07 14:21:35 +01:00
// This is unique to the legacy search system. With pills
// it is difficult to "suggest" a subset of operators,
// and there's a more natural mechanism under that paradigm,
// where the user just deletes one or more pills. So you
// won't see this is in the new code.
if (attacher.result.length < max_items) {
const subset_suggestions = get_operator_subset_suggestions(operators);
attacher.concat(subset_suggestions);
}
return attacher.result.slice(0, max_items);
};
exports.get_suggestions_legacy = function (query) {
const result = exports.get_search_result_legacy(query);
return exports.finalize_search_result(result);
};
exports.get_suggestions = function (base_query, query) {
const result = exports.get_search_result(base_query, query);
return exports.finalize_search_result(result);
};
exports.finalize_search_result = function (result) {
js: Automatically convert _.each to for…of. This commit was automatically generated by the following script, followed by lint --fix and a few small manual lint-related cleanups. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import { Context } from "ast-types/lib/path-visitor"; import K from "ast-types/gen/kinds"; import { NodePath } from "ast-types/lib/node-path"; import assert from "assert"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); const checkStatement = (node: n.Node): node is K.StatementKind => n.Statement.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; let inLoop = false; let replaceReturn = false; const visitLoop = (...args: string[]) => function(this: Context, path: NodePath) { for (const arg of args) { this.visit(path.get(arg)); } const old = { inLoop }; inLoop = true; this.visit(path.get("body")); inLoop = old.inLoop; return false; }; recast.visit(ast, { visitDoWhileStatement: visitLoop("test"), visitExpressionStatement(path) { const { expression, comments } = path.node; let valueOnly; if ( n.CallExpression.check(expression) && n.MemberExpression.check(expression.callee) && !expression.callee.computed && n.Identifier.check(expression.callee.object) && expression.callee.object.name === "_" && n.Identifier.check(expression.callee.property) && ["each", "forEach"].includes(expression.callee.property.name) && [2, 3].includes(expression.arguments.length) && checkExpression(expression.arguments[0]) && (n.FunctionExpression.check(expression.arguments[1]) || n.ArrowFunctionExpression.check(expression.arguments[1])) && [1, 2].includes(expression.arguments[1].params.length) && n.Identifier.check(expression.arguments[1].params[0]) && ((valueOnly = expression.arguments[1].params[1] === undefined) || n.Identifier.check(expression.arguments[1].params[1])) && (expression.arguments[2] === undefined || n.ThisExpression.check(expression.arguments[2])) ) { const old = { inLoop, replaceReturn }; inLoop = false; replaceReturn = true; this.visit( path .get("expression") .get("arguments") .get(1) .get("body") ); inLoop = old.inLoop; replaceReturn = old.replaceReturn; const [right, { body, params }] = expression.arguments; const loop = b.forOfStatement( b.variableDeclaration("let", [ b.variableDeclarator( valueOnly ? params[0] : b.arrayPattern([params[1], params[0]]) ), ]), valueOnly ? right : b.callExpression( b.memberExpression(right, b.identifier("entries")), [] ), checkStatement(body) ? body : b.expressionStatement(body) ); loop.comments = comments; path.replace(loop); changed = true; } this.traverse(path); }, visitForStatement: visitLoop("init", "test", "update"), visitForInStatement: visitLoop("left", "right"), visitForOfStatement: visitLoop("left", "right"), visitFunction(path) { this.visit(path.get("params")); const old = { replaceReturn }; replaceReturn = false; this.visit(path.get("body")); replaceReturn = old.replaceReturn; return false; }, visitReturnStatement(path) { if (replaceReturn) { assert(!inLoop); // could use labeled continue if this ever fires const { argument, comments } = path.node; if (argument === null) { const s = b.continueStatement(); s.comments = comments; path.replace(s); } else { const s = b.expressionStatement(argument); s.comments = comments; path.replace(s, b.continueStatement()); } return false; } this.traverse(path); }, visitWhileStatement: visitLoop("test"), }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
for (const sug of result) {
const first = sug.description.charAt(0).toUpperCase();
sug.description = first + sug.description.slice(1);
js: Automatically convert _.each to for…of. This commit was automatically generated by the following script, followed by lint --fix and a few small manual lint-related cleanups. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import { Context } from "ast-types/lib/path-visitor"; import K from "ast-types/gen/kinds"; import { NodePath } from "ast-types/lib/node-path"; import assert from "assert"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); const checkStatement = (node: n.Node): node is K.StatementKind => n.Statement.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; let inLoop = false; let replaceReturn = false; const visitLoop = (...args: string[]) => function(this: Context, path: NodePath) { for (const arg of args) { this.visit(path.get(arg)); } const old = { inLoop }; inLoop = true; this.visit(path.get("body")); inLoop = old.inLoop; return false; }; recast.visit(ast, { visitDoWhileStatement: visitLoop("test"), visitExpressionStatement(path) { const { expression, comments } = path.node; let valueOnly; if ( n.CallExpression.check(expression) && n.MemberExpression.check(expression.callee) && !expression.callee.computed && n.Identifier.check(expression.callee.object) && expression.callee.object.name === "_" && n.Identifier.check(expression.callee.property) && ["each", "forEach"].includes(expression.callee.property.name) && [2, 3].includes(expression.arguments.length) && checkExpression(expression.arguments[0]) && (n.FunctionExpression.check(expression.arguments[1]) || n.ArrowFunctionExpression.check(expression.arguments[1])) && [1, 2].includes(expression.arguments[1].params.length) && n.Identifier.check(expression.arguments[1].params[0]) && ((valueOnly = expression.arguments[1].params[1] === undefined) || n.Identifier.check(expression.arguments[1].params[1])) && (expression.arguments[2] === undefined || n.ThisExpression.check(expression.arguments[2])) ) { const old = { inLoop, replaceReturn }; inLoop = false; replaceReturn = true; this.visit( path .get("expression") .get("arguments") .get(1) .get("body") ); inLoop = old.inLoop; replaceReturn = old.replaceReturn; const [right, { body, params }] = expression.arguments; const loop = b.forOfStatement( b.variableDeclaration("let", [ b.variableDeclarator( valueOnly ? params[0] : b.arrayPattern([params[1], params[0]]) ), ]), valueOnly ? right : b.callExpression( b.memberExpression(right, b.identifier("entries")), [] ), checkStatement(body) ? body : b.expressionStatement(body) ); loop.comments = comments; path.replace(loop); changed = true; } this.traverse(path); }, visitForStatement: visitLoop("init", "test", "update"), visitForInStatement: visitLoop("left", "right"), visitForOfStatement: visitLoop("left", "right"), visitFunction(path) { this.visit(path.get("params")); const old = { replaceReturn }; replaceReturn = false; this.visit(path.get("body")); replaceReturn = old.replaceReturn; return false; }, visitReturnStatement(path) { if (replaceReturn) { assert(!inLoop); // could use labeled continue if this ever fires const { argument, comments } = path.node; if (argument === null) { const s = b.continueStatement(); s.comments = comments; path.replace(s); } else { const s = b.expressionStatement(argument); s.comments = comments; path.replace(s, b.continueStatement()); } return false; } this.traverse(path); }, visitWhileStatement: visitLoop("test"), }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
}
// Typeahead expects us to give it strings, not objects,
// so we maintain our own hash back to our objects
const lookup_table = {};
js: Automatically convert _.each to for…of. This commit was automatically generated by the following script, followed by lint --fix and a few small manual lint-related cleanups. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import { Context } from "ast-types/lib/path-visitor"; import K from "ast-types/gen/kinds"; import { NodePath } from "ast-types/lib/node-path"; import assert from "assert"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); const checkStatement = (node: n.Node): node is K.StatementKind => n.Statement.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; let inLoop = false; let replaceReturn = false; const visitLoop = (...args: string[]) => function(this: Context, path: NodePath) { for (const arg of args) { this.visit(path.get(arg)); } const old = { inLoop }; inLoop = true; this.visit(path.get("body")); inLoop = old.inLoop; return false; }; recast.visit(ast, { visitDoWhileStatement: visitLoop("test"), visitExpressionStatement(path) { const { expression, comments } = path.node; let valueOnly; if ( n.CallExpression.check(expression) && n.MemberExpression.check(expression.callee) && !expression.callee.computed && n.Identifier.check(expression.callee.object) && expression.callee.object.name === "_" && n.Identifier.check(expression.callee.property) && ["each", "forEach"].includes(expression.callee.property.name) && [2, 3].includes(expression.arguments.length) && checkExpression(expression.arguments[0]) && (n.FunctionExpression.check(expression.arguments[1]) || n.ArrowFunctionExpression.check(expression.arguments[1])) && [1, 2].includes(expression.arguments[1].params.length) && n.Identifier.check(expression.arguments[1].params[0]) && ((valueOnly = expression.arguments[1].params[1] === undefined) || n.Identifier.check(expression.arguments[1].params[1])) && (expression.arguments[2] === undefined || n.ThisExpression.check(expression.arguments[2])) ) { const old = { inLoop, replaceReturn }; inLoop = false; replaceReturn = true; this.visit( path .get("expression") .get("arguments") .get(1) .get("body") ); inLoop = old.inLoop; replaceReturn = old.replaceReturn; const [right, { body, params }] = expression.arguments; const loop = b.forOfStatement( b.variableDeclaration("let", [ b.variableDeclarator( valueOnly ? params[0] : b.arrayPattern([params[1], params[0]]) ), ]), valueOnly ? right : b.callExpression( b.memberExpression(right, b.identifier("entries")), [] ), checkStatement(body) ? body : b.expressionStatement(body) ); loop.comments = comments; path.replace(loop); changed = true; } this.traverse(path); }, visitForStatement: visitLoop("init", "test", "update"), visitForInStatement: visitLoop("left", "right"), visitForOfStatement: visitLoop("left", "right"), visitFunction(path) { this.visit(path.get("params")); const old = { replaceReturn }; replaceReturn = false; this.visit(path.get("body")); replaceReturn = old.replaceReturn; return false; }, visitReturnStatement(path) { if (replaceReturn) { assert(!inLoop); // could use labeled continue if this ever fires const { argument, comments } = path.node; if (argument === null) { const s = b.continueStatement(); s.comments = comments; path.replace(s); } else { const s = b.expressionStatement(argument); s.comments = comments; path.replace(s, b.continueStatement()); } return false; } this.traverse(path); }, visitWhileStatement: visitLoop("test"), }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
for (const obj of result) {
lookup_table[obj.search_string] = obj;
js: Automatically convert _.each to for…of. This commit was automatically generated by the following script, followed by lint --fix and a few small manual lint-related cleanups. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import { Context } from "ast-types/lib/path-visitor"; import K from "ast-types/gen/kinds"; import { NodePath } from "ast-types/lib/node-path"; import assert from "assert"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); const checkStatement = (node: n.Node): node is K.StatementKind => n.Statement.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; let inLoop = false; let replaceReturn = false; const visitLoop = (...args: string[]) => function(this: Context, path: NodePath) { for (const arg of args) { this.visit(path.get(arg)); } const old = { inLoop }; inLoop = true; this.visit(path.get("body")); inLoop = old.inLoop; return false; }; recast.visit(ast, { visitDoWhileStatement: visitLoop("test"), visitExpressionStatement(path) { const { expression, comments } = path.node; let valueOnly; if ( n.CallExpression.check(expression) && n.MemberExpression.check(expression.callee) && !expression.callee.computed && n.Identifier.check(expression.callee.object) && expression.callee.object.name === "_" && n.Identifier.check(expression.callee.property) && ["each", "forEach"].includes(expression.callee.property.name) && [2, 3].includes(expression.arguments.length) && checkExpression(expression.arguments[0]) && (n.FunctionExpression.check(expression.arguments[1]) || n.ArrowFunctionExpression.check(expression.arguments[1])) && [1, 2].includes(expression.arguments[1].params.length) && n.Identifier.check(expression.arguments[1].params[0]) && ((valueOnly = expression.arguments[1].params[1] === undefined) || n.Identifier.check(expression.arguments[1].params[1])) && (expression.arguments[2] === undefined || n.ThisExpression.check(expression.arguments[2])) ) { const old = { inLoop, replaceReturn }; inLoop = false; replaceReturn = true; this.visit( path .get("expression") .get("arguments") .get(1) .get("body") ); inLoop = old.inLoop; replaceReturn = old.replaceReturn; const [right, { body, params }] = expression.arguments; const loop = b.forOfStatement( b.variableDeclaration("let", [ b.variableDeclarator( valueOnly ? params[0] : b.arrayPattern([params[1], params[0]]) ), ]), valueOnly ? right : b.callExpression( b.memberExpression(right, b.identifier("entries")), [] ), checkStatement(body) ? body : b.expressionStatement(body) ); loop.comments = comments; path.replace(loop); changed = true; } this.traverse(path); }, visitForStatement: visitLoop("init", "test", "update"), visitForInStatement: visitLoop("left", "right"), visitForOfStatement: visitLoop("left", "right"), visitFunction(path) { this.visit(path.get("params")); const old = { replaceReturn }; replaceReturn = false; this.visit(path.get("body")); replaceReturn = old.replaceReturn; return false; }, visitReturnStatement(path) { if (replaceReturn) { assert(!inLoop); // could use labeled continue if this ever fires const { argument, comments } = path.node; if (argument === null) { const s = b.continueStatement(); s.comments = comments; path.replace(s); } else { const s = b.expressionStatement(argument); s.comments = comments; path.replace(s, b.continueStatement()); } return false; } this.traverse(path); }, visitWhileStatement: visitLoop("test"), }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
}
js: Convert _.map(a, …) to a.map(…). And convert the corresponding function expressions to arrow style while we’re here. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import K from "ast-types/gen/kinds"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; recast.visit(ast, { visitCallExpression(path) { const { callee, arguments: args } = path.node; if ( n.MemberExpression.check(callee) && !callee.computed && n.Identifier.check(callee.object) && callee.object.name === "_" && n.Identifier.check(callee.property) && callee.property.name === "map" && args.length === 2 && checkExpression(args[0]) && checkExpression(args[1]) ) { const [arr, fn] = args; path.replace( b.callExpression(b.memberExpression(arr, b.identifier("map")), [ n.FunctionExpression.check(fn) || n.ArrowFunctionExpression.check(fn) ? b.arrowFunctionExpression( fn.params, n.BlockStatement.check(fn.body) && fn.body.body.length === 1 && n.ReturnStatement.check(fn.body.body[0]) ? fn.body.body[0].argument || b.identifier("undefined") : fn.body ) : fn, ]) ); changed = true; } this.traverse(path); }, }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-08 02:43:49 +01:00
const strings = result.map(obj => obj.search_string);
return {
strings: strings,
lookup_table: lookup_table,
};
};
window.search_suggestion = exports;