Skip to content

Commit e577f58

Browse files
committed
Implement fuzzy search
1 parent b7866cb commit e577f58

File tree

1 file changed

+37
-115
lines changed

1 file changed

+37
-115
lines changed

src/CodeSnippetDisplay.tsx

Lines changed: 37 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,6 @@ export class CodeSnippetDisplay extends React.Component<
295295
name: string
296296
): JSX.Element => {
297297
const displayName = language + name;
298-
console.log(displayName);
299-
console.log(this.state.searchValue);
300-
console.log(this.state.codeSnippets.length);
301-
console.log(id);
302-
console.log(this.state.matchIndices);
303-
// console.log(this.state.matchIndices[id]);
304298

305299
// check if the searchValue is not ''
306300
if (this.state.searchValue !== '') {
@@ -358,7 +352,6 @@ export class CodeSnippetDisplay extends React.Component<
358352
displayName.substring(currIndex + 1, displayName.length)
359353
);
360354
}
361-
console.log(elements);
362355
return <span>{elements}</span>;
363356
}
364357
}
@@ -371,8 +364,6 @@ export class CodeSnippetDisplay extends React.Component<
371364
event: React.MouseEvent<HTMLSpanElement, MouseEvent>
372365
): Promise<void> {
373366
const contentsService = CodeSnippetContentsService.getInstance();
374-
console.log(event.currentTarget);
375-
console.log(event.target);
376367
const target = event.target as HTMLElement;
377368
const oldPath = 'snippets/' + target.innerHTML + '.json';
378369

@@ -388,8 +379,6 @@ export class CodeSnippetDisplay extends React.Component<
388379
new_element.setSelectionRange(0, new_element.value.length);
389380

390381
new_element.onblur = async (): Promise<void> => {
391-
// console.log(target.innerHTML);
392-
// console.log(new_element.value);
393382
if (target.innerHTML !== new_element.value) {
394383
const newPath = 'snippets/' + new_element.value + '.json';
395384
try {
@@ -1128,7 +1117,6 @@ export class CodeSnippetDisplay extends React.Component<
11281117
<div
11291118
className={CODE_SNIPPET_METADATA}
11301119
onMouseEnter={(): void => {
1131-
console.log(id);
11321120
showPreview(
11331121
{
11341122
id: id,
@@ -1216,130 +1204,64 @@ export class CodeSnippetDisplay extends React.Component<
12161204
return null;
12171205
}
12181206

1219-
// /**
1220-
// * Return an object with the entry of (id, matched_indices)
1221-
// * @param id unique id of snippet
1222-
// * @param regex regular expression to match
1223-
// * @param str name or language of the code snippet
1224-
// * @param char_list list of characters searched
1225-
// */
1226-
// matchSnippet(
1227-
// id: number,
1228-
// regex: RegExp,
1229-
// str: string,
1230-
// char_list: string[]
1231-
// ): [number, number[]] {
1232-
// const match_indices = [];
1233-
// let match = [];
1234-
// while ((match = regex.exec(str))) {
1235-
// if (match != []) {
1236-
// const matched_string = match[0];
1237-
// const start_idx = match['index'];
1238-
// for (const match_ch of match.slice(1)) {
1239-
// const match_index = matched_string.indexOf(match_ch) + start_idx;
1240-
// match_indices.push(match_index);
1241-
// }
1242-
1243-
// // Object.keys(matches).length
1244-
// // if (Object.keys(matches).length !== char_list.length) {
1245-
// // return null;
1246-
// // }
1247-
1248-
// return [id, match_indices];
1249-
// }
1250-
// }
1251-
// // console.log(regex.exec(str));
1252-
// return null;
1253-
// }
1254-
12551207
filterSnippets = (searchValue: string, filterTags: string[]): void => {
1256-
// console.log(regex);
1257-
1258-
// const str = 'Pythonmost_frequent';
1259-
1260-
// let match = [];
1261-
// let match_index = [];
1262-
// while(match = regex.exec(str)) {
1263-
// match_index.push(match.index)
1264-
// // console.log(match);
1265-
// // console.log("match found at " + match.index);
1266-
// }
1267-
1268-
// if(match_index.length !== char_list.length){
1269-
// match_index = []
1270-
// }
1271-
// console.log(match_index);
1272-
1273-
// const found = [...'Pythonmost_frequent'.matchAll(regex)];
1274-
// console.log(found.index);
1275-
1276-
// TODO: filter codes nippet with regex!
12771208
// filter with search
1278-
const matchIndices: number[][] = [];
1209+
let matchIndices: number[][] = [];
1210+
const matchResults: StringExt.IMatchResult[] = [];
12791211
let filteredSnippets = this.props.codeSnippets;
1212+
const filteredSnippetsScore: {
1213+
score: number;
1214+
snippet: ICodeSnippet;
1215+
}[] = [];
12801216
if (searchValue !== '') {
1281-
filteredSnippets = filteredSnippets.filter(snippet => {
1282-
const indices = StringExt.findIndices(
1217+
filteredSnippets.forEach(snippet => {
1218+
const matchResult = StringExt.matchSumOfSquares(
12831219
(snippet.language + snippet.name).toLowerCase(),
1284-
searchValue.toLowerCase()
1220+
searchValue.replace(' ', '').toLowerCase()
12851221
);
1286-
if (indices) {
1287-
matchIndices.push(indices);
1222+
1223+
if (matchResult) {
1224+
matchResults.push(matchResult);
1225+
filteredSnippetsScore.push({
1226+
score: matchResult.score,
1227+
snippet: snippet
1228+
});
12881229
}
1289-
return indices !== null;
12901230
});
12911231

1292-
// remove space
1293-
// const tempSearchValue = searchValue.replace(/ /g, '');
1294-
// const char_list = tempSearchValue.split('');
1295-
1296-
// // form regular expression
1297-
// let re = '';
1298-
// for (const ch of char_list) {
1299-
// if ("[].*?^$+|(){}".includes(ch)) {
1300-
// re += '(\\' + ch + ').*?';
1301-
// }
1302-
// else{
1303-
// re += '(' + ch + ').*?';
1304-
// }
1305-
// }
1306-
// re = re.substring(0, re.length - 3);
1307-
// console.log('regex:' + re);
1308-
1309-
// const regex = new RegExp(re, 'i');
1310-
1311-
// filteredSnippets = this.props.codeSnippets.filter(codeSnippet => {
1312-
// // console.log(codeSnippet.language + codeSnippet.name);
1313-
// const matchIndex = this.matchSnippet(
1314-
// codeSnippet.id,
1315-
// regex,
1316-
// codeSnippet.language + codeSnippet.name,
1317-
// char_list
1318-
// );
1319-
// // console.log(matchIndex);
1320-
1321-
// if (matchIndex) {
1322-
// matchIndices[matchIndex[0]] = matchIndex[1];
1323-
// }
1324-
// return matchIndex !== null;
1325-
// });
1232+
// sort snippets by its score
1233+
filteredSnippetsScore.sort((a, b) => a.score - b.score);
1234+
const newFilteredSnippets: ICodeSnippet[] = [];
1235+
filteredSnippetsScore.forEach(snippetScore =>
1236+
newFilteredSnippets.push(snippetScore.snippet)
1237+
);
1238+
filteredSnippets = newFilteredSnippets;
1239+
1240+
// sort the matchResults by its score
1241+
matchResults.sort((a, b) => a.score - b.score);
1242+
matchResults.forEach(res => matchIndices.push(res.indices));
13261243
}
13271244

13281245
// filter with tags
13291246
if (filterTags.length !== 0) {
1330-
filteredSnippets = filteredSnippets.filter(codeSnippet => {
1247+
const newMatchIndices = matchIndices.slice();
1248+
filteredSnippets = filteredSnippets.filter((codeSnippet, id) => {
13311249
return filterTags.some(tag => {
13321250
if (codeSnippet.tags) {
1333-
return codeSnippet.tags.includes(tag);
1251+
if (codeSnippet.tags.includes(tag)) {
1252+
return true;
1253+
}
13341254
}
1255+
// if the snippet does not have the tag, remove its mathed index
1256+
const matchedIndex = matchIndices[id];
1257+
const indexToDelete = newMatchIndices.indexOf(matchedIndex);
1258+
newMatchIndices.splice(indexToDelete, 1);
13351259
return false;
13361260
});
13371261
});
1262+
matchIndices = newMatchIndices;
13381263
}
13391264

1340-
console.log(filteredSnippets);
1341-
console.log(matchIndices);
1342-
13431265
this.setState(
13441266
{
13451267
codeSnippets: filteredSnippets,

0 commit comments

Comments
 (0)