AutoHotkey 语言 利用 AutoHotkey 实现自动化排版

AutoHotkey阿木 发布于 4 天前 5 次阅读


AutoHotkey Language: Automated Formatting with Code Editing Models

Introduction

AutoHotkey (AHK) is a scripting language designed for automating the Windows GUI and general scripting. It is widely used for creating macros, automating repetitive tasks, and enhancing productivity. One of the common challenges in programming is maintaining code readability and consistency, especially as projects grow in size and complexity. Automated formatting tools can significantly aid in this process by ensuring that code adheres to a consistent style and structure. In this article, we will explore how to implement automated formatting in AutoHotkey using code editing models.

Understanding Automated Formatting

Automated formatting is the process of automatically adjusting the layout and style of code to adhere to a specific coding standard. This includes tasks such as:

- Indentation and spacing
- Line breaks and alignment
- Naming conventions
- Comment formatting

Automated formatting not only improves the readability of code but also makes it easier to maintain and collaborate on.

Code Editing Models in AutoHotkey

Code editing models are frameworks that provide a structured approach to code editing. They can be used to implement features such as syntax highlighting, code completion, and automated formatting. In AutoHotkey, we can leverage these models to create a custom formatting tool.

1. Syntax Highlighting

Syntax highlighting is the process of visually distinguishing different elements of code based on their type. This can be achieved using a code editing model that supports syntax highlighting.

ahk
Persistent
SingleInstance, Force

Gui, Add, Edit, vCode, ; Your code here
Gui, Show, , AutoHotkey Code Editor

OnMessage(0x0102, "WM_LBUTTONDOWN")
return

WM_LBUTTONDOWN:
MouseGetPos, MX, MY
Code := SubStr(Code, 1, MY)
Line := RegExMatch(Code, "m)(?<=r?)(.)", LineMatch) ? LineMatch.Value1 : ""
if (Line ~= "^s$") {
Line := SubStr(Code, 1, InStr(Code, Line) - 1)
}
HighlightSyntax(Line)
return

HighlightSyntax(Line) {
Local Color := A_GuiColor
Local ForeColor := A_ForeColor
Local BackColor := A_BackColor
Local Indent := 0
Local IndentChar := " "
Local IndentWidth := StrLen(IndentChar)
Local IndentRegex := "(" . Repeated(IndentChar, 0, 100) . ")"

Loop, Parse, Line, `n, `r
{
Local CurrentLine := A_LoopField
Local CurrentIndent := RegExMatch(CurrentLine, IndentRegex, IndentMatch) ? StrLen(IndentMatch.Value1) : 0
Local CurrentIndentWidth := CurrentIndent IndentWidth
Local CurrentLineLength := StrLen(CurrentLine)
Local CurrentLineWithoutIndent := SubStr(CurrentLine, CurrentIndentWidth + 1)

; Highlight comments
if (CurrentLineWithoutIndent ~= "^;") {
SetColor(BackColor, BackColor)
SetText("s" . CurrentLine)
}
; Highlight keywords
else if (CurrentLineWithoutIndent ~= "^(if|else|while|for|switch|return|break|continue)$") {
SetColor(BackColor, "0x00FF00")
SetText("s" . SubStr(CurrentLine, 1, CurrentIndentWidth) . SubStr(CurrentLine, CurrentIndentWidth + 1))
}
; Highlight variable names
else if (CurrentLineWithoutIndent ~= "^w+$") {
SetColor(BackColor, "0xFF0000")
SetText("s" . SubStr(CurrentLine, 1, CurrentIndentWidth) . SubStr(CurrentLine, CurrentIndentWidth + 1))
}
; Reset color
SetColor(Color, ForeColor)
}
return
}

Repeated(String, Count, Start := 1) {
Local Result := ""
Loop, % Count
Result .= String
return Result
}

2. Code Completion

Code completion is a feature that suggests possible completions for a partially typed code snippet. This can be implemented using a code editing model that supports code completion.

ahk
Persistent
SingleInstance, Force

Gui, Add, Edit, vCode, ; Your code here
Gui, Show, , AutoHotkey Code Editor

