Skip to content

Commit

Permalink
[Code Compress/Pretty] Format whole document when no text selected, i…
Browse files Browse the repository at this point in the history
…ssue #830.
  • Loading branch information
zufuliu committed Aug 2, 2024
1 parent 7fae25e commit 17106fb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
39 changes: 26 additions & 13 deletions src/Bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string[]> styles = make_unique_for_overwrite<std::string[]>(styleCount);
Expand All @@ -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;
Expand Down Expand Up @@ -950,7 +950,6 @@ std::string SaveToStreamRTF(const char *styledText, size_t textLength, Sci_Posit
}

os += RTF_PARAGRAPH_END RTF_BODYCLOSE;
return os;
}

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

Expand Down Expand Up @@ -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;
}

}
Expand All @@ -1305,21 +1302,27 @@ 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 {
SciCall_EnsureStyledTo(endPos);
const std::unique_ptr<char[]> styledText = make_unique_for_overwrite<char[]>(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);
Expand Down Expand Up @@ -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 (...) {
Expand Down
4 changes: 2 additions & 2 deletions src/Notepad4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 17106fb

Please sign in to comment.