Skip to content

Commit

Permalink
Implement "Toggle Block Comment", issue #718.
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed Sep 25, 2023
1 parent baf1cc1 commit 3e79284
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 42 deletions.
2 changes: 1 addition & 1 deletion locale/de/Notepad2.rc
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ BEGIN
MENUITEM "&Duplicate\tAlt+D", IDM_EDIT_SELECTIONDUPLICATE
MENUITEM SEPARATOR
MENUITEM "T&oggle Line Comment\tCtrl+/", IDM_EDIT_LINECOMMENT
MENUITEM "Block &Comment\tCtrl+Q", IDM_EDIT_STREAMCOMMENT
MENUITEM "Toggle Block &Comment\tCtrl+Q", IDM_EDIT_STREAMCOMMENT
MENUITEM "&Indent\tTab", IDM_EDIT_INDENT
MENUITEM "&Unindent\tShift+Tab", IDM_EDIT_UNINDENT
MENUITEM SEPARATOR
Expand Down
2 changes: 1 addition & 1 deletion locale/fr-FR/Notepad2.rc
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ BEGIN
MENUITEM "&Dupliquer\tAlt+D", IDM_EDIT_SELECTIONDUPLICATE
MENUITEM SEPARATOR
MENUITEM "Déplier les commentaires\tCtrl+/", IDM_EDIT_LINECOMMENT
MENUITEM "Commenter un bloc\tCtrl+Q", IDM_EDIT_STREAMCOMMENT
MENUITEM "Toggle Block &Comment\tCtrl+Q", IDM_EDIT_STREAMCOMMENT
MENUITEM "&Indenter\tTab", IDM_EDIT_INDENT
MENUITEM "&Désindenter\tShift+Tab", IDM_EDIT_UNINDENT
MENUITEM SEPARATOR
Expand Down
2 changes: 1 addition & 1 deletion locale/it/Notepad2.rc
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ BEGIN
MENUITEM "&Duplica\tAlt+D", IDM_EDIT_SELECTIONDUPLICATE
MENUITEM SEPARATOR
MENUITEM "Rimu&ovi linea commento\tCtrl+/", IDM_EDIT_LINECOMMENT
MENUITEM "&Commenta blocco\tCtrl+Q", IDM_EDIT_STREAMCOMMENT
MENUITEM "Toggle Block &Comment\tCtrl+Q", IDM_EDIT_STREAMCOMMENT
MENUITEM "&Indenta\tTab", IDM_EDIT_INDENT
MENUITEM "Rim&uovi indentazione\tShift+Tab", IDM_EDIT_UNINDENT
MENUITEM SEPARATOR
Expand Down
2 changes: 1 addition & 1 deletion locale/ja/Notepad2.rc
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ BEGIN
MENUITEM "選択範囲を複製(&D)\tAlt+D", IDM_EDIT_SELECTIONDUPLICATE
MENUITEM SEPARATOR
MENUITEM "行コメント(切替)(&O)\tCtrl+/", IDM_EDIT_LINECOMMENT
MENUITEM "ブロックコメント(&C)\tCtrl+Q", IDM_EDIT_STREAMCOMMENT
MENUITEM "Toggle Block &Comment\tCtrl+Q", IDM_EDIT_STREAMCOMMENT
MENUITEM "インデント(&I)\tTab", IDM_EDIT_INDENT
MENUITEM "逆インデント(&U)\tShift+Tab", IDM_EDIT_UNINDENT
MENUITEM SEPARATOR
Expand Down
2 changes: 1 addition & 1 deletion locale/ko/Notepad2.rc
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ BEGIN
MENUITEM "복제(&D)\tAlt+D", IDM_EDIT_SELECTIONDUPLICATE
MENUITEM SEPARATOR
MENUITEM "줄 주석 전환(&O)\tCtrl+/", IDM_EDIT_LINECOMMENT
MENUITEM "블럭 주석(&C)\tCtrl+Q", IDM_EDIT_STREAMCOMMENT
MENUITEM "Toggle Block &Comment\tCtrl+Q", IDM_EDIT_STREAMCOMMENT
MENUITEM "들여쓰기(&I)\tTab", IDM_EDIT_INDENT
MENUITEM "들여쓰기 해제(&U)\tShift+Tab", IDM_EDIT_UNINDENT
MENUITEM SEPARATOR
Expand Down
2 changes: 1 addition & 1 deletion locale/pt-BR/Notepad2.rc
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ BEGIN
MENUITEM "&Duplicate\tAlt+D", IDM_EDIT_SELECTIONDUPLICATE
MENUITEM SEPARATOR
MENUITEM "T&oggle Line Comment\tCtrl+/", IDM_EDIT_LINECOMMENT
MENUITEM "Block &Comment\tCtrl+Q", IDM_EDIT_STREAMCOMMENT
MENUITEM "Toggle Block &Comment\tCtrl+Q", IDM_EDIT_STREAMCOMMENT
MENUITEM "&Indent\tTab", IDM_EDIT_INDENT
MENUITEM "&Unindent\tShift+Tab", IDM_EDIT_UNINDENT
MENUITEM SEPARATOR
Expand Down
2 changes: 1 addition & 1 deletion locale/zh-Hans/Notepad2.rc
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ BEGIN
MENUITEM "重复(&D)\tAlt+D", IDM_EDIT_SELECTIONDUPLICATE
MENUITEM SEPARATOR
MENUITEM "切换行注释(&O)\tCtrl+/", IDM_EDIT_LINECOMMENT
MENUITEM "块注释(&C)\tCtrl+Q", IDM_EDIT_STREAMCOMMENT
MENUITEM "切换块注释(&C)\tCtrl+Q", IDM_EDIT_STREAMCOMMENT
MENUITEM "增加缩进(&I)\tTab", IDM_EDIT_INDENT
MENUITEM "减少缩进(&U)\tShift+Tab", IDM_EDIT_UNINDENT
MENUITEM SEPARATOR
Expand Down
2 changes: 1 addition & 1 deletion locale/zh-Hant/Notepad2.rc
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ BEGIN
MENUITEM "重複(&D)\tAlt+D", IDM_EDIT_SELECTIONDUPLICATE
MENUITEM SEPARATOR
MENUITEM "切換行註解(&O)\tCtrl+/", IDM_EDIT_LINECOMMENT
MENUITEM "區註解(&C)\tCtrl+Q", IDM_EDIT_STREAMCOMMENT
MENUITEM "切換區註解(&C)\tCtrl+Q", IDM_EDIT_STREAMCOMMENT
MENUITEM "增加縮排(&I)\tTab", IDM_EDIT_INDENT
MENUITEM "減少縮排(&U)\tShift+Tab", IDM_EDIT_UNINDENT
MENUITEM SEPARATOR
Expand Down
146 changes: 115 additions & 31 deletions src/EditAutoC.c
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,10 @@ static inline bool IsGenericTypeStyle(int style) {
return BitTestEx(GenericTypeStyleMask, style);
}

static inline bool IsCommentStyle(int style) {
return BitTestEx(CommentStyleMask, style);
}

bool IsAutoCompletionWordCharacter(uint32_t ch) {
if (ch < 0x80) {
return IsDocWordChar(ch);
Expand Down Expand Up @@ -1643,14 +1647,14 @@ static bool EditCompleteWordCore(int iCondition, bool autoInsert) {
retry = false;
}
} else {
if ((!(autoCompletionConfig.fCompleteScope & AutoCompleteScope_Commont) && BitTestEx(CommentStyleMask, iCurrentStyle))
if ((!(autoCompletionConfig.fCompleteScope & AutoCompleteScope_Commont) && IsCommentStyle(iCurrentStyle))
|| (!(autoCompletionConfig.fCompleteScope & AutoCompleteScope_String) && BitTestEx(AllStringStyleMask, iCurrentStyle))
|| (!(autoCompletionConfig.fCompleteScope & AutoCompleteScope_PlainText) && BitTestEx(PlainTextStyleMask, iCurrentStyle))) {
retry = false;
}
if (retry && bScanWordsInDocument) {
memcpy(ignoredStyleMask, IgnoreWordStyleMask, sizeof(IgnoreWordStyleMask));
if (!(autoCompletionConfig.fScanWordScope & AutoCompleteScope_Commont) && !BitTestEx(CommentStyleMask, iCurrentStyle)) {
if (!(autoCompletionConfig.fScanWordScope & AutoCompleteScope_Commont) && !IsCommentStyle(iCurrentStyle)) {
for (UINT i = 0; i < 8; i++) {
ignoredStyleMask[i] |= CommentStyleMask[i];
}
Expand Down Expand Up @@ -2577,15 +2581,87 @@ void EditEncloseSelectionNewLine(LPCWSTR pwszOpen, LPCWSTR pwszClose) {
EditEncloseSelection(start, end);
}

static bool EditUncommentBlock(LPCWSTR pwszOpen, LPCWSTR pwszClose, bool newLine) {
const Sci_Position iSelStart = SciCall_GetSelectionStart();
int style = SciCall_GetStyleIndexAt(iSelStart);
if (IsCommentStyle(style)) {
const Sci_Position iSelEnd = SciCall_GetSelectionEnd();
Sci_Position iStartPos = iSelStart;
Sci_Position iEndPos = iSelEnd;
// find comment block, TODO: add IsBlockCommentStyle()
style = SciCall_GetStyleIndexAt(iEndPos);
if (!IsCommentStyle(style)) {
style = SciCall_GetStyleIndexAt(iEndPos - 1);
if (!IsCommentStyle(style)) {
return false;
}
} else {
do {
++iEndPos;
style = SciCall_GetStyleIndexAt(iEndPos);
} while (IsCommentStyle(style));
}
do {
--iStartPos;
style = SciCall_GetStyleIndexAt(iStartPos);
} while (IsCommentStyle(style));

const UINT cpEdit = SciCall_GetCodePage();
char mszOpen[64] = "";
char mszClose[64] = "";
WideCharToMultiByte(cpEdit, 0, pwszOpen, -1, mszOpen, COUNTOF(mszOpen), NULL, NULL);
WideCharToMultiByte(cpEdit, 0, pwszClose, -1, mszClose, COUNTOF(mszClose), NULL, NULL);

// find inner most comment block for current selection
struct Sci_TextToFindFull ttfClose = { { iSelStart, iEndPos }, mszClose, { 0, 0 } };
iEndPos = SciCall_FindTextFull(SCFIND_NONE, &ttfClose);
if (iEndPos < 0) {
return false;
}

struct Sci_TextToFindFull ttfOpen = { { iSelEnd, iStartPos + 1 }, mszOpen, { 0, 0 } };
iStartPos = SciCall_FindTextFull(SCFIND_NONE, &ttfOpen);
if (iStartPos < 0 || ttfOpen.chrgText.cpMax > iEndPos) {
return false;
}

if (newLine) {
const Sci_Line iStartLine = SciCall_LineFromPosition(iStartPos);
const Sci_Line iEndLine = SciCall_LineFromPosition(iEndPos);
if (iStartLine == iEndLine) {
return false;
}
if (SciCall_GetLineIndentPosition(iStartLine) != iStartPos
|| SciCall_GetLineIndentPosition(iEndLine) != iEndPos) {
return false;
}
iStartPos = SciCall_PositionFromLine(iStartLine);
iEndPos = SciCall_PositionFromLine(iEndLine);
ttfOpen.chrgText.cpMax = SciCall_PositionFromLine(iStartLine + 1);
ttfClose.chrgText.cpMax = SciCall_PositionFromLine(iEndLine + 1);
}

SciCall_BeginUndoAction();
SciCall_DeleteRange(iEndPos, ttfClose.chrgText.cpMax - iEndPos);
SciCall_DeleteRange(iStartPos, ttfOpen.chrgText.cpMax - iStartPos);
SciCall_EndUndoAction();
return true;
}
return false;
}

void EditToggleCommentBlock(void) {
LPCWSTR pwszOpen = NULL;
LPCWSTR pwszClose = NULL;
bool newLine = false;
switch (pLexCurrent->rid) {
case NP2LEX_BLOCKDIAG:
case NP2LEX_GRAPHVIZ: {
const int lineState = SciCall_GetLineState(SciCall_LineFromPosition(SciCall_GetSelectionStart()));
if (lineState) {
EditEncloseSelection(L"<!--", L"-->");
pwszOpen = L"<!--"; pwszClose = L"-->";
} else {
EditEncloseSelection(L"/*", L"*/");
pwszOpen = L"/*"; pwszClose = L"*/";
}
} break;

Expand All @@ -2595,20 +2671,20 @@ void EditToggleCommentBlock(void) {
const HtmlTextBlock block = GetCurrentHtmlTextBlock(pLexCurrent->iLexer);
switch (block) {
case HtmlTextBlock_Tag:
EditEncloseSelection(L"<!--", L"-->");
pwszOpen = L"<!--"; pwszClose = L"-->";
break;

case HtmlTextBlock_CDATA:
case HtmlTextBlock_JavaScript:
case HtmlTextBlock_PHP:
case HtmlTextBlock_CSS:
EditEncloseSelection(L"/*", L"*/");
pwszOpen = L"/*"; pwszClose = L"*/";
break;

case HtmlTextBlock_SGML:
// A brief SGML tutorial
// https://www.w3.org/TR/WD-html40-970708/intro/sgmltut.html
EditEncloseSelection(L"--", L"--");
pwszOpen = L"--"; pwszClose = L"--";
break;

default:
Expand All @@ -2620,18 +2696,18 @@ void EditToggleCommentBlock(void) {
case NP2LEX_INNOSETUP: {
const int lineState = SciCall_GetLineState(SciCall_LineFromPosition(SciCall_GetSelectionStart()));
if (lineState & InnoLineStateCodeSection) {
EditEncloseSelection(L"{", L"}");
pwszOpen = L"{"; pwszClose = L"}";
} else if (lineState & InnoLineStatePreprocessor) {
EditEncloseSelection(L"/*", L"*/");
pwszOpen = L"/*"; pwszClose = L"*/";
}
}
break;

case NP2LEX_MATLAB:
if (np2LexLangIndex == IDM_LEXER_SCILAB) {
EditEncloseSelection(L"/*", L"*/");
pwszOpen = L"/*"; pwszClose = L"*/";
} else {
EditEncloseSelectionNewLine(L"%{", L"%}");
pwszOpen = L"%{"; pwszClose = L"%}"; newLine = true;
}
break;

Expand Down Expand Up @@ -2664,90 +2740,98 @@ void EditToggleCommentBlock(void) {
case NP2LEX_TYPESCRIPT:
case NP2LEX_VERILOG:
case NP2LEX_VHDL:
EditEncloseSelection(L"/*", L"*/");
pwszOpen = L"/*"; pwszClose = L"*/";
break;

case NP2LEX_AUTOIT3:
EditEncloseSelectionNewLine(L"#cs", L"#ce");
pwszOpen = L"#cs"; pwszClose = L"#ce";; newLine = true;
break;

case NP2LEX_CMAKE:
EditEncloseSelection(L"#[[", L"]]");
pwszOpen = L"#[["; pwszClose = L"]]";
break;

case NP2LEX_COFFEESCRIPT:
EditEncloseSelection(L"###", L"###");
pwszOpen = L"###"; pwszClose = L"###";
break;

case NP2LEX_FORTRAN:
EditEncloseSelectionNewLine(L"#if 0", L"#endif");
pwszOpen = L"#if 0"; pwszClose = L"#endif";; newLine = true;
break;

case NP2LEX_FSHARP:
case NP2LEX_MATHEMATICA:
case NP2LEX_OCAML:
EditEncloseSelection(L"(*", L"*)");
pwszOpen = L"(*"; pwszClose = L"*)";
break;

case NP2LEX_HASKELL:
EditEncloseSelection(L"{-", L"-}");
pwszOpen = L"{-"; pwszClose = L"-}";
break;

case NP2LEX_JAMFILE:
case NP2LEX_LISP:
EditEncloseSelection(L"#|", L"|#");
pwszOpen = L"#|"; pwszClose = L"|#";
break;

case NP2LEX_JULIA:
EditEncloseSelection(L"#=", L"=#");
pwszOpen = L"#="; pwszClose = L"=#";
break;

case NP2LEX_LATEX:
EditEncloseSelectionNewLine(L"\\begin{comment}", L"\\end{comment}");
pwszOpen = L"\\begin{comment}"; pwszClose = L"\\end{comment}";; newLine = true;
break;

case NP2LEX_LUA:
EditEncloseSelection(L"--[[", L"--]]");
pwszOpen = L"--[["; pwszClose = L"--]]";
break;

case NP2LEX_MARKDOWN:
EditEncloseSelection(L"<!--", L"-->");
pwszOpen = L"<!--"; pwszClose = L"-->";
break;

case NP2LEX_NIM:
EditEncloseSelection(L"#[", L"]#");
pwszOpen = L"#["; pwszClose = L"]#";
break;

case NP2LEX_PASCAL:
EditEncloseSelection(L"{", L"}");
pwszOpen = L"{"; pwszClose = L"}";
break;

case NP2LEX_POWERSHELL:
EditEncloseSelection(L"<#", L"#>");
pwszOpen = L"<#"; pwszClose = L"#>";
break;

case NP2LEX_REBOL:
EditEncloseSelectionNewLine(L"comment {", L"}");
pwszOpen = L"comment {"; pwszClose = L"}";; newLine = true;
break;

case NP2LEX_RLANG:
EditEncloseSelectionNewLine(L"if (FALSE) {", L"}");
pwszOpen = L"if (FALSE) {"; pwszClose = L"}";; newLine = true;
break;

case NP2LEX_TCL:
EditEncloseSelectionNewLine(L"if (0) {", L"}");
pwszOpen = L"if (0) {"; pwszClose = L"}";; newLine = true;
break;

case NP2LEX_TEXINFO:
EditEncloseSelectionNewLine(L"@ignore", L"@end ignore");
pwszOpen = L"@ignore"; pwszClose = L"@end ignore";; newLine = true;
break;

case NP2LEX_WASM:
EditEncloseSelection(L"(;", L";)");
pwszOpen = L"(;"; pwszClose = L";)";
break;

//CommentBlock--Autogenerated -- end of section automatically generated
}

if (pwszOpen != NULL && !EditUncommentBlock(pwszOpen, pwszClose, newLine)) {
if (newLine) {
EditEncloseSelectionNewLine(pwszOpen, pwszClose);
} else {
EditEncloseSelection(pwszOpen, pwszClose);
}
}
}

// see Style_SniffShebang() in Styles.c
Expand Down
2 changes: 1 addition & 1 deletion src/Notepad2.rc
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ BEGIN
MENUITEM "&Duplicate\tAlt+D", IDM_EDIT_SELECTIONDUPLICATE
MENUITEM SEPARATOR
MENUITEM "T&oggle Line Comment\tCtrl+/", IDM_EDIT_LINECOMMENT
MENUITEM "Block &Comment\tCtrl+Q", IDM_EDIT_STREAMCOMMENT
MENUITEM "Toggle Block &Comment\tCtrl+Q", IDM_EDIT_STREAMCOMMENT
MENUITEM "&Indent\tTab", IDM_EDIT_INDENT
MENUITEM "&Unindent\tShift+Tab", IDM_EDIT_UNINDENT
MENUITEM SEPARATOR
Expand Down
4 changes: 2 additions & 2 deletions tools/LexerConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -1374,9 +1374,9 @@ def BuildLexerCommentString():
if isinstance(block_comment_string, tuple):
assert len(block_comment_string) == 2, (rid, block_comment_string)
newline = config.get('block_comment_on_new_line', False)
suffix = 'NewLine' if newline else ''
suffix = '; newLine = true;' if newline else ''
start, end = escape_c_string(block_comment_string[0]), escape_c_string(block_comment_string[1])
code = (f'{indent}EditEncloseSelection{suffix}(L"{start}", L"{end}");', indent + 'break;', '')
code = (f'{indent}pwszOpen = L"{start}"; pwszClose = L"{end}";{suffix}', indent + 'break;', '')
commentBlock[rid] = code
else:
complexBlock.append(rid)
Expand Down

0 comments on commit 3e79284

Please sign in to comment.