Module: RuVim::TextMetricsExt
- Defined in:
- ext/ruvim/ruvim_ext.c
Class Method Summary collapse
-
.char_index_for_screen_col(*args) ⇒ Object
char_index_for_screen_col(line, target_screen_col, tabstop: 2, align: :floor) Returns a character index whose screen column is <= target.
-
.clip_cells_for_width(*args) ⇒ Object
clip_cells_for_width(text, width, source_col_start: 0, tabstop: 2) Returns [cells_array, display_col].
Class Method Details
.char_index_for_screen_col(*args) ⇒ Object
char_index_for_screen_col(line, target_screen_col, tabstop: 2, align: :floor) Returns a character index whose screen column is <= target.
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'ext/ruvim/ruvim_ext.c', line 413 static VALUE rb_char_index_for_screen_col(int argc, VALUE *argv, VALUE self) { VALUE line, v_target, opts; rb_scan_args(argc, argv, "2:", &line, &v_target, &opts); int tabstop = 2; int align_ceil = 0; if (!NIL_P(opts)) { VALUE v; static ID id_tabstop, id_align, id_ceil; if (!id_tabstop) { id_tabstop = rb_intern("tabstop"); id_align = rb_intern("align"); id_ceil = rb_intern("ceil"); } v = rb_hash_lookup2(opts, ID2SYM(id_tabstop), Qnil); if (!NIL_P(v)) tabstop = NUM2INT(v); v = rb_hash_lookup2(opts, ID2SYM(id_align), Qnil); if (!NIL_P(v) && SYM2ID(v) == id_ceil) align_ceil = 1; } if (NIL_P(line)) line = rb_str_new("", 0); if (TYPE(line) != T_STRING) line = rb_String(line); int target = NUM2INT(v_target); if (target < 0) target = 0; const char *ptr = RSTRING_PTR(line); const char *end = ptr + RSTRING_LEN(line); rb_encoding *enc = rb_utf8_encoding(); int screen_col = 0; int char_index = 0; /* Walk grapheme clusters using oniguruma regex \X */ /* Simplified: walk codepoints, treating combining marks as part of the previous character (width 0 doesn't advance screen_col). */ while (ptr < end) { /* Measure one grapheme cluster: base char + combining marks */ int cluster_width = 0; int cluster_chars = 0; int first = 1; while (ptr < end) { int clen = rb_enc_precise_mbclen(ptr, end, enc); if (!MBCLEN_CHARFOUND_P(clen)) { if (first) { ptr++; cluster_chars++; cluster_width = 1; } break; } clen = MBCLEN_CHARFOUND_LEN(clen); unsigned int code = rb_enc_codepoint(ptr, end, enc); if (!first) { /* Check if combining/zero-width |
.clip_cells_for_width(*args) ⇒ Object
clip_cells_for_width(text, width, source_col_start: 0, tabstop: 2) Returns [cells_array, display_col]
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'ext/ruvim/ruvim_ext.c', line 301 static VALUE rb_clip_cells_for_width(int argc, VALUE *argv, VALUE self) { VALUE text, v_width, opts; rb_scan_args(argc, argv, "2:", &text, &v_width, &opts); int max_width = NUM2INT(v_width); if (max_width < 0) max_width = 0; int source_col_start = 0, tabstop = 2; if (!NIL_P(opts)) { VALUE v; static ID id_source_col_start, id_tabstop; if (!id_source_col_start) { id_source_col_start = rb_intern("source_col_start"); id_tabstop = rb_intern("tabstop"); } v = rb_hash_lookup2(opts, ID2SYM(id_source_col_start), Qnil); if (!NIL_P(v)) source_col_start = NUM2INT(v); v = rb_hash_lookup2(opts, ID2SYM(id_tabstop), Qnil); if (!NIL_P(v)) tabstop = NUM2INT(v); } if (NIL_P(text)) text = rb_str_new("", 0); if (TYPE(text) != T_STRING) text = rb_String(text); const char *ptr = RSTRING_PTR(text); const char *end = ptr + RSTRING_LEN(text); rb_encoding *enc = rb_utf8_encoding(); VALUE cell_class = get_cell_class(); static ID id_new = 0; if (!id_new) id_new = rb_intern("new"); VALUE cells = rb_ary_new(); int display_col = 0; int source_col = source_col_start; VALUE space_str = rb_str_new(" ", 1); VALUE question_str = rb_str_new("?", 1); while (ptr < end) { int clen = rb_enc_precise_mbclen(ptr, end, enc); if (!MBCLEN_CHARFOUND_P(clen)) { /* invalid byte */ if (display_col >= max_width) break; rb_ary_push(cells, rb_funcall(cell_class, id_new, 3, question_str, INT2FIX(source_col), INT2FIX(1))); display_col++; source_col++; ptr++; continue; } clen = MBCLEN_CHARFOUND_LEN(clen); unsigned int code = rb_enc_codepoint(ptr, end, enc); /* Printable ASCII fast path */ if (code >= 0x20 && code <= 0x7E) { if (display_col >= max_width) break; VALUE ch = rb_str_new(ptr, 1); rb_ary_push(cells, rb_funcall(cell_class, id_new, 3, ch, INT2FIX(source_col), INT2FIX(1))); display_col++; source_col++; ptr += 1; continue; } /* Tab */ if (code == '\t') { int w = tabstop - (display_col % tabstop); if (w == 0) w = tabstop; if (display_col + w > max_width) break; for (int i = 0; i < w; i++) { rb_ary_push(cells, rb_funcall(cell_class, id_new, 3, space_str, INT2FIX(source_col), INT2FIX(1))); } display_col += w; source_col++; ptr += 1; continue; } /* Control chars */ if (code < 0x20 || code == 0x7F || (code >= 0x80 && code <= 0x9F)) { if (display_col >= max_width) break; rb_ary_push(cells, rb_funcall(cell_class, id_new, 3, question_str, INT2FIX(source_col), INT2FIX(1))); display_col++; source_col++; ptr += clen; continue; } /* Multi-byte character */ int w = codepoint_width(code); if (display_col + w > max_width) break; VALUE ch = rb_enc_str_new(ptr, clen, enc); rb_ary_push(cells, rb_funcall(cell_class, id_new, 3, ch, INT2FIX(source_col), INT2FIX(w))); display_col += w; source_col++; ptr += clen; } VALUE result = rb_ary_new_capa(2); rb_ary_push(result, cells); rb_ary_push(result, INT2FIX(display_col)); return result; } |