Code::Blocks Forums

Developer forums (C::B DEVELOPMENT STRICTLY!) => Development => Topic started by: Loaden on September 30, 2010, 07:31:57 am

Title: Issue about wxScintilla
Post by: Loaden on September 30, 2010, 07:31:57 am
Hi, all!
I encountered a problem about scintilla.
Quote
void CodeRefactoring::Find(cbStyledTextCtrl* control, const wxString& file, const wxString& target)
{
    const int end = control->GetLength();
    int start = 0;
    int pos = 0;

    for (;;)
    {
        int lengthFound;
        pos = control->FindText(start, end, target, wxSCI_FIND_WHOLEWORD | wxSCI_FIND_MATCHCASE, &lengthFound);
        if (pos != wxSCI_INVALID_POSITION)
        {
            start = pos + lengthFound;

           // TODO (Loaden) not work?
            const int style = control->GetStyleAt(pos); // always been zero?
            if (control->IsString(style) || control->IsComment(style))
                continue;

            int line = control->LineFromPosition(pos);
            wxString text = control->GetLine(line).Trim(true).Trim(false);
            m_SearchDataMap[file].push_back(crSearchData(pos, line, text));
        }
        else
            break;
    }
}
Please see the bold style code, why the return value always been zero.
Any comment are welcome!

Example code:
Code
int test()
{
    // test
    printf("test");
}
When call control->FindText, we will get three result, include the comment(test) and string (test).

Here are "find references" result:
Quote
main.cpp|1|int test()|
main.cpp|3|// test|
main.cpp|4|printf("test");|
Title: Re: Issue about wxScintilla
Post by: Jenna on September 30, 2010, 08:31:23 am
I did a quick test with trunk (modified my IncrementalSearch-plugin to spit out the style for every found string, and I get:
Code
pos: 4; style: 11
pos: 20; style: 2
pos: 37; style: 6
three different styles.
Title: Re: Issue about wxScintilla
Post by: MortenMacFly on September 30, 2010, 10:14:52 am
Code
pos: 4; style: 11
pos: 20; style: 2
pos: 37; style: 6
three different styles.
Same here with a dummy project (plugin), however, debugging into the code Loaden mentioned I also get zero always. So the root must be somewhere else, not in scintilla.
Title: Re: Issue about wxScintilla
Post by: ollydbg on September 30, 2010, 10:56:03 am
debugging into the code Loaden mentioned I also get zero always.
Confirmed. strange...
Title: Re: Issue about wxScintilla
Post by: Jenna on September 30, 2010, 12:01:58 pm
debugging into the code Loaden mentioned I also get zero always.
Confirmed. strange...
No, not strange.
The code do not use the cbEditor, but just the cbStyledTextctrl, but without a lexer and without highlighting any code, so GetStyleAt can not return anything but the default value, because the text is not styled.

The following patch should do the trick:
Code
Index: coderefactoring.cpp
===================================================================
--- coderefactoring.cpp (Revision 6654)
+++ coderefactoring.cpp (Arbeitskopie)
@@ -156,6 +156,9 @@
                 continue; // failed
             control->SetText(detector.GetWxStr());
         }
+        cbEditor::ApplyStyles(control);
+        EditorColourSet EdColSet;
+        EdColSet.Apply(EdColSet.GetLanguageForFilename(files[i]), control);
 
         Find(control, files[i], targetText);
     }

Shall I commit it ?
Title: Re: Issue about wxScintilla
Post by: ollydbg on September 30, 2010, 12:09:50 pm
thanks jen for the explanation.
Yes, I found that in coderefactoring.cpp line 131.
There are code snippet
Code
    // now that list is filled, we'll search
    cbStyledTextCtrl* control = new cbStyledTextCtrl(editor->GetParent(), wxID_ANY, wxDefaultPosition, wxSize(0, 0));

Quote
Shall I commit it ?
I full agree.



