Author Topic: Smarter searching in code  (Read 348 times)


  • Senior Community Member
  • Posts: 1506
  • Hero Points: 129
  • Text
Smarter searching in code
« on: July 14, 2017, 06:01:59 pm »
Searching for something like all assignments to a particular variable name can be tedious.
For example you might like to find all assignments to a1, but there are a lot of ways it might appear in the code:
    a1 = 3;
    a1  = 4;
    a1 =

Searching for "a1 =" won't match most of these.

I could use a regex and search for "a1 *=", but Slick oughta do that for me!

Think "Source Diff", but for search: I supply a search string with space delimited terms, and Slick searches for that in the code, but effectively ignores all differences in whitespace, and potentially linebreaks as well.

Another possible thing (*much* more ambitious) would be a cross between the C preprocessor and some simplified regex language.

I frequently do regex based F&R, but entering expressions to match ints, floats, strings, function arguments, etc is very tedious. For things like function arguments it technically isn't possible since it requires matching parenthesis.

I just thought of this, so I haven't worked out the details, but for example, give this code:
    a = f(2, "hello", 5);
    b = f(2, g_ConstString, 5);
Search for all "f" function calls where the 2nd parameter is a string and move the string out to a separate variable like so:
    const char *var_a = "hello";
    a = f(2, var_a, 5);

This simple case could be done with a regex, but the regex starts getting complex when you try to generalize it to handle a case like
    c = f( 2 * (3 + 4), "another", 5);

The syntax could be something like:
    Search for: #ID = f(#ARG, #STRING, #ARG)
    Replace with: const char *var_$1 = $3; $1 = f($2, var, $4)
Slick already knows how to do basic parsing - like matching parens, comma separated arguments, even statements. Leveraging that with a regex like language would be pretty powerful.