Author Topic: CC scope  (Read 16704 times)

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: CC scope
« Reply #15 on: November 19, 2012, 11:08:27 pm »
1, if you remove the whole "{" block, you will remove all the EOL characters "\n", so after parsing the buffer, you variable location (line information) may be changed.
I never thought of that... I will see what I can do.

2, if you remove the "{" block directly, which means:
The posted patch already deals with this.
Code
+            buffer.Prepend(searchData->control->GetTextRange(i, scanPos));
+            scanPos = scopeStart + 1;
i is the position of a "}" brace, and the + 1 includes the matching "{" brace in scanPos, so
Code
for (...)
{
     int a;
}
currently becomes
Code
for (...)
{}

Yes, this patch did conflict with the one for parsing of for loops. But sorry those days I'm a little busy, so I have no time to deeply test and merge those two patches. :(
...maybe ALPHA can do it - what I meant is this:
http://developer.berlios.de/patch/?func=detailpatch&patch_id=3345&group_id=5358
Have not looked at that patch yet, but if it is not too complicated, I will merge them.

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: CC scope
« Reply #16 on: November 20, 2012, 11:36:19 pm »
1, if you remove the whole "{" block, you will remove all the EOL characters "\n", so after parsing the buffer, you variable location (line information) may be changed.
I never thought of that... I will see what I can do.
This should deal with it:
Code
Index: src/plugins/codecompletion/nativeparser.cpp
===================================================================
--- src/plugins/codecompletion/nativeparser.cpp (revision 8585)
+++ src/plugins/codecompletion/nativeparser.cpp (working copy)
@@ -1857,7 +1857,33 @@
         if (blockStart >= blockEnd)
             blockStart = blockEnd;
 
-        wxString buffer = searchData->control->GetTextRange(blockStart, blockEnd);
+        wxString buffer; // = searchData->control->GetTextRange(blockStart, blockEnd);
+        // condense out-of-scope braces {...}
+        int scanPos = blockEnd;
+        for (int curPos = pos; curPos > blockStart; --curPos)
+        {
+            if (searchData->control->GetCharAt(curPos) != wxT('}'))
+                continue;
+            const int style = searchData->control->GetStyleAt(curPos);
+            if (   searchData->control->IsString(style)
+                || searchData->control->IsCharacter(style)
+                || searchData->control->IsComment(style))
+            {
+                continue;
+            }
+            const int scopeStart = searchData->control->BraceMatch(curPos);
+            if (scopeStart < blockStart)
+                break;
+            buffer.Prepend(searchData->control->GetTextRange(curPos, scanPos));
+            const int startLn = searchData->control->LineFromPosition(scopeStart);
+            const int endLn   = searchData->control->LineFromPosition(curPos);
+            if (startLn < endLn) // maintain correct line numbers for parsed tokens
+                buffer.Prepend( wxString(wxT('\n'), endLn - startLn) );
+            scanPos = scopeStart + 1;
+            curPos  = scopeStart;
+        }
+        buffer.Prepend(searchData->control->GetTextRange(blockStart, scanPos));
+
         buffer.Trim();
         if (   !buffer.IsEmpty()
             && !m_Parser->ParseBuffer(buffer, false, false, true, searchData->file, m_LastFuncTokenIdx, initLine) )

Yes, this patch did conflict with the one for parsing of for loops. But sorry those days I'm a little busy, so I have no time to deeply test and merge those two patches. :(
...maybe ALPHA can do it - what I meant is this:
http://developer.berlios.de/patch/?func=detailpatch&patch_id=3345&group_id=5358
Have not looked at that patch yet, but if it is not too complicated, I will merge them.
The only conflict I can see (are there other conflicts I have missed?) is that:
Code
int main()
{
    for (int var_it = 0; var_it < 5; ++var_it)
    {
        int fooBar = var_it;
    }
    // "fooBar" correctly does not exist here (with the above patch)
    // but "var_it" does exist (from loop declaration patch)
    return 0;
}
When modifying buffer, I think the proper way to condense for loop arguments is:
Code
for (int var_it = 0; var_it < 5; ++var_it)
{
    // stuff...
}
to:
Code
for (;;)
{

}
However, I am not certain about the other cases that the loop declaration patch handles; any suggestions?

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: CC scope
« Reply #17 on: November 22, 2012, 04:48:57 am »
Submitted patch 3375.  It should address all issues discussed here.