Skip to content

Commit

Permalink
Merge pull request #4984 from RaiKoHoff/Dev_Master
Browse files Browse the repository at this point in the history
Python Line Comment : respect indents
  • Loading branch information
hpwamr authored Sep 1, 2023
2 parents 098d966 + 7602025 commit 5f0caed
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/Edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -3741,10 +3741,10 @@ void EditToggleLineCommentsSimple(LPCWSTR pwszComment, bool bInsertAtStart, LnCm

if (StrIsNotEmpty(pwszComment)) {
char mszPostfix[64 * 3] = { '\0' };
WideCharToMultiByteEx(Encoding_SciCP, 0, pwszComment, -1, mszPrefix, COUNTOF(mszPrefix), NULL, NULL);
WideCharToMultiByte(Encoding_SciCP, 0, pwszComment, -1, mszPrefix, COUNTOF(mszPrefix), NULL, NULL);
StringCchCopyA(mszComment, COUNTOF(mszComment), mszPrefix);
if (StrIsNotEmpty(Settings2.LineCommentPostfixStrg)) {
WideCharToMultiByteEx(Encoding_SciCP, 0, Settings2.LineCommentPostfixStrg, -1, mszPostfix, COUNTOF(mszPostfix), NULL, NULL);
WideCharToMultiByte(Encoding_SciCP, 0, Settings2.LineCommentPostfixStrg, -1, mszPostfix, COUNTOF(mszPostfix), NULL, NULL);
StringCchCatA(mszComment, COUNTOF(mszComment), mszPostfix);
}
}
Expand Down
74 changes: 30 additions & 44 deletions src/StyleLexers/EditLexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@

void Lexer_GetStreamCommentStrgs(LPWSTR beg_out, LPWSTR end_out, size_t maxlen)
{
#define SET_STREAMCOMMENT_STRG(X,Y) { StringCchCopy(beg_out, maxlen, (X)); StringCchCopy(end_out, maxlen, (Y)); }

if (beg_out && end_out && maxlen) {

switch (SciCall_GetLexer()) {
case SCLEX_AU3:
StringCchCopy(beg_out, maxlen, L"#cs");
StringCchCopy(end_out, maxlen, L"#ce");
SET_STREAMCOMMENT_STRG(L"#cs", L"#ce");
break;
case SCLEX_AVS:
case SCLEX_CPP:
Expand All @@ -28,40 +29,36 @@ void Lexer_GetStreamCommentStrgs(LPWSTR beg_out, LPWSTR end_out, size_t maxlen)
case SCLEX_RUST:
case SCLEX_SQL:
case SCLEX_VHDL:
StringCchCopy(beg_out, maxlen, L"/*");
StringCchCopy(end_out, maxlen, L"*/");
SET_STREAMCOMMENT_STRG(L"/*", L"*/");
break;
case SCLEX_HTML: {
int const cStyleBeg = SciCall_GetStyleIndexAt(Sci_GetLineStartPosition(SciCall_GetSelectionStart()));
int const cStyleEnd = SciCall_GetStyleIndexAt(SciCall_GetSelectionEnd());
if (((min_i(cStyleBeg, cStyleEnd) >= SCE_HPHP_DEFAULT) && (max_i(cStyleBeg, cStyleEnd) <= SCE_HPHP_OPERATOR)) ||
((min_i(cStyleBeg, cStyleEnd) >= SCE_HJ_START) && (max_i(cStyleBeg, cStyleEnd) <= SCE_HJA_REGEX))) {
StringCchCopy(beg_out, maxlen, L"/*");
StringCchCopy(end_out, maxlen, L"*/");
SET_STREAMCOMMENT_STRG(L"/*", L"*/");
break;
}
}
// [[fallthrough]] // -> XML
case SCLEX_XML:
StringCchCopy(beg_out, maxlen, L"<!--");
StringCchCopy(end_out, maxlen, L"-->");
SET_STREAMCOMMENT_STRG(L"<!--", L"-->");
break;
case SCLEX_INNOSETUP:
case SCLEX_PASCAL:
StringCchCopy(beg_out, maxlen, L"{");
StringCchCopy(end_out, maxlen, L"}");
SET_STREAMCOMMENT_STRG(L"{", L"}");
break;
case SCLEX_LUA:
StringCchCopy(beg_out, maxlen, L"--[[");
StringCchCopy(end_out, maxlen, L"]]");
SET_STREAMCOMMENT_STRG(L"--[[", L"]]");
break;
case SCLEX_COFFEESCRIPT:
StringCchCopy(beg_out, maxlen, L"###");
StringCchCopy(end_out, maxlen, L"###");
SET_STREAMCOMMENT_STRG(L"###", L"###");
break;
case SCLEX_MATLAB:
StringCchCopy(beg_out, maxlen, L"%{");
StringCchCopy(end_out, maxlen, L"%}");
SET_STREAMCOMMENT_STRG(L"%{", L"%}");
break;
case SCLEX_PYTHON:
SET_STREAMCOMMENT_STRG(L"_=\"\"\"", L"\"\"\"");
break;
// ------------------
case SCLEX_CONTAINER:
Expand All @@ -82,7 +79,6 @@ void Lexer_GetStreamCommentStrgs(LPWSTR beg_out, LPWSTR end_out, size_t maxlen)
case SCLEX_PERL:
case SCLEX_POWERSHELL:
case SCLEX_PROPERTIES:
case SCLEX_PYTHON:
case SCLEX_R:
case SCLEX_REGISTRY:
case SCLEX_RUBY:
Expand All @@ -92,8 +88,7 @@ void Lexer_GetStreamCommentStrgs(LPWSTR beg_out, LPWSTR end_out, size_t maxlen)
case SCLEX_VBSCRIPT:
case SCLEX_YAML:
default:
StringCchCopy(beg_out, maxlen, L"");
StringCchCopy(end_out, maxlen, L"");
SET_STREAMCOMMENT_STRG(L"", L"");
break;
}
}
Expand All @@ -102,6 +97,8 @@ void Lexer_GetStreamCommentStrgs(LPWSTR beg_out, LPWSTR end_out, size_t maxlen)

bool Lexer_GetLineCommentStrg(LPWSTR pre_out, size_t maxlen)
{
#define SET_COMMENT_STRG(X,S) { StringCchCopy(pre_out, maxlen, (X)); return (S); }

if (pre_out && maxlen) {

switch (SciCall_GetLexer()) {
Expand All @@ -112,12 +109,10 @@ bool Lexer_GetLineCommentStrg(LPWSTR pre_out, size_t maxlen)
case SCLEX_KOTLIN:
case SCLEX_PASCAL:
case SCLEX_RUST:
StringCchCopy(pre_out, maxlen, L"//");
return false;
SET_COMMENT_STRG(L"//", false);
case SCLEX_VB:
case SCLEX_VBSCRIPT:
StringCchCopy(pre_out, maxlen, L"'");
return false;
SET_COMMENT_STRG(L"'", false);
case SCLEX_AVS:
case SCLEX_BASH:
case SCLEX_CMAKE:
Expand All @@ -128,39 +123,34 @@ bool Lexer_GetLineCommentStrg(LPWSTR pre_out, size_t maxlen)
case SCLEX_PERL:
case SCLEX_PHPSCRIPT:
case SCLEX_POWERSHELL:
case SCLEX_PYTHON:
case SCLEX_R:
case SCLEX_RUBY:
case SCLEX_TCL:
case SCLEX_TOML:
case SCLEX_YAML:
StringCchCopy(pre_out, maxlen, L"#");
return true;
SET_COMMENT_STRG(L"#", true);
case SCLEX_PYTHON:
SET_COMMENT_STRG(L"#", false);
case SCLEX_AHK:
case SCLEX_ASM:
case SCLEX_AU3:
case SCLEX_INNOSETUP:
case SCLEX_NSIS: // "#" could also be used instead
case SCLEX_PROPERTIES:
case SCLEX_REGISTRY:
StringCchCopy(pre_out, maxlen, L";");
return true;
SET_COMMENT_STRG(L";", true);
case SCLEX_LUA:
case SCLEX_SQL:
case SCLEX_VHDL:
StringCchCopy(pre_out, maxlen, L"--");
return true;
SET_COMMENT_STRG(L"--", true);
case SCLEX_BATCH: // "::" could also be used instead
StringCchCopy(pre_out, maxlen, L"rem ");
return true;
SET_COMMENT_STRG(L"rem ", true);
case SCLEX_LATEX:
case SCLEX_MATLAB:
StringCchCopy(pre_out, maxlen, L"%");
return true;
SET_COMMENT_STRG(L"%", true);
case SCLEX_FORTRAN:
case SCLEX_F77:
StringCchCopy(pre_out, maxlen, L"!");
return true;
SET_COMMENT_STRG(L"!", true);
// ------------------
case SCLEX_CONTAINER:
assert("SciCall_GetLexer() UNDEFINED!" && 0);
Expand All @@ -173,21 +163,17 @@ bool Lexer_GetLineCommentStrg(LPWSTR pre_out, size_t maxlen)
int const cStyleBeg = SciCall_GetStyleIndexAt(Sci_GetLineStartPosition(SciCall_GetSelectionStart()));
int const cStyleEnd = SciCall_GetStyleIndexAt(SciCall_GetSelectionEnd());
if (((min_i(cStyleBeg, cStyleEnd) >= SCE_HPHP_DEFAULT) && (max_i(cStyleBeg, cStyleEnd) <= SCE_HPHP_OPERATOR)) || (min_i(cStyleBeg, cStyleEnd) == SCE_HPHP_COMPLEX_VARIABLE) ||
((min_i(cStyleBeg, cStyleEnd) >= SCE_HJ_START) && (max_i(cStyleBeg, cStyleEnd) <= SCE_HJA_REGEX))) {
StringCchCopy(pre_out, maxlen, L"//");
return false;
}
((min_i(cStyleBeg, cStyleEnd) >= SCE_HJ_START) && (max_i(cStyleBeg, cStyleEnd) <= SCE_HJA_REGEX)))
SET_COMMENT_STRG(L"//", false);
if (((min_i(cStyleBeg, cStyleEnd) >= SCE_HP_START) && (max_i(cStyleBeg, cStyleEnd) <= SCE_HP_IDENTIFIER)) ||
((min_i(cStyleBeg, cStyleEnd) >= SCE_HPA_START) && (max_i(cStyleBeg, cStyleEnd) <= SCE_HPA_IDENTIFIER))) {
StringCchCopy(pre_out, maxlen, L"#");
return false;
SET_COMMENT_STRG(L"#", false);
}
}
// [[fallthrough]] // -> XML
case SCLEX_XML:
default:
StringCchCopy(pre_out, maxlen, L"");
break;
SET_COMMENT_STRG(L"", false);
}
}
return false;
Expand Down

0 comments on commit 5f0caed

Please sign in to comment.