其他分享
首页 > 其他分享> > aardio 代码格式化 自动保存 自定义 ctrl + s bug:这个快捷键是全局拦截

aardio 代码格式化 自动保存 自定义 ctrl + s bug:这个快捷键是全局拦截

作者:互联网

aardio 代码格式化工具
https://gitee.com/pengchenggang/aardio---code-formatting-tool

修改内容

1 代码进行了一定的修改,默认ctrl+t 改成了 ctrl+s
2 原先是通过选择进行格式化,全部保存的时候,会自动全选再保存。逻辑都删除了,改成直接全部格式化保存。这样就不闪了,代码如下。

bug:这个快捷键是全局拦截

//aardio代码格式化工具
//风行者 制作 v21.0
//感谢 游戏 提供的beautifier.min.js 和 script2 库源码 

import ide;
import gdi;
import winex;
import win.ui;
import win.inputBox;
import web.script.json
import web.script2;
import fonts.fontAwesome;

var hwndStatusBar = winex.findEx(ide.getMainHwnd(), , "Afx\:RibbonStatusBar", "");
var hwnd = winex.findEx(hwndStatusBar, , , "aardio代码格式化");
if (hwnd) return;

win.modifyStyle(hwndStatusBar, , 0x2000000 /*_WS_CLIPCHILDREN*/ );
/*DSG{{*/
winform = win.form(text="aardio代码格式化";right=19;bottom=20;border="none";exmode="none";max=false;min=false;mode="child";parent=hwndStatusBar)
winform.add(
plus={cls="plus";left=0;top=0;right=20;bottom=23;bgcolor=-28642;color=16777215;font=LOGFONT(h=-16);iconStyle={font=LOGFONT(h=-20;name='FontAwesome')};iconText='\uF121';notify=1;z=1}
)
/*}}*/

winform.plus.skin({
    background = {
        active = 0xFF0081FF;
        default = 0xFF1E90FF;
        hover = 0xFF339AFF
    };
    color = {
        default = 0xFFFFFFFF
    }
})

var ws = web.script2();

ws.addCode("window=this;");
jsCode = $ "\beautifier.min.js"

ws.addCode(jsCode);

var opt = {
    indent_char = " ";
    indent_size = "4";
    space_before_conditional = true; //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";
}

import fsys.table
var fTab = fsys.table("/key.table")

winform.plus.oncommand = function(id, event) {
    
    // 编辑控件
    var codeEditor = ide.getActiveCodeEditor()
    if (!codeEditor) return;
    
    // 返回编辑代码
    var code = ide.getActiveCode()
    if (!code) return;
    
    var text = codeEditor.selText
    
    var formatCode
    
    var sSel, eSel = codeEditor.getsel()
    if (#text) {
        formatCode = ws.json.beautifier.aardio(text, opt);
    }
    else {
        formatCode = ws.json.beautifier.aardio(code, opt);
        //codeEditor.selectAll()
    }
    /*
  	//space_before_conditional = false; 时使用
    if (!opt.space_before_conditional) {
        formatCode = string.replace(formatCode, "@@) {", "){")
    }
    
*/
    codeEditor.text = formatCode;
    //设置选区文本
    //codeEditor.selText = formatCode;
    
    codeEditor.setFocus(eSel: sSel);
    ide.save()
    /*
var path = ide.getActiveDocPath()
    if (#path and io.exist(path)) {
        //备份
        ide.save()
        var sp = io.splitpath(path)
        fTab.load()
        
        if (fTab.path) {
            var fpath
            if string.endWith(fTab.path, "\") {
                fpath = fTab.path
            }
            else {
                fpath = fTab.path + "\"
            }
            
            string.save(fpath + sp.name + "_old" + sp.ext, code)
        }
        else {
            string.save(sp.dir + sp.name + "_old" + sp.ext, code)
        }
        
    }
*/
    
}

var intervalId = winform.setInterval(
    100,
    function(hwnd, msg, id, tick) {
        
        var chwnd = winex.findEx(hwndStatusBar, , "@msctls_updown32")
        
        if (chwnd) {
            var x, y = win.getPos(chwnd)
            
            if (x != winform.xtmp) {
                winform.setPos(x - winform.width - (85 * gdi.getDpiScale()), 2)
                winform.xtmp = x;
                winform.redraw();
            }
        }
    })

winform.onClose = function(hwnd, message, wParam, lParam) {
    winform.clearInterval(intervalId);
}

import win.ui.menu;
winform.plus.wndproc = function(hwnd, message, wParam, lParam) {
    
    select (message) {
        case 0x205 /*_WM_RBUTTONUP*/ {
            //鼠标右键弹起,下面获取坐标
            var x, y = win.getMessagePos(lParam);
            var menu = win.ui.popmenu(winform);
            menu.add("退出", function() {
                winform.close()
            })
            
            menu.popup(x, y);
            return true;
        }
    }
}

import win.ui.tooltip;

// 在所有者窗口 winform 上创建 tooltip 提示控件 
var tooltipCtrl = win.ui.tooltip(winform);

// 添加一个自动模式的控件提示(TOOLINFO 对象,简称 tool )
var toolInfoPlus = tooltipCtrl.addTool(winform.plus, "格式化代码")

/*快捷键设置{{*/

if (io.exist("/key.table")) {
    fTab.load()
    //定义快捷键Ctrl+T
    hkid = winform.reghotkey(function(id, mod, vk) {
        winform.plus.oncommand();
    }, fTab.k, fTab.v);
}
else {
    
    inputbox = win.inputBox(winform.hwnd)
    
    inputbox.input.close()
    inputbox.add({
        hotkey = {
            cls = "hotkey";
            left = 20;
            top = 46;
            right = 423;
            bottom = 71;
            dl = 1;
            dt = 1;
            edge = 1;
            password = password;
            z = 3
        }
    })
    inputbox.text = "格式化代码快捷键设置"
    inputbox.info.text = "请在下面输入快捷键:"
    inputbox.hotkey.sethotkey(2, 84)
    
    inputbox.btnOk.oncommand = function(id, event) {
        if (inputbox.hotkey.gethotkey()) {
            var k1, v1 = inputbox.hotkey.gethotkey()
            
            // 定义快捷键
            hkid = winform.reghotkey(function(id, mod, vk) {
                winform.plus.oncommand();
            }, k1, v1);
            
            //保存值
            fTab.mixin(
                k = k1; v = v1;);
        }
        else {
            //使用默认快捷键Ctrl+T
            hkid = winform.reghotkey(function(id, mod, vk) {
                winform.plus.oncommand();
            }, 0x2 /*_MOD_CONTROL*/ , 84);
            
            //保存值
            fTab.mixin(
                k = 2; v = 84;);
        }
        
        inputbox.endModal();
        
    };
    
    inputbox.doModal()
    
}

/*}}*/

winform.show()
win.loopMessage();

标签:自定义,ctrl,win,快捷键,inputbox,var,import,fTab,winform
来源: https://www.cnblogs.com/pengchenggang/p/16223499.html