Skip to content

Commit

Permalink
feat: use jsonls to format JSON files due to the removal of fixjson s…
Browse files Browse the repository at this point in the history
…upport in none-ls.
  • Loading branch information
nshen committed Mar 6, 2024
1 parent 745cc63 commit 228ca87
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,29 @@ require("insis").setup({

语言环境相关模块请逐个打开,否则重启后一次会安装很多服务,需要等待较长时间。

<details>
<summary>JSON 编辑</summary>

```lua
require("insis").setup({
json = {
enable = true,
-- 以下为默认值,可以省略
lsp = "jsonls",
---@type "jsonls" | "prettier"
formatter = "jsonls",
format_on_save = false,
}
})
```

启用 `json` 功能,重启后

- 会自动安装 Treesitter 的 JSON 语法高亮。
- 自动安装并配置 [jsonls](https://github.com/microsoft/vscode-json-languageservice) Language Server

</details>

<details>
<summary>Markdown 编辑</summary>

Expand Down
5 changes: 2 additions & 3 deletions lua/insis/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,8 @@ local UserConfig = {
json = {
enable = false,
lsp = "jsonls",
-- npm install -g fixjson
---@type "fixjson" | "prettier"
formatter = "fixjson",
---@type "jsonls" | "prettier"
formatter = "jsonls",
format_on_save = false,
},

Expand Down
8 changes: 2 additions & 6 deletions lua/insis/env/json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,18 @@ return function(config)
end,

getToolEnsureList = function()
if config.formatter == "fixjson" then
return { "fixjson" }
end
if config.formatter == "prettier" then
return { "prettier" }
end
return {}
end,

getNulllsSources = function()
local null_ls = pRequire("null-ls")
if not null_ls then
return {}
end
if config.formatter == "fixjson" then
return { null_ls.builtins.formatting.fixjson }
elseif config.formatter == "prettier" then
if config.formatter == "prettier" then
return { null_ls.builtins.formatting.prettier.with({
filetypes = { "json" },
}) }
Expand Down
7 changes: 4 additions & 3 deletions lua/insis/lsp/config/json.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
local cfg = require("insis").config.json
local common = require("insis.lsp.common-config")
local opts = {
capabilities = common.capabilities,
flags = common.flags,
on_attach = function(client, bufnr)
-- use fixjson to format
-- https://github.com/rhysd/fixjson
common.disableFormat(client)
if cfg and cfg.formatter ~= "jsonls" then
common.disableFormat(client)
end
common.keyAttach(bufnr)
end,
settings = {
Expand Down

1 comment on commit 228ca87

@nshen
Copy link
Owner Author

@nshen nshen commented on 228ca87 Mar 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.