浏览代码

[Refactor]: Remove vim-rooter dependency (#1319)

kylo252 3 年之前
父节点
当前提交
08c0dca307
共有 2 个文件被更改,包括 12 次插入17 次删除
  1. 10 16
      lua/lsp/null-ls/services.lua
  2. 2 1
      lua/lsp/utils.lua

+ 10 - 16
lua/lsp/null-ls/services.lua

@@ -1,29 +1,23 @@
 local M = {}
 
-local logger = require("core.log"):get_default()
-
 local function find_root_dir()
-  if lvim.builtin.rooter.active then
-    --- use vim-rooter to set root_dir
-    vim.cmd "let root_dir = FindRootDirectory()"
-    return vim.api.nvim_get_var "root_dir"
-  end
-
-  -- TODO: Rework this to not make it javascript specific
-  --- use LSP to set root_dir
+  local util = require "lspconfig/util"
   local lsp_utils = require "lsp.utils"
-  local ts_client = lsp_utils.get_active_client_by_ft "typescript"
-  if ts_client == nil then
-    logger.error "Unable to determine root directory since tsserver didn't start correctly"
-    return nil
-  end
 
-  return ts_client.config.root_dir
+  local status_ok, ts_client = lsp_utils.is_client_active "typescript"
+  if status_ok then
+    return ts_client.config.root_dir
+  end
+  local dirname = vim.fn.expand "%:p:h"
+  return util.root_pattern "package.json"(dirname)
 end
 
 local function from_node_modules(command)
+  local logger = require("core.log"):get_default()
   local root_dir = find_root_dir()
+
   if not root_dir then
+    logger.error(string.format("Unable to find the [%s] node module.", command))
     return nil
   end
 

+ 2 - 1
lua/lsp/utils.lua

@@ -4,12 +4,13 @@ function M.is_client_active(name)
   local clients = vim.lsp.get_active_clients()
   for _, client in pairs(clients) do
     if client.name == name then
-      return true
+      return true, client
     end
   end
   return false
 end
 
+-- FIXME: this should return a list instead
 function M.get_active_client_by_ft(filetype)
   if not lvim.lang[filetype] or not lvim.lang[filetype].lsp then
     return nil