Skip to content

Commit 02dd73c

Browse files
committed
Language tags added and working. Search by code working.
1 parent 9bfce5d commit 02dd73c

File tree

7 files changed

+105
-228
lines changed

7 files changed

+105
-228
lines changed

docs/contributor/codebase.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Description of each file in ``src/``
6161
confirmation dialog as a modal window after snippet creation.
6262
- FilterTools.tsx: this contains a react component that renders a
6363
search bar and filter box.
64-
- MoreOptions.ts: this contains a lumino widget that creates dropdown
64+
- CodeSnippetMenu.ts: this contains a lumino widget that creates dropdown
6565
dialog when three dots icon is clicked.
6666
- PreviewSnippet.ts: this contains a lumino widget used to create
6767
preview minimap.

src/CodeSnippetDisplay.tsx

Lines changed: 12 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,22 +1304,24 @@ export class CodeSnippetDisplay extends React.Component<
13041304
return null;
13051305
}
13061306

1307-
// This function will be called to process user input for a narrowed search
1308-
// using various options. The user will enter a single search term which will
1309-
// searched across multiple options (name, lang, code).
1310-
filterSnippetsHelper = (searchValue: string, options: string[]) => {
1307+
filterSnippets = (searchValue: string, filterTags: string[]): void => {
1308+
// filter with search
1309+
let matchIndices: number[][] = [];
13111310
const matchResults: StringExt.IMatchResult[] = [];
1312-
const filteredSnippets = this.props.codeSnippets;
1313-
//let language: boolean = false;
1314-
//let code: boolean = false;
1311+
let filteredSnippets = this.props.codeSnippets;
13151312
const filteredSnippetsScore: {
13161313
score: number;
13171314
snippet: ICodeSnippet;
13181315
}[] = [];
13191316
if (searchValue !== '') {
1317+
// language, title, code
13201318
filteredSnippets.forEach(snippet => {
13211319
const matchResult = StringExt.matchSumOfSquares(
1322-
(snippet.language + snippet.name).toLowerCase(),
1320+
(
1321+
snippet.language +
1322+
snippet.name +
1323+
snippet.code.join('\n')
1324+
).toLowerCase(),
13231325
searchValue.replace(' ', '').toLowerCase()
13241326
);
13251327

@@ -1331,131 +1333,6 @@ export class CodeSnippetDisplay extends React.Component<
13311333
});
13321334
}
13331335
});
1334-
}
1335-
};
1336-
1337-
filterSnippets = (
1338-
searchValue: string,
1339-
filterTags: string[],
1340-
languageTags: string[]
1341-
): void => {
1342-
// filter with search
1343-
let matchIndices: number[][] = [];
1344-
const matchResults: StringExt.IMatchResult[] = [];
1345-
let filteredSnippets = this.props.codeSnippets;
1346-
let name = false;
1347-
//let language: boolean = false;
1348-
//let code: boolean = false;
1349-
const filteredSnippetsScore: {
1350-
score: number;
1351-
snippet: ICodeSnippet;
1352-
}[] = [];
1353-
// if (searchValue !== '') {
1354-
// filteredSnippets.forEach(snippet => {
1355-
// const matchResult = StringExt.matchSumOfSquares(
1356-
// (snippet.language + snippet.name).toLowerCase(),
1357-
// searchValue.replace(' ', '').toLowerCase()
1358-
// );
1359-
1360-
// if (matchResult) {
1361-
// matchResults.push(matchResult);
1362-
// filteredSnippetsScore.push({
1363-
// score: matchResult.score,
1364-
// snippet: snippet
1365-
// });
1366-
// }
1367-
// });
1368-
if (searchValue !== '') {
1369-
// title/code/language
1370-
if (searchValue.includes('/*')) {
1371-
const searchVals: string[] = searchValue.split('/*');
1372-
if (searchVals[0] !== undefined) {
1373-
name = true;
1374-
filteredSnippets.forEach(snippet => {
1375-
//search by name
1376-
const matchResult = StringExt.matchSumOfSquares(
1377-
snippet.name.toLowerCase(),
1378-
searchVals[0].replace(' ', '').toLowerCase()
1379-
);
1380-
1381-
if (matchResult) {
1382-
matchResults.push(matchResult);
1383-
filteredSnippetsScore.push({
1384-
score: matchResult.score,
1385-
snippet: snippet
1386-
});
1387-
}
1388-
});
1389-
}
1390-
1391-
if (searchVals[1] !== undefined) {
1392-
// THIS DOESNT WORK. Have to check a snippet language and remove.
1393-
//search by language
1394-
console.log('language');
1395-
let filtered: ICodeSnippet[] = filteredSnippets;
1396-
if (name) {
1397-
//if a name is entered, narrow by both name and language
1398-
filtered = [];
1399-
filteredSnippetsScore.forEach(snippetScore =>
1400-
filtered.push(snippetScore.snippet)
1401-
);
1402-
}
1403-
filtered.forEach(snippet => {
1404-
const matchResult2 = StringExt.matchSumOfSquares(
1405-
snippet.language.toLowerCase(),
1406-
searchVals[1].replace(' ', '').toLowerCase()
1407-
);
1408-
1409-
if (matchResult2) {
1410-
matchResults.push(matchResult2);
1411-
filteredSnippetsScore.push({
1412-
score: matchResult2.score,
1413-
snippet: snippet
1414-
});
1415-
}
1416-
});
1417-
}
1418-
// if (searchVals[2] !== undefined) {
1419-
// // search for search term through the code lines
1420-
// console.log(searchVals[2]);
1421-
// filteredSnippets.forEach(snippet => {
1422-
// for (let snippetLine in snippet.code) {
1423-
// // go through each line of the code
1424-
// let matchResult3 = StringExt.matchSumOfSquares(
1425-
// snippetLine.toLowerCase(),
1426-
// searchVals[2].toLowerCase()
1427-
// );
1428-
1429-
// if (matchResult3) {
1430-
// matchResults.push(matchResult3);
1431-
// filteredSnippetsScore.push({
1432-
// score: matchResult3.score,
1433-
// snippet: snippet
1434-
// });
1435-
// }
1436-
// }
1437-
// });
1438-
// }
1439-
} else {
1440-
filteredSnippets.forEach(snippet => {
1441-
const matchResult = StringExt.matchSumOfSquares(
1442-
(
1443-
snippet.language +
1444-
snippet.name +
1445-
snippet.code.join('\n')
1446-
).toLowerCase(),
1447-
searchValue.replace(' ', '').toLowerCase()
1448-
);
1449-
1450-
if (matchResult) {
1451-
matchResults.push(matchResult);
1452-
filteredSnippetsScore.push({
1453-
score: matchResult.score,
1454-
snippet: snippet
1455-
});
1456-
}
1457-
});
1458-
}
14591336

14601337
// sort snippets by its score
14611338
filteredSnippetsScore.sort((a, b) => a.score - b.score);
@@ -1470,8 +1347,6 @@ export class CodeSnippetDisplay extends React.Component<
14701347
matchResults.forEach(res => matchIndices.push(res.indices));
14711348
}
14721349

1473-
// filter with tags --- create a helper function with this code and call on both tags & languages
1474-
// 2 if statements filter by cell tags then language tags
14751350
if (filterTags.length !== 0) {
14761351
const newMatchIndices = matchIndices.slice();
14771352
filteredSnippets = filteredSnippets.filter((codeSnippet, id) => {
@@ -1489,20 +1364,7 @@ export class CodeSnippetDisplay extends React.Component<
14891364
const indexToDelete = newMatchIndices.indexOf(matchedIndex);
14901365
newMatchIndices.splice(indexToDelete, 1);
14911366
return false;
1492-
}) /*||
1493-
languageTags.some(lang => {
1494-
if (codeSnippet.language) {
1495-
if (codeSnippet.language === lang) {
1496-
//if a snippet's language matches one of the selected ones
1497-
return true;
1498-
}
1499-
}
1500-
// if the snippet does not have the tag, remove its mathed index
1501-
const matchedIndex = matchIndices[id];
1502-
const indexToDelete = newMatchIndices.indexOf(matchedIndex);
1503-
newMatchIndices.splice(indexToDelete, 1);
1504-
return false;
1505-
})*/;
1367+
});
15061368
});
15071369
matchIndices = newMatchIndices;
15081370
}
@@ -1521,8 +1383,6 @@ export class CodeSnippetDisplay extends React.Component<
15211383
};
15221384

15231385
getActiveTags(): [string[], string[]] {
1524-
//TODO: go through snippet languages and add those to a list & pass into FilterTools,
1525-
// in filterTools add a new language section and add list of lang tags there
15261386
const tags: string[] = [];
15271387
const languages: string[] = [];
15281388
for (const codeSnippet of this.props.codeSnippets) {
@@ -1714,7 +1574,7 @@ export class CodeSnippetDisplay extends React.Component<
17141574
<div className={CODE_SNIPPET_HEADER_BOX}>
17151575
<FilterTools
17161576
languages={this.getActiveTags()[1]}
1717-
tags={this.getActiveTags()[0]}
1577+
allTags={this.getActiveTags()}
17181578
onFilter={this.filterSnippets}
17191579
/>
17201580
</div>

src/CodeSnippetSearchTools.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import { Widget } from '@lumino/widgets';
3-
import { showMoreOptions } from './MoreOptions';
3+
import { showMoreOptions } from './CodeSnippetMenu';
44

55
import ReactDOM from 'react-dom';
66
import SearchOption from './CodeSnippetSearchOption';

src/CodeSnippetService.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,34 @@ export class CodeSnippetService {
4949
}
5050
});
5151

52+
console.log(this.settingManager.get('snippets'));
53+
5254
const defaultSnippets = this.convertToICodeSnippetList(
5355
this.settingManager.default('snippets') as JSONArray
5456
);
55-
const userSnippets = this.convertToICodeSnippetList(
56-
this.settingManager.get('snippets').user as JSONArray
57-
);
57+
if (this.settingManager.get('snippets').user === undefined) {
58+
// set the user setting + default in the beginning
59+
this.settingManager
60+
.set('snippets', (defaultSnippets as unknown) as PartialJSONValue)
61+
.then(() => {
62+
const userSnippets = this.convertToICodeSnippetList(
63+
this.settingManager.get('snippets').user as JSONArray
64+
);
65+
this.codeSnippetList = userSnippets;
66+
});
67+
} else {
68+
const userSnippets = this.convertToICodeSnippetList(
69+
this.settingManager.get('snippets').user as JSONArray
70+
);
5871

59-
if (userSnippets.length !== 0) {
6072
this.codeSnippetList = userSnippets;
61-
} else {
62-
this.codeSnippetList = defaultSnippets;
6373
}
64-
65-
// set the user setting + default in the beginning
66-
this.settingManager.set(
67-
'snippets',
68-
(this.codeSnippetList as unknown) as PartialJSONValue
69-
);
7074
}
7175

