Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scalafmt is not working on save #598

Closed
gogamid opened this issue Aug 20, 2023 · 2 comments
Closed

scalafmt is not working on save #598

gogamid opened this issue Aug 20, 2023 · 2 comments

Comments

@gogamid
Copy link

gogamid commented Aug 20, 2023

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
Copy link
Member

ckipp01 commented Aug 21, 2023

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 scalameta/metals#5559.

@gogamid
Copy link
Author

gogamid commented Aug 21, 2023

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

version = "3.7.10"
runner.dialect = scala213

Thank you

@gogamid gogamid closed this as completed Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants