Author Topic: Issue about wxScintilla  (Read 9351 times)

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Issue about wxScintilla
« 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");|
« Last Edit: September 30, 2010, 07:34:37 am by Loaden »

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7252
Re: Issue about wxScintilla
« Reply #1 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.

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9723
Re: Issue about wxScintilla
« Reply #2 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.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6077
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Issue about wxScintilla
« Reply #3 on: September 30, 2010, 10:56:03 am »
debugging into the code Loaden mentioned I also get zero always.
Confirmed. strange...
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 Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7252
Re: Issue about wxScintilla
« Reply #4 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 ?

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6077
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Issue about wxScintilla
« Reply #5 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.



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 Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: Issue about wxScintilla
« Reply #6 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.

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7252
Re: Issue about wxScintilla
« Reply #7 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.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6077
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Issue about wxScintilla
« Reply #8 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.
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 MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9723
Re: Issue about wxScintilla
« Reply #9 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...?!
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6077
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Issue about wxScintilla
« Reply #10 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.

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 Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: Issue about wxScintilla
« Reply #11 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.
« Last Edit: September 30, 2010, 03:00:50 pm by Loaden »

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: Issue about wxScintilla
« Reply #12 on: September 30, 2010, 03:01:27 pm »
Sorry! Found a bug, fixed now. :(

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: Issue about wxScintilla
« Reply #13 on: September 30, 2010, 03:24:15 pm »
More improved.
This is the final version. :)