microsoft / PTVS

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

Extract Method on multiple lines inside of a function does nothing #7875

Open StellaHuang95 opened 2 months ago

StellaHuang95 commented 2 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

textDocument/codeAction:

rawjson {
  "jsonrpc": "2.0",
  "id": 135,
  "result": [
    {
      "title": "Extract method",
      "command": {
        "title": "Extract method",
        "command": "pylance.extractMethod",
        "arguments": [
          "file:///c%3A/Users/stellahuang/source/repos/PythonApplication13/PythonApplication13/PythonApplication13.py",
          {
            "start": {
              "line": 1,
              "character": 4
            },
            "end": {
              "line": 3,
              "character": 16
            }
          }
        ]
      },
      "kind": "refactor.extract.method"
    }
  ]
}

workspace/applyEdit request:

rawjson {
  "jsonrpc": "2.0",
  "id": 16,
  "method": "workspace/applyEdit",
  "params": {
    "label": "Command 'pylance.extractMethod'",
    "edit": {
      "changes": {
        "file:///c%3A/Users/stellahuang/source/repos/PythonApplication13/PythonApplication13/PythonApplication13.py": [
          {
            "range": {
              "start": {
                "line": 1,
                "character": 4
              },
              "end": {
                "line": 3,
                "character": 16
              }
            },
            "newText": "return new_func()"
          },
          {
            "range": {
              "start": {
                "line": 3,
                "character": 17
              },
              "end": {
                "line": 3,
                "character": 17
              }
            },
            "newText": "\n\ndef new_func():\n    x = 1\n    y = 2\n    return x * y"
          }
        ]
      }
    },
    "metadata": {
      "isRefactoring": true
    }
  }
}

workspace/applyEdit response

result.failurereason    Exception while applying edits: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: position
   at Microsoft.VisualStudio.Text.SnapshotPoint..ctor(ITextSnapshot snapshot, Int32 position)
   at Microsoft.VisualStudio.Text.SnapshotPoint.Add(Int32 offset)
   at Microsoft.VisualStudio.LanguageServer.Client.Utilities.GetSnapshotPositionFromProtocolPosition(ITextSnapshot textSnapshot, Position position)
   at Microsoft.VisualStudio.LanguageServer.Client.WorkspaceEditBroker.ApplyTextEdit(TextEdit textEdit, ITextSnapshot snapshot, ITextEdit vsTextEdit)
   at Microsoft.VisualStudio.LanguageServer.Client.WorkspaceEditBroker.ApplyTextEditsToBuffer(IEnumerable`1 textEdits, ITextSnapshot snapshot, EditOptions editOptions, ITextBuffer textBuffer)
   at Microsoft.VisualStudio.LanguageServer.Client.WorkspaceEditBroker.ApplySingleEdit(Uri uri, Nullable`1 version, IEnumerable`1 textEdits, IDocumentLanguageServiceClient currentDocumentClient, EditOptions editOptions, CancellationToken cancellationToken)
   at Microsoft.VisualStudio.LanguageServer.Client.WorkspaceEditBroker.ApplyDocumentEdit(TextDocumentEdit textDocumentEdit, ILanguageServiceClient languageClient, ApplyWorkspaceEditResponse response, EditOptions editOptions, CancellationToken cancellationToken)
   at Microsoft.VisualStudio.LanguageServer.Client.WorkspaceEditBroker.ApplyWorkspaceEdit(WorkspaceEdit workspaceEdit, ILanguageServiceClient languageClient, CancellationToken cancellationToken)
   at Microsoft.VisualStudio.LanguageServer.Client.WorkspaceEditBroker.<ApplyWorkspaceEditAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.LanguageServer.Client.RemoteLanguageTarget.<OnWorkspaceApplyEditAsync>d__19.MoveNext()