Closes #91695 - adds emoji support

This commit is contained in:
Eric Amodio 2020-10-12 14:38:07 -04:00
parent 31419adc34
commit 0ac9d25b81
6 changed files with 156 additions and 2 deletions

View file

@ -0,0 +1,101 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs');
const https = require('https');
const path = require('path');
async function generate() {
/**
* @type {Map<string, string>}
*/
const shortcodeMap = new Map();
// Get emoji data from https://github.com/milesj/emojibase
// https://github.com/milesj/emojibase/
const files = ['github.raw.json'] //, 'emojibase.raw.json']; //, 'iamcal.raw.json', 'joypixels.raw.json'];
for (const file of files) {
await download(
`https://raw.githubusercontent.com/milesj/emojibase/master/packages/data/en/shortcodes/${file}`,
file,
);
/**
* @type {Record<string, string | string[]>}}
*/
// eslint-disable-next-line import/no-dynamic-require
const data = require(path.join(process.cwd(), file));
for (const [emojis, codes] of Object.entries(data)) {
const emoji = emojis
.split('-')
.map(c => String.fromCodePoint(parseInt(c, 16)))
.join('');
for (const code of Array.isArray(codes) ? codes : [codes]) {
if (shortcodeMap.has(code)) {
// console.warn(`${file}: ${code}`);
continue;
}
shortcodeMap.set(code, emoji);
}
}
fs.unlink(file, () => { });
}
// Get gitmoji data from https://github.com/carloscuesta/gitmoji
// https://github.com/carloscuesta/gitmoji/blob/master/src/data/gitmojis.json
await download(
'https://raw.githubusercontent.com/carloscuesta/gitmoji/master/src/data/gitmojis.json',
'gitmojis.json',
);
/**
* @type {({ code: string; emoji: string })[]}
*/
// eslint-disable-next-line import/no-dynamic-require
const gitmojis = require(path.join(process.cwd(), 'gitmojis.json')).gitmojis;
for (const emoji of gitmojis) {
if (emoji.code.startsWith(':') && emoji.code.endsWith(':')) {
emoji.code = emoji.code.substring(1, emoji.code.length - 2);
}
if (shortcodeMap.has(emoji.code)) {
// console.warn(`GitHub: ${emoji.code}`);
continue;
}
shortcodeMap.set(emoji.code, emoji.emoji);
}
fs.unlink('gitmojis.json', () => { });
// Sort the emojis for easier diff checking
const list = [...shortcodeMap.entries()];
list.sort();
const map = list.reduce((m, [key, value]) => {
m[key] = value;
return m;
}, Object.create(null));
fs.writeFileSync(path.join(process.cwd(), 'resources/emojis.json'), JSON.stringify(map), 'utf8');
}
function download(url, destination) {
return new Promise(resolve => {
const stream = fs.createWriteStream(destination);
https.get(url, rsp => {
rsp.pipe(stream);
stream.on('finish', () => {
stream.close();
resolve();
});
});
});
}
void generate();

View file

@ -22,6 +22,7 @@
"scripts": {
"compile": "gulp compile-extension:git",
"watch": "gulp watch-extension:git",
"update-emoji": "node ./build/update-emoji.js",
"update-grammar": "node ./build/update-grammars.js",
"test": "mocha"
},

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,39 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { workspace, Uri } from 'vscode';
import { getExtensionContext } from './main';
import { TextDecoder } from 'util';
const emojiRegex = /:([-+_a-z0-9]+):/g;
let emojiMap: Record<string, string> | undefined;
let emojiMapPromise: Promise<void> | undefined;
export async function ensureEmojis() {
if (emojiMap === undefined) {
if (emojiMapPromise === undefined) {
emojiMapPromise = loadEmojiMap();
}
await emojiMapPromise;
}
}
async function loadEmojiMap() {
const context = getExtensionContext();
const uri = (Uri as any).joinPath(context.extensionUri, 'resources', 'emojis.json');
emojiMap = JSON.parse(new TextDecoder('utf8').decode(await workspace.fs.readFile(uri)));
}
export function emojify(message: string) {
if (emojiMap === undefined) {
return message;
}
return message.replace(emojiRegex, (s, code) => {
return emojiMap?.[code] || s;
});
}

View file

@ -169,7 +169,14 @@ export async function _activate(context: ExtensionContext): Promise<GitExtension
}
}
let _context: ExtensionContext;
export function getExtensionContext(): ExtensionContext {
return _context;
}
export async function activate(context: ExtensionContext): Promise<GitExtension> {
_context = context;
const result = await _activate(context);
context.subscriptions.push(registerAPICommands(result));
return result;

View file

@ -8,6 +8,7 @@ import { CancellationToken, ConfigurationChangeEvent, Disposable, env, Event, Ev
import { Model } from './model';
import { Repository, Resource } from './repository';
import { debounce } from './decorators';
import { emojify, ensureEmojis } from './emoji';
const localize = nls.loadMessageBundle();
@ -132,6 +133,8 @@ export class GitTimelineProvider implements TimelineProvider {
limit = options.limit === undefined ? undefined : options.limit + 1;
}
await ensureEmojis();
const commits = await repo.logFile(uri, {
maxEntries: limit,
hash: options.cursor,
@ -155,12 +158,14 @@ export class GitTimelineProvider implements TimelineProvider {
const items = commits.map<GitTimelineItem>((c, i) => {
const date = dateType === 'authored' ? c.authorDate : c.commitDate;
const item = new GitTimelineItem(c.hash, commits[i + 1]?.hash ?? `${c.hash}^`, c.message, date?.getTime() ?? 0, c.hash, 'git:file:commit');
const message = emojify(c.message);
const item = new GitTimelineItem(c.hash, commits[i + 1]?.hash ?? `${c.hash}^`, message, date?.getTime() ?? 0, c.hash, 'git:file:commit');
item.iconPath = new (ThemeIcon as any)('git-commit');
if (showAuthor) {
item.description = c.authorName;
}
item.detail = `${c.authorName} (${c.authorEmail}) — ${c.hash.substr(0, 8)}\n${dateFormatter.format(date)}\n\n${c.message}`;
item.detail = `${c.authorName} (${c.authorEmail}) — ${c.hash.substr(0, 8)}\n${dateFormatter.format(date)}\n\n${message}`;
item.command = {
title: 'Open Comparison',
command: 'git.timeline.openDiff',