This repository has been archived by the owner on Apr 14, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
442 additions
and
380 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
root = true | ||
|
||
[*] | ||
indent_size = 2 | ||
indent_style = space | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import * as vscode from 'vscode'; | ||
import * as vscode from "vscode"; | ||
|
||
import ValeServerProvider from './features/vsProvider'; | ||
import ValeServerProvider from "./features/vsProvider"; | ||
|
||
export function activate(context: vscode.ExtensionContext) { | ||
let linter = new ValeServerProvider(); | ||
linter.activate(context.subscriptions); | ||
let linter = new ValeServerProvider(); | ||
linter.activate(context.subscriptions); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,139 +1,148 @@ | ||
import * as vscode from 'vscode'; | ||
import * as vscode from "vscode"; | ||
|
||
import * as path from 'path'; | ||
import * as request from 'request-promise-native'; | ||
import * as path from "path"; | ||
import * as request from "request-promise-native"; | ||
|
||
/** | ||
* An Alert From Vale. | ||
*/ | ||
interface IValeConfigJSON { | ||
readonly Project: string; | ||
readonly StylesPath: string; | ||
readonly Project: string; | ||
readonly StylesPath: string; | ||
} | ||
|
||
export default function InitCommands(subscriptions: vscode.Disposable[]) { | ||
subscriptions.push( | ||
vscode.commands.registerCommand('vale.addToAccept', addToAccept), | ||
vscode.commands.registerCommand('vale.addToReject', addToReject), | ||
subscriptions.push( | ||
vscode.commands.registerCommand("vale.addToAccept", addToAccept), | ||
vscode.commands.registerCommand("vale.addToReject", addToReject), | ||
|
||
vscode.commands.registerCommand('vale.openAccept', openAccept), | ||
vscode.commands.registerCommand('vale.openReject', openReject) | ||
); | ||
vscode.commands.registerCommand("vale.openAccept", openAccept), | ||
vscode.commands.registerCommand("vale.openReject", openReject) | ||
); | ||
} | ||
|
||
const addToAccept = async () => { | ||
await addToVocab("accept"); | ||
await addToVocab("accept"); | ||
}; | ||
const addToReject = async () => { | ||
await addToVocab("reject"); | ||
await addToVocab("reject"); | ||
}; | ||
|
||
const openAccept = async () => { | ||
await openVocabFile("accept"); | ||
await openVocabFile("accept"); | ||
}; | ||
const openReject = async () => { | ||
await openVocabFile("reject"); | ||
await openVocabFile("reject"); | ||
}; | ||
|
||
/** | ||
* Get the user's active Vale Server configuration. | ||
*/ | ||
const getConfig = async (server: string): Promise<IValeConfigJSON> => { | ||
let config: IValeConfigJSON = {} as IValeConfigJSON; | ||
let config: IValeConfigJSON = {} as IValeConfigJSON; | ||
|
||
await request.get({ uri: server + '/config', json: true }) | ||
.catch((error) => { | ||
throw new Error(`Vale Server could not connect: ${error}.`); | ||
}) | ||
.then((body) => { | ||
config = body; | ||
}); | ||
await request | ||
.get({ uri: server + "/config", json: true }) | ||
.catch((error) => { | ||
throw new Error(`Vale Server could not connect: ${error}.`); | ||
}) | ||
.then((body) => { | ||
config = body; | ||
}); | ||
|
||
return config; | ||
return config; | ||
}; | ||
|
||
const openVocabFile = async (name: string) => { | ||
const configuration = vscode.workspace.getConfiguration(); | ||
const server: string = configuration.get( | ||
'vale.server.serverURL', | ||
'http://localhost:7777' | ||
); | ||
const config: IValeConfigJSON = await getConfig(server); | ||
|
||
const src = path.join( | ||
config.StylesPath, | ||
'Vocab', | ||
config.Project, | ||
name + '.txt'); | ||
vscode.workspace.openTextDocument(src).then( | ||
doc => vscode.window.showTextDocument(doc) | ||
); | ||
const configuration = vscode.workspace.getConfiguration(); | ||
const server: string = configuration.get( | ||
"vale.server.serverURL", | ||
"http://localhost:7777" | ||
); | ||
const config: IValeConfigJSON = await getConfig(server); | ||
|
||
const src = path.join( | ||
config.StylesPath, | ||
"Vocab", | ||
config.Project, | ||
name + ".txt" | ||
); | ||
vscode.workspace | ||
.openTextDocument(src) | ||
.then((doc) => vscode.window.showTextDocument(doc)); | ||
}; | ||
|
||
/** | ||
* Add the currently-selected word to the user's active Vocab. | ||
*/ | ||
const addToVocab = async (filename: string) => { | ||
const editor = vscode.window.activeTextEditor; | ||
if (!editor) { | ||
return; | ||
} | ||
|
||
const configuration = vscode.workspace.getConfiguration(); | ||
const server: string = configuration.get( | ||
'vale.server.serverURL', | ||
'http://localhost:7777' | ||
); | ||
const config: IValeConfigJSON = await getConfig(server); | ||
|
||
const word: string = editor.document.getText(editor.selection); | ||
const name: string = config.Project; | ||
const styles: string = config.StylesPath; | ||
|
||
await request.get({ | ||
uri: server + '/vocab', | ||
qs: { | ||
name: name, | ||
file: filename | ||
}, | ||
json: true | ||
const editor = vscode.window.activeTextEditor; | ||
if (!editor) { | ||
return; | ||
} | ||
|
||
const configuration = vscode.workspace.getConfiguration(); | ||
const server: string = configuration.get( | ||
"vale.server.serverURL", | ||
"http://localhost:7777" | ||
); | ||
const config: IValeConfigJSON = await getConfig(server); | ||
|
||
const word: string = editor.document.getText(editor.selection); | ||
const name: string = config.Project; | ||
const styles: string = config.StylesPath; | ||
|
||
await request | ||
.get({ | ||
uri: server + "/vocab", | ||
qs: { | ||
name: name, | ||
file: filename, | ||
}, | ||
json: true, | ||
}) | ||
.catch((error) => { | ||
throw new Error(`Vale Server could not connect: ${error}.`); | ||
throw new Error(`Vale Server could not connect: ${error}.`); | ||
}) | ||
.then((contents: Array<string>) => { | ||
contents.push(word); | ||
|
||
// TODO: Do we need to (shoud we?) sort ourselves? | ||
let body = [...new Set(contents)].sort((a, b) => { | ||
if (a.toLowerCase() > b.toLowerCase()) { | ||
return 1; | ||
} else if (a.toLowerCase() < b.toLowerCase()) { | ||
return -1; | ||
} | ||
return 0; | ||
}); | ||
|
||
request.post({ | ||
uri: server + '/update', | ||
qs: { | ||
path: name + '.' + filename, | ||
text: body.join('\n') | ||
}, | ||
json: true | ||
}).catch((error) => { | ||
throw new Error(`Vale Server could not connect: ${error}.`); | ||
}).then(() => { | ||
const src = path.join(styles, 'Vocab', name, filename + '.txt'); | ||
vscode.window.showInformationMessage( | ||
`Successfully added '${word}' to '${name}' vocab.`, | ||
...['View File']).then(selection => { | ||
if (selection === 'View File') { | ||
vscode.workspace.openTextDocument(src).then( | ||
doc => vscode.window.showTextDocument(doc) | ||
); | ||
} | ||
}); | ||
contents.push(word); | ||
|
||
// TODO: Do we need to (shoud we?) sort ourselves? | ||
let body = [...new Set(contents)].sort((a, b) => { | ||
if (a.toLowerCase() > b.toLowerCase()) { | ||
return 1; | ||
} else if (a.toLowerCase() < b.toLowerCase()) { | ||
return -1; | ||
} | ||
return 0; | ||
}); | ||
|
||
request | ||
.post({ | ||
uri: server + "/update", | ||
qs: { | ||
path: name + "." + filename, | ||
text: body.join("\n"), | ||
}, | ||
json: true, | ||
}) | ||
.catch((error) => { | ||
throw new Error(`Vale Server could not connect: ${error}.`); | ||
}) | ||
.then(() => { | ||
const src = path.join(styles, "Vocab", name, filename + ".txt"); | ||
vscode.window | ||
.showInformationMessage( | ||
`Successfully added '${word}' to '${name}' vocab.`, | ||
...["View File"] | ||
) | ||
.then((selection) => { | ||
if (selection === "View File") { | ||
vscode.workspace | ||
.openTextDocument(src) | ||
.then((doc) => vscode.window.showTextDocument(doc)); | ||
} | ||
}); | ||
}); | ||
}); | ||
}; |
Oops, something went wrong.