-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.js
139 lines (119 loc) · 4.04 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var vscode = require('vscode')
var path = require('path')
var ncp = require('copy-paste')
var EOL = require('os').EOL
var lastCommand = null
function activate(context) {
var config = vscode.workspace.getConfiguration('sendToTerminal')
var disposable = vscode.commands.registerCommand('sendToTerminal.run', function () {
var editor = vscode.window.activeTextEditor
if (!editor) {
return
}
runCommand(commands, editor, config)
})
context.subscriptions.push(disposable)
var disposable = vscode.commands.registerCommand('sendToTerminal.runFocus', function () {
var editor = vscode.window.activeTextEditor
if (!editor) {
return
}
runCommand(focusCommands, editor, config)
})
context.subscriptions.push(disposable)
var disposable = vscode.commands.registerCommand('sendToTerminal.runLast', function () {
var editor = vscode.window.activeTextEditor
if (!editor) {
return
}
runLastCommand(editor)
})
context.subscriptions.push(disposable)
}
exports.activate = activate
function deactivate() {}
exports.deactivate = deactivate
function buildCommand(command, editor) {
var document = editor.document
var cmdStr = command.cmd
var extName = path.extname(document.fileName)
var root = vscode.workspace.rootPath
var relativeFile = "." + document.fileName.replace(root, "")
var position = editor.selection.active
var line = position.line + 1
var column = position.character + 1
cmdStr = cmdStr.replace(/\${line}/g, line)
cmdStr = cmdStr.replace(/\${column}/g, column)
cmdStr = cmdStr.replace(/\${relativeFile}/g, relativeFile)
cmdStr = cmdStr.replace(/\${file}/g, `${document.fileName}`)
cmdStr = cmdStr.replace(/\${workspaceRoot}/g, `${vscode.workspace.rootPath}`)
cmdStr = cmdStr.replace(/\${fileBasename}/g, `${path.basename(document.fileName)}`)
cmdStr = cmdStr.replace(/\${fileDirname}/g, `${path.dirname(document.fileName)}`)
cmdStr = cmdStr.replace(/\${fileExtname}/g, `${extName}`)
cmdStr = cmdStr.replace(/\${fileBasenameNoExt}/g, `${path.basename(document.fileName, extName)}`)
cmdStr = cmdStr.replace(/\${cwd}/g, `${process.cwd()}`)
// replace environment variables ${env.Name}
cmdStr = cmdStr.replace(/\${env\.([^}]+)}/g, (sub, envName) => {
return process.env[envName]
})
return cmdStr
}
function clearBeforeRun(config) {
return config.get('clearBeforeRun')
}
function commands(config) {
return config.get('commands') || []
}
function focusCommands(config) {
return config.get('focusCommands') || []
}
function matchPattern(pattern, fileName) {
return pattern && pattern.length > 0 && new RegExp(pattern).test(fileName)
}
function runCommand(fn, editor, config) {
var _commands = fn(config)
if (_commands.length === 0) {
return
}
var column = editor.viewColumn
var document = editor.document
var fileName = document.fileName
var command = _commands.find((item) => {
var pattern = item.match || ''
var isMatch = pattern.length === 0 || matchPattern(pattern, fileName)
return isMatch
})
if (command == null) {
return
}
var _command = buildCommand(command, editor)
command = clearBeforeRun(config) ? ` clear; ${_command}` : ` ${_command}`
lastCommand = command
ncp.paste((err, clipboard) => {
ncp.copy(command + EOL, () => {
vscode.commands.executeCommand('workbench.action.terminal.focus').then(() => {
vscode.commands.executeCommand('workbench.action.terminal.paste').then(() => {
vscode.window.showTextDocument(document, column)
ncp.copy(clipboard)
})
})
})
})
}
function runLastCommand(editor) {
if (lastCommand == null) {
return
}
var column = editor.viewColumn
var document = editor.document
ncp.paste((err, clipboard) => {
ncp.copy(lastCommand + EOL, () => {
vscode.commands.executeCommand('workbench.action.terminal.focus').then(() => {
vscode.commands.executeCommand('workbench.action.terminal.paste').then(() => {
vscode.window.showTextDocument(document, column)
ncp.copy(clipboard)
})
})
})
})
}