Author Topic: Source formatter (line breaks)  (Read 9208 times)

Offline The_Immortal

  • Single posting newcomer
  • *
  • Posts: 3
Source formatter (line breaks)
« 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:



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


Could you help me please?



Thank you!
« Last Edit: June 06, 2013, 09:49:35 pm by The_Immortal »

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12810
    • Travis build status
Re: Source formatter (line breaks)
« Reply #1 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
(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!]

Offline The_Immortal

  • Single posting newcomer
  • *
  • Posts: 3
Re: Source formatter (line breaks)
« Reply #2 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!

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12810
    • Travis build status
Re: Source formatter (line breaks)
« Reply #3 on: June 06, 2013, 11:48:09 pm »
Maybe you I have something regarding this?
No
(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!]

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: Source formatter (line breaks)
« Reply #4 on: June 07, 2013, 05:32:27 am »
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?).

Offline The_Immortal

  • Single posting newcomer
  • *
  • Posts: 3
Re: Source formatter (line breaks)
« Reply #5 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!

Offline stahta01

  • Lives here!
  • ****
  • Posts: 6995
    • My Best Post
Re: Source formatter (line breaks)
« Reply #6 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

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.
« Last Edit: June 15, 2013, 01:47:19 am by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2833
Re: Source formatter (line breaks)
« Reply #7 on: June 15, 2013, 02:14:40 pm »
hello,
i have made a short patch to add this feature:

Code: [Select]
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

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12810
    • Travis build status
Re: Source formatter (line breaks)
« Reply #8 on: June 16, 2013, 09:37:43 pm »
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!]

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9597
Re: Source formatter (line breaks)
« Reply #9 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.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: http://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: http://wiki.codeblocks.org/index.php?title=FAQ