Skip to content

Commit

Permalink
Type annotation improvements and refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
mhutchie committed Feb 25, 2019
1 parent f400697 commit 50d3ef8
Show file tree
Hide file tree
Showing 9 changed files with 300 additions and 328 deletions.
6 changes: 3 additions & 3 deletions media/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ body.notGitRepository h1, body.notGitRepository p{
content:'';
display:block;
position:absolute;
left:0;
right:0;
left:0;
right:0;
bottom:0;
height:2px;
background-color:rgba(128,128,128,0.2);
background-color:rgba(128,128,128,0.2);
}
#commitTable tr.commit.commitDetailsOpen td{
background-color:rgba(128,128,128,0.25);
Expand Down
9 changes: 1 addition & 8 deletions src/dataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,13 @@ export class DataSource {

for (i = 0; i < commits.length; i++) {
commitLookup[commits[i].hash] = i;
commitNodes.push({ hash: commits[i].hash, parents: [], author: commits[i].author, email: commits[i].email, date: commits[i].date, message: commits[i].message, refs: [], current: false });
commitNodes.push({ hash: commits[i].hash, parentHashes: commits[i].parentHashes, author: commits[i].author, email: commits[i].email, date: commits[i].date, message: commits[i].message, refs: [], current: false });
}
for (i = 0; i < refs.length; i++) {
if (typeof commitLookup[refs[i].hash] === 'number') {
commitNodes[commitLookup[refs[i].hash]].refs.push(refs[i]);
}
}
for (i = commits.length - 1; i >= 0; i--) {
for (j = 0; j < commits[i].parentHashes.length; j++) {
if (typeof commitLookup[commits[i].parentHashes[j]] === 'number') {
commitNodes[i].parents.push(commitLookup[commits[i].parentHashes[j]]);
}
}
}

if (unsavedChanges !== null) {
commitNodes[0].current = true;
Expand Down
118 changes: 58 additions & 60 deletions src/gitGraphView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,74 +45,74 @@ export class GitGraphView {
}
}, null, this.disposables);

