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

lsp format volar delete my code #4066

Closed
bobbyrahmanda13 opened this issue Mar 11, 2024 · 18 comments
Closed

lsp format volar delete my code #4066

bobbyrahmanda13 opened this issue Mar 11, 2024 · 18 comments
Labels
bug Something isn't working upstream

Comments

@bobbyrahmanda13
Copy link

before:
image

after: :lua vim.lsp.buf.format()
image

image
image

@gegoune
Copy link

gegoune commented Mar 11, 2024

#3925 (comment) (and #3925 (comment)).

@RayGuo-ergou
Copy link
Contributor

It's formatted by tsserver. Try :LspStop tsserver then format, you will see the code format correctly.

To resolve I think you can,

  • as @gegoune posted disabled tsserver format for vue files (if you still want volar)
  • My personal recommendation, use a linter (prettier, eslint or biome) rather than LSP format. They are more configurable than native lsp format.

@johnsoncodehk johnsoncodehk added the bug Something isn't working label Mar 11, 2024
@bobbyrahmanda13
Copy link
Author

bobbyrahmanda13 commented Mar 11, 2024

It's formatted by tsserver. Try :LspStop tsserver then format, you will see the code format correctly.

To resolve I think you can,

  • as @gegoune posted disabled tsserver format for vue files (if you still want volar)
  • My personal recommendation, use a linter (prettier, eslint or biome) rather than LSP format. They are more configurable than native lsp format.

if I use :LspStop tsserver then all my Lsp are inactive, so it won't work, lsp on the .ts and .js formatting extension still works but if on vue then volar will be active so that's an error, I've tried it.

@bobbyrahmanda13
Copy link
Author

bobbyrahmanda13 commented Mar 11, 2024

my config lsp

local lspconfig = require 'lspconfig'
local util = require 'lspconfig.util'
local bind = vim.keymap.set

local borderLsp = "rounded"
-- local borderLsp = { "╔", "═", "╗", "║", "╝", "═", "╚", "║" }

vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(
  vim.lsp.handlers.hover, {
    border = borderLsp
  }
)

vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(
  vim.lsp.handlers.signature_help, {
    border = borderLsp
  }
)
require('lspconfig.ui.windows').default_options.border = borderLsp

-- create underline
-- local hl_groups = { 'DiagnosticUnderlineError','DiagnosticUnderlineWarn','DiagnosticUnderlineInfo','DiagnosticUnderlineHint' }
-- for _, hl in ipairs(hl_groups) do
--   vim.cmd.highlight(hl .. ' gui=undercurl')
-- end

