Author Topic: ClangComplete plugin  (Read 54913 times)

Offline Lalaland

  • Multiple posting newcomer
  • *
  • Posts: 23
Re: ClangComplete plugin
« Reply #30 on: November 18, 2011, 07:52:18 am »
Couple of improvements.

No more showing of things you cannot use:
(ClangComplete)


(Code Complete)



I also finally started sorting by libclang's "priority". Objects are sorted by priority first, and then by alphabetical order.
Summary, junk(such as definitions) floats to the bottom, but the usefull things (such as methods and members) are alphabetically sorted as you would expect.


The plugin is also now derived from cbCodeCompletionPlugin, but that does not do much as only the CodeComplete function does something.

EDIT: I have now also hidden everything starting with an '_'. Those internals should not be used anyways.
« Last Edit: November 18, 2011, 04:24:37 pm by Lalaland »

Offline Lalaland

  • Multiple posting newcomer
  • *
  • Posts: 23
Re: ClangComplete plugin
« Reply #31 on: November 19, 2011, 01:29:48 am »
I am having an issue. The code completion dialog will not show up when called in the middle of a word.
I am using the same code as the official plugin to generate the location for the code completion.

Code: [Select]
    int pos   = control->GetCurrentPos();
    int start = control->WordStartPosition(pos, true);
    wxString final = GetStringFromArray(items, _(" "));

    control->AutoCompSetCancelAtStart(false);
    control->AutoCompSetAutoHide(true);

    control->AutoCompShow(pos-start,final );

Items is an array of the completions.

The following shows how it works with no letters, and then fails when trying to complete with at least one.

(I have posted so many pictures that I might as well try a video now)
http://youtu.be/AjbFL_n0qos

This is the only work left to completing in the middle of the word.


Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: ClangComplete plugin
« Reply #32 on: November 19, 2011, 03:14:40 am »
My personal opinion : I don't use these pictures. The names I choose are explicit, and I never learned the meaning of these symbols.
If you were wondering, I have added a table defining these symbols to the Code Completion plugin wiki-page.

@Lalaland: I am afraid I do not yet know enough about the process of code completion to help you, however, I did want to say: good job so far.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5281
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: ClangComplete plugin
« Reply #33 on: November 19, 2011, 05:01:10 am »
My personal opinion : I don't use these pictures. The names I choose are explicit, and I never learned the meaning of these symbols.
If you were wondering, I have added a table defining these symbols to the Code Completion plugin wiki-page.
This table is quite good, nice work!!!

Quote
@Lalaland: I am afraid I do not yet know enough about the process of code completion to help you, however, I did want to say: good job so far.
I will try to give some help :D
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 ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5281
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: ClangComplete plugin
« Reply #34 on: November 19, 2011, 05:59:42 am »
If you set a breakpoint in the function body: NativeParser::AI

