Skip to content

Commit c788f2f

Browse files
committed
Fix unrequested codecompletion menu: When asking for auto-completion, a background task starts, and then the insertion-point changes by the user (probably he doesn't want to wait for the CC results), the codecompletion menu still apeared at a (possibly) undesired location. Should fix Issue#5
1 parent b93bf17 commit c788f2f

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

PythonCodeCompletion/PythonCodeCompletion.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ END_EVENT_TABLE()
4646
// constructor
4747
PythonCodeCompletion::PythonCodeCompletion() : m_ActiveCalltipDef(_(""))
4848
{
49+
m_comp_position.line = 0;
50+
m_comp_position.column = 0;
4951
// Make sure our resources are available.
5052
// In the generated boilerplate code we have no resources but when
5153
// we add some, it will be nice that this code is in place already ;)
@@ -318,9 +320,29 @@ PythonCodeCompletion::CCProviderStatus PythonCodeCompletion::GetProviderStatusFo
318320
std::vector<PythonCodeCompletion::CCToken> PythonCodeCompletion::GetAutocompList(bool isAuto, cbEditor* ed, int& tknStart, int& tknEnd)
319321
{
320322
Manager::Get()->GetLogManager()->DebugLog(_("PYCC: GetAutocompList called"));
323+
324+
cbStyledTextCtrl* control = ed->GetControl();
325+
const int line = control->LineFromPosition(tknStart);
326+
const int lnStart = control->PositionFromLine(line);
327+
int column = tknStart - lnStart;
328+
for (; column > 0; --column)
329+
{
330+
if ( !wxIsspace(control->GetCharAt(lnStart + column - 1))
331+
|| (column != 1 && !wxIsspace(control->GetCharAt(lnStart + column - 2))) )
332+
{
333+
break;
334+
}
335+
}
336+
321337
std::vector<CCToken> tokens;
322338
if (m_state == STATE_COMPLETION_RETURNED)
323339
{
340+
if ((m_comp_position.line != line)||(m_comp_position.column != column))
341+
{
342+
Manager::Get()->GetLogManager()->DebugLog( _("PYCC: Position has changed since last CC request") );
343+
m_state=STATE_NONE;
344+
return tokens;
345+
}
324346
for (int i = 0; i<m_comp_results.Count(); ++i)
325347
{
326348
long int category=-1;
@@ -329,7 +351,6 @@ std::vector<PythonCodeCompletion::CCToken> PythonCodeCompletion::GetAutocompList
329351
PythonCodeCompletion::CCToken t(i,m_comp_results[i].BeforeFirst('?'),category);
330352
tokens.push_back(t);
331353
}
332-
cbStyledTextCtrl* control = ed->GetControl();
333354
control->ClearRegisteredImages();
334355
for (int i = 0; i < m_pImageList->GetImageCount(); i++)
335356
control->RegisterImage(i+1,m_pImageList->GetBitmap(i));
@@ -341,7 +362,6 @@ std::vector<PythonCodeCompletion::CCToken> PythonCodeCompletion::GetAutocompList
341362
// if ( (!ed->AutoCompActive()) // not already active autocompletion
342363
// || (ch == _T('.')))
343364
Manager::Get()->GetLogManager()->DebugLog(_("PYCC: Checking lexical state..."));
344-
cbStyledTextCtrl *control=ed->GetControl();
345365
int pos = tknEnd;
346366
int style = control->GetStyleAt(pos);
347367
wxChar ch = control->GetCharAt(pos);
@@ -357,6 +377,8 @@ std::vector<PythonCodeCompletion::CCToken> PythonCodeCompletion::GetAutocompList
357377
wxString phrase=control->GetTextRange(tknStart,tknEnd);
358378
Manager::Get()->GetLogManager()->DebugLog(_T("PYCC: Looking for ")+phrase+_T(" in ")+ed->GetFilename()+wxString::Format(_T(" %i"),pos));
359379
m_state = STATE_COMPLETION_REQUEST;
380+
m_comp_position.line = line;
381+
m_comp_position.column = column;
360382
RequestCompletion(control,pos,ed->GetFilename());
361383
return tokens;
362384
}

PythonCodeCompletion/PythonCodeCompletion.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ class PythonCodeCompletion : public cbCodeCompletionPlugin
154154
wxImageList* m_pImageList; //Type icons displayed in the code completion popup
155155
CCCallTip m_ActiveCalltipDef; //contains the call tip definition retrieved from the server
156156
wxArrayString m_comp_results; //contains an array of completion results retrieved from the server
157-
157+
struct {
158+
int line;
159+
int column;
160+
} m_comp_position;
158161
DECLARE_EVENT_TABLE();
159162
};
160163

0 commit comments

Comments
 (0)