Komodo / KomodoEdit

Komodo Edit is a fast and free multi-language code editor. Written in JS, Python, C++ and based on the Mozilla platform.
http://www.komodoide.com/komodo-edit
Other
2.15k stars 301 forks source link

Allow for use of $1 in regex find and replace #2919

Open babobski opened 7 years ago

babobski commented 7 years ago

Short Summary

I wanted to preform a search and replace with regex, and use the group in the replace output. It is only not replacing the $1 but just outputing $1

Steps to Reproduce

Create a file with this content:

.mfp-arrow {
    -webkit-user-select: none;
    -moz-user-select: none;
    user-select: none;
    position: absolute;
    opacity: 0.65;
    filter: alpha(opacity=65);
    margin: 0;
    top: 50%;
    margin-top: -55px;
    padding: 0;
    width: 90px;
    height: 110px;
    -webkit-tap-highlight-color: transparent;
    &:active {
        margin-top: -54px;
    }
    &:hover {
        opacity: 1;
        filter: alpha(opacity=100);
    }
    &:focus {
        opacity: 1;
        filter: alpha(opacity=100);
    }
    &:before {
        content: '';
        display: block;
        width: 0;
        height: 0;
        position: absolute;
        left: 0;
        top: 0;
        margin-top: 35px;
        margin-left: 35px;
        border: medium inset transparent;
        border-top-width: 21px;
        border-bottom-width: 21px;
        opacity: 0.7;
    }
    &:after {
        content: '';
        display: block;
        width: 0;
        height: 0;
        position: absolute;
        left: 0;
        top: 0;
        margin-top: 35px;
        margin-left: 35px;
        border: medium inset transparent;
        border-top-width: 13px;
        border-bottom-width: 13px;
        top: 8px;
    }
    .mfp-b {
        content: '';
        display: block;
        width: 0;
        height: 0;
        position: absolute;
        left: 0;
        top: 0;
        margin-top: 35px;
        margin-left: 35px;
        border: medium inset transparent;
        border-top-width: 21px;
        border-bottom-width: 21px;
        opacity: 0.7;
    }
    .mfp-a {
        content: '';
        display: block;
        width: 0;
        height: 0;
        position: absolute;
        left: 0;
        top: 0;
        margin-top: 35px;
        margin-left: 35px;
        border: medium inset transparent;
        border-top-width: 13px;
        border-bottom-width: 13px;
        top: 8px;
    }
}

In you're find field : (alpha\(opacity\=[0-9'"]+\)) And in you're replace: ~"$1"

Expected results

.mfp-arrow {
    -webkit-user-select: none;
    -moz-user-select: none;
    user-select: none;
    position: absolute;
    opacity: 0.65;
    filter: ~"alpha(opacity=65)";
    margin: 0;
    top: 50%;
    margin-top: -55px;
    padding: 0;
    width: 90px;
    height: 110px;
    -webkit-tap-highlight-color: transparent;
    &:active {
        margin-top: -54px;
    }
    &:hover {
        opacity: 1;
        filter: ~"alpha(opacity=100)";
    }
    &:focus {
        opacity: 1;
        filter: ~"alpha(opacity=100)";
    }
    &:before {
        content: '';
        display: block;
        width: 0;
        height: 0;
        position: absolute;
        left: 0;
        top: 0;
        margin-top: 35px;
        margin-left: 35px;
        border: medium inset transparent;
        border-top-width: 21px;
        border-bottom-width: 21px;
        opacity: 0.7;
    }
    &:after {
        content: '';
        display: block;
        width: 0;
        height: 0;
        position: absolute;
        left: 0;
        top: 0;
        margin-top: 35px;
        margin-left: 35px;
        border: medium inset transparent;
        border-top-width: 13px;
        border-bottom-width: 13px;
        top: 8px;
    }
    .mfp-b {
        content: '';
        display: block;
        width: 0;
        height: 0;
        position: absolute;
        left: 0;
        top: 0;
        margin-top: 35px;
        margin-left: 35px;
        border: medium inset transparent;
        border-top-width: 21px;
        border-bottom-width: 21px;
        opacity: 0.7;
    }
    .mfp-a {
        content: '';
        display: block;
        width: 0;
        height: 0;
        position: absolute;
        left: 0;
        top: 0;
        margin-top: 35px;
        margin-left: 35px;
        border: medium inset transparent;
        border-top-width: 13px;
        border-bottom-width: 13px;
        top: 8px;
    }
}

Actual results

.mfp-arrow {
    -webkit-user-select: none;
    -moz-user-select: none;
    user-select: none;
    position: absolute;
    opacity: 0.65;
    filter: ~'$1';
    margin: 0;
    top: 50%;
    margin-top: -55px;
    padding: 0;
    width: 90px;
    height: 110px;
    -webkit-tap-highlight-color: transparent;
    &:active {
        margin-top: -54px;
    }
    &:hover {
        opacity: 1;
        filter: ~'$1';
    }
    &:focus {
        opacity: 1;
        filter: ~'$1';
    }
    &:before {
        content: '';
        display: block;
        width: 0;
        height: 0;
        position: absolute;
        left: 0;
        top: 0;
        margin-top: 35px;
        margin-left: 35px;
        border: medium inset transparent;
        border-top-width: 21px;
        border-bottom-width: 21px;
        opacity: 0.7;
    }
    &:after {
        content: '';
        display: block;
        width: 0;
        height: 0;
        position: absolute;
        left: 0;
        top: 0;
        margin-top: 35px;
        margin-left: 35px;
        border: medium inset transparent;
        border-top-width: 13px;
        border-bottom-width: 13px;
        top: 8px;
    }
    .mfp-b {
        content: '';
        display: block;
        width: 0;
        height: 0;
        position: absolute;
        left: 0;
        top: 0;
        margin-top: 35px;
        margin-left: 35px;
        border: medium inset transparent;
        border-top-width: 21px;
        border-bottom-width: 21px;
        opacity: 0.7;
    }
    .mfp-a {
        content: '';
        display: block;
        width: 0;
        height: 0;
        position: absolute;
        left: 0;
        top: 0;
        margin-top: 35px;
        margin-left: 35px;
        border: medium inset transparent;
        border-top-width: 13px;
        border-bottom-width: 13px;
        top: 8px;
    }
}

Platform Information

Komodo X.2.3 Windows 10

Workarround

Create a userscript for this action.

(function(){
    var koDoc = ko.views.manager.currentView.koDoc;
    if (koDoc !== 'undefined') {
        var buffer = koDoc.buffer;
        var newText = buffer.replace(/(alpha\(opacity\=[0-9'"]+\))/g, '~"$1"');

        koDoc.buffer = newText;
    }
}).apply();
Naatan commented 7 years ago

You were likely using different Rx engines. @babobski what engine were you using?

mitchell-as commented 7 years ago

It looks like he's using the find/replace pane, which uses Python's regex engine. That means \1 needs to be used instead of $1.

Naatan commented 7 years ago

Is it feasible to facilitate both $1 and \1 @mitchell-as? I'd say so from a user perspective, but there might be edge cases I can't think of on the spot.

mitchell-as commented 7 years ago

@Naatan Yes I think it's feasible as long as it doesn't choke Python's existing replace syntax. Adding support for $n would just require some post-processing and perhaps special handling of \$ sequences.

Naatan commented 7 years ago

Alright, thanks @mitchell-as .