Title: Re: Issue about wxScintilla
Post by: Loaden on September 30, 2010, 12:23:20 pm
debugging into the code Loaden mentioned I also get zero always.
Confirmed. strange...
No, not strange.
The code do not use the cbEditor, but just the cbStyledTextctrl, but without a lexer and without highlighting any code, so GetStyleAt can not return anything but the default value, because the text is not styled.

The following patch should do the trick:
Code
Index: coderefactoring.cpp
===================================================================
--- coderefactoring.cpp (Revision 6654)
+++ coderefactoring.cpp (Arbeitskopie)
@@ -156,6 +156,9 @@
                 continue; // failed
             control->SetText(detector.GetWxStr());
         }
+        cbEditor::ApplyStyles(control);
+        EditorColourSet EdColSet;
+        EdColSet.Apply(EdColSet.GetLanguageForFilename(files[i]), control);
 
         Find(control, files[i], targetText);
     }

Shall I commit it ?
Thank Jens! :D
I can't solved this issue so long time.
I am very happy to see what is the reason.
Thanks a lot!

But, I am worry about the search performance?
So, I want look into it.
Title: Re: Issue about wxScintilla
Post by: Jenna on September 30, 2010, 01:28:07 pm
After a quick test with ThreadSearch-plugin and search for wxString, searching is about 25 times slower: ~400ms against ~9800ms .

I just used wxStropWatch and measured the for-loop in SearchInFiles about line 142.
Title: Re: Issue about wxScintilla
Post by: ollydbg on September 30, 2010, 01:40:15 pm
I think:
the lexer/parser in scintilla is not as fast as CC's parser. So, I'd rather add a function in Parserthread class to do the search. Currently all the comments were skipped in CC's Tokenizer.
Title: Re: Issue about wxScintilla
Post by: MortenMacFly on September 30, 2010, 02:05:11 pm
After a quick test with ThreadSearch-plugin and search for wxString, searching is about 25 times slower: ~400ms against ~9800ms .
Currently all the comments were skipped in CC's Tokenizer.
Which makes me wonder: Wat's the actual purpose of using the styles in scintilla to quey for comments? I mean: This can only be less performant...?!
Title: Re: Issue about wxScintilla
Post by: ollydbg on September 30, 2010, 02:13:30 pm
Which makes me wonder: Wat's the actual purpose of using the styles in scintilla to quey for comments? I mean: This can only be less performant...?!
The re-factoring model was designed by Loaden, so I think he can some idea. See:
Code
int test()
{
    // test
    printf("test");
}

A simple plain text search will give 3 matches.
But in-fact, the one we interested was only the function name. So, both matched text result in comments and c-strings should be removed. Sometimes, the matched text in comments should also be involved.
Currently the only way is do a query to scintilla.

I'm thinking a better way by CC.

Title: Re: Issue about wxScintilla
Post by: Loaden on September 30, 2010, 02:43:33 pm
Which makes me wonder: Wat's the actual purpose of using the styles in scintilla to quey for comments? I mean: This can only be less performant...?!
The re-factoring model was designed by Loaden, so I think he can some idea. See:
Code
int test()
{
    // test
    printf("test");
}

A simple plain text search will give 3 matches.
But in-fact, the one we interested was only the function name. So, both matched text result in comments and c-strings should be removed. Sometimes, the matched text in comments should also be involved.
Currently the only way is do a query to scintilla.

I'm thinking a better way by CC.

Thank ollydbg explained.
I found a way to solved, will be less performant littler.

We can't solved this issue by CC.
It's only scintilla related.

EDIT:
Sorry! Found a bug, fixed now.
Title: Re: Issue about wxScintilla
Post by: Loaden on September 30, 2010, 03:01:27 pm
Sorry! Found a bug, fixed now. :(
Title: Re: Issue about wxScintilla
Post by: Loaden on September 30, 2010, 03:24:15 pm
More improved.
This is the final version. :)