Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: The_Immortal on June 06, 2013, 09:45:40 pm

Title: Source formatter (line breaks)
Post by: The_Immortal on June 06, 2013, 09:45:40 pm
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:

(http://content.screencast.com/users/The_Immortal/folders/Snagit/media/6febc6ba-84c8-407b-bebe-f10216a43902/06.06.2013-23.43.png)

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


Could you help me please?



Thank you!
Title: Re: Source formatter (line breaks)
Post by: oBFusCATed on June 06, 2013, 11:00:39 pm
Could you help me please?
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
Title: Re: Source formatter (line breaks)
Post by: The_Immortal on June 06, 2013, 11:11:30 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!
Title: Re: Source formatter (line breaks)
Post by: oBFusCATed on June 06, 2013, 11:48:09 pm
Maybe you I have something regarding this?
No
Title: Re: Source formatter (line breaks)
Post by: Alpha on June 07, 2013, 05:32:27 am
Maybe you I have something regarding this?
No
Well, possibly.  The last version of astyle (http://astyle.sourceforge.net/notes.html) added this ability.  However, I do not believe the feature has yet made it into Code::Blocks' formatter plugin (or has it?).
Title: Re: Source formatter (line breaks)
Post by: 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!
Title: Re: Source formatter (line breaks)
Post by: stahta01 on June 15, 2013, 01:22:59 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 (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/ (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.
Title: Re: Source formatter (line breaks)
Post by: BlueHazzard on June 15, 2013, 02:14:40 pm
hello,
i have made a short patch to add this feature:

Code
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
Title: Re: Source formatter (line breaks)
Post by: oBFusCATed on June 16, 2013, 09:37:43 pm
Please post the patch at the project's page on berlios.
Title: Re: Source formatter (line breaks)
Post by: MortenMacFly on June 17, 2013, 07:07:41 am
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.