-
Notifications
You must be signed in to change notification settings - Fork 10
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
11 changed files
with
128 additions
and
91 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
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,47 @@ | ||
import { TextDocumentContentProvider, Uri, workspace, EventEmitter } from 'vscode' | ||
import prettier from 'prettier/standalone' | ||
import parserBabel from 'prettier/parser-babylon' | ||
import { parseQuery } from '../utils' | ||
import { DOC_SCHEMA } from '../meta' | ||
import { ExtensionModule } from '../module' | ||
import { Exec } from '../exec' | ||
|
||
async function getCompiledResult (filepath: string) { | ||
const result = await Exec(filepath) || '' | ||
|
||
return prettier.format(result, { semi: false, parser: 'babel', plugins: [parserBabel] }) | ||
} | ||
|
||
async function getExecResult (filepath: string) { | ||
const result = await Exec(filepath, { exec: true }) || '' | ||
|
||
// remove first line or compiled code | ||
return result.split('\n').slice(1).join('\n') | ||
} | ||
|
||
class DocumentProvider implements TextDocumentContentProvider { | ||
onDidChangeEmitter = new EventEmitter<Uri>() | ||
onDidChange = this.onDidChangeEmitter.event | ||
|
||
async provideTextDocumentContent (uri: Uri) { | ||
const query = parseQuery(uri.query) | ||
const filepath = query.filepath | ||
const action = query.action | ||
|
||
if (!filepath || !action) | ||
return '' | ||
|
||
if (action === 'execute') | ||
return await getExecResult(filepath) | ||
if (action === 'compile') | ||
return await getCompiledResult(filepath) | ||
} | ||
} | ||
|
||
export const documentProvider = new DocumentProvider() | ||
|
||
const m: ExtensionModule = () => { | ||
return workspace.registerTextDocumentContentProvider(DOC_SCHEMA, documentProvider) | ||
} | ||
|
||
export default m |
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
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
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
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 @@ | ||
export function parseQuery (queryString: string) { | ||
const query: Record<string, string> = {} | ||
const pairs = (queryString[0] === '?' ? queryString.substr(1) : queryString).split('&') | ||
for (let i = 0; i < pairs.length; i++) { | ||
const pair = pairs[i].split('=') | ||
query[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '') | ||
} | ||
return query | ||
} |