You will get the call stack when you enter some chars in the middle of a word.
Code: [Select]
> bt 30
#0  NativeParser::AI (this=0x3bf6040, result=std::set with 0 elements, searchData=0x22ed28, lineText="", isPrefix=true, caseSensitive=false, search_scope=0x22ecb8, caretPos=-1) at e:\code\cb\cb_trunk\src\plugins\codecompletion\nativeparser.cpp:2338
#1  0x65ec4ae1 in NativeParser::MarkItemsByAI (this=0x3bf6040, searchData=0x22ed28, result=std::set with 0 elements, reallyUseAI=true, isPrefix=true, caseSensitive=false, caretPos=-1) at e:\code\cb\cb_trunk\src\plugins\codecompletion\nativeparser.cpp:1745
#2  0x65ec47f9 in NativeParser::MarkItemsByAI (this=0x3bf6040, result=std::set with 0 elements, reallyUseAI=true, isPrefix=true, caseSensitive=false, caretPos=-1) at e:\code\cb\cb_trunk\src\plugins\codecompletion\nativeparser.cpp:1690
#3  0x65ea7554 in CodeCompletion::CodeComplete (this=0x3bf5ff8) at e:\code\cb\cb_trunk\src\plugins\codecompletion\codecompletion.cpp:1042
#4  0x65eac167 in CodeCompletion::DoCodeComplete (this=0x3bf5ff8) at e:\code\cb\cb_trunk\src\plugins\codecompletion\codecompletion.cpp:1776
#5  0x65eb2935 in CodeCompletion::EditorEventHook (this=0x3bf5ff8, editor=0x5685008, event=...) at e:\code\cb\cb_trunk\src\plugins\codecompletion\codecompletion.cpp:3346
#6  0x65f0958d in EditorHooks::HookFunctor<CodeCompletion>::Call (this=0x2f2e480, editor=0x5685008, event=...) at e:\code\cb\cb_trunk\src\include\editor_hooks.h:54
#7  0x00a943d1 in EditorHooks::CallHooks (editor=0x5685008, event=...) at e:\code\cb\cb_trunk\src\sdk\editor_hooks.cpp:60
#8  0x00a5a353 in cbEditor::OnScintillaEvent (this=0x5685008, event=...) at e:\code\cb\cb_trunk\src\sdk\cbeditor.cpp:3554
#9  0x00a59a67 in cbEditor::OnEditorCharAdded (this=0x5685008, event=...) at e:\code\cb\cb_trunk\src\sdk\cbeditor.cpp:3362
#10 0x627720b6 in wxEvtHandler::ProcessEventIfMatches(wxEventTableEntryBase const&, wxEvtHandler*, wxEvent&) () from E:\code\cb\wx\wxWidgets-2.8.12\lib\gcc_dll\wxmsw28u_gcc_custom.dll
#11 0x62772520 in wxEvtHandler::SearchDynamicEventTable(wxEvent&) () from E:\code\cb\wx\wxWidgets-2.8.12\lib\gcc_dll\wxmsw28u_gcc_custom.dll
#12 0x627725d3 in wxEvtHandler::ProcessEvent(wxEvent&) () from E:\code\cb\wx\wxWidgets-2.8.12\lib\gcc_dll\wxmsw28u_gcc_custom.dll
#13 0x6285b59d in wxWindowBase::TryParent(wxEvent&) () from E:\code\cb\wx\wxWidgets-2.8.12\lib\gcc_dll\wxmsw28u_gcc_custom.dll
#14 0x00b72642 in wxScintilla::NotifyParent (this=0x56af358, _scn=0x22f31c) at e:\code\cb\cb_trunk\src\sdk\wxscintilla\src\wxscintilla.cpp:5084
#15 0x00b752dc in ScintillaWX::NotifyParent (this=0x56ae008, scn=...) at e:\code\cb\cb_trunk\src\sdk\wxscintilla\src\ScintillaWX.cpp:521
#16 0x00bf287c in Editor::NotifyChar (this=0x56ae008, ch=67) at e:\code\cb\cb_trunk\src\sdk\wxscintilla\src\scintilla\src\Editor.cxx:4377
#17 0x00bf0cc8 in Editor::AddCharUTF (this=0x56ae008, s=0x55ca288 "C", len=1, treatAsDBCS=false) at e:\code\cb\cb_trunk\src\sdk\wxscintilla\src\scintilla\src\Editor.cxx:4081
#18 0x00c05a49 in ScintillaBase::AddCharUTF (this=0x56ae008, s=0x55ca288 "C", len=1, treatAsDBCS=false) at e:\code\cb\cb_trunk\src\sdk\wxscintilla\src\scintilla\src\ScintillaBase.cxx:76
#19 0x00b76da3 in ScintillaWX::DoAddChar (this=0x56ae008, key=67) at e:\code\cb\cb_trunk\src\sdk\wxscintilla\src\ScintillaWX.cpp:1067
#20 0x00b71b67 in wxScintilla::OnChar (this=0x56af358, evt=...) at e:\code\cb\cb_trunk\src\sdk\wxscintilla\src\wxscintilla.cpp:4811
#21 0x627720b6 in wxEvtHandler::ProcessEventIfMatches(wxEventTableEntryBase const&, wxEvtHandler*, wxEvent&) () from E:\code\cb\wx\wxWidgets-2.8.12\lib\gcc_dll\wxmsw28u_gcc_custom.dll
#22 0x6277222a in wxEventHashTable::HandleEvent(wxEvent&, wxEvtHandler*) () from E:\code\cb\wx\wxWidgets-2.8.12\lib\gcc_dll\wxmsw28u_gcc_custom.dll
#23 0x627725f6 in wxEvtHandler::ProcessEvent(wxEvent&) () from E:\code\cb\wx\wxWidgets-2.8.12\lib\gcc_dll\wxmsw28u_gcc_custom.dll
#24 0x627b2c93 in wxWindow::HandleChar(unsigned int, long, bool) () from E:\code\cb\wx\wxWidgets-2.8.12\lib\gcc_dll\wxmsw28u_gcc_custom.dll
#25 0x627b471a in wxWindow::MSWWindowProc(unsigned int, unsigned int, long) () from E:\code\cb\wx\wxWidgets-2.8.12\lib\gcc_dll\wxmsw28u_gcc_custom.dll
#26 0x627af438 in wxWndProc () from E:\code\cb\wx\wxWidgets-2.8.12\lib\gcc_dll\wxmsw28u_gcc_custom.dll
#27 0x7e418734 in USER32!GetDC () from C:\WINDOWS\system32\user32.dll
#28 0x000503b6 in ?? ()
warning: (Internal error: pc 0x101 in read in psymtab, but not in symtab.)
warning: (Internal error: pc 0x101 in read in psymtab, but not in symtab.)
warning: (Internal error: pc 0x101 in read in psymtab, but not in symtab.)
warning: (Internal error: pc 0x101 in read in psymtab, but not in symtab.)
#29 0x00000102 in ?? ()
>>>>>>cb_gdb:

