Skip to content

Commit

Permalink
Allow for searching by tag, description
Browse files Browse the repository at this point in the history
  • Loading branch information
eschwartz committed Oct 7, 2021
1 parent d9a4870 commit 2c2d0db
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 18 deletions.
11 changes: 7 additions & 4 deletions src/DecoratorProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ export default class DecoratorProvider extends vscode.Disposable {
const d: vscode.DecorationOptions[] = [];
while ((match = regEx.exec(text))) {
const name = match[1];
const emoji = this.emojiProvider.lookup(name);
if (!emoji) {
const emojis = this.emojiProvider.lookup(name);
if (!emojis.length) {
continue;
}

const startPos = activeEditor.document.positionAt(match.index + 1);
const endPos = activeEditor.document.positionAt(match.index + match[0].length - 1);
d.push({

const emojiDecorations = emojis.map(emoji => ({
range: new vscode.Range(startPos, endPos),
hoverMessage: this.hoverMessage(emoji),
renderOptions: {
Expand All @@ -82,7 +83,9 @@ export default class DecoratorProvider extends vscode.Disposable {
color: 'rgba(255, 255, 255, 0.55)',
},
},
});
}));

d.push(...emojiDecorations);
}
activeEditor.setDecorations(this.decorationType, d);
}
Expand Down
22 changes: 18 additions & 4 deletions src/EmojiCompletionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,15 @@ export default class EmojiCompletionProvider implements vscode.CompletionItemPro

const kind = vscode.CompletionItemKind.Text;
return this.emojiProvider.emojis.map((x) => {
const item = new vscode.CompletionItem(`:${x.name}: ${x.emoji}`, kind);
item.filterText = `:${x.name}:`;
const item = new vscode.CompletionItem({
label: `:${x.name}: ${x.emoji}`,
// If the search term doesn't match
// the actual emoji name, show the
// search term, as well
description: x.name === x.lookupKey ?
undefined : x.lookupKey
}, kind);
item.filterText = `:${x.lookupKey}:`;
item.documentation = new vscode.MarkdownString(`# ${x.emoji}`);
item.insertText = x.emoji;
item.range = replacementSpan;
Expand All @@ -73,8 +80,15 @@ export default class EmojiCompletionProvider implements vscode.CompletionItemPro

const kind = vscode.CompletionItemKind.Text;
return this.emojiProvider.emojis.map((x) => {
const item = new vscode.CompletionItem(`::${x.name} ${x.emoji}`, kind);
item.filterText = `::${x.name}`;
const item = new vscode.CompletionItem({
label: `::${x.name} ${x.emoji}`,
// If the search term doesn't match
// the actual emoji name, show the
// search term, as well
description: x.name === x.lookupKey ?
undefined : x.lookupKey
}, kind);
item.filterText = `::${x.lookupKey}`;
item.documentation = new vscode.MarkdownString(`# ${x.emoji}`);
item.insertText = `:${x.name}:`;
item.range = replacementSpan;
Expand Down
38 changes: 28 additions & 10 deletions src/emoji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,49 @@ import { gemoji } from 'gemoji';
export interface Emoji {
readonly name: string;
readonly emoji: string;
readonly lookupKey: string;
}

export class EmojiProvider {
private _emojiMap?: Map<string, Emoji>;
private _emojiMap?: Map<string, Emoji[]>;
private _emojis?: ReadonlyArray<Emoji>;

public get emojis(): ReadonlyArray<Emoji> {
if (!this._emojis) {
this._emojis = Array.from(this.emojiMap.values());
// Grab all Emoji[] values for each alias
// then flatten into an array
this._emojis = Array.from(this.emojiMap.values())
.reduce((arr, emojis) => [...arr, ...emojis], []);
}
return this._emojis;
}

public lookup(name: string): Emoji | undefined {
return this.emojiMap.get(name.toLowerCase());
/**
* Find a list of matching emojis
* Lookup by name, tag, or description
*/
public lookup(key: string): Emoji[] {
return this.emojiMap.get(key.toLowerCase()) || [];
}

private get emojiMap(): Map<string, Emoji> {
private get emojiMap(): Map<string, Emoji[]> {
if (!this._emojiMap) {
this._emojiMap = new Map<string, Emoji>();
this._emojiMap = new Map<string, Emoji[]>();
for (const g of gemoji) {
for (const name of g.names) {
if (!this._emojiMap.has(name)) {
this._emojiMap.set(name, { name, emoji: g.emoji });
}
// We can lookup emojis by name, tag, or description
const lookupKeys = [
...g.names,
...g.tags,
g.description
];

for (const key of lookupKeys) {
// Each lookup key may have multiple matching emojis
const otherEmojis = this._emojiMap.get(key) || [];
this._emojiMap.set(key, [
...otherEmojis,
{ name: g.names[0], emoji: g.emoji, lookupKey: key }
]);
}
}
}
Expand Down

0 comments on commit 2c2d0db

Please sign in to comment.