Skip to content

Commit

Permalink
fix: rplugin project root detection
Browse files Browse the repository at this point in the history
Closes #38.
  • Loading branch information
luckasRanarison committed Aug 16, 2024
1 parent 2ab47b0 commit d9a2c2f
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions rplugin/node/tailwind-tools/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ class Plugin {
constructor(plugin) {
this.nvim = plugin.nvim;

plugin.registerFunction(
"TailwindGetConfig",
this.getTailwindConfig.bind(this),
{ sync: true }
);

plugin.registerFunction(
"TailwindGetUtilities",
this.getUtilities.bind(this),
Expand All @@ -29,9 +23,34 @@ class Plugin {
);
}

// TODO: Get the running language server root directory?
getProjectRoot() {
return this.nvim.call("getcwd");
/**
* @returns {Promise<string>}
*/
async getProjectRoot() {
let currentDir = await this.nvim.call("expand", "%:p:h");

while (true) {
const parentDir = path.dirname(currentDir);

if (parentDir === currentDir) break;
if (this.findConfigPath(currentDir)) return currentDir;

currentDir = parentDir;
}

return currentDir;
}

findConfigPath(directory) {
const configFiles = [
"tailwind.config.js",
"tailwind.config.ts",
"tailwind.config.cjs",
];

return configFiles
.map((filename) => path.join(directory, filename))
.find((filePath) => fs.existsSync(filePath));
}

async getTailwindConfig() {
Expand All @@ -43,11 +62,7 @@ class Plugin {
const _require = utils.getNodeModuleResolver(rootDir);
const resolveConfig = _require("tailwindcss/resolveConfig");
const loadConfig = _require("tailwindcss/lib/public/load-config");

const configExtensions = ["js", "ts", "cjs"];
const configPath = configExtensions
.map((ext) => path.join(rootDir, `tailwind.config.${ext}`))
.find((filePath) => fs.existsSync(filePath));
const configPath = this.findConfigPath(rootDir);

return resolveConfig(configPath ? loadConfig(configPath) : {});
}
Expand Down Expand Up @@ -83,6 +98,7 @@ class Plugin {

/**
* @param {string[]} classes
* @returns {Promise<string>}
*/
async expandUtilities(classes) {
const config = await this.getTailwindConfig();
Expand Down

0 comments on commit d9a2c2f

Please sign in to comment.