vim.cmd([[highlight ErrorMsg guibg=#3d0000 ]])
vim.cmd([[highlight WarningMsg guibg=#513A1A ]])
vim.cmd([[highlight DiagnosticHintLn guibg=#024649 ]])
vim.cmd([[highlight DiagnosticInfoLn guibg=#024649 ]])

local signsIcon = { ERROR = "", WARN = "", HINT = "󰠠 ", INFO = "" }

vim.diagnostic.config({
  float = { border = borderLsp },
  -- virtual_text = true, --default true
  -- underline = true,
  -- severity_sort = true,
  signs = {
    text = {
      [vim.diagnostic.severity.ERROR] = signsIcon.ERROR,
      [vim.diagnostic.severity.WARN] = signsIcon.WARN,
      [vim.diagnostic.severity.INFO] = signsIcon.INFO,
      [vim.diagnostic.severity.HINT] = signsIcon.HINT,
    },
    --   linehl = {
    --     [vim.diagnostic.severity.ERROR] = 'ErrorMsg',
    --     [vim.diagnostic.severity.WARN] = 'WarningMsg',
    --     [vim.diagnostic.severity.INFO] = 'DiagnosticInfoLn',
    --     [vim.diagnostic.severity.HINT] = 'DiagnosticHintLn',
    --   },
    numhl = {
      [vim.diagnostic.severity.ERROR] = 'ErrorMsg',
      [vim.diagnostic.severity.WARN] = 'WarningMsg',
      [vim.diagnostic.severity.INFO] = 'DiagnosticInfo',
      [vim.diagnostic.severity.HINT] = 'DiagnosticHint',
    },
  }
})

-- deprecated
-- local on_attach = function(_, bufnr)
--   local opts = { buffer = bufnr, noremap = true, silent = true }
vim.api.nvim_create_autocmd('LspAttach', {
  group = vim.api.nvim_create_augroup('UserLspConfig', {}),
  callback = function(ev)
    -- Enable completion triggered by <c-x><c-o>
    vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc'

    -- Buffer local mappings.
    -- See `:help vim.lsp.*` for documentation on any of the below functions
    local opts = { buffer = ev.buf }
    bind('n', 'gdc', vim.lsp.buf.declaration, opts)
    bind('n', 'gdf', vim.lsp.buf.definition, opts)
    bind('n', 'K', vim.lsp.buf.hover, opts)
    bind('n', '<leader>vd', vim.diagnostic.open_float, opts)
    bind('n', '[d', vim.diagnostic.goto_next, opts)
    bind('n', ']d', vim.diagnostic.goto_prev, opts)
    bind('n', '<leader>vca', vim.lsp.buf.code_action, opts)
    bind('n', '<leader>vrr', vim.lsp.buf.references, opts)
    -- bind('n', '<leader>vrr', "<cmd>Telescope lsp_references<CR>", opts)
    bind('n', '<leader>vrn', vim.lsp.buf.rename, opts)
    bind('i', '<C-h>', vim.lsp.buf.signature_help, opts)
    bind('n', '<leader>hf', function()
      vim.lsp.buf.format({ async = true})
    end, opts)
  end,
})

-- cmp
-- used to enable autocompletion
local capabilities = require("cmp_nvim_lsp").default_capabilities()

-- Enable the following language servers
local servers = {"tsserver","volar"}

for _, lsp in ipairs(servers) do
  lspconfig[lsp].setup {
    -- on_attach = on_attach,
    capabilities = capabilities,
  }
end

lspconfig["tsserver"].setup({
  -- on_attach = on_attach,
  capabilities = capabilities,
  root_dir = vim.loop.cwd,
  init_options = {
    hostInfo = "neovim",
    plugins = {
      {
        name = "@vue/typescript-plugin",
        location = "/home/rahman/.local/share/pnpm/global/5/node_modules/@vue/typescript-plugin",
        languages = {"vue"}
      },
    },
  },
  filetypes = {
    "javascript",
    "typescript",
    "vue"
  },
})

-- local volar_path=''
local tslib_path='/home/rahman/.local/share/pnpm/global/5/node_modules/typescript/lib/'

lspconfig["volar"].setup({
  -- on_attach = on_attach,
  capabilities = capabilities,
  filetypes = {'typescript', 'javascript', 'javascriptreact', 'typescriptreact', 'vue', 'json'},
  root_dir = util.root_pattern 'package.json',
  init_options = {
    typescript = {
      tsdk = tslib_path
      -- Alternative location if installed as root:
      -- tsdk = '/usr/local/lib/node_modules/typescript/lib'
    },
  }
})

-- luasnip setup
local luasnip = require 'luasnip'

-- nvim-cmp setup
local cmp = require 'cmp'
cmp.setup {
  snippet = {
    expand = function(args)
      luasnip.lsp_expand(args.body)
    end,
  },
  mapping = cmp.mapping.preset.insert({
    ['<C-b>'] = cmp.mapping.scroll_docs(-4), -- Up
    ['<C-f>'] = cmp.mapping.scroll_docs(4), -- Down
    -- C-b (back) C-f (forward) for snippet placeholder navigation.
    ['<C-Space>'] = cmp.mapping.complete(),
    ['<C-e>'] = cmp.mapping.abort(),
    ['<CR>'] = cmp.mapping.confirm({ select = true }),

    ['<Tab>'] = cmp.mapping(function(fallback)
      if cmp.visible() then
        cmp.select_next_item()
      elseif luasnip.expand_or_jumpable() then
        luasnip.expand_or_jump()
      elseif has_words_before() then
        cmp.complete()
      else
        fallback()
      end
    end, { 'i', 's' }),

    ['<S-Tab>'] = cmp.mapping(function(fallback)
      if cmp.visible() then
        cmp.select_prev_item()
      elseif luasnip.jumpable(-1) then
        luasnip.jump(-1)
      else
        fallback()
      end
    end, { 'i', 's' }),
  }),
  sources = {
    { name = 'nvim_lsp' },
    { name = 'luasnip' },
  },
  window = {
    completion = cmp.config.window.bordered({ 
      border = borderLsp 
    }),
    documentation = cmp.config.window.bordered({ 
      border = borderLsp 
    }),
  },
}

@gegoune
Copy link

gegoune commented Mar 11, 2024

You are not showing crucial part of your configuration: what and how triggers formatting, which does not happen automatically so you must have configured it somehow. You can find how to filter out tsserver from LSPs that receive format request in comments I linked to.

(also, what is it with people pasting screenshots here rather than code itself?)

@bobbyrahmanda13
Copy link
Author

photo_2024-03-11_16-59-12
photo_2024-03-11_16-59-15

When using tsserver, nothing happens

@RayGuo-ergou
Copy link
Contributor

RayGuo-ergou commented Mar 11, 2024

if I use :LspStop tsserver then all my Lsp are inactive, so it won't work, lsp on the .ts and .js formatting extension still works but if on vue then volar will be active so that's an error, I've tried it.

Not sure what do you mean but you should pass correct param into LspStop otherwise it will stop all servers.

With tsserver:
with tsserver

Without tsserver:
without tsserver

And this should not be the solution to your problem, it's just demonstrate where's the issue.

@bobbyrahmanda13
Copy link
Author

bobbyrahmanda13 commented Mar 11, 2024

Can it be made so that both work because we also need .ts/.js to make composable, because before there was no problem

@gegoune
Copy link

gegoune commented Mar 11, 2024

@bobbyrahmanda13 You are ignoring everything that is said in this issue while people are genuinely trying to help you.
Once again, your issue is caused by some bug in tsserver or tsserver plugin for vue (I don't know). Until it's fixed you can mitigate it by filtering out tsserver from LSP formatting (see my second link in original response).

@RayGuo-ergou
Copy link
Contributor

Can it be made so that both work because we also need .ts/.js to make composable, because before there was no problem

You have to config yourself to what you want.

e.g. Use volar in *.vue and use tsserver in *.{js,ts}. To achieve that you need few condition check, simply like if the filetype is vue then just use volar. Check gegoune's first comment(the 2nd link), everything you need is there just need to tweak a little bit.

@Namakete
Copy link

Namakete commented Mar 24, 2024

Did anyone fix this?

@RayGuo-ergou
Copy link
Contributor

If you go through the threat, you will find the solution. ( maybe just a work around but works ).

Btw it's tsserver not volar.

@codymikol
Copy link
Contributor

Hey @johnsoncodehk, I see this is marked as completed, but I am still experiencing this on v2.0.13. Is this intended to be closed because it was fixed in volarjs/volar.js@d4fbbd7 or closed due to being an upstream issue?

Thanks for all your hard work on this project!

@johnsoncodehk
Copy link
Member

@codymikol Since you mentioned v2.0.13, this did not release the VSCode extension. Are you using another IDE? If so, please make sure to update @vue/typescript-plugin to the latest version.

@codymikol
Copy link
Contributor

Currently using neovim with mason, not sure how that manages the @vue/typescript plugin, I'll dig into that a little tonight.

@codymikol
Copy link
Contributor

codymikol commented Apr 14, 2024

@johnsoncodehk I should have known how that in particular was set up since I made a little shim package to do that and completely forgot about it. 😅

But essentially, that is just takes the installed version of this language-tools repository and uses the @vue/typescript-plugin package for that version.

So in my case I'm using the latest version of @vue/typescript-plugin, but I'm still experiencing this bug unfortunately :/

@codymikol
Copy link
Contributor

I'll do a little digging and see if I can find where exactly its happening.

@codymikol
Copy link
Contributor

@johnsoncodehk Apologies, after digging into this, I was all of a sudden unable to replicate my issues. I think I must have blown the cobwebs out of something cached. You can close this again, sorry for the confusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working upstream
Projects
None yet
Development

No branches or pull requests

6 participants