Author Topic: Syntax Highlighting - User Defined Types, identifiers, function declarations  (Read 6282 times)

Offline n0083

  • Single posting newcomer
  • *
  • Posts: 2
Hello,

Is it possible to have C::B highlight my user defined types (classes, typedefs, etc...), function name declarations, and variable identifiers automatically (without having to add keywords).

I'm looking for something similar to the level of syntax highlighting control of netbeans or http://www.wholetomato.com/products/features/color.asp.

The following post back in 2006 said "no", but i was hoping that after 4 years this feature might be added (though, i cannot find a way):
http://forums.codeblocks.org/index.php/topic,2817.0.html.

Thanks in advance,

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5916
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
I have discussed this feature with Loaden. (If I remember correct, there are some other discussion about this)

But making the code more colorfull is a good idea????

to implement this, we just need to add the keyword to some scintilla keyword list, and then scintilla edit control will do the job.

Codelite has this kind of feature, see the code snippet from Codelite IDE:

Code
void ContextCpp::OnFileSaved()
{
    PERF_FUNCTION();

    VariableList var_list;
    std::map< std::string, Variable > var_map;
    std::map< wxString, TagEntryPtr> foo_map;
    std::map<std::string, std::string> ignoreTokens;

    wxArrayString varList;
    wxArrayString projectTags;

    LEditor &rCtrl = GetCtrl();
    VALIDATE_WORKSPACE();

    // if there is nothing to color, go ahead and return
    if ( !(TagsManagerST::Get()->GetCtagsOptions().GetFlags() & CC_COLOUR_WORKSPACE_TAGS) && !(TagsManagerST::Get()->GetCtagsOptions().GetFlags() & CC_COLOUR_VARS) ) {
        return;
    }

    // wxSCI_C_WORD2
    if (TagsManagerST::Get()->GetCtagsOptions().GetFlags() & CC_COLOUR_WORKSPACE_TAGS) {

        // get list of all tags from the workspace
        TagsManagerST::Get()->GetAllTagsNames(projectTags);
    }
    // wxSCI_C_GLOBALCLASS
    if (TagsManagerST::Get()->GetCtagsOptions().GetFlags() & CC_COLOUR_VARS) {
        //---------------------------------------------------------------------
        // Colour local variables
        //---------------------------------------------------------------------
        PERF_BLOCK("Getting Locals") {

            const wxCharBuffer patbuf = _C(rCtrl.GetText());

            // collect list of variables
            get_variables( patbuf.data(), var_list, ignoreTokens, false);

        }

        // list all functions of this file
        std::vector< TagEntryPtr > tags;
        TagsManagerST::Get()->GetFunctions(tags, rCtrl.GetFileName().GetFullPath());

        PERF_BLOCK("Adding Functions") {

            VariableList::iterator viter = var_list.begin();
            for (; viter != var_list.end(); viter++ ) {
                Variable vv = *viter;
                varList.Add(_U(vv.m_name.c_str()));
            }

            // parse all function's arguments and add them as well
            for (size_t i=0; i<tags.size(); i++) {
                wxString sig = tags.at(i)->GetSignature();
                const wxCharBuffer cb = _C(sig);
                VariableList vars_list;
                get_variables(cb.data(), vars_list, ignoreTokens, true);
                VariableList::iterator it = vars_list.begin();
                for (; it != vars_list.end(); it++ ) {
                    Variable var = *it;
                    wxString name = _U(var.m_name.c_str());
                    if (varList.Index(name) == wxNOT_FOUND) {
                        // add it
                        varList.Add(name);
                    }
                }
            }

        }
    }
    PERF_BLOCK("Setting Keywords") {

        size_t cc_flags = TagsManagerST::Get()->GetCtagsOptions().GetFlags();
        if (cc_flags & CC_COLOUR_WORKSPACE_TAGS) {
            wxString flatStr;
            for (size_t i=0; i< projectTags.GetCount(); i++) {
                // add only entries that does not appear in the variable list
                //if (varList.Index(projectTags.Item(i)) == wxNOT_FOUND) {
                flatStr << projectTags.Item(i) << wxT(" ");
            }
            rCtrl.SetKeyWords(1, flatStr);
        } else {
            rCtrl.SetKeyWords(1, wxEmptyString);
        }

        if (cc_flags & CC_COLOUR_VARS) {
            // convert it to space delimited string
            wxString varFlatStr;
            for (size_t i=0; i< varList.GetCount(); i++) {
                varFlatStr << varList.Item(i) << wxT(" ");
            }
            rCtrl.SetKeyWords(3, varFlatStr);
        } else {
            rCtrl.SetKeyWords(3, wxEmptyString);
        }
    }
}

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.