|
1 |
| -import * as vscode from 'vscode'; |
| 1 | +import * as vscode from "vscode"; |
2 | 2 |
|
3 |
| -import * as path from 'path'; |
4 |
| -import * as request from 'request-promise-native'; |
| 3 | +import * as path from "path"; |
| 4 | +import * as request from "request-promise-native"; |
5 | 5 |
|
6 | 6 | /**
|
7 | 7 | * An Alert From Vale.
|
8 | 8 | */
|
9 | 9 | interface IValeConfigJSON {
|
10 |
| - readonly Project: string; |
11 |
| - readonly StylesPath: string; |
| 10 | + readonly Project: string; |
| 11 | + readonly StylesPath: string; |
12 | 12 | }
|
13 | 13 |
|
14 | 14 | export default function InitCommands(subscriptions: vscode.Disposable[]) {
|
15 |
| - subscriptions.push( |
16 |
| - vscode.commands.registerCommand('vale.addToAccept', addToAccept), |
17 |
| - vscode.commands.registerCommand('vale.addToReject', addToReject), |
| 15 | + subscriptions.push( |
| 16 | + vscode.commands.registerCommand("vale.addToAccept", addToAccept), |
| 17 | + vscode.commands.registerCommand("vale.addToReject", addToReject), |
18 | 18 |
|
19 |
| - vscode.commands.registerCommand('vale.openAccept', openAccept), |
20 |
| - vscode.commands.registerCommand('vale.openReject', openReject) |
21 |
| - ); |
| 19 | + vscode.commands.registerCommand("vale.openAccept", openAccept), |
| 20 | + vscode.commands.registerCommand("vale.openReject", openReject) |
| 21 | + ); |
22 | 22 | }
|
23 | 23 |
|
24 | 24 | const addToAccept = async () => {
|
25 |
| - await addToVocab("accept"); |
| 25 | + await addToVocab("accept"); |
26 | 26 | };
|
27 | 27 | const addToReject = async () => {
|
28 |
| - await addToVocab("reject"); |
| 28 | + await addToVocab("reject"); |
29 | 29 | };
|
30 | 30 |
|
31 | 31 | const openAccept = async () => {
|
32 |
| - await openVocabFile("accept"); |
| 32 | + await openVocabFile("accept"); |
33 | 33 | };
|
34 | 34 | const openReject = async () => {
|
35 |
| - await openVocabFile("reject"); |
| 35 | + await openVocabFile("reject"); |
36 | 36 | };
|
37 | 37 |
|
38 | 38 | /**
|
39 | 39 | * Get the user's active Vale Server configuration.
|
40 | 40 | */
|
41 | 41 | const getConfig = async (server: string): Promise<IValeConfigJSON> => {
|
42 |
| - let config: IValeConfigJSON = {} as IValeConfigJSON; |
| 42 | + let config: IValeConfigJSON = {} as IValeConfigJSON; |
43 | 43 |
|
44 |
| - await request.get({ uri: server + '/config', json: true }) |
45 |
| - .catch((error) => { |
46 |
| - throw new Error(`Vale Server could not connect: ${error}.`); |
47 |
| - }) |
48 |
| - .then((body) => { |
49 |
| - config = body; |
50 |
| - }); |
| 44 | + await request |
| 45 | + .get({ uri: server + "/config", json: true }) |
| 46 | + .catch((error) => { |
| 47 | + throw new Error(`Vale Server could not connect: ${error}.`); |
| 48 | + }) |
| 49 | + .then((body) => { |
| 50 | + config = body; |
| 51 | + }); |
51 | 52 |
|
52 |
| - return config; |
| 53 | + return config; |
53 | 54 | };
|
54 | 55 |
|
55 | 56 | const openVocabFile = async (name: string) => {
|
56 |
| - const configuration = vscode.workspace.getConfiguration(); |
57 |
| - const server: string = configuration.get( |
58 |
| - 'vale.server.serverURL', |
59 |
| - 'http://localhost:7777' |
60 |
| - ); |
61 |
| - const config: IValeConfigJSON = await getConfig(server); |
62 |
| - |
63 |
| - const src = path.join( |
64 |
| - config.StylesPath, |
65 |
| - 'Vocab', |
66 |
| - config.Project, |
67 |
| - name + '.txt'); |
68 |
| - vscode.workspace.openTextDocument(src).then( |
69 |
| - doc => vscode.window.showTextDocument(doc) |
70 |
| - ); |
| 57 | + const configuration = vscode.workspace.getConfiguration(); |
| 58 | + const server: string = configuration.get( |
| 59 | + "vale.server.serverURL", |
| 60 | + "http://localhost:7777" |
| 61 | + ); |
| 62 | + const config: IValeConfigJSON = await getConfig(server); |
| 63 | + |
| 64 | + const src = path.join( |
| 65 | + config.StylesPath, |
| 66 | + "Vocab", |
| 67 | + config.Project, |
| 68 | + name + ".txt" |
| 69 | + ); |
| 70 | + vscode.workspace |
| 71 | + .openTextDocument(src) |
| 72 | + .then((doc) => vscode.window.showTextDocument(doc)); |
71 | 73 | };
|
72 | 74 |
|
73 | 75 | /**
|
74 | 76 | * Add the currently-selected word to the user's active Vocab.
|
75 | 77 | */
|
76 | 78 | const addToVocab = async (filename: string) => {
|
77 |
| - const editor = vscode.window.activeTextEditor; |
78 |
| - if (!editor) { |
79 |
| - return; |
80 |
| - } |
81 |
| - |
82 |
| - const configuration = vscode.workspace.getConfiguration(); |
83 |
| - const server: string = configuration.get( |
84 |
| - 'vale.server.serverURL', |
85 |
| - 'http://localhost:7777' |
86 |
| - ); |
87 |
| - const config: IValeConfigJSON = await getConfig(server); |
88 |
| - |
89 |
| - const word: string = editor.document.getText(editor.selection); |
90 |
| - const name: string = config.Project; |
91 |
| - const styles: string = config.StylesPath; |
92 |
| - |
93 |
| - await request.get({ |
94 |
| - uri: server + '/vocab', |
95 |
| - qs: { |
96 |
| - name: name, |
97 |
| - file: filename |
98 |
| - }, |
99 |
| - json: true |
| 79 | + const editor = vscode.window.activeTextEditor; |
| 80 | + if (!editor) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + const configuration = vscode.workspace.getConfiguration(); |
| 85 | + const server: string = configuration.get( |
| 86 | + "vale.server.serverURL", |
| 87 | + "http://localhost:7777" |
| 88 | + ); |
| 89 | + const config: IValeConfigJSON = await getConfig(server); |
| 90 | + |
| 91 | + const word: string = editor.document.getText(editor.selection); |
| 92 | + const name: string = config.Project; |
| 93 | + const styles: string = config.StylesPath; |
| 94 | + |
| 95 | + await request |
| 96 | + .get({ |
| 97 | + uri: server + "/vocab", |
| 98 | + qs: { |
| 99 | + name: name, |
| 100 | + file: filename, |
| 101 | + }, |
| 102 | + json: true, |
100 | 103 | })
|
101 | 104 | .catch((error) => {
|
102 |
| - throw new Error(`Vale Server could not connect: ${error}.`); |
| 105 | + throw new Error(`Vale Server could not connect: ${error}.`); |
103 | 106 | })
|
104 | 107 | .then((contents: Array<string>) => {
|
105 |
| - contents.push(word); |
106 |
| - |
107 |
| - // TODO: Do we need to (shoud we?) sort ourselves? |
108 |
| - let body = [...new Set(contents)].sort((a, b) => { |
109 |
| - if (a.toLowerCase() > b.toLowerCase()) { |
110 |
| - return 1; |
111 |
| - } else if (a.toLowerCase() < b.toLowerCase()) { |
112 |
| - return -1; |
113 |
| - } |
114 |
| - return 0; |
115 |
| - }); |
116 |
| - |
117 |
| - request.post({ |
118 |
| - uri: server + '/update', |
119 |
| - qs: { |
120 |
| - path: name + '.' + filename, |
121 |
| - text: body.join('\n') |
122 |
| - }, |
123 |
| - json: true |
124 |
| - }).catch((error) => { |
125 |
| - throw new Error(`Vale Server could not connect: ${error}.`); |
126 |
| - }).then(() => { |
127 |
| - const src = path.join(styles, 'Vocab', name, filename + '.txt'); |
128 |
| - vscode.window.showInformationMessage( |
129 |
| - `Successfully added '${word}' to '${name}' vocab.`, |
130 |
| - ...['View File']).then(selection => { |
131 |
| - if (selection === 'View File') { |
132 |
| - vscode.workspace.openTextDocument(src).then( |
133 |
| - doc => vscode.window.showTextDocument(doc) |
134 |
| - ); |
135 |
| - } |
136 |
| - }); |
| 108 | + contents.push(word); |
| 109 | + |
| 110 | + // TODO: Do we need to (shoud we?) sort ourselves? |
| 111 | + let body = [...new Set(contents)].sort((a, b) => { |
| 112 | + if (a.toLowerCase() > b.toLowerCase()) { |
| 113 | + return 1; |
| 114 | + } else if (a.toLowerCase() < b.toLowerCase()) { |
| 115 | + return -1; |
| 116 | + } |
| 117 | + return 0; |
| 118 | + }); |
| 119 | + |
| 120 | + request |
| 121 | + .post({ |
| 122 | + uri: server + "/update", |
| 123 | + qs: { |
| 124 | + path: name + "." + filename, |
| 125 | + text: body.join("\n"), |
| 126 | + }, |
| 127 | + json: true, |
| 128 | + }) |
| 129 | + .catch((error) => { |
| 130 | + throw new Error(`Vale Server could not connect: ${error}.`); |
| 131 | + }) |
| 132 | + .then(() => { |
| 133 | + const src = path.join(styles, "Vocab", name, filename + ".txt"); |
| 134 | + vscode.window |
| 135 | + .showInformationMessage( |
| 136 | + `Successfully added '${word}' to '${name}' vocab.`, |
| 137 | + ...["View File"] |
| 138 | + ) |
| 139 | + .then((selection) => { |
| 140 | + if (selection === "View File") { |
| 141 | + vscode.workspace |
| 142 | + .openTextDocument(src) |
| 143 | + .then((doc) => vscode.window.showTextDocument(doc)); |
| 144 | + } |
| 145 | + }); |
137 | 146 | });
|
138 | 147 | });
|
139 | 148 | };
|
0 commit comments