scalameta / nvim-metals

A Metals plugin for Neovim
https://scalameta.org/metals/
Apache License 2.0
455 stars 74 forks source link

Import code action not editing document correctly #656

Open pritchett opened 4 months ago

pritchett commented 4 months ago

Describe the bug

When activating a code action to import a class, the document gets edited incorrectly. The line you are on gets messed up.

https://github.com/scalameta/nvim-metals/assets/35229419/16bb4769-a492-40bf-a7d0-eb69d8159ed3

Expected behavior

The import is correct but the current line should not be edited.

Operating system

macOS

Version of Metals

v1.2.2

Commit of nvim-metals

4f9bf0c821092f20dd34c1a4ea5b07b0500ff089

ckipp01 commented 4 months ago

Sorry I missed this @pritchett. I'm unable to reproduce this however. I am using the following to test:

//> using scala 3.4.0
//> using dep "org.scalatest::scalatest:3.2.18"

class ExampleTest extends AnyFlatSpec with Matchers {}

When I trigger a code action on AnyFlatSpec I see the following:

[Trace - 11:24:45 AM] Sending response 'textDocument/codeAction - (246)'. Processing request took 123ms
Result: [
  {
    "title": "Import \u0027AnyFlatSpec\u0027 from package \u0027org.scalatest.flatspec\u0027",
    "kind": "quickfix",
    "diagnostics": [
      {
        "range": {
          "start": {
            "line": 3,
            "character": 26
          },
          "end": {
            "line": 3,
            "character": 37
          }
        },
        "severity": 1,
        "code": "6",
        "source": "bloop",
        "message": "Not found: type AnyFlatSpec",
        "data": {
          "actions": []
        }
      }
    ],
    "edit": {
      "changes": {
        "file:///Users/ckipp/Documents/scala-workspace/minimal/project.scala": [
          {
            "range": {
              "start": {
                "line": 2,
                "character": 0
              },
              "end": {
                "line": 2,
                "character": 0
              }
            },
            "newText": "import org.scalatest.flatspec.AnyFlatSpec\n"
          }
        ]
      }
    }
  },
  {
    "title": "Create new symbol \u0027AnyFlatSpec\u0027...",
    "kind": "quickfix",
    "diagnostics": [
      {
        "range": {
          "start": {
            "line": 3,
            "character": 26
          },
          "end": {
            "line": 3,
            "character": 37
          }
        },
        "severity": 1,
        "code": "6",
        "source": "bloop",
        "message": "Not found: type AnyFlatSpec",
        "data": {
          "actions": []
        }
      }
    ],
    "edit": {
      "changes": {}
    },
    "command": {
      "title": "Create new scala file",
      "command": "new-scala-file",
      "arguments": [
        "file:///Users/ckipp/Documents/scala-workspace/minimal/",
        "AnyFlatSpec"
      ]
    }
  },
  {
    "title": "Create companion object for ExampleTest",
    "kind": "refactor.rewrite",
    "diagnostics": [],
    "edit": {
      "changes": {
        "file:///Users/ckipp/Documents/scala-workspace/minimal/project.scala": [
          {
            "range": {
              "start": {
                "line": 3,
                "character": 54
              },
              "end": {
                "line": 3,
                "character": 54
              }
            },
            "newText": "\n\nobject ExampleTest:\n  ???"
          }
        ]
      }
    },
    "command": {
      "title": "Goto location for position",
      "command": "goto-position",
      "arguments": [
        {
          "uri": "file:///Users/ckipp/Documents/scala-workspace/minimal/project.scala",
          "range": {
            "start": {
              "line": 6,
              "character": 0
            },
            "end": {
              "line": 6,
              "character": 0
            }
          }
        }
      ]
    }
  }
]

This has the correct edit that I'd expect. What you can do is create a .metals/lsp.trace.json file. Then restart metals and open the file you are showing in your video, trigger the code action from FlatSpec and past the result from Metals? I have a suspicion that it's coming back from Metals correctly but something with your local config/other plugins/etc is messing with how this ends up being applied for you locally.

pritchett commented 3 months ago

lsp.trace.json This generated quite a bit of output in a few seconds! Just search from the bottom, I found on line 21851 an edit that has "FlatSpMatchersMatchers" which seems wrong, but I'm not entirely sure what I'm looking at here.

pritchett commented 3 months ago

This has stopped happening after I updated metals to 1.3.0