diff --git a/uppsrc/Core/AString.hpp b/uppsrc/Core/AString.hpp index e1c2d6e73..cb132a38f 100644 --- a/uppsrc/Core/AString.hpp +++ b/uppsrc/Core/AString.hpp @@ -106,7 +106,8 @@ void AString::Replace(const tchar *find, int findlen, const tchar *replace, i i = j + findlen; } r.Cat(p + i, B::GetCount() - i); - *this = r; + Free(); + Set(r); } template diff --git a/uppsrc/Core/String.h b/uppsrc/Core/String.h index dabeff14e..32a084a1d 100644 --- a/uppsrc/Core/String.h +++ b/uppsrc/Core/String.h @@ -670,6 +670,8 @@ public: WString0() { Zero(); } ~WString0() { Free(); } + + WString0& operator=(const WString0& s) { Free(); Set(s); return *this; } }; class WString : public Moveable > diff --git a/uppsrc/ide/ide.h b/uppsrc/ide/ide.h index 00bb4aedc..9f7d8c856 100644 --- a/uppsrc/ide/ide.h +++ b/uppsrc/ide/ide.h @@ -874,6 +874,9 @@ public: void TextInitCaps(); void SwapCase(); void ToCString(); + void ToComment(); + void CommentLines(); + void UnComment(); void MacroMenu(Bar& menu); bool HasMacros(); diff --git a/uppsrc/ide/ide.key b/uppsrc/ide/ide.key index 5fa6c0cb1..68dfc091b 100644 --- a/uppsrc/ide/ide.key +++ b/uppsrc/ide/ide.key @@ -108,6 +108,9 @@ KEY(INITCAPS, "Capitalize the first character of words", 0) KEY(SWAPCASE, "Swap case", 0) KEY(TOASCII, "To ASCII", 0) KEY(TOCSTRING, "Convert to C string", 0) +KEY(TOCOMMENT, "Comment code", K_ALT_K) +KEY(COMMENTLINES, "Comment code lines", K_SHIFT|K_ALT_K) +KEY(UNCOMMENT, "Uncomment code", K_CTRL_K|K_ALT_K) KEY(DIFF, "Compare with file..", 0) KEY(SVNDIFF, "Show svn history of file..", 0) diff --git a/uppsrc/ide/idebar.cpp b/uppsrc/ide/idebar.cpp index d88838789..6ec8614b6 100644 --- a/uppsrc/ide/idebar.cpp +++ b/uppsrc/ide/idebar.cpp @@ -109,19 +109,24 @@ void Ide::EditSpecial(Bar& menu) .Help("Copy the current identifier to the clipboard"); menu.Add(AK_FORMATCODE, THISBACK(FormatCode)) .Help("Reformat code in editor"); - menu.Add(AK_TOUPPER, THISBACK(TextToUpper)) + menu.Add(editor.IsSelection(), AK_TOUPPER, THISBACK(TextToUpper)) .Help("Convert letters in selection to uppercase"); - menu.Add(AK_TOLOWER, THISBACK(TextToLower)) + menu.Add(editor.IsSelection(), AK_TOLOWER, THISBACK(TextToLower)) .Help("Convert letters in selection to lowercase"); - menu.Add(AK_TOASCII, THISBACK(TextToAscii)) + menu.Add(editor.IsSelection(), AK_TOASCII, THISBACK(TextToAscii)) .Help("Covert text to 7-bit ASCII removing all accents and special symbols"); - menu.Add(AK_INITCAPS, THISBACK(TextInitCaps)) + menu.Add(editor.IsSelection(), AK_INITCAPS, THISBACK(TextInitCaps)) .Help("Capitalize the first character of words in selection"); - menu.Add(AK_SWAPCASE, THISBACK(SwapCase)) + menu.Add(editor.IsSelection(), AK_SWAPCASE, THISBACK(SwapCase)) .Help("Swap the case of letters in selection"); - menu.Add(AK_TOCSTRING, THISBACK(ToCString)) + menu.Add(editor.IsSelection(), AK_TOCSTRING, THISBACK(ToCString)) .Help("Convert selection to CString"); -} + menu.Add(editor.IsSelection(), AK_TOCOMMENT, THISBACK(ToComment)) + .Help("Comment code"); + menu.Add(editor.IsSelection(), AK_COMMENTLINES, THISBACK(CommentLines)) + .Help("Comment code lines"); + menu.Add(editor.IsSelection(), AK_UNCOMMENT, THISBACK(UnComment)) + .Help("Uncomment code");} void Ide::SearchMenu(Bar& menu) { diff --git a/uppsrc/ide/idetool.cpp b/uppsrc/ide/idetool.cpp index 11c810dfc..1e2661220 100644 --- a/uppsrc/ide/idetool.cpp +++ b/uppsrc/ide/idetool.cpp @@ -240,14 +240,14 @@ String FormatElapsedTime(double run) void Ide::AlterText(WString (*op)(const WString& in)) { - if(designer) + if(designer || !editor.IsSelection()) return; editor.NextUndo(); - if(!editor.IsSelection()) - editor.SelectAll(); WString w = editor.GetSelectionW(); editor.RemoveSelection(); + int l = editor.GetCursor(); editor.Paste((*op)(w)); + editor.SetSelection(l, editor.GetCursor()); } void Ide::TextToUpper() @@ -294,6 +294,52 @@ void Ide::ToCString() AlterText(sCString); } +static WString sComment(const WString& s) +{ + return "/*" + s + "*/"; +} + +void Ide::ToComment() +{ + AlterText(sComment); +} + +static WString sCommentLines(const WString& s) +{ + String r; + StringStream ss(s.ToString()); + for(;;) { + String line = ss.GetLine(); + if(ss.IsError()) + return s; + else + if(!line.IsVoid()) + r << "//" << line << "\n"; + if(ss.IsEof()) + break; + } + return r.ToWString(); +} + +void Ide::CommentLines() +{ + AlterText(sCommentLines); +} + +static WString sUncomment(const WString& s) +{ + WString h = s; + h.Replace("/*", ""); + h.Replace("//", ""); + h.Replace("*/", ""); + return h; +} + +void Ide::UnComment() +{ + AlterText(sUncomment); +} + void Ide::Times() { WithStatisticsLayout statdlg;