Skip to content

Commit a749f13

Browse files
committed
PythonCodeCompletion: use codecompletion event sinks for calltips and completions
1 parent 611adfd commit a749f13

2 files changed

Lines changed: 67 additions & 15 deletions

File tree

‎PythonCodeCompletion/PythonCodeCompletion.cpp‎

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ void PythonCodeCompletion::OnAttach()
8686
EditorHooks::HookFunctorBase* myhook = new EditorHooks::HookFunctor<PythonCodeCompletion>(this, &PythonCodeCompletion::EditorEventHook);
8787
m_EditorHookId = EditorHooks::RegisterHook(myhook);
8888

89+
// register event sinks
90+
Manager* pm = Manager::Get();
91+
pm->RegisterEventSink(cbEVT_COMPLETE_CODE, new cbEventFunctor<PythonCodeCompletion, CodeBlocksEvent>(this, &PythonCodeCompletion::CompleteCodeEvt));
92+
pm->RegisterEventSink(cbEVT_SHOW_CALL_TIP, new cbEventFunctor<PythonCodeCompletion, CodeBlocksEvent>(this, &PythonCodeCompletion::ShowCallTipEvt));
93+
8994
ConfigManager *mgr=Manager::Get()->GetConfigManager(_T("PythonCC"));
9095

9196
// static wxString GetHomeFolder() { return home_folder; }
@@ -457,6 +462,60 @@ void PythonCodeCompletion::ShowCallTip()
457462
ed->GetControl()->CallTipSetHighlight(start, end);
458463
}
459464

