Skip to content

Commit a75dfa2

Browse files
authored
Merge pull request #164 from kamecha/fix-eol-fileformat
Fix EOL handling in write via GinEdit
2 parents f0305ad + 30ae428 commit a75dfa2

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

denops/gin/command/edit/write.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ export async function exec(
6868
}
6969
try {
7070
await fs.copy(f, original);
71-
const data = encode(`${content.join("\n")}\n`, fileencoding);
71+
const eol = getEol(fileformat);
72+
const data = encode(`${content.join(eol)}${eol}`, fileencoding);
7273
await Deno.writeFile(original, bomb ? addBom(data) : data);
7374
await fn.setbufvar(denops, bufnr, "&modified", 0);
7475
await execBare(denops, [
@@ -86,6 +87,19 @@ export async function exec(
8687
}
8788
}
8889

90+
function getEol(fileformat: string): string {
91+
switch (fileformat) {
92+
case "dos":
93+
return "\r\n";
94+
case "unix":
95+
return "\n";
96+
case "mac":
97+
return "\r";
98+
default:
99+
return "\n";
100+
}
101+
}
102+
89103
function encode(str: string, encoding: string): Uint8Array {
90104
const utf8Encoder = new TextEncoder();
91105
const utf8Bytes = utf8Encoder.encode(str);

0 commit comments

Comments
 (0)