aardio格式化插件
By
iyca
at 20 天前 • 0人收藏 • 426人看过
以前写过一个基于beautify.js(1.2.0)的aardio格式化插件,但是由于beautify.js(1.2.0)版不支持中文,所以遇到中文变量名时,格式化后运行会出错,而且当时并没测试,只能当玩具玩玩。最近看到有些群友在用,并且还在更新,但是处理方式只是处理beautify.js返回的结果,并不能很好的解决这个问题,所以花了点时间把beautify.js更新到1.14.3版(这个版本可以支持中文,而且还支持格式化css,html),并加入自定义快捷键。
效果如图:
插件代码如下:
//格式化代码 import winex; import win.ui; import gdi; import console; import ide; var hwndStatusBar = winex.findEx(ide.getMainHwnd(), , "Afx\:RibbonStatusBar", ""); var hwnd = winex.findEx(hwndStatusBar, , , "格式化代码插件"); if (hwnd) { return; } win.modifyStyle(hwndStatusBar, , 0x2000000 /*_WS_CLIPCHILDREN*/ ); /*DSG{{*/ mainForm = win.form(text="格式化代码插件";right=36;bottom=20;bgcolor=16777215;border="none";exmode="none";max=false;min=false;mode="child";parent=hwndStatusBar) mainForm.add( btn_format={cls="button";text="{ }";left=0;top=0;right=22;bottom=22;z=1}; btn_other={cls="button";text='\u25B2';left=22;top=0;right=36;bottom=22;font=LOGFONT(h=-9;name='宋体');z=2} ) /*}}*/ var rcStatusBar = win.getRect(hwndStatusBar); var stausBarHeight = rcStatusBar.height(); mainForm.height = stausBarHeight - 2; mainForm.btn_format.height = stausBarHeight - 2; mainForm.btn_format.width = stausBarHeight - 2; import web.script.json; import web.script; var vm = web.script(); var jsFileName = "\beautifier.min.js"; // js-beautify v1.14.3 var js = string.load(jsFileName); vm.addCode(js); beautify = { aardio = function(text){ if(!#text){ return ""; } var opt = { indent_char = " "; indent_size = "4"; space_before_conditional = false;//Space before conditional: "if(x)" / "if (x)" brace_style = "end-expand";//"collapse","expand","end-expand","none" keep_array_indentation = true;//Keep array indentation? end_with_newline = true;//End script and style with newline? comma_first = false;//Use comma-first list style? break_chained_methods = false;//Break lines on chained methods? indent_scripts = "keep";//HTML <style>, <script> formatting:"keep","normal","separate" e4x = false;//Support e4x/jsx syntax unescape_strings = false;// Unescape printable chars encoded as \xNN or \uNNNN? indent_inner_html = false;//Indent <head> and <body> sections? jslint_happy = false;// Use JSLint-happy formatting tweaks? indent_empty_lines = true;//Keep indentation on empty lines? //Remove all extra newlines: preserve_newlines = false;max_preserve_newlines = "-1"; preserve_newlines = true; max_preserve_newlines = "2";//Allow unlimited newlines between tokens: max_preserve_newlines = "0" wrap_line_length = "0"; } var formatCode = vm.json["beautifier.aardio"](text,opt); if(!opt.space_before_conditional){ formatCode = string.replace(formatCode,"@@) {","){") } return formatCode; }; js = function(text) { if(!#text){ return ""; } var opt = { space_before_conditional = true; keep_array_indentation = true; preserve_newlines = true; end_with_newline = true; comma_first = false; indent_size = "4"; break_chained_methods = false; max_preserve_newlines = "2"; brace_style = "end-expand"; indent_scripts = "keep"; e4x = false; unescape_strings = false; indent_inner_html = false; wrap_line_length = "0"; jslint_happy = false; indent_empty_lines = true; indent_char = " "; } return vm.json["beautifier.js"](text,opt); }; css = function(text) { if(!#text){ return ""; } var opt = { selector_separator_newline = true; indent_size = "4"; preserve_newlines = false; newline_between_rules = false; space_around_combinator = false; brace_style = "collapse"; space_around_selector_separator = false; end_with_newline = false; indent_char = " " } return vm.json["beautifier.css"](text,opt); }; html = function(text) { if(!#text){ return ""; } var opt = { preserve_newlines = true; brace_style = "collapse"; keep_array_indentation = false; jslint_happy = false; indent_size = "4"; indent_with_tabs = false; indent_char = " "; extra_liners = {'html', 'head', '/html'}; } return vm.json["beautifier.html"](text,opt); } } mainForm.btn_format.oncommand = function(id, event,formatType="aardio") { var ed = ide.getActiveCodeEditor(); if (!ed) return; var text = ed.selText; if (#text) { ed.selText = beautify[formatType](text); } else { text = ed.text; ed.selectAll(); ed.selText = beautify[formatType](text); } } import key.hotkey; var hotkey = key.hotkey(mainForm); //创建超级热键,必须用于窗口程序中 import fsys.config; var config = fsys.config("/formatCode"); import win.ui.menu; mainForm.btn_format.wndproc = function(hwnd, message, wParam, lParam) { select (message) { case 0x205 /*_WM_RBUTTONUP*/ { var x, y = win.getMessagePos(lParam); var menu = win.ui.popmenu(mainForm); menu.addTable({ { "设置快捷键"; function(id) { var frm_setting = win.form(text = "格式化代码插件快捷键设置"; right = 308; bottom = 150; border = "dialog frame"; max = false; min = false) frm_setting.add( btn_ok = { cls = "button";text = "确定";left = 112;top = 113;right = 206;bottom = 141;z = 7 }; cbx_key = { cls = "combobox";left = 204;top = 43;right = 287;bottom = 63;edge = 1;items = {};mode = "dropdownlist";vscroll = 1;z = 5 }; chx_alt = { cls = "checkbox";text = "Alt";left = 112;top = 43;right = 163;bottom = 63;z = 3 }; chx_ctrl = { cls = "checkbox";text = "Ctrl";left = 27;top = 27;right = 78;bottom = 47;z = 1 }; chx_shift = { cls = "checkbox";text = "shift";left = 27;top = 61;right = 78;bottom = 81;z = 2 }; static = { cls = "static";text = "+";left = 89;top = 43;right = 105;bottom = 59;transparent = 1;z = 4 }; static2 = { cls = "static";text = "+";left = 177;top = 43;right = 193;bottom = 59;transparent = 1;z = 6 }; ) frm_setting.cbx_key.items = {"End";"Home";"Left";"Up";"Right";"Down";"0";"1";"2";"3";"4";"5";"6";"7";"8";"9";"A";"B";"C";"D";"E";"F";"G";"H";"I";"J";"K";"L";"M";"N";"O";"P";"Q";"R";"S";"T";"U";"V";"W";"X";"Y";"Z";"F1";"F2";"F3";"F4";"F5";"F6";"F7";"F8";"F9";"F10";"F11";"F12";"~";"-";"=";"[";"]";";";"'";"\";",";".";"/";"<";">";"Numpad0";"Numpad1";"Numpad2";"Numpad3";"Numpad4";"Numpad5";"Numpad6";"Numpad7";"Numpad8";"Numpad9";} var hotkeySetting = table.clone(config.setting.hotkey); var index = table.find(hotkeySetting, "CTRL"); if (index) { frm_setting.chx_ctrl.checked = 1; table.remove(hotkeySetting, index); } var index = table.find(hotkeySetting, "SHIFT"); if (index) { frm_setting.chx_shift.checked = 1; table.remove(hotkeySetting, index); } var index = table.find(hotkeySetting, "ALT"); if (index) { frm_setting.chx_alt.checked = 1; table.remove(hotkeySetting, index); } if (#hotkeySetting) { frm_setting.cbx_key.selText = hotkeySetting[1]; } else { frm_setting.cbx_key.selText = "F6"; } frm_setting.btn_ok.oncommand = function(id, event) { var result = {}; if (frm_setting.chx_ctrl.checked) { table.push(result, "CTRL"); } if (frm_setting.chx_shift.checked) { table.push(result, "SHIFT"); } if (frm_setting.chx_alt.checked) { table.push(result, "ALT"); } if (#frm_setting.cbx_key.selText) { table.push(result, frm_setting.cbx_key.selText); } config.setting.hotkey = result; config.saveAll(); hotkey.clear(); var hotkeySetting = table.clone(config.setting.hotkey); table.push(hotkeySetting,function(){ if (win.getFocus() == ide.getActiveCodeEditorHwnd()) { mainForm.btn_format.oncommand(); } }); hotkey.reg(table.pop(hotkeySetting, #hotkeySetting)); frm_setting.endModal(); } frm_setting.doModal(ide.getMainHwnd()); } };{ /*分隔线*/} { "退出程序"; function(id) { mainForm.close(); } } }) menu.popup(x, y); return true; } } } mainForm.onClose = function(hwnd, message, wParam, lParam) { mainForm.clearInterval(intervalId); hotkey.reset(); } var hotkeySetting = table.clone(config.setting.hotkey):{}; if (!#hotkeySetting) { table.push(hotkeySetting, "F6"); } table.push(hotkeySetting,function(){ if (win.getFocus() == ide.getActiveCodeEditorHwnd()) { mainForm.btn_format.oncommand(); } }); hotkey.reg(table.pop(hotkeySetting, #hotkeySetting)); var cxprev; var intervalId = mainForm.setInterval( 500, function(hwnd, msg, id, tick) { var x, y, cx, cy = win.getPos(hwndStatusBar); if (cx != cxprev) { var rcStatusBar = win.getRect(hwndStatusBar); var stausBarHeight = rcStatusBar.height(); mainForm.height = stausBarHeight - 2; mainForm.btn_format.height = stausBarHeight - 2; mainForm.btn_format.width = stausBarHeight - 2; mainForm.setPos(cx - mainForm.width - (205 * gdi.getDpiScale()), 2); cxprev = cx; mainForm.redraw(); } } ) var pop_formatMenu = win.ui.popmenu(mainForm) pop_formatMenu.addTable( { { "格式化 JS"; function(id) { mainForm.btn_format.oncommand(,,"js"); } }; { "格式化 CSS"; function(id) { mainForm.btn_format.oncommand(,,"css"); } }; { "格式化 HTML"; function(id) { mainForm.btn_format.oncommand(,,"html"); } }; } ) mainForm.btn_other.oncommand = function(id,event){ var x, y = win.getPos(mainForm.btn_other.hwnd); pop_formatMenu.popup(x,y,,0x20/*_TPM_BOTTOMALIGN*/); } mainForm.show(); win.loopMessage();
具体文件打包:
使用方法,解压后,运行formatCode.aardio,就会在状态栏右下角会出现插件按钮。
1 个回复 | 最后更新于 20 天前
登录后方可回帖
很不错哦(´-ω-`)