7276
private convertToICodeSnippetList(snippets: JSONArray): ICodeSnippet[] {
7377
const snippetList: ICodeSnippet[] = [];
7478

75-
snippets.forEach((snippet) => {
79+
snippets.forEach(snippet => {
7680
snippetList.push((snippet as unknown) as ICodeSnippet);
7781
});
7882
return snippetList;
@@ -94,7 +98,7 @@ export class CodeSnippetService {
9498

9599
getSnippet(snippetName: string): ICodeSnippet[] {
96100
return this.codeSnippetList.filter(
97-
(snippet) => snippet.name.toLowerCase() === snippetName.toLowerCase()
101+
snippet => snippet.name.toLowerCase() === snippetName.toLowerCase()
98102
);
99103
}
100104

@@ -112,7 +116,7 @@ export class CodeSnippetService {
112116

113117
await this.settingManager
114118
.set('snippets', (this.codeSnippetList as unknown) as PartialJSONValue)
115-
.catch((_) => {
119+
.catch(_ => {
116120
return false;
117121
});
118122
return true;
@@ -140,7 +144,7 @@ export class CodeSnippetService {
140144

141145
await this.settingManager
142146
.set('snippets', (this.codeSnippetList as unknown) as PartialJSONValue)
143-
.catch((_) => {
147+
.catch(_ => {
144148
return false;
145149
});
146150

@@ -156,7 +160,7 @@ export class CodeSnippetService {
156160
}
157161
await this.settingManager
158162
.set('snippets', (this.codeSnippetList as unknown) as PartialJSONValue)
159-
.catch((_) => {
163+
.catch(_ => {
160164
return false;
161165
});
162166
return true;
@@ -184,7 +188,7 @@ export class CodeSnippetService {
184188

185189
await this.settingManager
186190
.set('snippets', (this.codeSnippetList as unknown) as PartialJSONValue)
187-
.catch((_) => {
191+
.catch(_ => {
188192
return false;
189193
});
190194
return true;
@@ -216,7 +220,7 @@ export class CodeSnippetService {
216220

217221
await this.settingManager
218222
.set('snippets', (this.codeSnippetList as unknown) as PartialJSONValue)
219-
.catch((_) => {
223+
.catch(_ => {
220224
return false;
221225
});
222226
return true;
@@ -233,7 +237,7 @@ export class CodeSnippetService {
233237

234238
await this.settingManager
235239
.set('snippets', (this.codeSnippetList as unknown) as PartialJSONValue)
236-
.catch((_) => {
240+
.catch(_ => {
237241
return false;
238242
});
239243
return true;

src/CodeSnippetWidget.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const DROP_TARGET_CLASS = 'jp-codeSnippet-dropTarget';
4949
const CODE_SNIPPET_EDITOR = 'jp-codeSnippet-editor';
5050

5151
const commands = {
52-
OPEN_CODE_SNIPPET_EDITOR: `${CODE_SNIPPET_EDITOR}:open`,
52+
OPEN_CODE_SNIPPET_EDITOR: `${CODE_SNIPPET_EDITOR}:open`
5353
};
5454

5555
/**
@@ -83,6 +83,7 @@ export class CodeSnippetWidget extends ReactWidget {
8383
}
8484

8585
updateCodeSnippetWidget(): void {
86+
console.log(this);
8687
const newSnippets = this.codeSnippetManager.snippets;
8788
this.renderCodeSnippetsSignal.emit(newSnippets);
8889
}

0 commit comments

Comments
 (0)