News:

As usual while waiting for the next release - don't forget to check the nightly builds in the forum.

Main Menu

Source formatter (line breaks)

Started by The_Immortal, June 06, 2013, 09:45:40 PM

Previous topic - Next topic

The_Immortal

Hi there!

Guys, I'm wondering is there an option that allows to put a line break for long code statements automatically?

Look at the picture please:



I never found any related to the problem option in Source formatter...


Could you help me please?



Thank you!

oBFusCATed

(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

The_Immortal

Quote from: oBFusCATed on June 06, 2013, 11:00:39 PM
Probably you could write a script to do it on a menu/key command.

See here for details: http://wiki.codeblocks.org/index.php?title=Scripting_Code::Blocks
Actually I'm looking for some ready solutions... :)

Maybe you I have something regarding this?

Thank you!

oBFusCATed

(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Alpha

Quote from: oBFusCATed on June 06, 2013, 11:48:09 PM
Quote from: The_Immortal on June 06, 2013, 11:11:30 PM
Maybe you I have something regarding this?
No
Well, possibly.  The last version of astyle added this ability.  However, I do not believe the feature has yet made it into Code::Blocks' formatter plugin (or has it?).

The_Immortal

Oh, guys... I just downloaded the latest version of AStyle (2.03) but have no idea how to involve it into CodeBlocks because there is no *.cbplugin file.


Could you help me please?

Thank you!

stahta01

#6
Quote from: The_Immortal on June 15, 2013, 12:53:38 AM
Oh, guys... I just downloaded the latest version of AStyle (2.03) but have no idea how to involve it into CodeBlocks because there is no *.cbplugin file.


Could you help me please?

Thank you!

If you want to convert a single C file at a time; you might wish to try this link
http://jimp03.zxq.net/Downloads.html

It is a GUI that wrap the AStyle; it either that, wait till the CB team integrates it, or learn to use the command line version you downloaded.

Edit2: A second GUI cite http://universalindent.sourceforge.net/

Edit3: You could also patch CB to use the newer AStyle; but, that is beyond my ability, so, I did not think of it, at first.

Edit4: Looks like the CB AStyle version is "2.04 beta" found in file astyle_main.cpp. So, likely just the GUI code needs added to CB to use the feature. If you are good at wxWidgets you might be able to do it. I am still bad at wxWidgets and have no time to learn how to do it.

Tim S.
C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

BlueHazzard

hello,
i have made a short patch to add this feature:

Index: astyleconfigdlg.cpp
===================================================================
--- astyleconfigdlg.cpp (revision 9157)
+++ astyleconfigdlg.cpp (working copy)
@@ -41,6 +41,7 @@
   EVT_RADIOBUTTON(XRCID("rbLisp"),       AstyleConfigDlg::OnStyleChange)
   EVT_RADIOBUTTON(XRCID("rbCustom"),     AstyleConfigDlg::OnStyleChange)
   EVT_BUTTON(XRCID("Preview"),           AstyleConfigDlg::OnPreview    )
+  EVT_CHECKBOX(XRCID("chkBreakeLines"),  AstyleConfigDlg::OnCheckBoxEvt)
END_EVENT_TABLE()

AstyleConfigDlg::AstyleConfigDlg(wxWindow* parent)
@@ -274,6 +275,14 @@
     SetStyle(aspsCustom);
}

+void AstyleConfigDlg::OnCheckBoxEvt(wxCommandEvent& event)
+{
+    if(event.IsChecked())
+        XRCCTRL(*this, "txtMaxLineLegth", wxTextCtrl)->Enable();
+    else
+        XRCCTRL(*this, "txtMaxLineLegth", wxTextCtrl)->Disable();
+}
+
void AstyleConfigDlg::OnPreview(wxCommandEvent& WXUNUSED(event))
{
   wxString text(XRCCTRL(*this, "txtSample", wxTextCtrl)->GetValue());
@@ -333,7 +342,14 @@
   XRCCTRL(*this, "chkConvertTabs",        wxCheckBox)->SetValue(cfg->ReadBool(_T("/convert_tabs"),         false));
   XRCCTRL(*this, "chkFillEmptyLines",     wxCheckBox)->SetValue(cfg->ReadBool(_T("/fill_empty_lines"),     false));
   XRCCTRL(*this, "chkAddBrackets",        wxCheckBox)->SetValue(cfg->ReadBool(_T("/add_brackets"),         false));
+  XRCCTRL(*this, "chkBreakeLines",        wxCheckBox)->SetValue(cfg->ReadBool(_T("/break_lines"),          false));
+  XRCCTRL(*this, "txtMaxLineLegth",       wxTextCtrl)->SetValue(cfg->Read(_T("/max_line_length"), _T("200")));

+  if(XRCCTRL(*this, "chkBreakeLines",wxCheckBox)->GetValue())
+    XRCCTRL(*this, "txtMaxLineLegth", wxTextCtrl)->Enable();
+  else
+    XRCCTRL(*this, "txtMaxLineLegth", wxTextCtrl)->Disable();
+
   SetStyle((AStylePredefinedStyle)style);
}

@@ -397,4 +413,6 @@
   cfg->Write(_T("/convert_tabs"),         XRCCTRL(*this, "chkConvertTabs",        wxCheckBox)->GetValue());
   cfg->Write(_T("/fill_empty_lines"),     XRCCTRL(*this, "chkFillEmptyLines",     wxCheckBox)->GetValue());
   cfg->Write(_T("/add_brackets"),         XRCCTRL(*this, "chkAddBrackets",        wxCheckBox)->GetValue());
+  cfg->Write(_T("/break_lines"),          XRCCTRL(*this, "chkBreakeLines",        wxCheckBox)->GetValue());
+  cfg->Write(_T("/max_line_length"),      XRCCTRL(*this, "txtMaxLineLegth",       wxTextCtrl)->GetValue());
}
Index: astyleconfigdlg.h
===================================================================
--- astyleconfigdlg.h (revision 9157)
+++ astyleconfigdlg.h (working copy)
@@ -19,6 +19,7 @@
protected:
         void OnStyleChange(wxCommandEvent& event);
         void OnPreview(wxCommandEvent& event);
