Made it Prettier(tm)

This commit is contained in:
Juha Ristolainen 2017-11-08 21:03:53 +01:00
parent 2392b782b7
commit 01cd77e519
5 changed files with 276 additions and 242 deletions

View File

@ -1,4 +1,3 @@
import { Pulse } from "./pulse";
import { getISOTimestamp, getLanguageName } from "./utils";
import * as axios from "axios";
@ -11,7 +10,11 @@ export class CodeStatsAPI {
constructor(apiKey: string, apiURL: string) {
this.API_KEY = apiKey;
this.UPDATE_URL = apiURL;
if (this.API_KEY === null || this.API_KEY === undefined || this.API_KEY === '') {
if (
this.API_KEY === null ||
this.API_KEY === undefined ||
this.API_KEY === ""
) {
return;
}
@ -23,7 +26,6 @@ export class CodeStatsAPI {
"Content-Type": "application/json"
}
});
}
public sendUpdate(pulse: Pulse): axios.AxiosPromise {
@ -44,14 +46,15 @@ export class CodeStatsAPI {
let json: string = JSON.stringify(data);
console.log(`JSON: ${json}`);
return this.axios.post(this.UPDATE_URL, json)
.then( (response) => {
return this.axios
.post(this.UPDATE_URL, json)
.then(response => {
console.log(response);
})
.then ( () => {
.then(() => {
pulse.reset();
})
.catch((error) => {
.catch(error => {
console.log(error);
});
}

View File

@ -8,5 +8,4 @@ export function activate(context: ExtensionContext): void {
}
// this method is called when your extension is deactivated
export function deactivate(): void {
}
export function deactivate(): void {}

View File

@ -1,8 +1,8 @@
export class Pulse {
xps: Map < string, number > ;
xps: Map<string, number>;
constructor() {
this.xps = new Map < string, number > ();
this.xps = new Map<string, number>();
}
public getXP(language: string): number {
@ -23,11 +23,11 @@ export class Pulse {
this.xps.set(language, xp);
}
public get getXPs(): Map < string, number > {
public get getXPs(): Map<string, number> {
return this.xps;
}
public reset(): void {
this.xps = new Map < string, number > ();
this.xps = new Map<string, number>();
}
}

View File

@ -1,81 +1,89 @@
// converted to ts from https://github.com/Nicd/code-stats-atom/blob/master/lib/utils.js
export function getISOTimestamp(date: Date): string {
const offset: number = -date.getTimezoneOffset();
const prefix: string = (offset >= 0) ? "+" : "-";
const prefix: string = offset >= 0 ? "+" : "-";
function pad(num: number): string {
const norm: number = Math.abs(Math.floor(num));
return ((norm < 10) ? "0" : "") + norm;
return (norm < 10 ? "0" : "") + norm;
}
return date.getFullYear() +
"-" + pad(date.getMonth() + 1) +
"-" + pad(date.getDate()) +
"T" + pad(date.getHours()) +
":" + pad(date.getMinutes()) +
":" + pad(date.getSeconds()) +
prefix + pad(offset / 60) +
pad(offset % 60);
return (
date.getFullYear() +
"-" +
pad(date.getMonth() + 1) +
"-" +
pad(date.getDate()) +
"T" +
pad(date.getHours()) +
":" +
pad(date.getMinutes()) +
":" +
pad(date.getSeconds()) +
prefix +
pad(offset / 60) +
pad(offset % 60)
);
}
export function getLanguageName(langId: string): string {
// currently supported language ids in vscode as of 2017-03-18
let languageNames: { [key:string]: string; } = {
"plaintext": "Plain text",
"Log": "Log",
"bat": "Batch",
"clojure": "Clojure",
"coffeescript": "CoffeeScript",
"c": "C",
"cpp": "C++",
"csharp": "C#",
"css": "CSS",
"diff": "Diff",
"dockerfile": "Docker",
"elixir": "Elixir",
"elm": "Elm",
"fsharpcss": "F#",
let languageNames: { [key: string]: string } = {
plaintext: "Plain text",
Log: "Log",
bat: "Batch",
clojure: "Clojure",
coffeescript: "CoffeeScript",
c: "C",
cpp: "C++",
csharp: "C#",
css: "CSS",
diff: "Diff",
dockerfile: "Docker",
elixir: "Elixir",
elm: "Elm",
fsharpcss: "F#",
"git-commit": "Git",
"git-rebase": "Git",
"go": "Go",
"groovy": "Groovy",
"handlebars": "Handlebars",
"hlsl": "HLSL",
"html": "HTML",
"ini": "Ini",
"properties": "Properties",
"java": "Java",
"javascriptreact": "JavaScript (React)",
"javascript": "JavaScript",
go: "Go",
groovy: "Groovy",
handlebars: "Handlebars",
hlsl: "HLSL",
html: "HTML",
ini: "Ini",
properties: "Properties",
java: "Java",
javascriptreact: "JavaScript (React)",
javascript: "JavaScript",
"jsx-tags": "JavaScript (JSX)",
"json": "JSON",
"less": "LESS",
"lua": "Lua",
"makefile": "Makefile",
"markdown": "Markdown",
json: "JSON",
less: "LESS",
lua: "Lua",
makefile: "Makefile",
markdown: "Markdown",
"objective-c": "Objective-C",
"perl": "Perl",
"perl6": "Perl 6",
"php": "PHP",
"powershell": "PowerShell",
"jade": "Pug",
"python": "Python",
"r": "R",
"razor": "Razor",
"ruby": "Ruby",
"rust": "Rust",
"scss": "SCSS",
"shaderlab": "Shaderlab",
"shellscript": "Shell Script",
"sql": "SQL",
"swift": "Swift",
"typescript": "TypeScript",
"typescriptreact": "TypeScript (React)",
"vb": "Visual Basic",
"xml": "XML",
"xsl": "XSL",
"yaml": "YAML"
perl: "Perl",
perl6: "Perl 6",
php: "PHP",
powershell: "PowerShell",
jade: "Pug",
python: "Python",
r: "R",
razor: "Razor",
ruby: "Ruby",
rust: "Rust",
scss: "SCSS",
shaderlab: "Shaderlab",
shellscript: "Shell Script",
sql: "SQL",
swift: "Swift",
typescript: "TypeScript",
typescriptreact: "TypeScript (React)",
vb: "Visual Basic",
xml: "XML",
xsl: "XSL",
yaml: "YAML"
};
let languageName: string = languageNames[langId];

View File

@ -1,5 +1,15 @@
// tslint:disable-next-line:max-line-length
import { Disposable, workspace, window, StatusBarItem, TextDocument, StatusBarAlignment, TextDocumentChangeEvent, Range, WorkspaceConfiguration } from "vscode";
import {
Disposable,
workspace,
window,
StatusBarItem,
TextDocument,
StatusBarAlignment,
TextDocumentChangeEvent,
Range,
WorkspaceConfiguration
} from "vscode";
import { Pulse } from "./pulse";
import { CodeStatsAPI } from "./code-stats-api";
@ -33,7 +43,11 @@ export class XpCounter {
}
let subscriptions: Disposable[] = [];
workspace.onDidChangeTextDocument(this.onTextDocumentChanged, this, subscriptions);
workspace.onDidChangeTextDocument(
this.onTextDocumentChanged,
this,
subscriptions
);
workspace.onDidChangeConfiguration(this.initAPI, this, subscriptions);
this.combinedDisposable = Disposable.from(...subscriptions);
}
@ -67,7 +81,10 @@ export class XpCounter {
if (promise !== null) {
promise.then(() => {
this.updateStatusBar(show, `${this.pulse.getXP(document.languageId)}`);
this.updateStatusBar(
show,
`${this.pulse.getXP(document.languageId)}`
);
});
}
}, this.UPDATE_DELAY);
@ -89,14 +106,21 @@ export class XpCounter {
}
private initAPI() {
let config: WorkspaceConfiguration = workspace.getConfiguration("codestats");
let config: WorkspaceConfiguration = workspace.getConfiguration(
"codestats"
);
if (!config) {
return;
}
const apiKey: string = config.get("apikey");
const apiURL: string = config.get("apiurl");
console.log("code-stats-vscode setting up with API URL", apiURL, "and key", apiKey);
console.log(
"code-stats-vscode setting up with API URL",
apiURL,
"and key",
apiKey
);
this.api = new CodeStatsAPI(apiKey, apiURL);
}
}