Author Topic: Code Completion with One Key Press  (Read 1277 times)

Offline AlterEgo

  • Multiple posting newcomer
  • *
  • Posts: 10
Code Completion with One Key Press
« on: January 11, 2017, 07:45:50 pm »
Is it possible to configure Code::Blocks so Code Completion activates after one character key press, rather than three?

I have checked Settings -> Editor -> Code Completion but cannot find an option to adjust the sensitivity there. I apologise if I have overlooked an option to do this. I have also searched for this online but have been unable to find any information on it.

The version of Code::Blocks I'm using is 16.01. Any help you could provide me with this would be much appreciated. Thank you.

Offline BlueHazzard

  • Lives here!
  • ****
  • Posts: 1267
Re: Code Completion with One Key Press
« Reply #1 on: January 11, 2017, 11:13:21 pm »
settings->Editor->General settings->Code completion->Autolaunch after typing # letters

i don't know why the settings for code completion are split up ???

Offline AlterEgo

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: Code Completion with One Key Press
« Reply #2 on: January 12, 2017, 07:32:33 pm »
Ah, thank you.

I have another problem now however. I have lost context awareness. As an example, when typing the letter 'p' inside a class, instead of being offered one of the access specifiers, I am confronted with a list of context meaningless keywords.

Is it possible to refine the options given to context sensitive keywords only?

Offline AlterEgo

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: Code Completion with One Key Press
« Reply #3 on: January 13, 2017, 07:59:19 pm »
To put it another way, have you ever used any of the JetBrains IDE's? In their IDE's, such as IntelliJ IDEA or Android Studio, from typing just one key you get very good, context specific auto complete options. Generally the options given go from most relevant to least. In Code::Blocks at the moment for me, that's not happening. I am given a list of keywords that have no appreciable application within the context they are offered. I end up having to type another few characters to get the options I'm looking for, which defeats the point of starting Code Completion on one key press.

Is it possible to adjust this so that relevant options are given on one key press?

Offline yvesdm3000

  • Almost regular
  • **
  • Posts: 223
Re: Code Completion with One Key Press
« Reply #4 on: January 13, 2017, 08:37:03 pm »
Maybe try the clang code completion ?
The 'staging' branch is the one with most of the features but with some minor regressions that still need to be ironed out, but its workable. Otherwise try the master branch.

Yves
Clang based code completion for Code::Blocks:   http://github.com/yvesdm3000/ClangLib

Online oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 9870
Re: Code Completion with One Key Press
« Reply #5 on: January 13, 2017, 09:16:18 pm »
Is it possible to adjust this so that relevant options are given on one key press?
Can you post a minimal self contained sample file/project that demonstrates the problem?
<debugger plugin maintainer>
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline AlterEgo

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: Code Completion with One Key Press
« Reply #6 on: January 13, 2017, 09:59:27 pm »
Here is an example file with the problem and an image demonstrating the problem. Just remove the .txt at the end of the example file. Thank you for taking the time to help me with this.

Online oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 9870
Re: Code Completion with One Key Press
« Reply #7 on: January 14, 2017, 01:19:18 pm »
What is expected to happen here? Empty list?
<debugger plugin maintainer>
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline AlterEgo

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: Code Completion with One Key Press
« Reply #8 on: January 14, 2017, 01:22:34 pm »
Within the context of a class, I would expect the access specifiers private, protected and public to show up as options upon typing the letter 'p'. Here as you see that's not what happens.

Offline AlterEgo

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: Code Completion with One Key Press
« Reply #9 on: January 15, 2017, 03:41:02 pm »
Any ideas oBFusCATed, is there a way to get the behaviour I'm looking for from the built in Code Completion?

Online oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 9870
Re: Code Completion with One Key Press
« Reply #10 on: January 15, 2017, 04:16:02 pm »
I doubt, the only thing you can do is start debugging the CC plugin.
You can probably try the CC based on clang mentioned by yvesdm3000.
<debugger plugin maintainer>
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 4902
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Code Completion with One Key Press
« Reply #11 on: January 16, 2017, 05:56:40 am »
settings->Editor->General settings->Code completion->Autolaunch after typing # letters

i don't know why the settings for code completion are split up ???
This is because the setting here will be shared by all kinds of code completion plugins, I mean we have a lot of cc plugins, such as the build in cc, the clang cc, the python cc.