465+
void PythonCodeCompletion::RequestCompletion(cbStyledTextCtrl *control, int pos, const wxString &filename)
466+
{
467+
XmlRpc::XmlRpcValue value;
468+
value.setSize(3);
469+
value[0] = filename.utf8_str(); //mb_str(wxConvUTF8);
470+
value[1] = control->GetText().utf8_str(); //mb_str(wxConvUTF8);
471+
value[2] = pos;
472+
py_server->ExecAsync(_T("complete_phrase"),value,this,ID_COMPLETE_PHRASE);
473+
}
474+
475+
void PythonCodeCompletion::RequestCallTip(cbStyledTextCtrl *control, int pos, const wxString &filename)
476+
{
477+
XmlRpc::XmlRpcValue value;
478+
value.setSize(3);
479+
value[0] = filename.utf8_str(); //mb_str(wxConvUTF8);
480+
value[1] = control->GetText().utf8_str(); //mb_str(wxConvUTF8);
481+
value[2] = pos;
482+
py_server->ExecAsync(_T("complete_tip"),value,this,ID_CALLTIP);
483+
Manager::Get()->GetLogManager()->Log(_T("PYCC: Started server request"));
484+
}
485+
486+
void PythonCodeCompletion::RequestDocString(cbStyledTextCtrl *control, int pos, const wxString &filename)
487+
{
488+
489+
}
490+
491+
void PythonCodeCompletion::CompleteCodeEvt(CodeBlocksEvent& event)
492+
{
493+
event.Skip();
494+
cbEditor *ed = Manager::Get()->GetEditorManager()->GetBuiltinActiveEditor();
495+
if(!ed)
496+
return;
497+
cbStyledTextCtrl *control = ed->GetControl();
498+
if(control->GetLexer()!=wxSCI_LEX_PYTHON)
499+
return;
500+
int pos = control->GetCurrentPos();
501+
wxString filename = ed->GetFilename();
502+
RequestCompletion(control, pos, filename);
503+
}
504+
505+
void PythonCodeCompletion::ShowCallTipEvt(CodeBlocksEvent& event)
506+
{
507+
event.Skip();
508+
cbEditor *ed = Manager::Get()->GetEditorManager()->GetBuiltinActiveEditor();
509+
if(!ed)
510+
return;
511+
cbStyledTextCtrl *control = ed->GetControl();
512+
if(control->GetLexer()!=wxSCI_LEX_PYTHON)
513+
return;
514+
int pos = control->GetCurrentPos();
515+
wxString filename = ed->GetFilename();
516+
RequestCallTip(control, pos, filename);
517+
}
518+
460519
void PythonCodeCompletion::EditorEventHook(cbEditor* editor, wxScintillaEvent& event)
461520
{
462521
if (!IsAttached())
@@ -503,8 +562,6 @@ void PythonCodeCompletion::EditorEventHook(cbEditor* editor, wxScintillaEvent& e
503562
bool m_CCAutoLaunch = true;
504563
int m_CCAutoLaunchChars = 2;
505564
const bool autoCC = m_CCAutoLaunch && (pos - wordStartPos >= m_CCAutoLaunchChars);
506-
wxString t=autoCC ? _("true") : _("false");
507-
Manager::Get()->GetLogManager()->Log(_("autoCC is ")+t);
508565

509566
// update calltip highlight while we type
510567
if (control->CallTipActive())
@@ -533,13 +590,8 @@ void PythonCodeCompletion::EditorEventHook(cbEditor* editor, wxScintillaEvent& e
533590
return;
534591
}
535592
wxString phrase=control->GetTextRange(wordStartPos,pos);
536-
XmlRpc::XmlRpcValue value;
537-
value.setSize(3);
538-
value[0] = editor->GetFilename().utf8_str(); //mb_str(wxConvUTF8);
539-
value[1] = control->GetText().utf8_str(); //mb_str(wxConvUTF8);
540-
value[2] = pos;
541593
Manager::Get()->GetLogManager()->Log(_T("PYCC: Looking for ")+phrase+_T(" in ")+editor->GetFilename()+wxString::Format(_T(" %i"),pos));
542-
py_server->ExecAsync(_T("complete_phrase"),value,this,ID_COMPLETE_PHRASE);
594+
RequestCompletion(control,pos,editor->GetFilename());
543595
event.Skip();
544596
return;
545597
}
@@ -622,13 +674,7 @@ void PythonCodeCompletion::EditorEventHook(cbEditor* editor, wxScintillaEvent& e
622674
symbol.Replace(_T("\t"),wxEmptyString);
623675
m_ActiveSymbol=symbol;
624676
Manager::Get()->GetLogManager()->Log(_T("PYCC: Found calltip symbol ")+symbol);
625-
XmlRpc::XmlRpcValue value;
626-
value.setSize(3);
627-
value[0] = editor->GetFilename().utf8_str(); //mb_str(wxConvUTF8);
628-
value[1] = control->GetText().utf8_str(); //mb_str(wxConvUTF8);
629-
value[2] = end_pos;
630-
py_server->ExecAsync(_T("complete_tip"),value,this,ID_CALLTIP);
631-
Manager::Get()->GetLogManager()->Log(_T("PYCC: Started server request"));
677+
RequestCallTip(control, end_pos, editor->GetFilename());
632678
}
633679
// allow others to handle this event
634680
event.Skip();

‎PythonCodeCompletion/PythonCodeCompletion.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ class PythonCodeCompletion : public cbCodeCompletionPlugin
133133
void OnClickedGotoDeclaration(wxCommandEvent& event);
134134
void OnGotoDeclaration(XmlRpcResponseEvent &event);
135135

136+
void RequestCompletion(cbStyledTextCtrl *control, int pos, const wxString &filename);
137+
void RequestCallTip(cbStyledTextCtrl *control, int pos, const wxString &filename);
138+
void RequestDocString(cbStyledTextCtrl *control, int pos, const wxString &filename);
139+
void CompleteCodeEvt(CodeBlocksEvent& event);
140+
void ShowCallTipEvt(CodeBlocksEvent& event);
141+
136142
private:
137143
wxString GetExtraFile(const wxString &short_name);
138144
XmlRpcInstance *py_server; //Code Completion Server (a python process running an XMLRPC server)

0 commit comments

Comments
 (0)