Skip to content

Commit d54ed21

Browse files
author
WebFreak001
committed
Added upload selection to dpaste.com
1 parent 57416ac commit d54ed21

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@
7272
"items": {
7373
"type": "string"
7474
},
75-
"default": ["/usr/include/dmd/druntime/import", "/usr/include/dmd/phobos"],
75+
"default": [
76+
"/usr/include/dmd/druntime/import",
77+
"/usr/include/dmd/phobos"
78+
],
7679
"description": "Array of paths to phobos and D runtime for automatic inclusion for auto completion"
7780
},
7881
"d.dcdClientPath": {
@@ -144,6 +147,10 @@
144147
{
145148
"command": "code-d.stop",
146149
"title": "code-d: Stop project"
150+
},
151+
{
152+
"command": "code-d.uploadSelection",
153+
"title": "code-d: Upload selection to dpaste.com"
147154
}
148155
],
149156
"jsonValidation": [
@@ -157,6 +164,9 @@
157164
"vscode:prepublish": "node ./node_modules/vscode/bin/compile",
158165
"compile": "node ./node_modules/vscode/bin/compile -watch -p ./"
159166
},
167+
"dependencies": {
168+
"request": "^2.67.0"
169+
},
160170
"devDependencies": {
161171
"typescript": "^1.6.2",
162172
"vscode": "0.10.x"

src/extension.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import * as vscode from 'vscode';
22
import { D_MODE } from "./dmode"
33
import { WorkspaceD } from "./workspace-d"
44
import { CompileButtons } from "./compile-buttons"
5+
import { uploadCode } from "./util"
56
import * as statusbar from "./statusbar"
7+
import * as path from "path"
68

79
let diagnosticCollection: vscode.DiagnosticCollection;
810

@@ -90,6 +92,22 @@ export function activate(context: vscode.ExtensionContext) {
9092
}
9193
}));
9294

95+
context.subscriptions.push(vscode.commands.registerCommand("code-d.uploadSelection", () => {
96+
if(vscode.window.activeTextEditor.selection.isEmpty)
97+
vscode.window.showErrorMessage("No code selected");
98+
else {
99+
let code = vscode.window.activeTextEditor.document.getText(vscode.window.activeTextEditor.selection);
100+
let name = path.basename(vscode.window.activeTextEditor.document.fileName);
101+
let syntax = vscode.window.activeTextEditor.document.languageId;
102+
uploadCode(name, syntax, code).then((url) => {
103+
vscode.window.showInformationMessage("Code pasted on " + url);
104+
});
105+
}
106+
}, (err) => {
107+
console.error(err);
108+
vscode.window.showErrorMessage("Failed to switch configuration. See console for details.");
109+
}));
110+
93111
context.subscriptions.push(vscode.commands.registerCommand("code-d.switchConfiguration", () => {
94112
vscode.window.showQuickPick(workspaced.listConfigurations()).then((config) => {
95113
if (config)

src/util.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as request from "request";
2+
3+
export function uploadCode(title: string, syntax: string, code: string): Thenable<string> {
4+
return new Promise((resolve, reject) => {
5+
request.post('http://dpaste.com/api/v2/', { form: { content: code, syntax: syntax, title: title, expiry_days: 7 } }, (err, httpResponse, body) => {
6+
if (err)
7+
return reject(err);
8+
resolve(body);
9+
});
10+
});
11+
}

0 commit comments

Comments
 (0)