Skip to content
This repository was archived by the owner on Aug 12, 2023. It is now read-only.

Commit a3b6a47

Browse files
committed
feat: add command :TypescriptRenameFolder
1 parent f66d447 commit a3b6a47

File tree

6 files changed

+142
-1
lines changed

6 files changed

+142
-1
lines changed

lua/typescript/commands.lua

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lua/typescript/rename-file.lua

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/commands.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { config } from "@ts/config";
22
import { goToSourceDefinition } from "@ts/go-to-source-definition";
3-
import { renameFile } from "@ts/rename-file";
3+
import { renameFile, renameFolder } from "@ts/rename-file";
44
import {
55
addMissingImports,
66
fixAll,
@@ -26,6 +26,42 @@ export const setupCommands = (bufnr: number): void => {
2626
{ bang: true }
2727
);
2828

29+
vim.api.nvim_buf_create_user_command(
30+
bufnr,
31+
"TypescriptRenameFolder",
32+
(opts) => {
33+
const sourceFile = vim.api.nvim_buf_get_name(bufnr);
34+
vim.ui.input(
35+
{ prompt: "Old path: ", default: sourceFile },
36+
(sourceInput) => {
37+
if (
38+
sourceInput === "" ||
39+
sourceInput === sourceFile ||
40+
sourceInput === undefined
41+
) {
42+
return;
43+
}
44+
vim.ui.input(
45+
{ prompt: "New path: ", default: sourceFile },
46+
(targetInput) => {
47+
if (
48+
targetInput === "" ||
49+
targetInput === sourceFile ||
50+
targetInput === undefined
51+
) {
52+
return;
53+
}
54+
renameFolder(sourceInput, targetInput, {
55+
force: opts.bang,
56+
});
57+
}
58+
);
59+
}
60+
);
61+
},
62+
{ bang: true }
63+
);
64+
2965
vim.api.nvim_buf_create_user_command(
3066
bufnr,
3167
"TypescriptGoToSourceDefinition",

src/rename-file.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,45 @@ interface Opts {
77
force?: boolean;
88
}
99

10+
export const renameFolder = (
11+
source: string,
12+
target: string,
13+
opts: Opts = {}
14+
): boolean => {
15+
debugLog(source, target);
16+
const sourceBufnr = vim.fn.bufadd(source);
17+
vim.fn.bufload(sourceBufnr);
18+
19+
if (!util.path.is_dir(source)) {
20+
debugLog("source is not a directory");
21+
return false;
22+
}
23+
24+
if (
25+
util.path.exists(target) &&
26+
util.path.is_dir(target) &&
27+
(opts.force === undefined || opts.force === false)
28+
) {
29+
const status = vim.fn.confirm("Folder exists! Overwrite?", "&Yes\n&No");
30+
if (status !== 1) {
31+
debugLog("user declined to overrwrite file; aborting");
32+
return false;
33+
}
34+
}
35+
vim.fn.mkdir(vim.fn.fnamemodify(target, ":p:h"), "p");
36+
const files = vim.fn.readdir(source);
37+
for (const file of files) {
38+
debugLog(file);
39+
if (
40+
file.endsWith(".ts") &&
41+
renameFile(source + file, target + file, opts)
42+
) {
43+
debugLog("OK");
44+
}
45+
}
46+
return true;
47+
};
48+
1049
export const renameFile = (
1150
source: string,
1251
target: string,

src/types/lspconfig.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ declare module "lspconfig" {
66
namespace util {
77
namespace path {
88
const exists: (path: string) => boolean;
9+
const is_dir: (path: string) => boolean;
910
}
1011
}
1112
}

src/types/nvim.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ declare namespace vim {
168168
const bufload: (bufnr: number) => void;
169169
const has: (feature: string) => 0 | 1;
170170
const mkdir: (name: string, path?: string, prot?: number) => void;
171+
const readdir: (name: string) => string[];
171172
const fnamemodify: (fname: string, mods: string) => string;
172173
}
173174

0 commit comments

Comments
 (0)