So, You can see in the AI's body:

Code: [Select]
   int pos = caretPos == -1 ? searchData->control->GetCurrentPos() : caretPos;
    if (pos < 0 || pos > searchData->control->GetLength())
        return 0;

    m_EditorStartWord = searchData->control->WordStartPosition(pos, true);
    m_EditorEndWord   = pos; //editor->GetControl()->WordEndPosition(pos, true);
    int line = searchData->control->LineFromPosition(pos);

    // Get the actual search text, such as "objA.m_aaa.m_bbb"
    wxString actual_search(lineText);
    if (actual_search.IsEmpty())
    {
        // Get the position at the start of current line
        const int startPos = searchData->control->PositionFromLine(line);
        actual_search = searchData->control->GetTextRange(startPos, pos).Trim();
    }

Note, the actual_search is the current line Text you might interests.

PS: I'm debugging the c::b's trunk code.
« Last Edit: November 19, 2011, 06:01:17 am by ollydbg »
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 Folco

  • Regular
  • ***
  • Posts: 343
    • Folco's blog (68k lover)
Re: ClangComplete plugin
« Reply #35 on: November 19, 2011, 02:54:35 pm »
Nice job and thanks Alpha. :)

Lalaland -> these pictures are not a "major part of the experience" for me. I humbly apologise to have another opinion than yours.
Kernel Extremist - PedroM power

Offline Lalaland

  • Multiple posting newcomer
  • *
  • Posts: 23
Re: ClangComplete plugin
« Reply #36 on: November 19, 2011, 03:16:14 pm »
Folco: It is fine, I will add the idea of providing an option to disable the images to the todo list.

ollydbg: Good idea to step through it with a debugger, I can not believe that I did not think of that first.

Offline Folco

  • Regular
  • ***
  • Posts: 343
    • Folco's blog (68k lover)
Re: ClangComplete plugin
« Reply #37 on: November 19, 2011, 03:59:23 pm »
Thanks for that. And congrats for doing such a plugin, it's obviously a huge work.
Kernel Extremist - PedroM power

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: ClangComplete plugin
« Reply #38 on: November 19, 2011, 07:51:49 pm »
This table is quite good, nice work!!!
Nice job and thanks Alpha. :)
:)

@Lalaland: I have a feature suggestion/request; assuming Clang reports whenever/wherever there is a parsing error, would it be possible for the ClangComplete plugin to put a red marker by the line(s) with errors, or underline (like spell-check) the specific token the error occurs in?