OnMessage(0x0102, "WM_LBUTTONDOWN")
return

WM_LBUTTONDOWN:
MouseGetPos, MX, MY
Code := SubStr(Code, 1, MY)
Line := RegExMatch(Code, "m)(?<=r?)(.)", LineMatch) ? LineMatch.Value1 : ""
if (Line ~= "^s$") {
Line := SubStr(Code, 1, InStr(Code, Line) - 1)
}
CompleteCode(Line)
return

CompleteCode(Line) {
Local Keywords := "if;else;while;for;switch;return;break;continue"
Local SuggestedCompletions := ""
Local CurrentWord := ""
Local CurrentWordStart := 0

Loop, Parse, Line, `n, `r
{
Local CurrentLine := A_LoopField
Local CurrentLineWithoutIndent := SubStr(CurrentLine, StrLen(IndentChar) (StrLen(IndentChar) - 1))
Local CurrentWordStart := InStr(CurrentLineWithoutIndent, " ")
if (CurrentWordStart) {
CurrentWord := SubStr(CurrentLineWithoutIndent, CurrentWordStart + 1)
}
else {
CurrentWord := CurrentLineWithoutIndent
}

if (InStr(Keywords, CurrentWord)) {
SuggestedCompletions .= CurrentWord . " "
}
}

if (SuggestedCompletions) {
MsgBox, % SuggestedCompletions
}
return
}

3. Automated Formatting

Automated formatting can be implemented using a code editing model that supports formatting rules. The following example demonstrates how to format code by indenting lines and aligning comments.

ahk
Persistent
SingleInstance, Force

Gui, Add, Edit, vCode, ; Your code here
Gui, Show, , AutoHotkey Code Editor

OnMessage(0x0102, "WM_LBUTTONDOWN")
return

WM_LBUTTONDOWN:
MouseGetPos, MX, MY
Code := SubStr(Code, 1, MY)
Line := RegExMatch(Code, "m)(?<=r?)(.)", LineMatch) ? LineMatch.Value1 : ""
if (Line ~= "^s$") {
Line := SubStr(Code, 1, InStr(Code, Line) - 1)
}
FormatCode(Line)
return

FormatCode(Line) {
Local IndentChar := " "
Local IndentWidth := StrLen(IndentChar)
Local IndentRegex := "(" . Repeated(IndentChar, 0, 100) . ")"

; Remove existing indentation
Line := RegExReplace(Line, IndentRegex, "")

; Add indentation to the beginning of each line
Local FormattedLine := ""
Loop, Parse, Line, `n, `r
{
Local CurrentLine := A_LoopField
Local CurrentIndent := StrLen(RegExReplace(CurrentLine, IndentRegex, ""))
Local Indentation := ""
Loop, % CurrentIndent / IndentWidth
Indentation .= IndentChar
FormattedLine .= Indentation . CurrentLine . "`n"
}

; Align comments
Local CommentRegex := ";."
Local CommentAlignment := ""
Loop, Parse, FormattedLine, `n, `r
{
Local CurrentLine := A_LoopField
Local CommentMatch := RegExMatch(CurrentLine, CommentRegex, CommentMatch)
if (CommentMatch) {
Local CommentPosition := StrLen(RegExReplace(CurrentLine, CommentRegex, ""))
Loop, % CommentPosition
CommentAlignment .= " "
}
}
FormattedLine := RegExReplace(FormattedLine, CommentRegex, CommentAlignment . "$0")

; Replace the original code with the formatted code
Code := RegExReplace(Code, "m)(?<=r?)(.)", FormattedLine)
GuiControl,, Code, % Code
return
}

Repeated(String, Count, Start := 1) {
Local Result := ""
Loop, % Count
Result .= String
return Result
}

Conclusion

In this article, we have explored the concept of automated formatting in AutoHotkey using code editing models. By implementing syntax highlighting, code completion, and automated formatting, we can enhance the readability and maintainability of AutoHotkey scripts. These features not only make the development process more efficient but also help in creating consistent and professional code.