News:

When registered with our forums, feel free to send a "here I am" post here to differ human beings from SPAM bots.

Main Menu

about bracecompletion

Started by blueshake, August 17, 2009, 05:28:44 AM

Previous topic - Next topic

blueshake

hello, jens:
refer to visual assist,I do a fes modifications for bracecompletion.
two modificactions:
1. the { } will stay in the same line now if current line have other words.
2. if the current caret postion's next char is not empty,the bracecompletion will not work.
here is the patch.
Index: cbeditor.cpp
===================================================================
--- cbeditor.cpp (revision 5731)
+++ cbeditor.cpp (working copy)
@@ -331,19 +331,26 @@
         }
         if ( IsCharacterOrString(style) )
             return;
-        wxString leftBrace(_T("([{"));
-        wxString rightBrace(_T(")]}"));
+        const wxString leftBrace(_T("([{"));
+        const wxString rightBrace(_T(")]}"));
         int index = leftBrace.find(ch);
-        if (index != wxNOT_FOUND)
+        //Manager::Get()->GetLogManager()->DebugLog(F(_T("current: %d"),control->GetCharAt(pos)));
+        //Manager::Get()->GetLogManager()->DebugLog(_T("current:")+control->GetCharAt(pos));
+        const wxString unWant(_T("\n\r\t\b "));
+        if (index != wxNOT_FOUND && unWant.find(control->GetCharAt(pos)) != wxNOT_FOUND)
         {
-            control->AddText(rightBrace.GetChar(index));
-            control->GotoPos(pos);
-            if (ch == _T('{'))
-            {
-                control->NewLine();
+                control->AddText(rightBrace.GetChar(index));
                 control->GotoPos(pos);
-                return;
-            }
+                if (ch == _T('{'))
+                {
+                    const wxRegEx reg(_T("^[ \t]*{}[ \t]*"));
+                    if (reg.Matches(control->GetCurLine()))
+                    {
+                        control->NewLine();
+                        control->GotoPos(pos);
+                        return;
+                    }
+                }
         }
         else
         {
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

killerbot

could you give some examples of the improvements [before/after] ?

mariocup

Hi blueshake,

Quote from: blueshake on August 17, 2009, 05:28:44 AM
2. if the current caret postion's next char is not empty,the bracecompletion will not work.
here is the patch.

I agree that the bracecompletion should not work if the next char is not empty. For example if you have a statement like


if (variable1 && variable2)


and now modify the condition


if (variable1 && (


then I do not want to add instantly the closing bracket, since this is not the desired place.

killerbot

but what if you have the following
if(test1)

and you want to add something like

&& (test2 || test3)

to obtain

if(test1 && (test2 || test3))

then I want the "()"

mariocup

Hi killerbot,

this is desirable too, but not a contradiction if we modify the condition for adding braces.

For example if the next char is a closing brace and you add a new "(" then "()" can be inserted, but in a case like


if (test && "(" test2)


the closing brace should not be added automatically in my eyes, since the user will have to delete the "unwanted" closing brace and this can be very annoying after some time and will not increase the productivity.


blueshake

#5
I think mariocup is right.
so these codes can be changed to be so.add a ) will work.
const wxString unWant(_T(")\n\r\t\b "));
see my attachment.
snap1 is new one , the {} will stay in the some line.
snap3 is old one, the {} will stay in twol line.
snap2 is example ,the next char is m ,so bracecompletion will not work.it will work if next one is  enter whitesapce , tab or ) (new added).

[attachment deleted by admin]
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

killerbot

and what will happen in your code when :


int main
{

Will the closing '}' end up on the next line ? I think it should.

blueshake

yes,it will work like below.
int main
{
}


hi, killerbot, have any plan to on this patch.
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

killerbot

I just applied it to my local copy, and will test it out.

blueshake

nice to hear that ,
here is my new patch.
the opening { work like this now ,it is more like what visual assist do.
see the attachment.
Index: cbeditor.cpp
===================================================================
--- cbeditor.cpp (revision 5731)
+++ cbeditor.cpp (working copy)
@@ -331,19 +331,25 @@
         }
         if ( IsCharacterOrString(style) )
             return;
-        wxString leftBrace(_T("([{"));
-        wxString rightBrace(_T(")]}"));
+        const wxString leftBrace(_T("([{"));
+        const wxString rightBrace(_T(")]}"));
         int index = leftBrace.find(ch);
-        if (index != wxNOT_FOUND)
+        const wxString unWant(_T(")\n\r\t\b "));
+        if (index != wxNOT_FOUND && unWant.find(control->GetCharAt(pos)) != wxNOT_FOUND)
         {
-            control->AddText(rightBrace.GetChar(index));
-            control->GotoPos(pos);
-            if (ch == _T('{'))
-            {
-                control->NewLine();
+                control->AddText(rightBrace.GetChar(index));
                 control->GotoPos(pos);
-                return;
-            }
+                if (ch == _T('{'))
+                {
+                    const wxRegEx reg(_T("^[ \t]*{}[ \t]*"));
+                    if (reg.Matches(control->GetCurLine()))
+                    {
+                        control->NewLine();
+                        control->GotoPos(pos);
+                        control->NewLine();
+                        return;
+                    }
+                }
         }
         else
         {



[attachment deleted by admin]
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

killerbot

updated my copy with your change.

I already have a first point of feedback :

the following no longer works :
if( --> if()
then I change this into:
if(true &&)
All is still OK, but now it goes wrong
if(true && ()
--> I don't get the closing one !!! And in this case I do want it.

blueshake

hi,killerbot,it work for me,
see my attachment.
when i type if( and closing ) will automaticlly add.
if(true && |)
in the "|" positon ,i type ( and it work too.
can you just give me more informations?

[attachment deleted by admin]
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

killerbot

I just tried it again, and again I end up with :
if(true && ()

could you copy paste here the codesnippet you have of the method you changed, so not as a patch, but just plain code. Maybe I didn't apply your patch correctly ...

blueshake

hi,killerbot,i paste the the codes,and upload the file as a attachment.

    void DoBraceCompletion(const wxChar& ch)
    {
        cbStyledTextCtrl* control = m_pOwner->GetControl();
        int pos = control->GetCurrentPos();
        int style = control->GetStyleAt(pos);
        if ( IsComment(style) || IsPreprocessor(style) )
            return;
        if ( ch == _T('\'') )
        {
            if ( control->GetCharAt(pos) == ch && pos > 1 && control->GetCharAt(pos-2) != _T('\\') )
            {
                control->DeleteBack();
                control->GotoPos(pos);
            }
            else
            {
                if ( control->GetCharAt(pos-2) == _T('\\') || IsCharacterOrString(style) )
                    return;
                control->AddText(ch);
                control->GotoPos(pos);
            }
            return;
        }
        if ( ch == _T('"') )
        {
            if (control->GetCharAt(pos) == ch && pos > 1 && control->GetCharAt(pos-2) != _T('\\') )
            {
                control->DeleteBack();
                control->GotoPos(pos);
            }
            else
            {
                if ( control->GetCharAt(pos-2) == _T('\\') || IsCharacter(style) )
                    return;
                control->AddText(ch);
                control->GotoPos(pos);
            }
            return;
        }
        if ( IsCharacterOrString(style) )
            return;
        const wxString leftBrace(_T("([{"));
        const wxString rightBrace(_T(")]}"));
        int index = leftBrace.find(ch);
        const wxString unWant(_T(")\n\r\t\b "));
        if (index != wxNOT_FOUND && unWant.find(control->GetCharAt(pos)) != wxNOT_FOUND)
        {
                control->AddText(rightBrace.GetChar(index));
                control->GotoPos(pos);
                if (ch == _T('{'))
                {
                    const wxRegEx reg(_T("^[ \t]*{}[ \t]*"));
                    if (reg.Matches(control->GetCurLine()))
                    {
                        control->NewLine();
                        control->GotoPos(pos);
                        control->NewLine();
                        return;
                    }
                }
        }
        else
        {
            index = rightBrace.find(ch);
            if (index != wxNOT_FOUND)
            {
                if (control->GetCharAt(pos) == ch)
                {
                    control->DeleteBack();
                    control->GotoPos(pos);
                    return;
                }
            }
        }
    }


[attachment deleted by admin]
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

killerbot

I can confirm it works now.
I didn't have the ')' in :
const wxString unWant(_T(")\n\r\t\b "));   :oops: