diff --git a/lsp/oxfmt.lua b/lsp/oxfmt.lua index e2a7666035..1c299cf01e 100644 --- a/lsp/oxfmt.lua +++ b/lsp/oxfmt.lua @@ -51,8 +51,12 @@ return { -- Oxfmt resolves configuration by walking upward and using the nearest config file -- to the file being processed. We therefore compute the root directory by locating -- the closest `.oxfmtrc.json` / `.oxfmtrc.jsonc` / `oxfmt.config.ts` (or `package.json` fallback) above the buffer. - local root_markers = - util.insert_package_json({ '.oxfmtrc.json', '.oxfmtrc.jsonc', 'oxfmt.config.ts' }, 'oxfmt', fname) + local root_markers = util.insert_package_json( + { '.oxfmtrc.json', '.oxfmtrc.jsonc', 'oxfmt.config.ts' }, + { 'oxfmt', 'vite%-plus' }, + fname + ) + root_markers = util.root_markers_with_field(root_markers, { 'vite.config.ts' }, 'vite%-plus', fname) on_dir(vim.fs.dirname(vim.fs.find(root_markers, { path = fname, upward = true })[1])) end, } diff --git a/lsp/oxlint.lua b/lsp/oxlint.lua index 06d395fe82..4030071e93 100644 --- a/lsp/oxlint.lua +++ b/lsp/oxlint.lua @@ -17,6 +17,7 @@ --- The default `on_attach` function provides an `:LspOxlintFixAll` command which --- can be used to fix all fixable diagnostics. See the `eslint` config entry for --- an example of how to use this to automatically fix all errors on write. +local util = require 'lspconfig.util' local function oxlint_conf_mentions_typescript(root_dir) local fn = vim.fs.joinpath(root_dir, '.oxlintrc.json') @@ -49,7 +50,17 @@ return { 'svelte', 'astro', }, - root_markers = { '.oxlintrc.json', '.oxlintrc.jsonc', 'oxlint.config.ts' }, + root_dir = function(bufnr, on_dir) + local fname = vim.api.nvim_buf_get_name(bufnr) + + local root_markers = util.insert_package_json( + { '.oxlintrc.json', '.oxlintrc.jsonc', 'oxlint.config.ts' }, + { 'oxlint', 'vite%-plus' }, + fname + ) + root_markers = util.root_markers_with_field(root_markers, { 'vite.config.ts' }, 'vite%-plus', fname) + on_dir(vim.fs.dirname(vim.fs.find(root_markers, { path = fname, upward = true })[1])) + end, workspace_required = true, on_attach = function(client, bufnr) vim.api.nvim_buf_create_user_command(bufnr, 'LspOxlintFixAll', function() diff --git a/lua/lspconfig/util.lua b/lua/lspconfig/util.lua index 2282e3e26a..7c1afed62a 100644 --- a/lua/lspconfig/util.lua +++ b/lua/lspconfig/util.lua @@ -55,17 +55,20 @@ end --- --- @param root_files string[] List of root-marker files to append to. --- @param new_names string[] Potential root-marker filenames (e.g. `{ 'package.json', 'package.json5' }`) to inspect for the given `field`. ---- @param field string Field to search for in the given `new_names` files. +--- @param field string | string[] Field(s) to search for in the given `new_names` files. --- @param fname string Full path of the current buffer name to start searching upwards from. function M.root_markers_with_field(root_files, new_names, field, fname) local path = vim.fn.fnamemodify(fname, ':h') local found = vim.fs.find(new_names, { path = path, upward = true, type = 'file' }) + local fields = type(field) == 'string' and { field } or field for _, f in ipairs(found or {}) do -- Match the given `field`. local file = assert(io.open(f, 'r')) for line in file:lines() do - if line:find(field) then + if vim.iter(fields):any(function(s) + return line:find(s) + end) then root_files[#root_files + 1] = vim.fs.basename(f) break end