Skip to content

Commit 50d3ef8

Browse files
committed
Type annotation improvements and refactoring.
1 parent f400697 commit 50d3ef8

File tree

9 files changed

+300
-328
lines changed

9 files changed

+300
-328
lines changed

media/main.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ body.notGitRepository h1, body.notGitRepository p{
110110
content:'';
111111
display:block;
112112
position:absolute;
113-
left:0;
114-
right:0;
113+
left:0;
114+
right:0;
115115
bottom:0;
116116
height:2px;
117-
background-color:rgba(128,128,128,0.2);
117+
background-color:rgba(128,128,128,0.2);
118118
}
119119
#commitTable tr.commit.commitDetailsOpen td{
120120
background-color:rgba(128,128,128,0.25);

src/dataSource.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,13 @@ export class DataSource {
7878

7979
for (i = 0; i < commits.length; i++) {
8080
commitLookup[commits[i].hash] = i;
81-
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 });
81+
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 });
8282
}
8383
for (i = 0; i < refs.length; i++) {
8484
if (typeof commitLookup[refs[i].hash] === 'number') {
8585
commitNodes[commitLookup[refs[i].hash]].refs.push(refs[i]);
8686
}
8787
}
88-
for (i = commits.length - 1; i >= 0; i--) {
89-
for (j = 0; j < commits[i].parentHashes.length; j++) {
90-
if (typeof commitLookup[commits[i].parentHashes[j]] === 'number') {
91-
commitNodes[i].parents.push(commitLookup[commits[i].parentHashes[j]]);
92-
}
93-
}
94-
}
9588

9689
if (unsavedChanges !== null) {
9790
commitNodes[0].current = true;

src/gitGraphView.ts

Lines changed: 58 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -45,74 +45,74 @@ export class GitGraphView {
4545
}
4646
}, null, this.disposables);
4747

