@@ -6,6 +6,7 @@ import { execute } from "../helper/mod.ts";
6
6
import * as unknownutil from "https://deno.land/x/unknownutil@v2.0.0/mod.ts" ;
7
7
import {
8
8
assertFileFormat ,
9
+ FileFormat ,
9
10
findFileFormat ,
10
11
isFileFormat ,
11
12
maybeFileFormat ,
@@ -54,6 +55,18 @@ async function ensurePrerequisites(denops: Denops): Promise<string> {
54
55
endtry
55
56
endfunction
56
57
58
+ function! DenopsStdBufferAppend_${ suffix } (bufnr, lnum, repl) abort
59
+ let modified = getbufvar(a:bufnr, '&modified')
60
+ let modifiable = getbufvar(a:bufnr, '&modifiable')
61
+ let foldmethod = getbufvar(a:bufnr, '&foldmethod')
62
+ call setbufvar(a:bufnr, '&modifiable', 1)
63
+ call setbufvar(a:bufnr, '&foldmethod', 'manual')
64
+ call appendbufline(a:bufnr, a:lnum, a:repl)
65
+ call setbufvar(a:bufnr, '&modified', modified)
66
+ call setbufvar(a:bufnr, '&modifiable', modifiable)
67
+ call setbufvar(a:bufnr, '&foldmethod', foldmethod)
68
+ endfunction
69
+
57
70
function! DenopsStdBufferReplace_${ suffix } (bufnr, repl, fileformat, fileencoding) abort
58
71
let modified = getbufvar(a:bufnr, '&modified')
59
72
let modifiable = getbufvar(a:bufnr, '&modifiable')
@@ -97,19 +110,6 @@ async function ensurePrerequisites(denops: Denops): Promise<string> {
97
110
return suffix ;
98
111
}
99
112
100
- export type OpenOptions = {
101
- mods ?: string ;
102
- cmdarg ?: string ;
103
- opener ?: string ;
104
- } ;
105
-
106
- export type OpenResult = {
107
- winid : number ;
108
- bufnr : number ;
109
- winnr : number ;
110
- tabpagenr : number ;
111
- } ;
112
-
113
113
/**
114
114
* Open a buffer
115
115
*/
@@ -131,6 +131,19 @@ export async function open(
131
131
) as OpenResult ;
132
132
}
133
133
134
+ export type OpenOptions = {
135
+ mods ?: string ;
136
+ cmdarg ?: string ;
137
+ opener ?: string ;
138
+ } ;
139
+
140
+ export type OpenResult = {
141
+ winid : number ;
142
+ bufnr : number ;
143
+ winnr : number ;
144
+ tabpagenr : number ;
145
+ } ;
146
+
134
147
/**
135
148
* Edit a buffer
136
149
*/
@@ -142,11 +155,82 @@ export async function reload(denops: Denops, bufnr: number): Promise<void> {
142
155
) ;
143
156
}
144
157
145
- export type ReplaceOptions = {
158
+ /**
159
+ * Decode content for the buffer with given format and encoding.
160
+ */
161
+ export async function decode (
162
+ denops : Denops ,
163
+ bufnr : number ,
164
+ data : Uint8Array ,
165
+ options : DecodeOptions = { } ,
166
+ ) : Promise < DecodeResult > {
167
+ const [ fileformat , fileformatsStr , fileencodingsStr ] = await batch . gather (
168
+ denops ,
169
+ async ( denops ) => {
170
+ await fn . getbufvar ( denops , bufnr , "&fileformat" ) ;
171
+ await fn . getbufvar ( denops , bufnr , "&fileformats" ) ;
172
+ await fn . getbufvar ( denops , bufnr , "&fileencodings" ) ;
173
+ } ,
174
+ ) ;
175
+ assertFileFormat ( fileformat ) ;
176
+ unknownutil . assertString ( fileformatsStr ) ;
177
+ unknownutil . assertString ( fileencodingsStr ) ;
178
+ const fileformats = fileformatsStr . split ( "," ) ;
179
+ const fileencodings = fileencodingsStr . split ( "," ) ;
180
+ unknownutil . assertArray ( fileformats , isFileFormat ) ;
181
+ unknownutil . assertArray ( fileencodings , unknownutil . isString ) ;
182
+ let enc : string ;
183
+ let text : string ;
184
+ if ( options . fileencoding ) {
185
+ enc = options . fileencoding ;
186
+ text = ( new TextDecoder ( enc ) ) . decode ( data ) ;
187
+ } else {
188
+ [ enc , text ] = tryDecode ( data , fileencodings ) ;
189
+ }
190
+ const ff = maybeFileFormat ( options . fileformat ) ??
191
+ findFileFormat ( text , fileformats ) ?? fileformat ;
192
+ return {
193
+ content : splitText ( text , ff ) ,
194
+ fileformat : ff ,
195
+ fileencoding : enc ,
196
+ } ;
197
+ }
198
+
199
+ export type DecodeOptions = {
146
200
fileformat ?: string ;
147
201
fileencoding ?: string ;
148
202
} ;
149
203
204
+ export type DecodeResult = {
205
+ content : string [ ] ;
206
+ fileformat : FileFormat ;
207
+ fileencoding : string ;
208
+ } ;
209
+
210
+ /**
211
+ * Append content under the current cursor position or given lnum of the buffer
212
+ */
213
+ export async function append (
214
+ denops : Denops ,
215
+ bufnr : number ,
216
+ repl : string [ ] ,
217
+ options : AppendOptions = { } ,
218
+ ) : Promise < void > {
219
+ const suffix = await ensurePrerequisites ( denops ) ;
220
+ const lnum = options . lnum ??
221
+ await ensure ( denops , bufnr , ( ) => fn . line ( denops , "." ) ) ;
222
+ await denops . call (
223
+ `DenopsStdBufferAppend_${ suffix } ` ,
224
+ bufnr ,
225
+ lnum ,
226
+ repl ,
227
+ ) ;
228
+ }
229
+
230
+ export type AppendOptions = {
231
+ lnum ?: number ;
232
+ } ;
233
+
150
234
/**
151
235
* Replace the buffer content
152
236
*/
@@ -166,54 +250,40 @@ export async function replace(
166
250
) ;
167
251
}
168
252
169
- export type AssignOptions = {
253
+ export type ReplaceOptions = {
170
254
fileformat ?: string ;
171
255
fileencoding ?: string ;
172
- preprocessor ?: ( repl : string [ ] ) => string [ ] ;
173
256
} ;
174
257
175
258
/**
176
259
* Assign content to the buffer with given format and encoding.
260
+ *
261
+ * @deprecated Use `decode()` and `replace()` individually instead.
177
262
*/
178
263
export async function assign (
179
264
denops : Denops ,
180
265
bufnr : number ,
181
- content : Uint8Array ,
266
+ data : Uint8Array ,
182
267
options : AssignOptions = { } ,
183
268
) : Promise < void > {
184
- const [ fileformat , fileformatsStr , fileencodingsStr ] = await batch . gather (
269
+ const { content , fileformat , fileencoding } = await decode (
185
270
denops ,
186
- async ( denops ) => {
187
- await fn . getbufvar ( denops , bufnr , "&fileformat" ) ;
188
- await fn . getbufvar ( denops , bufnr , "&fileformats" ) ;
189
- await fn . getbufvar ( denops , bufnr , "&fileencodings" ) ;
190
- } ,
271
+ bufnr ,
272
+ data ,
273
+ options ,
191
274
) ;
192
- assertFileFormat ( fileformat ) ;
193
- unknownutil . assertString ( fileformatsStr ) ;
194
- unknownutil . assertString ( fileencodingsStr ) ;
195
- const fileformats = fileformatsStr . split ( "," ) ;
196
- const fileencodings = fileencodingsStr . split ( "," ) ;
197
- unknownutil . assertArray ( fileformats , isFileFormat ) ;
198
- unknownutil . assertArray ( fileencodings , unknownutil . isString ) ;
199
- let enc : string ;
200
- let text : string ;
201
- if ( options . fileencoding ) {
202
- enc = options . fileencoding ;
203
- text = ( new TextDecoder ( enc ) ) . decode ( content ) ;
204
- } else {
205
- [ enc , text ] = tryDecode ( content , fileencodings ) ;
206
- }
207
- const ff = maybeFileFormat ( options . fileformat ) ??
208
- findFileFormat ( text , fileformats ) ?? fileformat ;
209
275
const preprocessor = options . preprocessor ?? ( ( v : string [ ] ) => v ) ;
210
- const repl = preprocessor ( splitText ( text , ff ) ) ;
276
+ const repl = preprocessor ( content ) ;
211
277
await replace ( denops , bufnr , repl , {
212
- fileformat : ff ,
213
- fileencoding : enc ,
278
+ fileformat,
279
+ fileencoding,
214
280
} ) ;
215
281
}
216
282
283
+ export type AssignOptions = DecodeOptions & {
284
+ preprocessor ?: ( repl : string [ ] ) => string [ ] ;
285
+ } ;
286
+
217
287
/**
218
288
* Concrete the buffer.
219
289
*
0 commit comments