From 3494f2e39a208276e7b84e78cb0ccb5dcb22fb7c Mon Sep 17 00:00:00 2001 From: zufuliu Date: Wed, 27 Sep 2023 20:06:59 +0800 Subject: [PATCH] Enable using `Ctrl + /` to toggle block comment for CSS, HTML, PHP and XML tags, issue #718. --- src/Edit.c | 7 ++-- src/Edit.h | 7 ++-- src/EditAutoC.c | 86 +++++++++++++++++++++++++------------------- src/Notepad2.c | 4 +-- tools/LexerConfig.py | 4 +-- 5 files changed, 60 insertions(+), 48 deletions(-) diff --git a/src/Edit.c b/src/Edit.c index cd7bc726ee..fd588fbe45 100644 --- a/src/Edit.c +++ b/src/Edit.c @@ -3348,12 +3348,11 @@ void EditEncloseSelection(LPCWSTR pwszOpen, LPCWSTR pwszClose) { } } -extern EditAutoCompletionConfig autoCompletionConfig; //============================================================================= // // EditToggleLineComments() // -void EditToggleLineComments(LPCWSTR pwszComment, bool bInsertAtStart) { +void EditToggleLineComments(LPCWSTR pwszComment, int commentFlag) { if (SciCall_IsRectangleSelection()) { NotifyRectangleSelection(); return; @@ -3372,7 +3371,7 @@ void EditToggleLineComments(LPCWSTR pwszComment, bool bInsertAtStart) { char commentPad = ' '; if (commentEnd == ' ') { cchComment -= 1; - } else if ((autoCompletionConfig.fAutoInsertMask & AutoInsertMask_SpaceAfterComment) == 0) { + } else if ((commentFlag & AutoInsertMask_SpaceAfterComment) == 0) { commentPad = '\0'; } @@ -3386,7 +3385,7 @@ void EditToggleLineComments(LPCWSTR pwszComment, bool bInsertAtStart) { } Sci_Position iCommentCol = 0; - if (!bInsertAtStart) { + if ((commentFlag & AutoInsertMask_CommentAtStart) == 0) { iCommentCol = 1024 - 1 - cchComment; for (Sci_Line iLine = iLineStart; iLine <= iLineEnd; iLine++) { const Sci_Position iLineEndPos = SciCall_GetLineEndPosition(iLine); diff --git a/src/Edit.h b/src/Edit.h index e8ab77ee73..13a43ddc4e 100644 --- a/src/Edit.h +++ b/src/Edit.h @@ -163,7 +163,7 @@ void EditMoveDown(void); void EditModifyLines(LPCWSTR pwszPrefix, LPCWSTR pwszAppend, bool skipEmptyLine); void EditAlignText(EditAlignMode nMode); void EditEncloseSelection(LPCWSTR pwszOpen, LPCWSTR pwszClose); -void EditToggleLineComments(LPCWSTR pwszComment, bool bInsertAtStart); +void EditToggleLineComments(LPCWSTR pwszComment, int commentFlag); void EditPadWithSpaces(bool bSkipEmpty, bool bNoUndoGroup); void EditStripFirstCharacter(void); void EditStripLastCharacter(void); @@ -311,6 +311,7 @@ enum { AutoInsertMask_SingleQuote = 32, // '' AutoInsertMask_Backtick = 64, // `` AutoInsertMask_SpaceAfterComma = 128, // ', ' + AutoInsertMask_CommentAtStart = true, // '// ' AutoInsertMask_SpaceAfterComment = 256, // '// ' // default settings AutoInsertMask_Default = 511, @@ -365,8 +366,8 @@ bool EditIsOpenBraceMatched(Sci_Position pos, Sci_Position startPos); void EditAutoCloseBraceQuote(int ch, AutoInsertCharacter what); void EditAutoCloseXMLTag(void); void EditAutoIndent(void); -void EditToggleCommentLine(void); -void EditToggleCommentBlock(void); +void EditToggleCommentLine(bool alternative); +void EditToggleCommentBlock(bool alternative); void EditInsertScriptShebangLine(void); typedef enum CallTipType { diff --git a/src/EditAutoC.c b/src/EditAutoC.c index da4195f02f..cb504eca0d 100644 --- a/src/EditAutoC.c +++ b/src/EditAutoC.c @@ -2360,40 +2360,39 @@ void EditAutoIndent(void) { } } -void EditToggleCommentLine(void) { +void EditToggleCommentLine(bool alternative) { + LPCWSTR pwszComment = NULL; switch (pLexCurrent->rid) { case NP2LEX_ASM: { - LPCWSTR ch; switch (autoCompletionConfig.iAsmLineCommentChar) { case AsmLineCommentChar_Semicolon: default: - ch = L";"; + pwszComment = L";"; break; case AsmLineCommentChar_Sharp: - ch = L"# "; + pwszComment = L"# "; break; case AsmLineCommentChar_Slash: - ch = L"//"; + pwszComment = L"//"; break; case AsmLineCommentChar_At: - ch = L"@ "; + pwszComment = L"@ "; break; } - EditToggleLineComments(ch, false); } break; case NP2LEX_BASH: if (np2LexLangIndex == IDM_LEXER_M4) { - EditToggleLineComments(L"dnl ", false); + pwszComment = L"dnl "; } else { - EditToggleLineComments(L"#", false); + pwszComment = L"#"; } break; case NP2LEX_CSS: if (np2LexLangIndex > IDM_LEXER_CSS) { - EditToggleLineComments(L"//", false); + pwszComment = L"//"; } break; @@ -2403,17 +2402,17 @@ void EditToggleCommentLine(void) { const HtmlTextBlock block = GetCurrentHtmlTextBlock(pLexCurrent->iLexer); switch (block) { case HtmlTextBlock_VBScript: - EditToggleLineComments(L"'", false); + pwszComment = L"'"; break; case HtmlTextBlock_Python: - EditToggleLineComments(L"#", false); + pwszComment = L"#"; break; case HtmlTextBlock_CDATA: case HtmlTextBlock_JavaScript: case HtmlTextBlock_PHP: - EditToggleLineComments(L"//", false); + pwszComment = L"//"; break; default: @@ -2427,15 +2426,15 @@ void EditToggleCommentLine(void) { const int lineState = SciCall_GetLineState(SciCall_LineFromPosition(SciCall_GetSelectionStart())); if (pLexCurrent->rid == NP2LEX_INNOSETUP) { if (lineState & InnoLineStateCodeSection) { - EditToggleLineComments(L"//", false); + pwszComment = L"//"; } else { - EditToggleLineComments(L";", false); + pwszComment = L";"; } } else { if (lineState & VimLineStateMaskVim9Script) { - EditToggleLineComments(L"#", false); + pwszComment = L"#"; } else { - EditToggleLineComments(L"\"", false); + pwszComment = L"\""; } } } @@ -2443,15 +2442,15 @@ void EditToggleCommentLine(void) { case NP2LEX_MATLAB: if (np2LexLangIndex == IDM_LEXER_SCILAB) { - EditToggleLineComments(L"//", false); + pwszComment = L"//"; } else { - EditToggleLineComments(L"%", false); + pwszComment = L"%"; } break; //CommentLine++Autogenerated -- start of section automatically generated case NP2LEX_ABAQUS: - EditToggleLineComments(L"**", false); + pwszComment = L"**"; break; case NP2LEX_ACTIONSCRIPT: @@ -2481,12 +2480,12 @@ void EditToggleCommentLine(void) { case NP2LEX_VERILOG: case NP2LEX_WINHEX: case NP2LEX_ZIG: - EditToggleLineComments(L"//", false); + pwszComment = L"//"; break; case NP2LEX_APDL: case NP2LEX_FORTRAN: - EditToggleLineComments(L"!", false); + pwszComment = L"!"; break; case NP2LEX_AUTOHOTKEY: @@ -2495,7 +2494,7 @@ void EditToggleCommentLine(void) { case NP2LEX_LISP: case NP2LEX_LLVM: case NP2LEX_REBOL: - EditToggleLineComments(L";", false); + pwszComment = L";"; break; case NP2LEX_AVISYNTH: @@ -2518,42 +2517,48 @@ void EditToggleCommentLine(void) { case NP2LEX_TCL: case NP2LEX_TOML: case NP2LEX_YAML: - EditToggleLineComments(L"#", false); + pwszComment = L"#"; break; case NP2LEX_BATCH: - EditToggleLineComments(L"@rem ", false); + pwszComment = L"@rem "; break; case NP2LEX_HASKELL: case NP2LEX_LUA: case NP2LEX_VHDL: - EditToggleLineComments(L"--", false); + pwszComment = L"--"; break; case NP2LEX_LATEX: - EditToggleLineComments(L"%", false); + pwszComment = L"%"; break; case NP2LEX_SQL: - EditToggleLineComments(L"-- ", false); + pwszComment = L"-- "; break; case NP2LEX_TEXINFO: - EditToggleLineComments(L"@c ", false); + pwszComment = L"@c "; break; case NP2LEX_VBSCRIPT: case NP2LEX_VISUALBASIC: - EditToggleLineComments(L"\'", false); + pwszComment = L"\'"; break; case NP2LEX_WASM: - EditToggleLineComments(L";;", false); + pwszComment = L";;"; break; //CommentLine--Autogenerated -- end of section automatically generated } + + if (pwszComment != NULL) { + EditToggleLineComments(pwszComment, autoCompletionConfig.fAutoInsertMask & AutoInsertMask_SpaceAfterComment); + } else if (!alternative) { + EditToggleCommentBlock(true); + } } void EditEncloseSelectionNewLine(LPCWSTR pwszOpen, LPCWSTR pwszClose) { @@ -2644,7 +2649,7 @@ static bool EditUncommentBlock(LPCWSTR pwszOpen, LPCWSTR pwszClose, bool newLine return false; } -void EditToggleCommentBlock(void) { +void EditToggleCommentBlock(bool alternative) { LPCWSTR pwszOpen = NULL; LPCWSTR pwszClose = NULL; bool newLine = false; @@ -2806,12 +2811,19 @@ void EditToggleCommentBlock(void) { //CommentBlock--Autogenerated -- end of section automatically generated } - if (pwszOpen != NULL && !EditUncommentBlock(pwszOpen, pwszClose, newLine)) { - if (newLine) { - EditEncloseSelectionNewLine(pwszOpen, pwszClose); - } else { - EditEncloseSelection(pwszOpen, pwszClose); + if (pwszOpen != NULL) { + if (!EditUncommentBlock(pwszOpen, pwszClose, newLine)) { + if (newLine) { + EditEncloseSelectionNewLine(pwszOpen, pwszClose); + } else { + if (alternative) { + SciCall_SetSelectionMode(SC_SEL_LINES); + } + EditEncloseSelection(pwszOpen, pwszClose); + } } + } else if (!alternative) { + EditToggleCommentLine(true); } } diff --git a/src/Notepad2.c b/src/Notepad2.c index 06fae68de3..df2239b10f 100644 --- a/src/Notepad2.c +++ b/src/Notepad2.c @@ -3714,11 +3714,11 @@ LRESULT MsgCommand(HWND hwnd, WPARAM wParam, LPARAM lParam) { break; case IDM_EDIT_LINECOMMENT: - EditToggleCommentLine(); + EditToggleCommentLine(false); break; case IDM_EDIT_STREAMCOMMENT: - EditToggleCommentBlock(); + EditToggleCommentBlock(false); break; case IDM_EDIT_URLENCODE: diff --git a/tools/LexerConfig.py b/tools/LexerConfig.py index 752e9f0450..c6cb2fd8a9 100644 --- a/tools/LexerConfig.py +++ b/tools/LexerConfig.py @@ -1355,9 +1355,9 @@ def BuildLexerCommentString(): line_comment_string = line_comment_string[0] if isinstance(line_comment_string, str): start = config.get('line_comment_at_line_start', False) - argument = 'true' if start else 'false' + argument = ' lineStart = true;' if start else '' start = escape_c_string(line_comment_string) - code = (f'{indent}EditToggleLineComments(L"{start}", {argument});', indent + 'break;', '') + code = (f'{indent}pwszComment = L"{start}";{argument}', indent + 'break;', '') commentLine[rid] = code else: complexLine.append(rid)