this.panel.webview.onDidReceiveMessage(async (message: RequestMessage) => {
this.panel.webview.onDidReceiveMessage(async (msg: RequestMessage) => {
if (this.dataSource === null) return;
switch (message.command) {
case 'loadBranches':
this.sendMessage({
command: 'loadBranches',
data: await this.dataSource.getBranches(message.data.showRemoteBranches)
});
return;
case 'loadCommits':
switch (msg.command) {
case 'addTag':
this.sendMessage({
command: 'loadCommits',
data: await this.dataSource.getCommits(message.data.branch, message.data.maxCommits, message.data.showRemoteBranches, message.data.currentBranch)
command: 'addTag',
status: await this.dataSource.addTag(msg.tagName, msg.commitHash)
});
return;
case 'addTag':
case 'checkoutBranch':
this.sendMessage({
command: 'addTag',
data: await this.dataSource.addTag(message.data.tagName, message.data.commitHash)
command: 'checkoutBranch',
status: await this.dataSource.checkoutBranch(msg.branchName, msg.remoteBranch)
});
return;
case 'deleteTag':
case 'commitDetails':
this.sendMessage({
command: 'deleteTag',
data: await this.dataSource.deleteTag(message.data)
command: 'commitDetails',
commitDetails: await this.dataSource.commitDetails(msg.commitHash)
});
return;
case 'copyCommitHashToClipboard':
this.copyCommitHashToClipboard(message.data);
this.copyCommitHashToClipboard(msg.commitHash);
return;
case 'createBranch':
this.sendMessage({
command: 'createBranch',
data: await this.dataSource.createBranch(message.data.branchName, message.data.commitHash)
status: await this.dataSource.createBranch(msg.branchName, msg.commitHash)
});
return;
case 'checkoutBranch':
case 'deleteBranch':
this.sendMessage({
command: 'checkoutBranch',
data: await this.dataSource.checkoutBranch(message.data.branchName, message.data.remoteBranch)
command: 'deleteBranch',
status: await this.dataSource.deleteBranch(msg.branchName, msg.forceDelete)
});
return;
case 'deleteBranch':
case 'deleteTag':
this.sendMessage({
command: 'deleteBranch',
data: await this.dataSource.deleteBranch(message.data.branchName, message.data.forceDelete)
command: 'deleteTag',
status: await this.dataSource.deleteTag(msg.tagName)
});
return;
case 'loadBranches':
this.sendMessage({
command: 'loadBranches',
branches: await this.dataSource.getBranches(msg.showRemoteBranches)
});
return;
case 'loadCommits':
this.sendMessage({
command: 'loadCommits',
... await this.dataSource.getCommits(msg.branchName, msg.maxCommits, msg.showRemoteBranches, msg.currentBranch)
});
return;
case 'renameBranch':
this.sendMessage({
command: 'renameBranch',
data: await this.dataSource.renameBranch(message.data.oldName, message.data.newName)
status: await this.dataSource.renameBranch(msg.oldName, msg.newName)
});
return;
case 'resetToCommit':
this.sendMessage({
command: 'resetToCommit',
data: await this.dataSource.resetToCommit(message.data.commitHash, message.data.resetMode)
});
return;
case 'commitDetails':
this.sendMessage({
command: 'commitDetails',
data: await this.dataSource.commitDetails(message.data)
status: await this.dataSource.resetToCommit(msg.commitHash, msg.resetMode)
});
return;
case 'viewDiff':
this.viewDiff(message.data.commitHash, message.data.oldFilePath, message.data.newFilePath, message.data.type);
this.viewDiff(msg.commitHash, msg.oldFilePath, msg.newFilePath, msg.type);
return;
}
}, null, this.disposables);
Expand All @@ -135,11 +135,8 @@ export class GitGraphView {

private async getHtmlForWebview() {
const config = new Config();
const jsPathOnDisk = vscode.Uri.file(path.join(this.extensionPath, 'media', 'main.min.js'));
const jsUri = jsPathOnDisk.with({ scheme: 'vscode-resource' });
const cssPathOnDisk = vscode.Uri.file(path.join(this.extensionPath, 'media', 'main.css'));
const cssUri = cssPathOnDisk.with({ scheme: 'vscode-resource' });
const isRepo = this.dataSource !== null && await this.dataSource.isGitRepository();
const jsUri = vscode.Uri.file(path.join(this.extensionPath, 'media', 'main.min.js')).with({ scheme: 'vscode-resource' });
const cssUri = vscode.Uri.file(path.join(this.extensionPath, 'media', 'main.css')).with({ scheme: 'vscode-resource' });
const nonce = getNonce();

let settings: GitGraphViewSettings = {
Expand All @@ -151,23 +148,13 @@ export class GitGraphView {
loadMoreCommits: config.loadMoreCommits()
};

let colourStyles = '';
let colourStyles = '', body;
for (let i = 0; i < settings.graphColours.length; i++) {
colourStyles += '.colour' + i + ' { background-color:' + settings.graphColours[i] + '; } ';
}

let html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src vscode-resource: 'nonce-${nonce}'; script-src vscode-resource: 'nonce-${nonce}';">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="${cssUri}">
<title>Git Graph</title>
<style nonce="${nonce}">${colourStyles}</style>
</head>`;
if (isRepo) {
html += `<body>
if (this.dataSource !== null && await this.dataSource.isGitRepository()) {
body = `<body>
<div id="controls">
<span class="unselectable">Branch: </span><select id="branchSelect"></select>
<label><input type="checkbox" id="showRemoteBranchesCheckbox" value="1" checked>Show Remote Branches</label>
Expand All @@ -182,20 +169,31 @@ export class GitGraphView {
<script src="${jsUri}"></script>
</body>`;
} else {
html += `<body class="notGitRepository"><h1>Git Graph</h1><p>The current workspace is not a Git Repository, unable to show Git Graph.</p></body>`;
body = `<body class="notGitRepository"><h1>Git Graph</h1><p>The current workspace is not a Git Repository, unable to show Git Graph.</p></body>`;
}
html += `</html>`;
return html;

return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src vscode-resource: 'nonce-${nonce}'; script-src vscode-resource: 'nonce-${nonce}';">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="${cssUri}">
<title>Git Graph</title>
<style nonce="${nonce}">${colourStyles}</style>
</head>
${body}
</html>`;
}

private sendMessage(msg: ResponseMessage) {
this.panel.webview.postMessage(msg);
}

private copyCommitHashToClipboard(str: string) {
vscode.env.clipboard.writeText(str).then(
() => this.sendMessage({ command: 'copyCommitHashToClipboard', data: true }),
() => this.sendMessage({ command: 'copyCommitHashToClipboard', data: false })
private copyCommitHashToClipboard(commitHash: string) {
vscode.env.clipboard.writeText(commitHash).then(
() => this.sendMessage({ command: 'copyCommitHashToClipboard', success: true }),
() => this.sendMessage({ command: 'copyCommitHashToClipboard', success: false })
);
}

Expand All @@ -204,7 +202,7 @@ export class GitGraphView {
let pathComponents = newFilePath.split('/');
let title = pathComponents[pathComponents.length - 1] + ' (' + (type === 'A' ? 'Added in ' + abbrevHash : type === 'D' ? 'Deleted in ' + abbrevHash : abbrevCommit(commitHash) + '^ ↔ ' + abbrevCommit(commitHash)) + ')';
vscode.commands.executeCommand('vscode.diff', encodeDiffDocUri(oldFilePath, commitHash + '^'), encodeDiffDocUri(newFilePath, commitHash), title, { preview: true });
this.sendMessage({ command: 'viewDiff', data: true });
this.sendMessage({ command: 'viewDiff', success: true });
}
}

Expand Down
14 changes: 8 additions & 6 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"outDir": "../out",
"declaration": true,
"lib": [
"es6"
],
"declaration": true,
"module": "commonjs",
"outDir": "../out",
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"sourceMap": true,
"strict": true
"strict": true,
"target": "es6"
}
}
}
Loading

0 comments on commit 50d3ef8

Please sign in to comment.