Any ideas oBFusCATed, is there a way to get the behaviour I'm looking for from the built in Code Completion?
Hi, I think it can be adjusted, but I don't know how to do that, in the source code:
Code: [Select]
void CodeCompletion::DoCodeComplete(int caretPos, cbEditor* ed, std::vector<CCToken>& tokens, bool preprocessorOnly)
There are some snippet:
Code: [Select]
if (colour_set)
                {
                    wxString lastSearch = m_NativeParser.LastAIGlobalSearch().Lower();
                    int iidx = ilist->GetImageCount();
                    FileType fTp = FileTypeOf(ed->GetShortName());
                    bool isC = (fTp == ftHeader || fTp == ftSource|| fTp == ftTemplateSource);
                    // theme keywords
                    HighlightLanguage lang = ed->GetLanguage();
                    if (lang == HL_NONE)
                        lang = colour_set->GetLanguageForFilename(ed->GetFilename());
                    wxString strLang = colour_set->GetLanguageName(lang);
                    // if its sourcecode/header file and a known fileformat, show the corresponding icon
                    if (isC && strLang == wxT("C/C++"))
                        stc->RegisterImage(iidx, wxBitmap(cpp_keyword_xpm));
                    else if (isC && strLang == wxT("D"))
                        stc->RegisterImage(iidx, wxBitmap(d_keyword_xpm));
                    else
                        stc->RegisterImage(iidx, wxBitmap(unknown_keyword_xpm));
                    // the first two keyword sets are the primary and secondary keywords (for most lexers at least)
                    // but this is now configurable in global settings
                    for (int i = 0; i <= wxSCI_KEYWORDSET_MAX; ++i)
                    {
                        if (!m_LexerKeywordsToInclude[i])
                            continue;

                        wxString keywords = colour_set->GetKeywords(lang, i);
                        wxStringTokenizer tkz(keywords, wxT(" \t\r\n"), wxTOKEN_STRTOK);
                        while (tkz.HasMoreTokens())
                        {
                            wxString kw = tkz.GetNextToken();
                            if (   kw.Lower().StartsWith(lastSearch)
                                && uniqueStrings.find(kw) == uniqueStrings.end() )
                            {
                                tokens.push_back(CCToken(wxNOT_FOUND, kw, iidx));
                            }
                        }
                    }
                }
You see, all the keywords were added directly to the "tokens", but I think they don't have much precedence, as I can see the cunstructor of CCToken.
Code: [Select]
       /** Structure representing a generic token, passed between CC plugins and CCManager. */
        struct CCToken
        {
            /** @brief Convenience constructor.
              *
              * Represents a generic token, passed between CC plugins and CCManager.
              *
              * @param _id Internal identifier for a CC plugin to reference the token in its data structure.
              * @param dispNm The string CCManager will use to display this token.
              * @param categ The category corresponding to the index of the registered image (during autocomplete).
              *              Negative values are reserved for CCManager.
              */
            CCToken(int _id, const wxString& dispNm, int categ = -1) :
                id(_id), category(categ), weight(5), displayName(dispNm), name(dispNm) {}

            /** @brief Construct a fully specified CCToken.
              *
              * Represents a generic token, passed between CC plugins and CCManager.
              *
              * @param _id Internal identifier for a CC plugin to reference the token in its data structure.
              * @param dispNm The verbose string CCManager will use to display this token.
              * @param nm Minimal name of the token that CCManager may choose to display in restricted circumstances.
              * @param _weight Lower numbers are placed earlier in listing, 5 is default; try to keep 0-10.
              * @param categ The category corresponding to the index of the registered image (during autocomplete).
              *              Negative values are reserved for CCManager.
              */
            CCToken(int _id, const wxString& dispNm, const wxString& nm, int _weight, int categ = -1) :
                id(_id), category(categ), weight(_weight), displayName(dispNm), name(nm) {}

            int id;               //!< CCManager will pass this back unmodified. Use it as an internal identifier for the token.
            int category;         //!< The category corresponding to the index of the registered image (during autocomplete).
            int weight;           //!< Lower numbers are placed earlier in listing, 5 is default; try to keep 0-10.
            wxString displayName; //!< Verbose string representing the token.
            wxString name;        //!< Minimal name of the token that CCManager may choose to display in restricted circumstances.
        };
So, we have to adjust the "weight" parameter. We need an algorithm to dynamically adjust the "weight" for each added keywords.  ;)
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline AlterEgo

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: Code Completion with One Key Press
« Reply #12 on: January 22, 2017, 01:36:44 pm »
How do you use Clang Code Completion? I've never used any extra Code::Blocks plugins before.

Offline yvesdm3000

  • Almost regular
  • **
  • Posts: 223
Re: Code Completion with One Key Press
« Reply #13 on: January 22, 2017, 02:13:08 pm »
Code: [Select]
[quote author=ollydbg link=topic=21656.msg147393#msg147393 date=1484542600]
[quote author=BlueHazzard link=topic=21656.msg147344#msg147344 date=1484172801]
settings->Editor->General settings->Code completion->Autolaunch after typing # letters

i don't know why the settings for code completion are split up ???
[/quote]
This is because the setting here will be shared by all kinds of code completion plugins, I mean we have a lot of cc plugins, such as the build in cc, the clang cc, the python cc.


It's a bit simpler than that, it's a CodeBlocks setting that will instruct when the CC plugin will be called for code-completion.
It might be better if this setting would be language-specific, but it's not very important or high priority item...

Yves
Clang based code completion for Code::Blocks:   http://github.com/yvesdm3000/ClangLib

Offline AlterEgo

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: Code Completion with One Key Press
« Reply #14 on: January 23, 2017, 10:59:47 pm »
Would someone explain to me how to set up Code::Blocks to use Clang Code Completion or link me to a good tutorial that explains how to do so please?