Code::Blocks Forums

Developer forums (C::B DEVELOPMENT STRICTLY!) => Development => Topic started by: Loaden on March 28, 2010, 02:36:46 PM

Title: Patch, AStyle plug-in support format only selected line
Post by: Loaden on March 28, 2010, 02:36:46 PM
Hello everyone!
I make a patch for AStyle plugin, and now, it's can be format only selected line.
And the other code can keep the original format.

Index: src/plugins/astyle/astyleplugin.cpp

===================================================================

--- src/plugins/astyle/astyleplugin.cpp (revision 6196)

+++ src/plugins/astyle/astyleplugin.cpp (working copy)

@@ -102,7 +102,7 @@

    {
        case mtEditorManager:
            menu->AppendSeparator();
-            menu->Append( idCodeFormatterActiveFile, _( "Format this file (AStyle)" ), _( "Format the source code in the current file" ) );
+            menu->Append( idCodeFormatterActiveFile, _( "Format use AStyle" ), _( "Format the selected source code (selected line) in the current file" ) );
            break;

        case mtProjectManager:
@@ -290,14 +290,68 @@


bool AStylePlugin::FormatEditor( cbEditor *ed )
{
-    if (ed->GetControl()->GetReadOnly())
+    cbStyledTextCtrl* control = ed->GetControl();
+    if (control->GetReadOnly())
    {
        cbMessageBox(_("The file is read-only!"), _("Error"), wxICON_ERROR);
        return false;
    }

-    wxString edText(ed->GetControl()->GetText());
+    bool onlySelected = false;
+    wxString edText;
+    wxString selText;
+    int leftBracesNumber = 0;
+    const int pos = control->GetCurrentPos();
+    int start = control->GetSelectionStart();
+    int end = control->GetSelectionEnd();
+    wxString fromWord;
+    if (start != end)
+    {
+        onlySelected = true;
+        start = control->GetLineIndentPosition(control->LineFromPosition(start));
+        control->GotoPos(start);
+        control->Home();
+        start = control->GetCurrentPos();
+        control->SetSelectionStart(start);
+        end = control->GetLineEndPosition(control->LineFromPosition(end));
+        control->SetSelectionEnd(end);
+        selText = control->GetTextRange(start, end);

+        wxChar ch;
+        int findBracesPos = start;
+        while (--findBracesPos > 0 )
+        {
+            ch = control->GetCharAt(findBracesPos);
+            int style = control->GetStyleAt(findBracesPos);
+            int lexer = control->GetLexer();
+            if (lexer == wxSCI_LEX_CPP)
+            {
+                if (style == wxSCI_C_COMMENT || style == wxSCI_C_COMMENTDOC
+                    || style == wxSCI_C_COMMENTDOCKEYWORD || style == wxSCI_C_COMMENTDOCKEYWORDERROR
+                    || style == wxSCI_C_COMMENTLINE || style == wxSCI_C_COMMENTLINEDOC
+                    || style == wxSCI_C_STRING || style == wxSCI_C_CHARACTER)
+                    continue;
+            }
+            else if (lexer == wxSCI_LEX_D)
+            {
+                if (style == wxSCI_D_COMMENT || style == wxSCI_D_COMMENTDOC
+                    || style == wxSCI_D_COMMENTDOCKEYWORD || style == wxSCI_D_COMMENTDOCKEYWORDERROR
+                    || style == wxSCI_D_COMMENTLINE || style == wxSCI_D_COMMENTLINEDOC
+                    || style == wxSCI_D_STRING || style == wxSCI_D_CHARACTER)
+                    continue;
+            }
+
+            if (ch == _T('}')) --leftBracesNumber;
+            else if (ch == _T('{')) ++leftBracesNumber;
+        }
+
+        for (int i = leftBracesNumber; i > 0; --i)
+            edText.Append(_T('{'));
+        edText.Append(selText);
+    }
+    else
+        edText = control->GetText();
+
    wxString formattedText;

    astyle::ASFormatter formatter;
@@ -308,9 +362,8 @@


    wxString eolChars;

-    switch (ed->GetControl()->GetEOLMode())
+    switch (control->GetEOLMode())
    {
-
        case wxSCI_EOL_CRLF:
            eolChars = _T("\r\n");
            break;
@@ -324,7 +377,7 @@

            break;
    }

-    if (edText.size() && edText.Last() != _T('\r') && edText.Last() != _T('\n'))
+    if (edText.size() && edText.Last() != _T('\r') && edText.Last() != _T('\n') && !onlySelected)
    {
        edText += eolChars;
    }
@@ -338,15 +391,17 @@

    std::vector<int> ed_breakpoints;

    // hack: we need to evaluate the special case of having a bookmark in the first line here
-
-    if (ed->HasBookmark(0))
+    if (!onlySelected)
    {
-        new_bookmark.push_back(0);
+        if (ed->HasBookmark(0))
+        {
+            new_bookmark.push_back(0);
+        }
+        if (ed->HasBreakpoint(0))
+        {
+            ed_breakpoints.push_back(0);
+        }
    }
-    if (ed->HasBreakpoint(0))
-    {
-        ed_breakpoints.push_back(0);
-    }

    wxSetCursor(*wxHOURGLASS_CURSOR);

@@ -373,14 +428,25 @@

        }
    }

-    int pos = ed->GetControl()->GetCurrentPos();
+    if (onlySelected && leftBracesNumber > 0)
+    {
+        while (leftBracesNumber > 0)
+        {
+            --leftBracesNumber;
+            formattedText = formattedText.SubString(formattedText.Find(_T('{')) + 1, formattedText.Length());
+        }
+        formattedText = formattedText.SubString(formattedText.Find(eolChars) + eolChars.Length(), formattedText.Length());
+    }

-    bool changed = BuffersDiffer( formattedText, edText );
+    bool changed = BuffersDiffer( formattedText, !onlySelected ? edText : selText);

    if ( changed )
    {
-        ed->GetControl()->BeginUndoAction();
-        ed->GetControl()->SetText(formattedText);
+        control->BeginUndoAction();
+        if (!onlySelected)
+            control->SetText(formattedText);
+        else
+            control->ReplaceSelection(formattedText);

        for (std::vector<int>::const_iterator i = new_bookmark.begin(); i != new_bookmark.end(); ++i)
        {
@@ -392,8 +458,8 @@

            ed->ToggleBreakpoint(*i);
        }

-        ed->GetControl()->EndUndoAction();
-        ed->GetControl()->GotoPos(pos);
+        control->EndUndoAction();
+        control->GotoPos(pos);
        ed->SetModified(true);
    }


Welcome to test! :lol:

EDIT: fix bug when leftBracesNumber == 0
   if (onlySelected)
   {
       while (leftBracesNumber > 0)
       {
           --leftBracesNumber;
           formattedText = formattedText.SubString(formattedText.Find(_T('{')) + 1, formattedText.Length());
       }
       formattedText = formattedText.SubString(formattedText.Find(eolChars) + eolChars.Length(), formattedText.Length());
   }


Need Change FROM:
if (onlySelected)
TO:
if (onlySelected && leftBracesNumber > 0)

[attachment deleted by admin]
Title: Re: Patch, AStyle plug-in support format only selected line
Post by: ollydbg on March 28, 2010, 03:04:59 PM
Nice work!!!!! :D
Title: Re: Patch, AStyle plug-in support format only selected line
Post by: Loaden on March 29, 2010, 01:50:19 AM
Quote from: ollydbg on March 28, 2010, 03:04:59 PM
Nice work!!!!! :D
I found a bug, and fixed now.  :(