Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Add .editorconfig and format files
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed Oct 20, 2020
1 parent ea3c766 commit fa52202
Show file tree
Hide file tree
Showing 6 changed files with 442 additions and 380 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
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
8 changes: 4 additions & 4 deletions src/extension.ts
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);
}
199 changes: 104 additions & 95 deletions src/features/vsCommands.ts
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));
}
});
});
});
};
Loading

0 comments on commit fa52202

Please sign in to comment.