microsoft / PTVS

Python Tools for Visual Studio
https://aka.ms/PTVS
Apache License 2.0
2.52k stars 673 forks source link

Extract method returns incorrect code #7864

Closed StellaHuang95 closed 2 months ago

StellaHuang95 commented 3 months ago

Steps to Reproduce

  1. Type
    x = 1
    y = 1
  2. Select the two lines and extract method on the lines

Expected behavior

def new_func():
    x = 1
    y = 1

new_func()

Actual behavior

def new_func():
    x = 1

new_func()
y = 1
StellaHuang95 commented 3 months ago

Extract Method on the three lines inside func below does nothing:

def func():
    x = 1
    y = 2
    return x * y
StellaHuang95 commented 2 months ago

workspace/applyEdit message is:


  "jsonrpc": "2.0",
  "id": 8,
  "method": "workspace/applyEdit",
  "params": {
    "label": "Command 'pylance.extractMethod'",
    "edit": {
      "changes": {
        "file:///c%3A/Users/stellahuang/source/repos/PythonApplication10/PythonApplication10/PythonApplication10.py": [
          {
            "range": {
              "start": {
                "line": 1,
                "character": 0
              },
              "end": {
                "line": 1,
                "character": 0
              }
            },
            "newText": "def new_func():\n    x = 1\n\n"
          },
          {
            "range": {
              "start": {
                "line": 1,
                "character": 0
              },
              "end": {
                "line": 1,
                "character": 5
              }
            },
            "newText": "new_func()"
          }
        ]
      }
    },
    "metadata": {
      "isRefactoring": true
    }
  }

so looks like pylance isn't returning the correct edits at the first place.

StellaHuang95 commented 2 months ago

but the textDocument/codeAction request is sending the wrong range to pylance, the _vs_selectionRange has the correct range.

  "traceparent": "00-319dc53d5a70fd459c7fa23603b3a7c5-46f9f679959dbc4d-01",
  "jsonrpc": "2.0",
  "id": 16,
  "method": "textDocument/codeAction",
  "params": {
    "textDocument": {
      "uri": "file:///C:/Users/stellahuang/source/repos/PythonApplication10/PythonApplication10/PythonApplication10.py"
    },
    "range": {
      "start": {
        "line": 1,
        "character": 0
      },
      "end": {
        "line": 1,
        "character": 5
      }
    },
    "context": {
      "_vs_selectionRange": {
        "start": {
          "line": 1,
          "character": 0
        },
        "end": {
          "line": 2,
          "character": 5
        }
      },
      "diagnostics": [],
      "triggerKind": 1
    }
  }
StellaHuang95 commented 2 months ago

https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1378352

See above for the response from LSP team. It's by design that the range is always current line in VS. If pylance depends on the range, we will need to use _vs_selectionRange instead.

StellaHuang95 commented 2 months ago
def func():
    x = 1
    y = 2
    return x * y

This is a separate issue, let me file another ticket to track it.