Skip to content

Commit 66ddf1c

Browse files
committed
Implement parsing selection
1 parent f489cfc commit 66ddf1c

File tree

4 files changed

+88
-7
lines changed

4 files changed

+88
-7
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Hover to view descriptions of existing attributes.
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
{
6-
"version": "0.2.0",
6+
"version": "0.2.0",
77
"configurations": [
88
{
99
"name": "Extension",

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,8 @@
3333
"tslint": "^5.8.0",
3434
"@types/node": "^7.0.43",
3535
"@types/mocha": "^2.2.42"
36-
}
36+
},
37+
"extensionDependencies": [
38+
"formulahendry.code-runner"
39+
]
3740
}

src/extension.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,21 @@
22
// The module 'vscode' contains the VS Code extensibility API
33
// Import the module and reference it with the alias vscode in your code below
44
import * as vscode from 'vscode';
5+
import * as runSnippet from './run_snippet';
56

67
// this method is called when your extension is activated
78
// your extension is activated the very first time the command is executed
89
export function activate(context: vscode.ExtensionContext) {
9-
1010
// Use the console to output diagnostic information (console.log) and errors (console.error)
1111
// This line of code will only be executed once when your extension is activated
12-
console.log('Congratulations, your extension "vscode-markdown-run-snippet" is now active!');
12+
// console.log('Congratulations, your extension "vscode-markdown-run-snippet" is now active!');
1313

1414
// The command has been defined in the package.json file
1515
// Now provide the implementation of the command with registerCommand
1616
// The commandId parameter must match the command field in package.json
1717
let disposable = vscode.commands.registerCommand('extension.runMarkdownSnippet', () => {
1818
// The code you place here will be executed every time your command is executed
19-
20-
// Display a message box to the user
21-
vscode.window.showInformationMessage('Hello World!');
19+
runSnippet.runMarkdownSnippet();
2220
});
2321

2422
context.subscriptions.push(disposable);

src/run_snippet.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'use strict';
2+
3+
import * as vscode from 'vscode';
4+
import { isString } from 'util';
5+
6+
export function runMarkdownSnippet() {
7+
let editor = vscode.window.activeTextEditor;
8+
let selection = editor !== undefined ? editor.selection : undefined;
9+
if (editor === undefined || selection === undefined) {
10+
vscode.window.showInformationMessage('No code selected.');
11+
return;
12+
}
13+
14+
let snippet = editor.document.getText(selection);
15+
let content = Content.parseSnippet(snippet);
16+
if (isString(content)) {
17+
let error = content;
18+
vscode.window.showInformationMessage(error);
19+
return;
20+
}
21+
console.log(content);
22+
23+
let uri = editor.document.uri;
24+
if (uri === undefined || uri.scheme !== 'file') {
25+
vscode.window.showInformationMessage('Opened file is not placed at the local filesystem.');
26+
return;
27+
}
28+
let config = vscode.workspace.getConfiguration("markdown-run-snippet");
29+
30+
// generate temporary file path
31+
let extension = content.filetype; // do something to get extension from filetype.
32+
let tmppath = gentmppath(uri, extension);
33+
// vscode.commands.executeCommand('code-runner.run', tmppath);
34+
}
35+
36+
class Content {
37+
public filetype: string;
38+
public content: string;
39+
40+
constructor(filetype: string, content: string) {
41+
this.filetype = filetype;
42+
this.content = content;
43+
}
44+
45+
static parseSnippet(orig_snippet: string): Content | string {
46+
// unimplemented!
47+
let snippet = orig_snippet.trim();
48+
if (!snippet.startsWith('```') || !snippet.endsWith('```')) {
49+
return "Selection is not started with ``` or is not ended with ```";
50+
}
51+
52+
snippet = snippet.replace(/^```/, '').replace(/```$/, '');
53+
54+
let splitted_snippet = snippet.split('\n');
55+
if (splitted_snippet.length == 0) {
56+
return 'No string inside ```s.';
57+
}
58+
59+
let filetype = splitted_snippet[0];
60+
if (filetype === "") {
61+
return "No filetype detected.";
62+
}
63+
let content = splitted_snippet.slice(1).join('\n');
64+
65+
return new Content(filetype, content);
66+
}
67+
}
68+
69+
function gentmppath(uri: vscode.Uri, ext: string): string {
70+
let fsPath = uri.fsPath;
71+
return (dirname(fsPath) + '/' + genrndname() + "." + ext);
72+
}
73+
74+
function dirname(path: string): string {
75+
return path.replace(/\\/g, '/').replace(/\/[^\/]*$/, '');
76+
}
77+
78+
function genrndname(): string {
79+
return 'junk_' + Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10);
80+
}

0 commit comments

Comments
 (0)