nyoom-engineering / nyoom.nvim

A Neovim framework and doom emacs alternative for the stubborn martian hacker. Powered by fennel and the oxocarbon theme
MIT License
1.4k stars 82 forks source link

Adding +onsave formatting for typescript development #87

Closed ldmsh closed 1 year ago

ldmsh commented 1 year ago

This isn't really an issue with the planned web module, just wondering if there's a better approach to this in the meantime.

After adding typescript-language-server to fnl/modules/tools/lsp/config.fnl:

;; fnl/modules/tools/lsp/config.fnl

(tset lsp-servers :tsserver {})
(tset (require :lspconfig.configs) :typescript-language-server
      {:default_config {:cmd [:typescript-language-server :--st-audio]
                        :filetypes [:typescript :typescriptreact :tsx]
                        :single_file_support true
                        :settings {:tsserver {:workspace {:library (vim.api.nvim_list_runtime_paths)}
                                              :diagnostics {:globals [:vim]}}}}})

Trying to get +onsave formatting to work by naively copying null-ls.builtIns used for other languages:

;; fnl/modules/checkers/diagnostics/config.fnl

                   (nyoom-module-p! typescriptreact
                                    (table.insert null-ls-sources
                                                  null-ls.builtins.formatting.prettier))

                   (nyoom-module-p! typescript
                                    (table.insert null-ls-sources
                                                  null-ls.builtins.formatting.prettier))

Have done the same for mason's config:

;; fnl/modules/tools/mason/config.fnl

;; formatters

(nyoom-module-p! format
  (do

   (nyoom-module-p! typescript
     (table.insert mason-tools :typescript-language-server))

   (nyoom-module-p! typescriptreact
     (table.insert mason-tools :typescript-language-server))

   ; ... 

))

Installed prettier via :Mason ui, and also added via npm install -g prettier though keep seeing:

Format request failed, no matching language servers.

tsserver LSP is working as expected, it's a great experience in nyoom, wow. Just the formatting which I can't seem to figure out.

:LspInfo

  Language client log: /Users/0000/.local/state/nvim/lsp.log
 Detected filetype:   typescriptreact

 4 client(s) attached to this buffer: 

 Client: tsserver (id: 1, pid: nil, bufnr: [1])
    filetypes:       javascript, javascriptreact, javascript.jsx, typescript, typescriptreact, typescript.tsx
    autostart:       true
    root directory:  Running in single file mode.
    cmd:             typescript-language-server --stdio

 Client: tailwindcss (id: 2, pid: nil, bufnr: [1])
    filetypes:       aspnetcorerazor, astro, astro-markdown, blade, django-html, htmldjango, edge, eelixir, elixir, ejs, erb, eruby, gohtml, haml, handlebars, hbs, html, html-eex, heex, jade, leaf, liquid, markdown, mdx, mustache, njk, nunjucks, php, razor, slim, twig, css, less, postcss, sass, scss, stylus, sugarss, javascript, javascriptreact, reason, rescript, typescript, typescriptreact, vue, svelte
    autostart:       true
    root directory:  Running in single file mode.
    cmd:             tailwindcss-language-server --stdio

 Client: null-ls (id: 3, pid: 5001, bufnr: [1])
    filetypes:       fnl, fennel, c, cuda, cpp, java, cs, clojure, lua, luau, markdown, nim, python, rust, sh, zig
    autostart:       false
    root directory:  Running in single file mode.
    cmd:             <function>

 Client: copilot (id: 4, pid: nil, bufnr: [1])
    filetypes:       
    autostart:       true
    root directory:  Running in single file mode.
    cmd:             node /Users/ldnsh/.local/share/nvim/site/pack/packer/opt/copilot.lua/copilot/index.js

 Configured servers list: tailwindcss, tsserver, pyright, rnix, nimls, marksman, zls, sumneko_lua, texlab, julials, bashls, jdtls, clojure_lsp

Thanks

shaunsingh commented 1 year ago

nyoom-module-p! only compiles the code under it if the module exists. In this case, typescriptreact isn't a valid module (yet) so that code doesn't actually compile. For now I'd omit the nyoom-module-p! and write it as follows:

;; fnl/modules/checkers/diagnostics/config.fnl
                                    (table.insert null-ls-sources
                                                  null-ls.builtins.formatting.prettier))

;; fnl/modules/tools/mason/config.fnl

(nyoom-module-p! format
  (do
     (table.insert mason-tools :typescript-language-server))
     (table.insert mason-tools :prettier))
ldmsh commented 1 year ago

Thanks again Shaun, works exactly as expected, really appreciate it.