mikedeboer/jsDAV — pull request #33 by gjtorikian (2012-03-02 01:21:26+00:00)
Title: Ext/replaceall

Discussions about this PR are split; the client-side stuff can be found here:

This change is intended to support find and replace across all files. In addition, it also supports multiline searches, _even if not searching on terms with a regular expression_ in them. Explanation for this below.

First, let's assume the user wants to do a find and replace. The situation is incredibly easy if regular expressions are not used--we can just pipe our grep findings into sed and be done with it. However, due to the requirements that regular expressions might be used, everything changes dramatically. grep has the `-P` option, which enables Perl-compatible syntaxes for regular expressions. So far, so good. However, it seems that on the replacement side, not many other Unix commands (sed, awk, gawk) support Perl syntax--except, of course, Perl itself. So, in the event of a replace, we're now piping the grep results into Perl, to both perform the transformation, AND, print the filename:linenumber:change out to STDOUT. This is so that we can take the output and format it into a pretty XML tree that the Cloud9 console uses.

Now, let's assume that the user wants to do a multiline search without regular expressions. grep supports this, but only once again passing the `-P` flag in order to detect the newline character (`\n`). Because we are running this search without regular expressions, we need to escape the entire syntax of the search, before passing it into grep.

Two simple files changes, a thousand word essay.

* * *

I have tested this find/replace on a variety of regular expressions, including support for grouping `( )`, quotation marks, and escaping shorthand operators (like searching for `\w`, both inside and outside of regular expressions). I'm confident it works, but some additional testing would not be a bad idea.