Offline Lalaland

  • Multiple posting newcomer
  • *
  • Posts: 23
Re: ClangComplete plugin
« Reply #39 on: November 20, 2011, 04:14:59 am »
Quote
@Lalaland: I have a feature suggestion/request; assuming Clang reports whenever/wherever there is a parsing error, would it be possible for the ClangComplete plugin to put a red marker by the line(s) with errors, or underline (like spell-check) the specific token the error occurs in?

Feature requesting is a little premature, I have to get the plugin working first.

God, this issue of the menu not popping up in the middle of the word is getting stranger and stranger. The actual issue seems to be that wxScintilla thinks that the word is not being matched(and this is for code completion at the start as well). At the same time, other random words(such as double or class) seem to work. And what really is strange is that the words which works changes determining on where you do the completion.


Here is a video showing what is happening. I turned off autohide, so you can see how wxScintilla does not think anything is matched. I am also using a keyboard shortcut to start autocompletion.

What you will see in this video is me typing in the starts of some keywords that appear in the list of valid completions, and the list correctly moving and highlighting the entry. If it does not do this, then it thinks the word is not in the list, and the list would usually close.
http://youtu.be/kAjb2RwxGqg
(The text at the bottom is showing the string I am passing to show, with '|' set as the separator)

I also double checked the length of the strings churned out by clang. They are the right length as both cstrings and wxStrings, and seem to be fine when printed out(I even tried removing the first character by increasing the cstring by 1, and that worked as expected).

Following the debugger showed that I am putting the right value for the distance to the start of the word, so that is obviously not the issue.

I am truly stumped.
Does anyone have any clue about what is happening?
« Last Edit: November 20, 2011, 06:34:30 am by Lalaland »

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: ClangComplete plugin
« Reply #40 on: November 20, 2011, 05:43:54 am »
Feature requesting is a little premature, I have to get the plugin working first.
You are right, I am quite early, sorry (although, it is sometimes hard to be patient... I will try to save these for later :)).

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5281
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: ClangComplete plugin
« Reply #41 on: November 20, 2011, 05:48:56 am »
Quote
@Lalaland: I have a feature suggestion/request; assuming Clang reports whenever/wherever there is a parsing error, would it be possible for the ClangComplete plugin to put a red marker by the line(s) with errors, or underline (like spell-check) the specific token the error occurs in?

Feature requesting is a little premature, I have to get the plugin working first.

God, this issue of the menu not popping up in the middle of the word is getting stranger and stranger. The actual issue seems to be that wxScintilla thinks that the word is not being matched(and this is for code completion at the start as well). At the same time, other random words(such as double or class) seem to work. And what really is strange is that the words which works changes determining on where you do the completion.


Here is a video showing what is happening. I turned off autohide, so you can see how wxScintilla does not think anything is matched. I am also using a keyboard shortcut to start autocompletion.
http://youtu.be/kAjb2RwxGqg
(The text at the bottom is showing the string I am passing to show, with '|' set as the separator)

I also double checked the length of the strings churned out by clang. They are the right length as both cstrings and wxStrings, and seem to be fine when printed out(I even tried removing the first character by increasing the cstring by 1, and that worked as expected).

Following the debugger showed that I am putting the right value for the distance to the start of the word, so that is obviously not the issue.

I am truly stumped.
Does anyone have any clue about what is happening?

I have watch your video two times, but I have no idea what's your problem is........ :(

Edit:
I have see that you get code completion in comments?
You can simply filter it out, some code like:
Code: [Select]

            // skip string or comment
            const int style = control->GetStyleAt(Your_osition);
            if (control->IsString(style) || control->IsComment(style))
            {
                 do some thing;
            }
« Last Edit: November 20, 2011, 06:07:28 am by ollydbg »
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 Lalaland

  • Multiple posting newcomer
  • *
  • Posts: 23
