Solution (workaround)
an Ahk implementation (as workaround -- I didnt find a way to do it in Eclipse)::
to use, just
- make sure your right most comment is placed in a good position (other comments will align to this right most one)
- run ahk
- select your code to reformat
- press the hotkey
^+g (ctrl + shift + g)
- the reformatted code will be pasted
/*
@logic,to_use::
1. make sure your right most comment is placed in a good position <strike> 1. use eclipse to reformat the comments `//`
1. scan through all lines to see which `//` is the right most
- ignore the lines with not code on the right
1. locate all the `//` & add enough space to align with the right most `//`
*/
alignJavaComment(content_CodeToAlign) {
; >> 1. scan through all lines to see which `//` is the right most
;~ stdout := FileOpen("*", "w")
arr_ind_CommentDelimiter := []
; https://www.autohotkey.com/docs/commands/LoopParse.htm
Loop, parse, content_CodeToAlign, `n, `r
{
line_curr := A_LoopField
; ;V1; ind_CommentDelimiter := RegExMatch(line_curr, "//") ; starts from 1 not 0 ; Position 1 is the first character. ; 0 means not found
; ;V1; arr_ind_CommentDelimiter.Push(ind_CommentDelimiter)
ind_CommentDelimiter := RegExMatch(line_curr, "//")
; determine if the comment is behind code, or the comment is the whole line
; ;M*; https://stackoverflow.com/questions/406230/regular-expression-to-match-a-line-that-doesnt-contain-a-word
; ;M*; can do sth like `^(?!\s*//)(.*)(//)`, but nah ..
; if the comment is the whole line -> dont align it -- push index 0
; @explain-minor: if the comment is the whole line -- the preceding part P1 is all white space -- RegExMatch cant find "^\s*$" & return 0
line_curr_P1 := SubStr(line_curr, 1, ind_CommentDelimiter - 1)
ind_det_ItsAllWhiteSpace := RegExMatch(line_curr_P1, "^\s*$")
;~ stdout.WriteLine("line_curr_P1: " . line_curr_P1)
;~ stdout.WriteLine("ind_det_ItsAllWhiteSpace: " . ind_det_ItsAllWhiteSpace)
;~ stdout.WriteLine("ind_CommentDelimiter: " . ind_CommentDelimiter)
if (ind_det_ItsAllWhiteSpace != 0) {
arr_ind_CommentDelimiter.Push(0)
}
; if the comment is behind code -> align it -- push corresponding index
; if there is no comment in this code -> dont align it -- push index 0
else {
arr_ind_CommentDelimiter.Push(ind_CommentDelimiter)
}
}
; https://www.autohotkey.com/docs/commands/Math.htm#Max
ind_RightMostCommentDelimiter := Max(arr_ind_CommentDelimiter*)
;~ stdout.WriteLine(ind_RightMostCommentDelimiter)
; >> 1. locate all the `//` & add enough space to align with the right most `//`
content_Aligned := ""
Loop, parse, content_CodeToAlign, `n, `r
{
line_curr := A_LoopField
ind_line_curr := A_Index
;~ stdout.WriteLine(ind_line_curr) ; starts from 1 not 0
ind_CommentDelimiter_curr := arr_ind_CommentDelimiter[ind_line_curr] ; starts from 1 not 0 ; Ahk array start from 1 not 0 (.... how i have no strong impression..)
;~ stdout.WriteLine(ind_CommentDelimiter_curr)
if (ind_CommentDelimiter_curr == 0) {
content_Aligned .= line_curr . "`n"
} else {
ind_DiffNumOfSpaces := ind_RightMostCommentDelimiter - ind_CommentDelimiter_curr
; https://www.autohotkey.com/board/topic/102416-insert-character-into-a-string-at-a-specific-position/
line_curr_P1 := SubStr(line_curr, 1, ind_CommentDelimiter_curr - 1)
line_curr_P2 := SubStr(line_curr, ind_CommentDelimiter_curr)
;~ stdout.WriteLine(ind_line_curr . ": " . line_curr_P1 . line_curr_P2 . " -- " . ind_DiffNumOfSpaces)
str_Spaces := "" ; need to reset in each Loop ...
Loop, %ind_DiffNumOfSpaces%
{
str_Spaces .= " "
}
content_Aligned .= line_curr_P1 . str_Spaces . line_curr_P2 . "`n"
}
}
return content_Aligned
}
; ;testing; content_Test := ""
; ;testing; . " public static void main(String[] args) {" . "`n"
; ;testing; . " Channel channel; // = ...;" . "`n"
; ;testing; . " // Does not block" . "`n"
; ;testing; . " ChannelFuture future = channel.connect(new InetSocketAddress(""192.168.0.1"", 25)); // statement A" . "`n"
; ;testing; . " future.addListener(" . "`n"
; ;testing; . " new ChannelFutureListener()" . "`n"
; ;testing; . " {" . "`n"
; ;testing; . " @Override" . "`n"
; ;testing; . " public void operationComplete(ChannelFuture future) { // statement A" . "`n"
; ;testing; . " if (future.isSuccess()) {" . "`n"
; ;testing; . " ByteBuf buffer = Unpooled.copiedBuffer(""Hello"", Charset.defaultCharset());" . "`n"
; ;testing; . " ChannelFuture wf = future.channel() // statement A" . "`n"
; ;testing; . " .writeAndFlush(buffer);" . "`n"
; ;testing; . " // ...." . "`n"
; ;testing; . " } else { // statement A" . "`n"
; ;testing; . " Throwable cause = future.cause();" . "`n"
; ;testing; . " cause.printStackTrace(); // statement A" . "`n"
; ;testing; . " }" . "`n"
; ;testing; . " }" . "`n"
; ;testing; . " });" . "`n"
; ;testing; . " }" . "`n"
; ;testing;
; ;testing; content_Aligned := alignJavaComment(content_Test)
; ;testing; stdout := FileOpen("*", "w")
; ;testing; stdout.WriteLine(content_Aligned)
^+g::
Clipboard := ""
SendInput, ^c
ClipWait, 0.3
content_Aligned := alignJavaComment(Clipboard)
Clipboard := content_Aligned
SendInput, ^v
return