Sublime Forum

VIew replace not working as expected for php code

#1

When selecting

#for explaining i included the selection
# view.sel()[0] = '$condition1 = isset($_POST[ $this->prefix . $customField['name'] ]);
                $condition2 = trim( $_POST[ $this->prefix . $customField['name'] ] );'
    view = self.view
    region = view.sel()[0]
    theLines = view.lines(region)
    if len(theLines) > 0:
        for region in theLines:
            # print(regionContentActual)
            if not region.empty():
                currentLine = view.substr(region)
                if ';' in currentLine:
                    lineVars = getVars(currentLine)
                    if lineVars:
                        lineVars = '$condition2, $_POST, $customField'
                        regionContentToAdd = 'var_dump(' + lineVars + ');'
                        newRegionContent = currentLine + regionContentToAdd
                        if regionContentToAdd in currentLine:
                            regionContentToAdd = re.sub(r"var_dump\((.*?)\);", "", currentLine)
                            newRegionContent = currentLine.replace(regionContentToAdd, "")
                        view.replace(edit, region, newRegionContent)

RESULTS

$condition1 = isset($_POST[ $this->prefix . $customField['name'] ]);var_dump($condition2, $_POST, $customField);
$condition2 = trim( $_POSvar_dump($condition2, $_POST, $customField);T[ $this->prefix . $customField['name'] ] );

INSTEAD OF

$condition1 = isset($_POST[ $this->prefix . $customField['name'] ]);var_dump($condition2, $_POST, $customField);
$condition2 = trim( $_POST[ $this->prefix . $customField['name'] ] );var_dump($condition2, $_POST, $customField);
0 Likes

#2

Your codes are incomplete (probably not important. idk.) and you don’t provide the input (what’s in the view buffer? what’s the state of selections?).

0 Likes

#3

I specifed on the virst line what is the selection.

# view.sel()[0] = '$condition1 = isset($_POST[ $this->prefix . $customField['name'] ]);
                $condition2 = trim( $_POST[ $this->prefix . $customField['name'] ]
0 Likes

#4

You have to do edits in reversed order. That is, modify the content from the end to the begin. Otherwise, after your first edit, all region offsets will be changed.

for region in reversed(theLines):
2 Likes

#5

Worked well Thank you.

0 Likes