python、shell、cmd根据本机环境动态代码提示

This commit is contained in:
fofolee 2020-06-24 12:40:52 +08:00
parent 0842f667ec
commit 1d7e26ba28
2 changed files with 70 additions and 41 deletions

View File

@ -495,24 +495,33 @@ let hasCustomIcon = () => {
let programCheck = () => { let programCheck = () => {
let mode = $('#program').val(); let mode = $('#program').val();
$('.customscript').hide();
$('.simulation').hide();
$('#showInTerm').prop("disabled", false);
if (!hasCustomIcon()) $("#icon").attr('src', `logo/${mode}.png`); if (!hasCustomIcon()) $("#icon").attr('src', `logo/${mode}.png`);
switch (mode) { switch (mode) {
case 'custom': case 'custom':
$('.customscript').show(); $('.customscript').show();
$('.simulation').hide();
$('#showInTerm').prop("disabled", false);
break; break;
case 'simulation': case 'simulation':
$('.simulation').show(); $('.simulation').show();
$('.customscript').hide();
$('#showInTerm').prop("disabled", true); $('#showInTerm').prop("disabled", true);
mode = 'javascript'; mode = 'javascript';
break; break;
case 'csharp':
case 'c':
mode = 'text/x-' + mode
break;
case 'python':
getPythonMods()
break;
case 'cmd':
getCmdCommand()
break;
case 'shell':
getShellCommand()
break;
default: default:
$('.customscript').hide();
$('.simulation').hide();
$('#showInTerm').prop("disabled", false);
(mode == 'csharp' || mode == 'c') && (mode = 'text/x-' + mode)
break; break;
} }
window.editor.setOption("mode", mode); window.editor.setOption("mode", mode);

View File

@ -267,46 +267,66 @@ runCodeInVm = (cmd, cb) => {
window.addEventListener('unhandledrejection', cbUnhandledRejection); window.addEventListener('unhandledrejection', cbUnhandledRejection);
} }
// shell 以环境变量下命令作为代码提示 // shell代码提示,当前环境变量下的所有命令
getShellCommand = () => { getShellCommand = () => {
var bin = localStorage['shellcommand'] var shellCommands = localStorage['shellCommands']
if (bin) { if (shellCommands) return
bin = JSON.parse(bin) localStorage['shellCommands'] = '[]'
} else { if(utools.isWindows()) return
bin = []
if (!utools.isWindows()) {
process.env.PATH.split(':').forEach(d => { process.env.PATH.split(':').forEach(d => {
try { fs.readdir(d, (err, files) => {
bin = bin.concat(fs.readdirSync(d).filter(x => x[0] != ".")) if (!err) {
} catch (e) { } var commands = files.filter(x => x[0] != "." || x[0] != '[')
localStorage['shellCommands'] = JSON.stringify(JSON.parse(localStorage['shellCommands']).concat(commands))
}
})
}) })
localStorage['shellcommand'] = JSON.stringify(bin)
}
}
return bin
} }
// cmd 以环境变量下命令作为代码提示 // cmd代码提示当前环境变量下的所有命令
getCmdCommand = () => { getCmdCommand = () => {
var bin = localStorage['cmdcommand'] var cmdCommands = localStorage['cmdCommands']
if (bin) { if (cmdCommands) return
bin = JSON.parse(bin) localStorage['cmdCommands'] = '[]'
} else { if(!utools.isWindows()) return
bin = []
if (utools.isWindows()) {
process.env.Path.split(';').forEach(d => { process.env.Path.split(';').forEach(d => {
try { fs.readdir(d, (err, files) => {
bin = bin.concat(fs.readdirSync(d).filter(x => x.length > 4 && x.slice(-4) == '.exe')) if (!err) {
} catch (e) { } var commands = []
files.forEach(x => (x.length > 4 && x.slice(-4) == '.exe') && commands.push(x.slice(0, -4)))
localStorage['cmdCommands'] = JSON.stringify(JSON.parse(localStorage['cmdCommands']).concat(commands))
}
})
}) })
bin = bin.concat(bin).join("|").replace(/\.exe/g, '').split("|")
localStorage['cmdcommand'] = JSON.stringify(bin)
}
}
return bin
} }
// NodeJs 代码提示 // python 代码提示,已安装的模块以及脚本内导入的模块的属性(方法)
getPythonMods = () => {
var pyModules = localStorage['pyModules']
if (pyModules) return
localStorage['pyModules'] = '[]'
child_process.exec(`python -c "print(__import__('sys').path)"`, (err, stdout, stderr) => {
if (err) return
stdout = JSON.parse(stdout.replace(/'/g, `"`)).forEach(s => {
fs.readdir(s, (err, m) => {
if (!err) {
var mods = []
m.forEach(d => (/\.py$|^[^-.]+$/.test(d)) && (d = d.split('.py')[0]) && (!mods.includes(d)) && mods.push(d))
localStorage['pyModules'] = JSON.stringify(JSON.parse(localStorage['pyModules']).concat(mods))
}
})
})
})
}
dirPythonMod = (mod, cb) => {
child_process.exec(`python -c "print(dir(__import__('${mod}')))"`, (err, stdout, stderr) => {
if (err) return cb([])
cb(JSON.parse(stdout.replace(/'/g, `"`)).filter(x => x.slice(0, 2) != '__'))
})
}
// NodeJs 代码提示,所有在沙箱内支持的对象
getNodeJsCommand = () => { getNodeJsCommand = () => {
var obj = getSandboxFuns() var obj = getSandboxFuns()
obj.Buffer = Buffer obj.Buffer = Buffer