Developer forums (C::B DEVELOPMENT STRICTLY!) > CodeCompletion redesign

question on how to trigger evt(cbEVT_COMPLETE_CODE)

<< < (4/5) > >>

ollydbg:
It looks like there are some item sort issue.

I see the CC gives the suggestion list as:

--- Code: ---"
aaa(): int\n13\r
Bbb(): int\n13\r
Gethod6(): void\n13\r
Kethod1(): void\n13\r
Kethod2(): void\n13\r
Method3(): void\n13\r
method4(): void\n13\r
method5(): void\n13
"

--- End code ---
Look, it is case insensitive, but I see in the function void AutoComplete::Select(const char *word), it use this

--- Code: --- if (ignoreCase)
cond = CompareNCaseInsensitive(word, item, lenWord);
else
cond = strncmp(word, item, lenWord);

--- End code ---
The strncmp is used, so it cause errors, in this case, CompareNCaseInsensitive should be used.

In CC, we have a sorter:

--- Code: ---    bool isPureAlphabetical = true;
    TokenSorter sortFunctor(isPureAlphabetical);
    std::sort(m_AutocompTokens.begin(), m_AutocompTokens.end(), sortFunctor);

--- End code ---
But it looks like TokenSorter is always case insensitive.

--- Code: ---// priority, then alphabetical
struct TokenSorter
{
    bool& m_PureAlphabetical;

    TokenSorter(bool& alphabetical) : m_PureAlphabetical(alphabetical)
    {
        m_PureAlphabetical = true;
    }

    bool operator()(const cbCodeCompletionPlugin::CCToken& a, const cbCodeCompletionPlugin::CCToken& b)
    {
        int diff = a.weight - b.weight;
        if (diff == 0)
        {
            // cannot use CmpNoCase() because it compares lower case but Scintilla compares upper
            diff = a.displayName.Upper().Cmp(b.displayName.Upper());
            if (diff == 0)
                diff = a.displayName.Cmp(b.displayName);
        }
        else
            m_PureAlphabetical = false;
        return diff < 0;
    }
};

--- End code ---
So, we should set "case insensitive" in our CC option, otherwise, this bug happens.

ollydbg:
This bug also happens in wx 2.8.12 based CodeBlocks. The method to solve this is to sort the item list depending on the "case sensitive" option of the Editor dialog.

ollydbg:
Here are patches to fix this issue:

--- Code: ---@@ -461,26 +461,31 @@ bool CCManager::ProcessArrow(int key)
 }
 
 // priority, then alphabetical
 struct TokenSorter
 {
-    bool& m_PureAlphabetical;
+    bool& m_PureAlphabetical;  // modify the passed argument(set to false) if weight are different
+    bool m_CaseSensitive;
 
-    TokenSorter(bool& alphabetical) : m_PureAlphabetical(alphabetical)
+    TokenSorter(bool& alphabetical, bool& caseSensitive): m_PureAlphabetical(alphabetical), m_CaseSensitive(caseSensitive)
     {
         m_PureAlphabetical = true;
     }
 
     bool operator()(const cbCodeCompletionPlugin::CCToken& a, const cbCodeCompletionPlugin::CCToken& b)
     {
         int diff = a.weight - b.weight;
         if (diff == 0)
         {
-            // cannot use CmpNoCase() because it compares lower case but Scintilla compares upper
-            diff = a.displayName.Upper().Cmp(b.displayName.Upper());
-            if (diff == 0)
+            if (m_CaseSensitive)
                 diff = a.displayName.Cmp(b.displayName);
+            else
+            {   // cannot use CmpNoCase() because it compares lower case but Scintilla compares upper
+                diff = a.displayName.Upper().Cmp(b.displayName.Upper());
+                if (diff == 0)
+                    diff = a.displayName.Cmp(b.displayName);
+            }
         }
         else
             m_PureAlphabetical = false;
         return diff < 0;
     }
@@ -537,11 +542,12 @@ void CCManager::OnCompleteCode(CodeBlocksEvent& event)
 
         return;
     }
 
     bool isPureAlphabetical = true;
-    TokenSorter sortFunctor(isPureAlphabetical);
+    bool isCaseSensitive = cfg->ReadBool(wxT("/case_sensitive"), false);
+    TokenSorter sortFunctor(isPureAlphabetical, isCaseSensitive);
     std::sort(m_AutocompTokens.begin(), m_AutocompTokens.end(), sortFunctor);
     if (isPureAlphabetical)
         stc->AutoCompSetOrder(wxSCI_ORDER_PRESORTED);
     else
         stc->AutoCompSetOrder(wxSCI_ORDER_CUSTOM);
@@ -559,11 +565,11 @@ void CCManager::OnCompleteCode(CodeBlocksEvent& event)
     items.RemoveLast();
 
     if (!stc->CallTipActive() && !stc->AutoCompActive())
         m_CallTipActive = wxSCI_INVALID_POSITION;
 
-    stc->AutoCompSetIgnoreCase(!cfg->ReadBool(wxT("/case_sensitive"), false));
+    stc->AutoCompSetIgnoreCase(!isCaseSensitive);
     stc->AutoCompSetMaxHeight(14);
     stc->AutoCompSetTypeSeparator(wxT('\n'));
     stc->AutoCompSetSeparator(wxT('\r'));
     Manager::Get()->GetLogManager()->DebugLog(F(_T("CCManager::OnCompleteCode(): before calling stc->AutoCompShow()")));
     stc->AutoCompShow(tknEnd - tknStart, items);



--- End code ---
This obeys the "case_sensitive" option setting by the user.

One question: why the m_PureAlphabetical = false, when weights are different? I think is if this is false, we can't call the Select function later, as it is not purely sorted by alphabetical.

Alpha:

--- Code: ---    bool isPureAlphabetical = true;
    TokenSorter sortFunctor(isPureAlphabetical);
    std::sort(m_AutocompTokens.begin(), m_AutocompTokens.end(), sortFunctor);
    if (isPureAlphabetical)
        stc->AutoCompSetOrder(wxSCI_ORDER_PRESORTED);
    else
        stc->AutoCompSetOrder(wxSCI_ORDER_CUSTOM);

--- End code ---
wxSCI_ORDER_CUSTOM tells scintilla to do some extra processing and maintain a search index internally so that we can supply an arbitrary order.  I suppose the cost for this extra sort is not very large compared to initially computing what tokens to even suggest, so maybe it makes sense to simplify and just always specify wxSCI_ORDER_CUSTOM.


--- Quote from: ollydbg on September 03, 2017, 01:00:00 am ---
--- Code: ----    TokenSorter(bool& alphabetical) : m_PureAlphabetical(alphabetical)
+    TokenSorter(bool& alphabetical, bool& caseSensitive): m_PureAlphabetical(alphabetical), m_CaseSensitive(caseSensitive)

--- End code ---

--- End quote ---
Probably pass caseSensitive by value.

ollydbg:

--- Quote from: Alpha on September 03, 2017, 06:24:50 pm ---
--- Quote from: ollydbg on September 03, 2017, 01:00:00 am ---
--- Code: ----    TokenSorter(bool& alphabetical) : m_PureAlphabetical(alphabetical)
+    TokenSorter(bool& alphabetical, bool& caseSensitive): m_PureAlphabetical(alphabetical), m_CaseSensitive(caseSensitive)

--- End code ---

--- End quote ---
Probably pass caseSensitive by value.

--- End quote ---
Thanks, I committed in r11157.


--- Quote ---wxSCI_ORDER_CUSTOM tells scintilla to do some extra processing and maintain a search index internally...
--- End quote ---
I may need some test later. Thanks.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version