Skip to content

Commit

Permalink
Enable using Ctrl + / to toggle block comment for CSS, HTML, PHP an…
Browse files Browse the repository at this point in the history
…d XML tags, issue #718.
  • Loading branch information
zufuliu committed Sep 27, 2023
1 parent 9481f2a commit 3494f2e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 48 deletions.
7 changes: 3 additions & 4 deletions src/Edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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';
}

Expand All @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions src/Edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
86 changes: 49 additions & 37 deletions src/EditAutoC.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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:
Expand All @@ -2427,31 +2426,31 @@ 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"\"";
}
}
}
break;

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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Notepad2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions tools/LexerConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3494f2e

Please sign in to comment.