+        void OnCheckBoxEvt(wxCommandEvent& event);

         virtual wxString GetTitle() const { return _("Source formatter"); }
         virtual wxString GetBitmapBaseName() const { return _T("astyle-plugin"); }
Index: formattersettings.cpp
===================================================================
--- formattersettings.cpp (revision 9157)
+++ formattersettings.cpp (working copy)
@@ -126,4 +126,9 @@
   formatter.setTabSpaceConversionMode(cfg->ReadBool(_T("/convert_tabs")));
   formatter.setEmptyLineFill(cfg->ReadBool(_T("/fill_empty_lines")));
   formatter.setAddBracketsMode(cfg->ReadBool(_T("/add_brackets")));
+
+  if(cfg->ReadBool(_T("/break_lines")))
+    formatter.setMaxCodeLength( wxAtoi(cfg->Read(_T("/max_line_length"))));
+  else
+    formatter.setMaxCodeLength(string::npos);
}
Index: resources/configuration.xrc
===================================================================
--- resources/configuration.xrc (revision 9157)
+++ resources/configuration.xrc (working copy)
@@ -381,6 +381,35 @@
<flag>wxTOP|wxALIGN_LEFT|wxALIGN_TOP</flag>
<border>8</border>
</object>
+ <object class="sizeritem">
+ <object class="wxCheckBox" name="chkBreakeLines">
+ <label>Enable Line breaking</label>
+ </object>
+ <flag>wxTOP|wxALIGN_LEFT|wxALIGN_TOP</flag>
+ <border>8</border>
+ </object>
+ <object class="sizeritem">
+ <object class="wxBoxSizer">
+ <object class="sizeritem">
+ <object class="wxStaticText" name="ID_STATICTEXT1">
+ <label>Break lines after (50-200)</label>
+ </object>
+ <flag>wxTOP|wxALIGN_LEFT|wxALIGN_TOP</flag>
+ <border>4</border>
+ </object>
+ <object class="sizeritem">
+ <object class="wxTextCtrl" name="txtMaxLineLegth">
+ <value>200</value>
+ </object>
+ <flag>wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL</flag>
+ <border>8</border>
+ <option>1</option>
+ </object>
+ </object>
+ <flag>wxALIGN_LEFT|wxALIGN_TOP</flag>
+ <border>8</border>
+ <option>1</option>
+ </object>
</object>
<flag>wxALL|wxEXPAND|wxALIGN_LEFT|wxALIGN_TOP</flag>
<border>8</border>


tested on win7 64bit
not many chages so it should work anywhere. (no boundary checking is performed for max line length value)

greetings

oBFusCATed

Please post the patch at the project's page on berlios.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

MortenMacFly

Quote from: BlueHazzard on June 15, 2013, 02:14:40 PM
i have made a short patch to add this feature:
Thanks for your patch. However, next time please consider to follow or coding guidelines, like format of brackets (i.e. in if-then clauses) and naming of methods: like "OnCheckBoxEvt" does not mean anything; use something as "OnPreview" instead which is easy to understand. This will make is easier for us to integrate proposed functionalities.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: [url="https://www.codeblocks.org/docs/main_codeblocks_en.html"]https://www.codeblocks.org/docs/main_codeblocks_en.html[/url]
C::B FAQ: [url="https://wiki.codeblocks.org/index.php?title=FAQ"]https://wiki.codeblocks.org/index.php?title=FAQ[/url]