基于autohotkey的源代码阅读助手

工作需要,制作了一个小工具来帮助阅读源代码。

  1. 按win+v会弹出一个inputbox,按确定后将剪切板的文本添加到工具中。
  2. inputbox中输入代码段的序号:
    • 输入数字(eg. 1,1-1,2-1)指定当前代码段的层次编号。
    • 输入+数字(eg. +1, +1-1, +2-1)代表将当前剪切板的内容添加到该层次中。
    • 输入-数字(eg. -1, -1-1, -2-1)代表删除该层次的代码段。
  3. 按win+b弹出一个editview,内容为格式化后的代码片段,以缩进代表层次,按层次编号排序从上到下显示,效果如下图所示:


[#1 C:\Users\xinyu.he\Documents\SourceTracer.ahk ]
|
| refresh_list() {
| global src_objects
| global src_indexs
| ...
| Loop, parse, src_indexs, n
| {
| ...
| if (val != "") {
| title := src_objects[val]["title"]
| src_content := left_aline(src_objects[val]["content"])
| intented_content := "[#" . val . " " . title . " ]" . "
n n" . src_content . "n n"
| intented_content := insert_intent(val, intented_content)
| content := content . intented_content
| }
| ...
| }
|
| GuiControl,, MyEdit, %content%
| }
|
| [#1-1 C:\Users\xinyu.he\Documents\SourceTracer.ahk ]
| |
| | left_aline(src) {
| | content := ""
| | pos := 0
| | min := 10000
| | ...
| | Loop, parse, src,
n
| | {
| | if (Trim(A_LoopField) != "")
| | {
| | line := A_LoopField
| | StringTrimLeft, line, line, min
| | content := content . line . "`n"
| | }
| | }
| | ...
| | return content
| | }
| |

源代码如下所示:

#SingleInstance Force
#Persistent

src_objects := []
src_indexs = 
Gui, Add, Edit, r25 w350 vMyEdit -Wrap HScroll VScroll

return

#v::
    Send ^c
    cur_src := Clipboard
    last_src := src_objects.MaxIndex()
    msg = [last]`n%last_src%
    InputBox, content, src tree, %msg%,,,140
    if(content && !ErrorLevel && cur_src) {
        StringGetPos, plus_pos, content, +
        StringGetPos, minus_pos, content, -
        if(minus_pos = 0) {
            StringTrimLeft, content, content, % minus_pos + 1
            if(src_objects[content] = "") {
                return
            }
            src_objects[content] := ""
            remove_src_indexs(content)
        }
        else if(plus_pos = 0) {
            StringTrimLeft, content, content, % plus_pos + 1
            if(src_objects[content] = "") {
                return
            }
            last_content := src_objects[content]["content"]
            src_objects[content]["content"] := last_content . "`n" . insert_node_header(cur_src)
        }else {
            if(src_objects[content] = "") {
                src_indexs := src_indexs . content . "`n" 
            }
            WinGetTitle, title, A
            title := parse_src_title(title)
            src_objects[content] := {"title":title, "content":cur_src}
        }
        refresh_list()
    }
    return

#b::
    Gui +LastFound +AlwaysOnTop +ToolWindow
    WinSet, Transparent, 200, 
    Gui, Show
    return

remove_src_indexs(target) {
    global src_indexs

    content := ""
    Loop, parse, src_indexs, `n
    {
        if (A_LoopField != target) {
            content := content . A_LoopField . "`n"
        }
    }
    src_indexs := content
}

insert_node_header(cur_src) {
    space := ""
    pos := RegExMatch(cur_src, "[^\s]")
    if (pos != 0){
        Loop, %pos% {
            space := space . " "
        }
        space := space . "...`n"
    }
    return space . cur_src
}

parse_src_title(title) {
    StringGetPos, pos, title, -
    pos := StrLen(title) - pos + 1
    if pos >= 0
        StringTrimRight, title, title, pos
    return title
}

refresh_list() {
    global src_objects
    global src_indexs
    Sort, src_indexs
    content := ""
    Loop, parse, src_indexs, `n
    {
        val := A_LoopField
        if (val != "") {
            title := src_objects[val]["title"]
            src_content := left_aline(src_objects[val]["content"])
            intented_content := "[#" . val . "  " . title . " ]" . "`n  `n" . src_content . "`n  `n"
            intented_content := insert_intent(val, intented_content)
            content := content . intented_content
        }
    }

    GuiControl,, MyEdit, %content%
}

left_aline(src) {
    content := ""
    pos := 0
    min := 10000
    Loop, parse, src, `n
    {
        if (A_LoopField != "")
        {
            pos := RegExMatch(A_LoopField, "[^\s]")
            if (pos != 0 && pos < min){
                min := pos
            }
        }
    }
    min -= 1
    content := ""
    Loop, parse, src, `n
    {
        if (Trim(A_LoopField) != "")
        {
            line := A_LoopField
            StringTrimLeft, line, line, min
            content := content . line . "`n"
        }
    }
    return content
}

insert_intent(head, src) {
    content := ""
    StringReplace, head, head, -, -, UseErrorLevel
    intent := ""
    Loop % ErrorLevel
    {
        intent := intent . "|  "
    }
    Loop, parse, src, `n
    {
        if (A_LoopField != "")
            if (A_Index == 1){
                content := content . intent . A_LoopField . "`n"
            }
            else {
                content := content . intent . "|  " . A_LoopField . "`n"
            }
    }
    return content
}

发表评论

您的电子邮箱地址不会被公开。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据