-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
provider.ts
77 lines (66 loc) · 1.75 KB
/
provider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"use strict";
import * as vscode from "vscode";
import { TextDocumentContentProvider, Uri } from "vscode";
import snippet from "./snippet";
import { getConfig } from "./config";
export default class SnippetProvider implements TextDocumentContentProvider {
/**
*
* @param {vscode.Uri} uri - a fake uri
* @returns {string} - Code Snippet
**/
public async provideTextDocumentContent(uri?: Uri): Promise<string> {
const request = decodeRequest(uri);
const response = await snippet.load(
request.language,
request.query,
request.answerNumber
);
if (response.data.trimLeft().startsWith("/LANG/QUESTION")) {
return `404 NOT FOUND
Unknown cheat sheet. Please try to reformulate your query.
Query format:
/LANG/QUESTION
Examples:
/python/read+json
/golang/run+external+program
/js/regex+search
See /:help for more info.
If the problem persists, file a GitHub issue at
github.com/chubin/cheat.sh or ping @igor_chubin
`;
}
return response.data;
}
}
export function encodeRequest(
query: string,
language: string,
verbose: boolean,
answerNumber: number
): vscode.Uri {
const data = JSON.stringify({
query: query,
language: language,
answerNumber,
verbose,
});
const titleTemplate = getConfig("title");
const title = titleTemplate.replace(
/(\$\{(.*?)\})/g,
(match: string, _, variableName: string) => {
return variableName === "language"
? language
: variableName === "query"
? query
: variableName === "index"
? answerNumber
: match;
}
);
return vscode.Uri.parse(`snippet:${title}?${data}`);
}
export function decodeRequest(uri: vscode.Uri): any {
const obj = JSON.parse(uri.query);
return obj;
}