@@ -5,7 +5,9 @@ import * as batch from "jsr:@denops/std@^7.0.0/batch";
5
5
import * as fn from "jsr:@denops/std@^7.0.0/function" ;
6
6
import * as option from "jsr:@denops/std@^7.0.0/option" ;
7
7
import { parse as parseBufname } from "jsr:@denops/std@^7.0.0/bufname" ;
8
+ import Encoding from "npm:[email protected] " ;
8
9
import { findWorktreeFromDenops } from "../../git/worktree.ts" ;
10
+ import { addBom } from "../../util/bom.ts" ;
9
11
import { exec as execBare } from "../../command/bare/command.ts" ;
10
12
11
13
export async function write (
@@ -32,15 +34,17 @@ export async function exec(
32
34
relpath : string ,
33
35
options : ExecOptions ,
34
36
) : Promise < void > {
35
- const [ verbose , fileencoding , fileformat , content ] = await batch . collect (
36
- denops ,
37
- ( denops ) => [
38
- option . verbose . get ( denops ) ,
39
- option . fileencoding . getBuffer ( denops , bufnr ) ,
40
- option . fileformat . getBuffer ( denops , bufnr ) ,
41
- fn . getbufline ( denops , bufnr , 1 , "$" ) ,
42
- ] ,
43
- ) ;
37
+ const [ verbose , fileencoding , fileformat , bomb , content ] = await batch
38
+ . collect (
39
+ denops ,
40
+ ( denops ) => [
41
+ option . verbose . get ( denops ) ,
42
+ option . fileencoding . getBuffer ( denops , bufnr ) ,
43
+ option . fileformat . getBuffer ( denops , bufnr ) ,
44
+ option . bomb . getBuffer ( denops , bufnr ) ,
45
+ fn . getbufline ( denops , bufnr , 1 , "$" ) ,
46
+ ] ,
47
+ ) ;
44
48
45
49
const worktree = await findWorktreeFromDenops ( denops , {
46
50
worktree : options . worktree ,
@@ -64,7 +68,8 @@ export async function exec(
64
68
}
65
69
try {
66
70
await fs . copy ( f , original ) ;
67
- await Deno . writeTextFile ( original , `${ content . join ( "\n" ) } \n` ) ;
71
+ const data = encode ( `${ content . join ( "\n" ) } \n` , fileencoding ) ;
72
+ await Deno . writeFile ( original , bomb ? addBom ( data ) : data ) ;
68
73
await fn . setbufvar ( denops , bufnr , "&modified" , 0 ) ;
69
74
await execBare ( denops , [
70
75
"add" ,
@@ -80,3 +85,12 @@ export async function exec(
80
85
await restore ( ) ;
81
86
}
82
87
}
88
+
89
+ function encode ( str : string , encoding : string ) : Uint8Array {
90
+ const utf8Encoder = new TextEncoder ( ) ;
91
+ const utf8Bytes = utf8Encoder . encode ( str ) ;
92
+ return Uint8Array . from ( Encoding . convert ( utf8Bytes , {
93
+ to : encoding ,
94
+ from : "UTF8" ,
95
+ } ) ) ;
96
+ }
0 commit comments