From 17106fb39bf4aa45af318d05f0f4bbe81e89e1ee Mon Sep 17 00:00:00 2001 From: zufuliu Date: Fri, 2 Aug 2024 18:50:46 +0800 Subject: [PATCH] [Code Compress/Pretty] Format whole document when no text selected, issue #830. --- src/Bridge.cpp | 39 ++++++++++++++++++++++++++------------- src/Notepad4.cpp | 4 ++-- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/Bridge.cpp b/src/Bridge.cpp index 4b5c2fd52c..8b70e187d8 100644 --- a/src/Bridge.cpp +++ b/src/Bridge.cpp @@ -746,7 +746,7 @@ constexpr int GetRTFFontSize(int size) noexcept { return size / (SC_FONT_SIZE_MULTIPLIER / 2); } -std::string SaveToStreamRTF(const char *styledText, size_t textLength, Sci_Position startPos, Sci_Position endPos) { +void SaveToStreamRTF(std::string &os, const char *styledText, size_t textLength, Sci_Position startPos, Sci_Position endPos) { uint8_t styleMap[STYLE_MAX + 1]; const auto [styleList, styleCount, cpEdit] = GetDocumentStyledText(styleMap, styledText, textLength); const std::unique_ptr styles = make_unique_for_overwrite(styleCount); @@ -761,7 +761,7 @@ std::string SaveToStreamRTF(const char *styledText, size_t textLength, Sci_Posit char fmtbuf[RTF_MAX_STYLEDEF]; memset(fmtbuf, 0, 4); unsigned fmtlen = sprintf(fmtbuf, RTF_HEADEROPEN RTF_FONTDEFOPEN, legacyACP); - std::string os(fmtbuf, fmtlen); + os += std::string_view{fmtbuf, fmtlen}; int fontCount = 0; int colorCount = 0; @@ -950,7 +950,6 @@ std::string SaveToStreamRTF(const char *styledText, size_t textLength, Sci_Posit } os += RTF_PARAGRAPH_END RTF_BODYCLOSE; - return os; } } @@ -998,9 +997,8 @@ int AddStyleSeparator(LPCEDITLEXER pLex, int ch, int chPrev, int style) noexcept return SpaceOption_None; } -std::string CodePretty(LPCEDITLEXER pLex, const char *styledText, size_t textLength) { +void CodePretty(std::string &output, LPCEDITLEXER pLex, const char *styledText, size_t textLength) { char fmtbuf[128]; - std::string output; std::string braceStack(1, '\0'); // sentinel memset(fmtbuf, 0, 4); @@ -1295,7 +1293,6 @@ std::string CodePretty(LPCEDITLEXER pLex, const char *styledText, size_t textLen if (fmtlen != 0) { output += std::string_view{fmtbuf, fmtlen}; } - return output; } } @@ -1305,10 +1302,13 @@ void EditFormatCode(int menu) noexcept { if (menu != IDM_EDIT_COPYRTF && pLex->iLexer != SCLEX_JSON && pLex->iLexer != SCLEX_CSS && pLex->iLexer != SCLEX_JAVASCRIPT) { return; } - const Sci_Position startPos = SciCall_GetSelectionStart(); - const Sci_Position endPos = SciCall_GetSelectionEnd(); + Sci_Position startPos = SciCall_GetSelectionStart(); + Sci_Position endPos = SciCall_GetSelectionEnd(); + bool wholeDoc = false; if (startPos == endPos) { - return; + wholeDoc = true; + startPos = 0; + endPos = SciCall_GetLength(); } try { @@ -1316,10 +1316,13 @@ void EditFormatCode(int menu) noexcept { const std::unique_ptr styledText = make_unique_for_overwrite(2*(endPos - startPos) + 2); const Sci_TextRangeFull tr { { startPos, endPos }, styledText.get() }; const size_t textLength = SciCall_GetStyledTextFull(&tr); + std::string output; + std::string_view result; + bool changed = false; if (menu == IDM_EDIT_COPYRTF) { // code from SciTEWin::CopyAsRTF() - const std::string output = SaveToStreamRTF(styledText.get(), textLength, startPos, endPos); + SaveToStreamRTF(output, styledText.get(), textLength, startPos, endPos); //printf("%s:\n%s\n", __func__, output.c_str()); const size_t len = output.length() + 1; // +1 for NUL HGLOBAL handle = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, len); @@ -1361,12 +1364,22 @@ void EditFormatCode(int menu) noexcept { } styledText[index] = '\0'; if (index < textLength) { - EditReplaceMainSelection(index, styledText.get()); + changed = true; + result = {styledText.get(), index}; } } else { - const std::string output = CodePretty(pLex, styledText.get(), textLength); + CodePretty(output, pLex, styledText.get(), textLength); if (output.length() != textLength) { - EditReplaceMainSelection(output.length(), output.c_str()); + changed = true; + result = output; + } + } + if (changed) { + if (wholeDoc) { + SciCall_TargetWholeDocument(); + SciCall_ReplaceTarget(result.length(), result.data()); + } else { + EditReplaceMainSelection(result.length(), result.data()); } } } catch (...) { diff --git a/src/Notepad4.cpp b/src/Notepad4.cpp index 2b43d14711..69c3b3c968 100644 --- a/src/Notepad4.cpp +++ b/src/Notepad4.cpp @@ -2476,6 +2476,8 @@ void MsgInitMenu(HWND hwnd, WPARAM wParam, LPARAM lParam) noexcept { CMD_OPEN_PATH_OR_LINK, CMD_TIMESTAMPS, IDM_EDIT_CLEARDOCUMENT, + IDM_EDIT_CODE_COMPRESS, + IDM_EDIT_CODE_PRETTY, IDM_EDIT_COMPLETEWORD, IDM_EDIT_COPY, IDM_EDIT_COPYALL, @@ -2530,8 +2532,6 @@ void MsgInitMenu(HWND hwnd, WPARAM wParam, LPARAM lParam) noexcept { IDM_EDIT_BASE64_HTML_EMBEDDED_IMAGE, IDM_EDIT_BASE64_SAFE_ENCODE, IDM_EDIT_CHAR2HEX, - IDM_EDIT_CODE_COMPRESS, - IDM_EDIT_CODE_PRETTY, IDM_EDIT_COLUMNWRAP, IDM_EDIT_CONVERTLOWERCASE, IDM_EDIT_CONVERTSPACES,