@@ -287,8 +287,8 @@ ArrayOf(String) nvim_buf_get_lines(uint64_t channel_id,
287
287
}
288
288
289
289
bool oob = false;
290
-
start = normalize_index(buf, start, &oob);
291
-
end = normalize_index(buf, end, &oob);
290
+
start = normalize_index(buf, start, true, &oob);
291
+
end = normalize_index(buf, end, true, &oob);
292
292
293
293
if (strict_indexing && oob) {
294
294
api_set_error(err, kErrorTypeValidation, "Index out of bounds");
@@ -374,15 +374,14 @@ void nvim_buf_set_lines(uint64_t channel_id, Buffer buffer, Integer start, Integ
374
374
}
375
375
376
376
bool oob = false;
377
-
start = normalize_index(buf, start, &oob);
378
-
end = normalize_index(buf, end, &oob);
377
+
start = normalize_index(buf, start, true, &oob);
378
+
end = normalize_index(buf, end, true, &oob);
379
379
380
380
if (strict_indexing && oob) {
381
381
api_set_error(err, kErrorTypeValidation, "Index out of bounds");
382
382
return;
383
383
}
384
384
385
-
386
385
if (start > end) {
387
386
api_set_error(err,
388
387
kErrorTypeValidation,
@@ -554,13 +553,13 @@ void nvim_buf_set_text(uint64_t channel_id, Buffer buffer, Integer start_row, In
554
553
555
554
// check range is ordered and everything!
556
555
// start_row, end_row within buffer len (except add text past the end?)
557
-
start_row = normalize_index(buf, start_row, &oob);
556
+
start_row = normalize_index(buf, start_row, true, &oob);
558
557
if (oob || start_row == buf->b_ml.ml_line_count + 1) {
559
558
api_set_error(err, kErrorTypeValidation, "start_row out of bounds");
560
559
return;
561
560
}
562
561
563
-
end_row = normalize_index(buf, end_row, &oob);
562
+
end_row = normalize_index(buf, end_row, true, &oob);
564
563
if (oob || end_row == buf->b_ml.ml_line_count + 1) {
565
564
api_set_error(err, kErrorTypeValidation, "end_row out of bounds");
566
565
return;
@@ -757,6 +756,108 @@ void nvim_buf_set_text(uint64_t channel_id, Buffer buffer, Integer start_row, In
757
756
try_end(err);
758
757
}
759
758
759
+
/// Gets a range from the buffer.
760
+
///
761
+
/// This differs from |nvim_buf_get_lines()| in that it allows retrieving only
762
+
/// portions of a line.
763
+
///
764
+
/// Indexing is zero-based. Column indices are end-exclusive.
765
+
///
766
+
/// Prefer |nvim_buf_get_lines()| when retrieving entire lines.
767
+
///
768
+
/// @param channel_id
769
+
/// @param buffer Buffer handle, or 0 for current buffer
770
+
/// @param start_row First line index
771
+
/// @param start_col Starting byte offset of first line
772
+
/// @param end_row Last line index
773
+
/// @param end_col Ending byte offset of last line (exclusive)
774
+
/// @param opts Optional parameters. Currently unused.
775
+
/// @param[out] err Error details, if any
776
+
/// @return Array of lines, or empty array for unloaded buffer.
777
+
ArrayOf(String) nvim_buf_get_text(uint64_t channel_id, Buffer buffer,
778
+
Integer start_row, Integer start_col,
779
+
Integer end_row, Integer end_col,
780
+
Dictionary opts, Error *err)
781
+
FUNC_API_SINCE(9)
782
+
{
783
+
Array rv = ARRAY_DICT_INIT;
784
+
785
+
if (opts.size > 0) {
786
+
api_set_error(err, kErrorTypeValidation, "opts dict isn't empty");
787
+
return rv;
788
+
}
789
+
790
+
buf_T *buf = find_buffer_by_handle(buffer, err);
791
+
792
+
if (!buf) {
793
+
return rv;
794
+
}
795
+
796
+
// return sentinel value if the buffer isn't loaded
797
+
if (buf->b_ml.ml_mfp == NULL) {
798
+
return rv;
799
+
}
800
+
801
+
bool oob = false;
802
+
start_row = normalize_index(buf, start_row, false, &oob);
803
+
end_row = normalize_index(buf, end_row, false, &oob);
804
+
805
+
if (oob) {
806
+
api_set_error(err, kErrorTypeValidation, "Index out of bounds");
807
+
return rv;
808
+
}
809
+
810
+
// nvim_buf_get_lines doesn't care if the start row is greater than the end
811
+
// row (it will just return an empty array), but nvim_buf_get_text does in
812
+
// order to maintain symmetry with nvim_buf_set_text.
813
+
if (start_row > end_row) {
814
+
api_set_error(err, kErrorTypeValidation, "start is higher than end");
815
+
return rv;
816
+
}
817
+
818
+
bool replace_nl = (channel_id != VIML_INTERNAL_CALL);
819
+
820
+
if (start_row == end_row) {
821
+
String line = buf_get_text(buf, start_row, start_col, end_col, replace_nl, err);
822
+
if (ERROR_SET(err)) {
823
+
return rv;
824
+
}
825
+
826
+
ADD(rv, STRING_OBJ(line));
827
+
return rv;
828
+
}
829
+
830
+
rv.size = (size_t)(end_row - start_row) + 1;
831
+
rv.items = xcalloc(rv.size, sizeof(Object));
832
+
833
+
rv.items[0] = STRING_OBJ(buf_get_text(buf, start_row, start_col, MAXCOL-1, replace_nl, err));
834
+
if (ERROR_SET(err)) {
835
+
goto end;
836
+
}
837
+
838
+
if (rv.size > 2) {
839
+
Array tmp = ARRAY_DICT_INIT;
840
+
tmp.items = &rv.items[1];
841
+
if (!buf_collect_lines(buf, rv.size - 2, start_row + 1, replace_nl, &tmp, err)) {
842
+
goto end;
843
+
}
844
+
}
845
+
846
+
rv.items[rv.size-1] = STRING_OBJ(buf_get_text(buf, end_row, 0, end_col, replace_nl, err));
847
+
if (ERROR_SET(err)) {
848
+
goto end;
849
+
}
850
+
851
+
end:
852
+
if (ERROR_SET(err)) {
853
+
api_free_array(rv);
854
+
rv.size = 0;
855
+
rv.items = NULL;
856
+
}
857
+
858
+
return rv;
859
+
}
860
+
760
861
/// Returns the byte offset of a line (0-indexed). |api-indexing|
761
862
///
762
863
/// Line 1 (index=0) has offset 0. UTF-8 bytes are counted. EOL is one byte.
@@ -1386,11 +1487,11 @@ static void fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
1386
1487
}
1387
1488
1388
1489
// Normalizes 0-based indexes to buffer line numbers
1389
-
static int64_t normalize_index(buf_T *buf, int64_t index, bool *oob)
1490
+
static int64_t normalize_index(buf_T *buf, int64_t index, bool end_exclusive, bool *oob)
1390
1491
{
1391
1492
int64_t line_count = buf->b_ml.ml_line_count;
1392
1493
// Fix if < 0
1393
-
index = index < 0 ? line_count + index +1 : index;
1494
+
index = index < 0 ? line_count + index + (int)end_exclusive : index;
1394
1495
1395
1496
// Check for oob
1396
1497
if (index > line_count) {
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4