Search code examples
wxwidgetsscintilla

wxStyledTextCtrl Code Completion


I am designing a simple editor for Lua to use in my software written in C++ using wxWidgets. I have been searching around to find a simple example on implementing code completion using wxStyledTextCtrl in C++.

I have checked the websites of both Scintilla and wxWidgets but could not find any. I wonder if someone could help with a code snippet.


Solution

  • Unfortunately, I've found that the original Scintilla documentation leaves a lot to be desired, and the wxStyledTextCtrl documentation is an almost-verbatim copy of the Scintilla docs.

    I recently discovered this wiki article from the ScintillaNET project and it has been quite helpful in getting started with autocompletion. The process is the same for any Scintilla implementation. I actually used it with IupScintilla.

    https://github.com/jacobslusser/ScintillaNET/wiki/Basic-Autocompletion

    private void scintilla_CharAdded(object sender, CharAddedEventArgs e)
    {
        // Find the word start
        var currentPos = scintilla.CurrentPosition;
        var wordStartPos = scintilla.WordStartPosition(currentPos, true);
    
        // Display the autocompletion list
        var lenEntered = currentPos - wordStartPos;
        if (lenEntered > 0)
        {
            scintilla.AutoCShow(lenEntered, "abstract as base break case catch checked continue default delegate do else event explicit extern false finally fixed for foreach goto if implicit in interface internal is lock namespace new null object operator out override params private protected public readonly ref return sealed sizeof stackalloc switch this throw true try typeof unchecked unsafe using virtual while");
        }
    }
    

    Here is a simple wxWidgets app that does the same thing:

    #include <wx/wx.h>
    #include <wx/stc/stc.h>
    
    class wxTestProject : public wxApp
    {
    public:
        bool OnInit();
        void OnChange( wxStyledTextEvent& event );
    };
    
    wxIMPLEMENT_APP(wxTestProject);
    
    bool wxTestProject::OnInit()
    {
        wxFrame* frame = new wxFrame( NULL, wxID_ANY, "wxTestProject",
            wxDefaultPosition, wxSize(640,480) );
    
        wxStyledTextCtrl* stc = new wxStyledTextCtrl( frame, wxID_ANY,
            wxDefaultPosition, wxDefaultSize, wxBORDER_NONE );
        stc->SetLexerLanguage( "lua" );
        stc->Bind( wxEVT_STC_CHANGE, &wxTestProject::OnChange, this );
    
        this->SetTopWindow( frame );
        frame->Show();
    
        return true;
    }
    
    void wxTestProject::OnChange( wxStyledTextEvent& event )
    {
        wxStyledTextCtrl* stc = (wxStyledTextCtrl*)event.GetEventObject();
    
        // Find the word start
        int currentPos = stc->GetCurrentPos();
        int wordStartPos = stc->WordStartPosition( currentPos, true );
    
        // Display the autocompletion list
        int lenEntered = currentPos - wordStartPos;
        if (lenEntered > 0)
        {
            stc->AutoCompShow(lenEntered, "and break do else elseif end false for function if in local nil not or repeat return then true until while");
        }
    }