48-
this.panel.webview.onDidReceiveMessage(async (message: RequestMessage) => {
48+
this.panel.webview.onDidReceiveMessage(async (msg: RequestMessage) => {
4949
if (this.dataSource === null) return;
50-
switch (message.command) {
51-
case 'loadBranches':
52-
this.sendMessage({
53-
command: 'loadBranches',
54-
data: await this.dataSource.getBranches(message.data.showRemoteBranches)
55-
});
56-
return;
57-
case 'loadCommits':
50+
switch (msg.command) {
51+
case 'addTag':
5852
this.sendMessage({
59-
command: 'loadCommits',
60-
data: await this.dataSource.getCommits(message.data.branch, message.data.maxCommits, message.data.showRemoteBranches, message.data.currentBranch)
53+
command: 'addTag',
54+
status: await this.dataSource.addTag(msg.tagName, msg.commitHash)
6155
});
6256
return;
63-
case 'addTag':
57+
case 'checkoutBranch':
6458
this.sendMessage({
65-
command: 'addTag',
66-
data: await this.dataSource.addTag(message.data.tagName, message.data.commitHash)
59+
command: 'checkoutBranch',
60+
status: await this.dataSource.checkoutBranch(msg.branchName, msg.remoteBranch)
6761
});
6862
return;
69-
case 'deleteTag':
63+
case 'commitDetails':
7064
this.sendMessage({
71-
command: 'deleteTag',
72-
data: await this.dataSource.deleteTag(message.data)
65+
command: 'commitDetails',
66+
commitDetails: await this.dataSource.commitDetails(msg.commitHash)
7367
});
7468
return;
7569
case 'copyCommitHashToClipboard':
76-
this.copyCommitHashToClipboard(message.data);
70+
this.copyCommitHashToClipboard(msg.commitHash);
7771
return;
7872
case 'createBranch':
7973
this.sendMessage({
8074
command: 'createBranch',
81-
data: await this.dataSource.createBranch(message.data.branchName, message.data.commitHash)
75+
status: await this.dataSource.createBranch(msg.branchName, msg.commitHash)
8276
});
8377
return;
84-
case 'checkoutBranch':
78+
case 'deleteBranch':
8579
this.sendMessage({
86-
command: 'checkoutBranch',
87-
data: await this.dataSource.checkoutBranch(message.data.branchName, message.data.remoteBranch)
80+
command: 'deleteBranch',
81+
status: await this.dataSource.deleteBranch(msg.branchName, msg.forceDelete)
8882
});
8983
return;
90-
case 'deleteBranch':
84+
case 'deleteTag':
9185
this.sendMessage({
92-
command: 'deleteBranch',
93-
data: await this.dataSource.deleteBranch(message.data.branchName, message.data.forceDelete)
86+
command: 'deleteTag',
87+
status: await this.dataSource.deleteTag(msg.tagName)
88+
});
89+
return;
90+
case 'loadBranches':
91+
this.sendMessage({
92+
command: 'loadBranches',
93+
branches: await this.dataSource.getBranches(msg.showRemoteBranches)
94+
});
95+
return;
96+
case 'loadCommits':
97+
this.sendMessage({
98+
command: 'loadCommits',
99+
... await this.dataSource.getCommits(msg.branchName, msg.maxCommits, msg.showRemoteBranches, msg.currentBranch)
94100
});
95101
return;
96102
case 'renameBranch':
97103
this.sendMessage({
98104
command: 'renameBranch',
99-
data: await this.dataSource.renameBranch(message.data.oldName, message.data.newName)
105+
status: await this.dataSource.renameBranch(msg.oldName, msg.newName)
100106
});
101107
return;
102108
case 'resetToCommit':
103109
this.sendMessage({
104110
command: 'resetToCommit',
105-
data: await this.dataSource.resetToCommit(message.data.commitHash, message.data.resetMode)
106-
});
107-
return;
108-
case 'commitDetails':
109-
this.sendMessage({
110-
command: 'commitDetails',
111-
data: await this.dataSource.commitDetails(message.data)
111+
status: await this.dataSource.resetToCommit(msg.commitHash, msg.resetMode)
112112
});
113113
return;
114114
case 'viewDiff':
115-
this.viewDiff(message.data.commitHash, message.data.oldFilePath, message.data.newFilePath, message.data.type);
115+
this.viewDiff(msg.commitHash, msg.oldFilePath, msg.newFilePath, msg.type);
116116
return;
117117
}
118118
}, null, this.disposables);
@@ -135,11 +135,8 @@ export class GitGraphView {
135135

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

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

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

159-
let html = `<!DOCTYPE html>
160-
<html lang="en">
161-
<head>
162-
<meta charset="UTF-8">
163-
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src vscode-resource: 'nonce-${nonce}'; script-src vscode-resource: 'nonce-${nonce}';">
164-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
165-
<link rel="stylesheet" type="text/css" href="${cssUri}">
166-
<title>Git Graph</title>
167-
<style nonce="${nonce}">${colourStyles}</style>
168-
</head>`;
169-
if (isRepo) {
170-
html += `<body>
156+
if (this.dataSource !== null && await this.dataSource.isGitRepository()) {
157+
body = `<body>
171158
<div id="controls">
172159
<span class="unselectable">Branch: </span><select id="branchSelect"></select>
173160
<label><input type="checkbox" id="showRemoteBranchesCheckbox" value="1" checked>Show Remote Branches</label>
@@ -182,20 +169,31 @@ export class GitGraphView {
182169
<script src="${jsUri}"></script>
183170
</body>`;
184171
} else {
185-
html += `<body class="notGitRepository"><h1>Git Graph</h1><p>The current workspace is not a Git Repository, unable to show Git Graph.</p></body>`;
172+
body = `<body class="notGitRepository"><h1>Git Graph</h1><p>The current workspace is not a Git Repository, unable to show Git Graph.</p></body>`;
186173
}
187-
html += `</html>`;
188-
return html;
174+
175+
return `<!DOCTYPE html>
176+
<html lang="en">
177+
<head>
178+
<meta charset="UTF-8">
179+
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src vscode-resource: 'nonce-${nonce}'; script-src vscode-resource: 'nonce-${nonce}';">
180+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
181+
<link rel="stylesheet" type="text/css" href="${cssUri}">
182+
<title>Git Graph</title>
183+
<style nonce="${nonce}">${colourStyles}</style>
184+
</head>
185+
${body}
186+
</html>`;
189187
}
190188

191189
private sendMessage(msg: ResponseMessage) {
192190
this.panel.webview.postMessage(msg);
193191
}
194192

195-
private copyCommitHashToClipboard(str: string) {
196-
vscode.env.clipboard.writeText(str).then(
197-
() => this.sendMessage({ command: 'copyCommitHashToClipboard', data: true }),
198-
() => this.sendMessage({ command: 'copyCommitHashToClipboard', data: false })
193+
private copyCommitHashToClipboard(commitHash: string) {
194+
vscode.env.clipboard.writeText(commitHash).then(
195+
() => this.sendMessage({ command: 'copyCommitHashToClipboard', success: true }),
196+
() => this.sendMessage({ command: 'copyCommitHashToClipboard', success: false })
199197
);
200198
}
201199

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

src/tsconfig.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
{
22
"compilerOptions": {
3-
"module": "commonjs",
4-
"target": "es6",
5-
"outDir": "../out",
3+
"declaration": true,
64
"lib": [
75
"es6"
86
],
9-
"declaration": true,
7+
"module": "commonjs",
8+
"outDir": "../out",
9+
"noFallthroughCasesInSwitch": true,
10+
"noImplicitAny": true,
1011
"sourceMap": true,
11-
"strict": true
12+
"strict": true,
13+
"target": "es6"
1214
}
13-
}
15+
}

0 commit comments

Comments
 (0)