0

I'm working on a TypeScript-React project using neovim, because I love it. I want a feature: when my cursor is on a name that has not been imported, I can press some key-combination to choose which library I would like to import. E.g.

Input:

import { Head } from "next/head"
import { Another } from "./another"


export default function Home() {
  const foo = <Another/>
  
  return (
    <div className="container">
      <Head>
        <title>Create Next App</title>
        <link rel="icon" href="/favicon.ico" />
      </Head>

Output:

import { Head } from "next/head"


export default function Home() {
  const foo = <Another/>
  
  return (
    <div className="container">
      <Head>
        <title>Create Next App</title>
        <link rel="icon" href="/favicon.ico" />
      </Head>

What I've tried:

  • I installed a plugin, vim-import-js, that works well on importing javascript module. But it doesn't work on .ts and .tsx.

I know very little about Lua, LSP, .tsconfig.json, and eslintrc.js. It seems like these files also need to be set to make these plugins to work. I will also appreciate if you can provide more context on how these settings are referenced to make things work.

2
1

Ok, I assume the following according to your post + image:

  • You're using neovim >= 0.5.0
  • You've already set up the server with (assuming that the following code is in a lua file like ~/.config/nvim/lua/rofl.lua and you're loading it in your ~/.config/nvim/init.vim with lua require('rofl')):
local nvim_lsp = require('lspconfig')
nvim_lsp.tsserver.setup({})

According to the README.md of nvim-lspconfig you just need to add the following mapping in your ~/.config/nvim/init.vim:

nnoremap <Space>a <Cmd>lua vim.lsp.buf.code_action()<CR>

or if you have a ~/.config/nvim/init.lua or any other *.lua file:

buf_set_keymap('n', '<space>a', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)

After that you should get a little dialog if you press <Space> and then a where you can decide which one to import. At least that works for me. I don't know that much about these extra-config files because the settings above worked for me out of the box (like the code action).

8
  • [“assuming that the following code is in a lua file like ~/.config/nvim/lua/rofl.lua”] ==> I know nothing about the project structure of ~/.config/nvim. Could you recommend any learning resource, please?
    – job_start
    Sep 26 at 0:46
  • It still doesn't work. I got message: no code action available.
    – job_start
    Sep 26 at 1:15
  • oh ok wait, so you're a completely beginner in (neo)vim?
    – TornaxO7
    Sep 26 at 8:50
  • ["so you're a completely beginner in (neo)vim?"] ==> I have a huge(somehow organized) .vimrc, but I know nothing about init.vim and Lua. (and I know very little on writing vimscript actually.)
    – job_start
    Sep 26 at 16:19
  • 1
    Hm... ok, I'd recommend you to read through the ':h pages. It seems to be a lot, but that's the fastest way (in my opinion) to get an overview of all features of vim. :h runtimepath will explain you the directory-structure of vim for example.
    – TornaxO7
    Sep 27 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.