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 { Pulse } from "./pulse";
import { getISOTimestamp, getLanguageName } from "./utils"; import { getISOTimestamp, getLanguageName } from "./utils";
import * as axios from "axios"; import * as axios from "axios";
@ -11,7 +10,11 @@ export class CodeStatsAPI {
constructor(apiKey: string, apiURL: string) { constructor(apiKey: string, apiURL: string) {
this.API_KEY = apiKey; this.API_KEY = apiKey;
this.UPDATE_URL = apiURL; 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; return;
} }
@ -23,7 +26,6 @@ export class CodeStatsAPI {
"Content-Type": "application/json" "Content-Type": "application/json"
} }
}); });
} }
public sendUpdate(pulse: Pulse): axios.AxiosPromise { public sendUpdate(pulse: Pulse): axios.AxiosPromise {
@ -44,14 +46,15 @@ export class CodeStatsAPI {
let json: string = JSON.stringify(data); let json: string = JSON.stringify(data);
console.log(`JSON: ${json}`); console.log(`JSON: ${json}`);
return this.axios.post(this.UPDATE_URL, json) return this.axios
.then( (response) => { .post(this.UPDATE_URL, json)
.then(response => {
console.log(response); console.log(response);
}) })
.then(() => { .then(() => {
pulse.reset(); pulse.reset();
}) })
.catch((error) => { .catch(error => {
console.log(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 // this method is called when your extension is deactivated
export function deactivate(): void { export function deactivate(): void {}
}

View File

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

View File

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