@@ -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')
@@ -142,6 +155,82 @@ export async function reload(denops: Denops, bufnr: number): Promise<void> {
142
155
) ;
143
156
}
144
157
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 = {
200
+ fileformat ?: string ;
201
+ fileencoding ?: string ;
202
+ } ;
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
+
145
234
/**
146
235
* Replace the buffer content
147
236
*/
@@ -168,49 +257,30 @@ export type ReplaceOptions = {
168
257
169
258
/**
170
259
* Assign content to the buffer with given format and encoding.
260
+ *
261
+ * @deprecated Use `decode()` and `replace()` individually instead.
171
262
*/
172
263
export async function assign (
173
264
denops : Denops ,
174
265
bufnr : number ,
175
- content : Uint8Array ,
266
+ data : Uint8Array ,
176
267
options : AssignOptions = { } ,
177
268
) : Promise < void > {
178
- const [ fileformat , fileformatsStr , fileencodingsStr ] = await batch . gather (
269
+ const { content , fileformat , fileencoding } = await decode (
179
270
denops ,
180
- async ( denops ) => {
181
- await fn . getbufvar ( denops , bufnr , "&fileformat" ) ;
182
- await fn . getbufvar ( denops , bufnr , "&fileformats" ) ;
183
- await fn . getbufvar ( denops , bufnr , "&fileencodings" ) ;
184
- } ,
271
+ bufnr ,
272
+ data ,
273
+ options ,
185
274
) ;
186
- assertFileFormat ( fileformat ) ;
187
- unknownutil . assertString ( fileformatsStr ) ;
188
- unknownutil . assertString ( fileencodingsStr ) ;
189
- const fileformats = fileformatsStr . split ( "," ) ;
190
- const fileencodings = fileencodingsStr . split ( "," ) ;
191
- unknownutil . assertArray ( fileformats , isFileFormat ) ;
192
- unknownutil . assertArray ( fileencodings , unknownutil . isString ) ;
193
- let enc : string ;
194
- let text : string ;
195
- if ( options . fileencoding ) {
196
- enc = options . fileencoding ;
197
- text = ( new TextDecoder ( enc ) ) . decode ( content ) ;
198
- } else {
199
- [ enc , text ] = tryDecode ( content , fileencodings ) ;
200
- }
201
- const ff = maybeFileFormat ( options . fileformat ) ??
202
- findFileFormat ( text , fileformats ) ?? fileformat ;
203
275
const preprocessor = options . preprocessor ?? ( ( v : string [ ] ) => v ) ;
204
- const repl = preprocessor ( splitText ( text , ff ) ) ;
276
+ const repl = preprocessor ( content ) ;
205
277
await replace ( denops , bufnr , repl , {
206
- fileformat : ff ,
207
- fileencoding : enc ,
278
+ fileformat,
279
+ fileencoding,
208
280
} ) ;
209
281
}
210
282
211
- export type AssignOptions = {
212
- fileformat ?: string ;
213
- fileencoding ?: string ;
283
+ export type AssignOptions = DecodeOptions & {
214
284
preprocessor ?: ( repl : string [ ] ) => string [ ] ;
215
285
} ;
216
286
0 commit comments