Re: ClangComplete plugin
« Reply #42 on: November 20, 2011, 06:11:32 am »
All of those words I type in(double , struct, class) etc should cause the menu to jump to and highlight the right entry.
Normally, when it does not work, the dialog simply goes away. I just set the setting that makes the codecompletion list stay, but show no highlighted entry.

Notice the failed attempts at typing in double and char the first time, despite them being clearly visible in the list.
Struct does work, and you can see it jump and highlight the right entry.

Then, when I switch to another location, typing in the start of "double" immediately jumps to the double keyword, and struct fails to work.

This is what causes my codecompletion inside a word to seem to fail, it is correctly creating the list, but it believes there are no matches for that start of the word and hides the list immediately(when I have the normal autohide setting enabled).

EDIT: You are right, only things typed in the default style should be auto-completed. I just added that now. Exceptions can be added as they are found.
« Last Edit: November 20, 2011, 06:29:43 am by Lalaland »

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5281
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: ClangComplete plugin
« Reply #43 on: November 20, 2011, 09:30:31 am »
I'm not sure what is the logic you show the auto completion list.
Edit:Oh, I see you use the similar code: https://github.com/Lalaland/ClangComplete/blob/master/ClangComplete.cpp
Code: [Select]
clang_disposeCodeCompleteResults(results);
    wxString final = GetStringFromArray(items, _("|"));
    final.RemoveLast();



    //control->CallTipShow(control->GetCurrentPos(), _("This is confusing"));
    control->AutoCompSetIgnoreCase(true);
    control->AutoCompSetCancelAtStart(false);
    control->AutoCompSetAutoHide(false);
    control->AutoCompSetSeparator('|');
    control->AutoCompStops(_(""));
   // final.RemoveLast();

//final = _("Hello boo");
    control->AutoCompShow(pos-start,final );
    wxString nums = _("pos: ");
    nums<<pos ;
    nums<<_(" start:");
    nums<<start;
     Manager::Get()->GetLogManager()->Log(final);

    return 0;


I just have a look at the current Codecompletion plugin's source code, see:

codecompletion.cpp line 1167.

Code: [Select]
           ed->GetControl()->AutoCompSetIgnoreCase(!caseSens);
            ed->GetControl()->AutoCompSetCancelAtStart(true);
            ed->GetControl()->AutoCompSetFillUps(m_CCFillupChars);
            ed->GetControl()->AutoCompSetChooseSingle(m_IsAutoPopup ? false : m_CCAutoSelectOne);
            ed->GetControl()->AutoCompSetAutoHide(true);
            ed->GetControl()->AutoCompSetDropRestOfWord(m_IsAutoPopup ? false : true);
            wxString final = GetStringFromArray(items, _T(" "));
            final.RemoveLast(); // remove last space

            ed->GetControl()->AutoCompShow(pos - start, final);
            return 0;
        }

You see:
the items were the collected tokens strings. and the most important call is:
Code: [Select]
ed->GetControl()->AutoCompShow(pos - start, final);

This will do what you would like in your clang code completion.
see its declaration:
Code: [Select]
   // Display a auto-completion list.
    // The lenEntered parameter indicates how many characters before
    // the caret should be used to provide context.
    void AutoCompShow(int lenEntered, const wxString& itemList);

Hope this helps. :D

Edit:
This is the sample code I used to test CC:
Code: [Select]
int abcd111;
int abcd212;
int abcd213;
int abcd414;
int abcd515;

abcd
« Last Edit: November 20, 2011, 10:09:07 am by ollydbg »
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 Lalaland

  • Multiple posting newcomer
  • *
  • Posts: 23
Re: ClangComplete plugin
« Reply #44 on: November 22, 2011, 09:45:46 pm »
God, the problem is solved. AutoCompShow needs the passed strings to be alphabetically sorted.
This fails because I sort my strings by priority.

Oh well, at least autocomplete in the middle of words works reliably now. I will post some pictures of this soon.

EDIT: And can we please add this to the doc

This ↓ tells me nothing about any sorting.
Code: [Select]
    // Display a auto-completion list.
    // The lenEntered parameter indicates how many characters before
    // the caret should be used to provide context.
« Last Edit: November 22, 2011, 09:47:48 pm by Lalaland »