scalameta / nvim-metals

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

scalafmt is not working on save #598

Closed gogamid closed 1 year ago

gogamid commented 1 year ago

Describe the bug

I thought scalafmt is builtin in nvim-metals, but when I format code, i get this error:

image

I use lazyvim

Configs: metals.lua

return {
  "scalameta/nvim-metals",
  dependencies = { "nvim-lua/plenary.nvim", "mfussenegger/nvim-dap" },
  ft = { "scala", "sbt" },
  keys = {
    {
      "<leader>cW",
      function()
        require("metals").hover_worksheet()
      end,
      desc = "Metals Worksheet",
    },
    {
      "<leader>cM",
      function()
        require("telescope").extensions.metals.commands()
      end,
      desc = "Telescope Metals Commands",
    },
  },
  event = "BufEnter *.worksheet.sc",
  config = function()
    local api = vim.api
    ----------------------------------
    -- OPTIONS -----------------------
    ----------------------------------
    -- global
    vim.opt_global.completeopt = { "menuone", "noinsert", "noselect" }
    vim.opt_global.shortmess:remove("F")
    vim.opt_global.shortmess:append("c")
    -- LSP Setup ---------------------
    ----------------------------------
    local metals_config = require("metals").bare_config()

    -- Example of settings
    metals_config.settings = {
      showImplicitArguments = true,
      showInferredType = true,
      superMethodLensesEnabled = true,
      showImplicitConversionsAndClasses = true,
    }

    -- *READ THIS*
    -- I *highly* recommend setting statusBarProvider to true, however if you do,
    -- you *have* to have a setting to display this in your statusline or else
    -- you'll not see any messages from metals. There is more info in the help
    -- docs about this
    metals_config.init_options.statusBarProvider = "on"

    -- Example if you are using cmp how to make sure the correct capabilities for snippets are set
    metals_config.capabilities = require("cmp_nvim_lsp").default_capabilities()

    -- Debug settings if you're using nvim-dap
    local dap = require("dap")

    dap.configurations.scala = {
      {
        type = "scala",
        request = "launch",
        name = "RunOrTest",
        metals = {
          runType = "runOrTestFile",
          --args = { "firstArg", "secondArg", "thirdArg" }, -- here just as an example
        },
      },
      {
        type = "scala",
        request = "launch",
        name = "Test Target",
        metals = {
          runType = "testTarget",
        },
      },
    }

    metals_config.on_attach = function(client, bufnr)
      local metals = require("metals")
      metals.setup_dap()

      local wk = require("which-key")
      wk.register({
        ["<localleader>"] = {
          h = {
            name = "hover",
            c = {
              function()
                metals.toggle_setting("showImplicitConversionsAndClasses")
              end,
              "Toggle show implicit conversions and classes",
            },
            i = {
              function()
                metals.toggle_setting("showImplicitArguments")
              end,
              "Toggle show implicit arguments",
            },
            t = {
              function()
                metals.toggle_setting("showInferredType")
              end,
              "Toggle show inferred type",
            },
          },
          t = {
            name = "Tree view",
            t = {
              function()
                require("metals.tvp").toggle_tree_view()
              end,
              "Toggle tree view",
            },
            r = {
              function()
                require("metals.tvp").reveal_in_tree()
              end,
              "Review in tree view",
            },
          },
          w = {
            function()
              metals.hover_worksheet({ border = "single" })
            end,
            "Hover worksheet",
          },
        },
      }, {
        buffer = bufnr,
      })
      wk.register({
        ["<localleader>t"] = {
          function()
            metals.type_of_range()
          end,
          "Type of range",
        },
      }, {
        mode = "v",
        buffer = bufnr,
      })
    end

    require("metals").initialize_or_attach(metals_config)
  end,
}

autocmd.lua

-- Autocmds are automatically loaded on the VeryLazy event
-- Default autocmds that are always set: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/autocmds.lua
-- Add any additional autocmds here
vim.api.nvim_create_autocmd({ "BufEnter" }, {
  pattern = { "*.md" },
  callback = function(args)
    local wk = require("which-key")
    wk.register({
      ["<localleader>"] = {
        r = {
          ":!pandoc -t revealjs -s -o index.html % --highlight=zenburn -V revealjs-url=https://unpkg.com/reveal.js/ -V theme=solarized<CR>",
          "Export to revealjs",
        },
      },
    }, {
      buffer = args.buf,
    })
  end,
})

Expected behavior

No response

Operating system

macOS

Version of Metals

1.0.0

Commit of nvim-metals

57cff9a

ckipp01 commented 1 year ago

Hey @gogamid, so scalafmt support is indeed part of Metals. By default scalafmt requires you to have a .scalafmt.conf file in order to know how to format your code. The pop up that you're seeing is Metals actually telling you that it can't find a config file. So you should be able to select the first option of creating the file, and then it should work as expected after that. We're actually discussing the behavior of this in https://github.com/scalameta/metals/pull/5559.

gogamid commented 1 year ago

after creating it and putting these lines format the scala code:

version = "3.7.10"
runner.dialect = scala213

Thank you