microsoft / vscode-isort

Import sorting for python using the isort library.
https://marketplace.visualstudio.com/items?itemName=ms-python.isort
MIT License
87 stars 21 forks source link

Ctrl+. doesn't work for isort Quick Fix #22

Closed gregvanl closed 2 years ago

gregvanl commented 2 years ago

Testing microsoft/vscode-python#18997

  1. Follow the test plan instructions until you get the red squiggly under the first import.
  2. Put the cursor on the first import.
  3. Press Ctrl+. to show Quick Fixes.

"No code actions available" shown in hover

image

  1. Hover over first import so that you see the Problem text along with link to Quick Fix (Ctrl+.)

image

  1. Try Ctrl+.

Again "No code actions available" hover

karthiknadig commented 2 years ago

@dbaeumer I need some input here. For this scenario, I think I am sending the correct data for code actions (over LSP) but, VS Code seems to think not. Somehow, the same info is shown on hover.

Request (for ctrl+.):

  {
    "jsonrpc": "2.0",
    "id": 21,
    "method": "textDocument/codeAction",
    "params": {
      "textDocument": {
        "uri": "file:///c%3A/GIT/repros/formatTest/something.py"
      },
      "range": {
        "start": { "line": 0, "character": 3 },
        "end": { "line": 0, "character": 3 }
      },
      "context": { "diagnostics": [] }
    }
  },

Response:

  {
    "jsonrpc": "2.0",
    "id": 21,
    "result": [
      {
        "title": "isort: Organize Imports",
        "kind": "source.organizeImports",
        "edit": null,
        "data": "file:///c%3A/GIT/repros/formatTest/something.py"
      },
      {
        "title": "isort: Fix import sorting and/or formatting",
        "kind": "quickfix",
        "diagnostics": null,
        "edit": null,
        "data": "file:///c%3A/GIT/repros/formatTest/something.py"
      }
    ]
  }
karthiknadig commented 2 years ago

@dbaeumer For hover it looks like we do get the diagnostics in context, but there is no diagnostic in context for ctrl+..

Request (for hover):

  {
    "jsonrpc": "2.0",
    "id": 31,
    "method": "textDocument/codeAction",
    "params": {
      "textDocument": {
        "uri": "file:///c%3A/GIT/repros/formatTest/something.py"
      },
      "range": {
        "start": { "line": 0, "character": 0 },
        "end": { "line": 0, "character": 0 }
      },
      "context": {
        "diagnostics": [
          {
            "range": {
              "start": { "line": 0, "character": 0 },
              "end": { "line": 0, "character": 0 }
            },
            "message": "Imports are incorrectly sorted and/or formatted.",
            "code": "E",
            "severity": 1,
            "source": "isort"
          }
        ],
        "only": ["quickfix"]
      }
    }
  }

Response:

  {
    "jsonrpc": "2.0",
    "id": 31,
    "result": [
      {
        "title": "isort: Organize Imports",
        "kind": "source.organizeImports",
        "edit": null,
        "data": "file:///c%3A/GIT/repros/formatTest/something.py"
      },
      {
        "title": "isort: Fix import sorting and/or formatting",
        "kind": "quickfix",
        "diagnostics": [
          {
            "range": {
              "start": { "line": 0, "character": 0 },
              "end": { "line": 0, "character": 0 }
            },
            "message": "Imports are incorrectly sorted and/or formatted.",
            "severity": 1,
            "code": "E",
            "source": "isort"
          }
        ],
        "edit": null,
        "data": "file:///c%3A/GIT/repros/formatTest/something.py"
      }
    ]
  }
karthiknadig commented 2 years ago

Figured out the issue. Range was the problem here. Somehow hover, is able to identify the range. But when using ctrl+. it does not accept the range. Updating the original diagnostic range from "start": { "line": 0, "character": 0 }, "end": { "line": 0, "character": 0 } to "start": { "line": 0, "character": 0 }, "end": { "line": 1, "character": 0 } seems to have fixed the issue.

Should VS Code be handing the range for ctrl+. the same way it does for hover?