1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 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 410 411 412 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 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
|
/* SPDX-FileCopyrightText: 2025 - Sébastien Wilmet
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#include "tepl-code-comment-view.h"
#include "tepl/tepl-signal-group.h"
/**
* SECTION:code-comment-view
* @Title: TeplCodeCommentView
* @Short_description: Code comment - GtkSourceView class extension
*
* #TeplCodeCommentView extends the #GtkSourceView class with the code comment
* and uncomment feature.
*/
struct _TeplCodeCommentViewPrivate
{
/* Weak ref: a subclass of GtkSourceView can potentially own a strong
* ref to a TeplCodeCommentView object.
*/
GtkSourceView *source_view;
TeplSignalGroup *buffer_signal_group;
};
enum
{
PROP_0,
PROP_SOURCE_VIEW,
PROP_CODE_COMMENT_IS_SUPPORTED,
N_PROPERTIES
};
static GParamSpec *properties[N_PROPERTIES];
G_DEFINE_TYPE_WITH_PRIVATE (TeplCodeCommentView, tepl_code_comment_view, G_TYPE_OBJECT)
static gboolean
str_is_empty (const gchar *str)
{
return (str == NULL || str[0] == '\0');
}
static gboolean
str_contains_newline_char (const gchar *str)
{
g_assert (str != NULL);
return (strchr (str, '\n') != NULL ||
strchr (str, '\r') != NULL);
}
static gboolean
str_contains_leading_or_trailing_whitespace (const gchar *str,
gboolean leading)
{
gchar *stripped_str;
gboolean ret;
g_assert (str != NULL);
stripped_str = g_strdup (str);
if (leading)
{
g_strchug (stripped_str);
}
else
{
g_strchomp (stripped_str);
}
ret = g_strcmp0 (stripped_str, str) != 0;
g_free (stripped_str);
return ret;
}
static gboolean
str_contains_leading_whitespace (const gchar *str)
{
return str_contains_leading_or_trailing_whitespace (str, TRUE);
}
static gboolean
str_contains_trailing_whitespace (const gchar *str)
{
return str_contains_leading_or_trailing_whitespace (str, FALSE);
}
static gboolean
metadata_line_comment_start_is_valid (const gchar *str)
{
if (str_is_empty (str) ||
str_contains_newline_char (str) ||
str_contains_leading_whitespace (str))
{
return FALSE;
}
return TRUE;
}
static gboolean
metadata_block_comment_start_is_valid (const gchar *str)
{
return metadata_line_comment_start_is_valid (str);
}
static gboolean
metadata_block_comment_end_is_valid (const gchar *str)
{
if (str_is_empty (str) ||
str_contains_newline_char (str) ||
str_contains_trailing_whitespace (str))
{
return FALSE;
}
return TRUE;
}
static void
warn_if_invalid_metadata (GtkSourceLanguage *language,
const gchar *metadata_name,
const gchar *metadata_value,
gboolean metadata_value_is_valid)
{
if (metadata_value != NULL && !metadata_value_is_valid)
{
g_warning ("TeplCodeCommentView: language “%s”: %s metadata “%s” is not supported.",
gtk_source_language_get_id (language),
metadata_name,
metadata_value);
}
}
static void
get_sanitized_metadata (GtkSourceLanguage *language,
const gchar **line_comment_start,
const gchar **block_comment_start,
const gchar **block_comment_end)
{
gboolean line_comment_start_is_valid;
gboolean block_comment_start_is_valid;
gboolean block_comment_end_is_valid;
*line_comment_start = gtk_source_language_get_metadata (language, "line-comment-start");
*block_comment_start = gtk_source_language_get_metadata (language, "block-comment-start");
*block_comment_end = gtk_source_language_get_metadata (language, "block-comment-end");
line_comment_start_is_valid = metadata_line_comment_start_is_valid (*line_comment_start);
block_comment_start_is_valid = metadata_block_comment_start_is_valid (*block_comment_start);
block_comment_end_is_valid = metadata_block_comment_end_is_valid (*block_comment_end);
warn_if_invalid_metadata (language,
"line-comment-start",
*line_comment_start,
line_comment_start_is_valid);
warn_if_invalid_metadata (language,
"block-comment-start",
*block_comment_start,
block_comment_start_is_valid);
warn_if_invalid_metadata (language,
"block-comment-end",
*block_comment_end,
block_comment_end_is_valid);
if (!line_comment_start_is_valid)
{
*line_comment_start = NULL;
}
if (!block_comment_start_is_valid ||
!block_comment_end_is_valid)
{
*block_comment_start = NULL;
*block_comment_end = NULL;
}
}
static void
language_notify_cb (GtkSourceBuffer *buffer,
GParamSpec *pspec,
TeplCodeCommentView *code_comment_view)
{
g_object_notify_by_pspec (G_OBJECT (code_comment_view), properties[PROP_CODE_COMMENT_IS_SUPPORTED]);
}
static void
buffer_changed (TeplCodeCommentView *code_comment_view)
{
GtkSourceBuffer *buffer;
tepl_signal_group_clear (&code_comment_view->priv->buffer_signal_group);
g_object_notify_by_pspec (G_OBJECT (code_comment_view), properties[PROP_CODE_COMMENT_IS_SUPPORTED]);
if (code_comment_view->priv->source_view == NULL)
{
return;
}
buffer = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (code_comment_view->priv->source_view)));
code_comment_view->priv->buffer_signal_group = tepl_signal_group_new (G_OBJECT (buffer));
tepl_signal_group_add (code_comment_view->priv->buffer_signal_group,
g_signal_connect (buffer,
"notify::language",
G_CALLBACK (language_notify_cb),
code_comment_view));
}
static void
buffer_notify_cb (GtkTextView *view,
GParamSpec *pspec,
TeplCodeCommentView *code_comment_view)
{
buffer_changed (code_comment_view);
}
static void
editable_notify_cb (GtkTextView *view,
GParamSpec *pspec,
TeplCodeCommentView *code_comment_view)
{
g_object_notify_by_pspec (G_OBJECT (code_comment_view), properties[PROP_CODE_COMMENT_IS_SUPPORTED]);
}
static void
set_source_view (TeplCodeCommentView *code_comment_view,
GtkSourceView *source_view)
{
if (source_view == NULL)
{
return;
}
g_return_if_fail (GTK_SOURCE_IS_VIEW (source_view));
g_assert (code_comment_view->priv->source_view == NULL);
g_set_weak_pointer (&code_comment_view->priv->source_view, source_view);
g_signal_connect_object (source_view,
"notify::buffer",
G_CALLBACK (buffer_notify_cb),
code_comment_view,
G_CONNECT_DEFAULT);
g_signal_connect_object (source_view,
"notify::editable",
G_CALLBACK (editable_notify_cb),
code_comment_view,
G_CONNECT_DEFAULT);
buffer_changed (code_comment_view);
}
static void
tepl_code_comment_view_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
TeplCodeCommentView *code_comment_view = TEPL_CODE_COMMENT_VIEW (object);
switch (prop_id)
{
case PROP_SOURCE_VIEW:
g_value_set_object (value, tepl_code_comment_view_get_source_view (code_comment_view));
break;
case PROP_CODE_COMMENT_IS_SUPPORTED:
g_value_set_boolean (value, tepl_code_comment_view_code_comment_is_supported (code_comment_view));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
tepl_code_comment_view_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
TeplCodeCommentView *code_comment_view = TEPL_CODE_COMMENT_VIEW (object);
switch (prop_id)
{
case PROP_SOURCE_VIEW:
set_source_view (code_comment_view, g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
tepl_code_comment_view_dispose (GObject *object)
{
TeplCodeCommentView *code_comment_view = TEPL_CODE_COMMENT_VIEW (object);
g_clear_weak_pointer (&code_comment_view->priv->source_view);
tepl_signal_group_clear (&code_comment_view->priv->buffer_signal_group);
G_OBJECT_CLASS (tepl_code_comment_view_parent_class)->dispose (object);
}
static void
tepl_code_comment_view_class_init (TeplCodeCommentViewClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->get_property = tepl_code_comment_view_get_property;
object_class->set_property = tepl_code_comment_view_set_property;
object_class->dispose = tepl_code_comment_view_dispose;
/**
* TeplCodeCommentView:source-view:
*
* The associated #GtkSourceView widget. #TeplCodeCommentView has a weak
* reference to the #GtkSourceView.
*
* Since: 6.14
*/
properties[PROP_SOURCE_VIEW] =
g_param_spec_object ("source-view",
"source-view",
"",
GTK_SOURCE_TYPE_VIEW,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS);
/**
* TeplCodeCommentView:code-comment-is-supported:
*
* Whether code comment/uncomment is supported.
*
* This property is %TRUE when all these conditions are met:
* - The necessary metadata is available, to know what character or
* string to insert or remove.
* - The #GtkTextView:editable property is %TRUE.
*
* Since: 6.14
*/
properties[PROP_CODE_COMMENT_IS_SUPPORTED] =
g_param_spec_boolean ("code-comment-is-supported",
"code-comment-is-supported",
"",
FALSE,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, N_PROPERTIES, properties);
}
static void
tepl_code_comment_view_init (TeplCodeCommentView *code_comment_view)
{
code_comment_view->priv = tepl_code_comment_view_get_instance_private (code_comment_view);
}
/**
* tepl_code_comment_view_new:
* @view: a #GtkSourceView widget.
*
* Returns: (transfer full): a new #TeplCodeCommentView object.
* Since: 6.14
*/
TeplCodeCommentView *
tepl_code_comment_view_new (GtkSourceView *view)
{
g_return_val_if_fail (GTK_SOURCE_IS_VIEW (view), NULL);
return g_object_new (TEPL_TYPE_CODE_COMMENT_VIEW,
"source-view", view,
NULL);
}
/**
* tepl_code_comment_view_get_source_view:
* @code_comment_view: a #TeplCodeCommentView.
*
* Returns: (transfer none) (nullable): the #TeplCodeCommentView:source-view.
* Since: 6.14
*/
GtkSourceView *
tepl_code_comment_view_get_source_view (TeplCodeCommentView *code_comment_view)
{
g_return_val_if_fail (TEPL_IS_CODE_COMMENT_VIEW (code_comment_view), NULL);
return code_comment_view->priv->source_view;
}
/**
* tepl_code_comment_view_code_comment_is_supported:
* @code_comment_view: a #TeplCodeCommentView.
*
* Returns: the current value of the
* #TeplCodeCommentView:code-comment-is-supported property.
* Since: 6.14
*/
gboolean
tepl_code_comment_view_code_comment_is_supported (TeplCodeCommentView *code_comment_view)
{
GtkSourceBuffer *buffer;
GtkSourceLanguage *language;
const gchar *line_comment_start;
const gchar *block_comment_start;
const gchar *block_comment_end;
g_return_val_if_fail (TEPL_IS_CODE_COMMENT_VIEW (code_comment_view), FALSE);
if (code_comment_view->priv->source_view == NULL)
{
return FALSE;
}
if (!gtk_text_view_get_editable (GTK_TEXT_VIEW (code_comment_view->priv->source_view)))
{
return FALSE;
}
buffer = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (code_comment_view->priv->source_view)));
language = gtk_source_buffer_get_language (buffer);
if (language == NULL)
{
return FALSE;
}
get_sanitized_metadata (language,
&line_comment_start,
&block_comment_start,
&block_comment_end);
return ((line_comment_start != NULL) ||
(block_comment_start != NULL && block_comment_end != NULL));
}
static void
comment_single_line (GtkTextBuffer *buffer,
gint line_num,
const gchar *line_comment_start,
const gchar *block_comment_start,
const gchar *block_comment_end)
{
GtkTextIter iter;
gtk_text_buffer_get_iter_at_line (buffer, &iter, line_num);
g_return_if_fail (gtk_text_iter_get_line (&iter) == line_num);
if (line_comment_start != NULL)
{
gtk_text_buffer_insert (buffer, &iter, line_comment_start, -1);
}
/* Don't insert block-comment-start/end on empty lines. For example with
* XML/HTML it would clutter the view.
* On the other hand, for line-comment-start it's nice to have a
* vertical "line" of comment chars, to better see what has been
* commented out.
*/
else if (!gtk_text_iter_ends_line (&iter))
{
gtk_text_buffer_insert (buffer, &iter, block_comment_start, -1);
gtk_text_iter_forward_to_line_end (&iter);
g_return_if_fail (gtk_text_iter_get_line (&iter) == line_num);
gtk_text_buffer_insert (buffer, &iter, block_comment_end, -1);
}
}
/**
* tepl_code_comment_view_comment_lines:
* @code_comment_view: a #TeplCodeCommentView.
* @start_iter: a #GtkTextIter.
* @end_iter: (nullable): a #GtkTextIter, or %NULL to comment only the line at
* @start_iter.
*
* Comments the lines between @start_iter and @end_iter included.
*
* If @end_iter is %NULL, only a single line is commented.
*
* Ensure that code comment is supported before calling this function. See
* tepl_code_comment_view_code_comment_is_supported().
*
* Since: 6.14
*/
void
tepl_code_comment_view_comment_lines (TeplCodeCommentView *code_comment_view,
const GtkTextIter *start_iter,
const GtkTextIter *end_iter)
{
GtkSourceBuffer *buffer;
GtkSourceLanguage *language;
const gchar *line_comment_start;
const gchar *block_comment_start;
const gchar *block_comment_end;
GtkTextIter my_start_iter;
GtkTextIter my_end_iter;
gint start_line;
gint end_line;
gint line_num;
g_return_if_fail (TEPL_IS_CODE_COMMENT_VIEW (code_comment_view));
if (code_comment_view->priv->source_view == NULL)
{
return;
}
buffer = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (code_comment_view->priv->source_view)));
g_return_if_fail (start_iter != NULL);
g_return_if_fail (gtk_text_iter_get_buffer (start_iter) == GTK_TEXT_BUFFER (buffer));
g_return_if_fail (end_iter == NULL ||
gtk_text_iter_get_buffer (end_iter) == GTK_TEXT_BUFFER (buffer));
g_return_if_fail (tepl_code_comment_view_code_comment_is_supported (code_comment_view));
language = gtk_source_buffer_get_language (buffer);
get_sanitized_metadata (language,
&line_comment_start,
&block_comment_start,
&block_comment_end);
my_start_iter = *start_iter;
my_end_iter = end_iter != NULL ? *end_iter : *start_iter;
gtk_text_iter_order (&my_start_iter, &my_end_iter);
start_line = gtk_text_iter_get_line (&my_start_iter);
end_line = gtk_text_iter_get_line (&my_end_iter);
gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));
for (line_num = start_line; line_num <= end_line; line_num++)
{
comment_single_line (GTK_TEXT_BUFFER (buffer),
line_num,
line_comment_start,
block_comment_start,
block_comment_end);
}
gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
}
/**
* tepl_code_comment_view_comment_selected_lines:
* @code_comment_view: a #TeplCodeCommentView.
*
* Comments the selected lines.
*
* Ensure that code comment is supported before calling this function. See
* tepl_code_comment_view_code_comment_is_supported().
*
* Since: 6.14
*/
void
tepl_code_comment_view_comment_selected_lines (TeplCodeCommentView *code_comment_view)
{
GtkTextBuffer *buffer;
GtkTextIter start;
GtkTextIter end;
g_return_if_fail (TEPL_IS_CODE_COMMENT_VIEW (code_comment_view));
if (code_comment_view->priv->source_view == NULL)
{
return;
}
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (code_comment_view->priv->source_view));
gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
tepl_code_comment_view_comment_lines (code_comment_view, &start, &end);
}
static gboolean
uncomment_line_with_line_comment_start (GtkTextBuffer *buffer,
gint line_num,
const gchar *line_comment_start)
{
GtkTextIter iter;
GtkTextIter leading_end;
GtkTextIter match_end;
gtk_text_buffer_get_iter_at_line (buffer, &iter, line_num);
g_return_val_if_fail (gtk_text_iter_get_line (&iter) == line_num, FALSE);
gtk_source_iter_get_leading_spaces_end_boundary (&iter, &leading_end);
if (gtk_source_iter_starts_string (&leading_end, line_comment_start, &match_end) &&
gtk_text_iter_get_line (&match_end) == line_num)
{
gtk_text_buffer_delete (buffer, &leading_end, &match_end);
return TRUE;
}
return FALSE;
}
static gboolean
uncomment_line_by_line_with_line_comment_start (GtkTextBuffer *buffer,
gint start_line,
gint end_line,
const gchar *line_comment_start)
{
gint line_num;
gboolean success = FALSE;
if (line_comment_start == NULL)
{
return FALSE;
}
for (line_num = start_line; line_num <= end_line; line_num++)
{
if (uncomment_line_with_line_comment_start (buffer, line_num, line_comment_start))
{
success = TRUE;
}
}
return success;
}
static gboolean
uncomment_line_with_block_comments (GtkTextBuffer *buffer,
gint line_num,
const gchar *block_comment_start,
const gchar *block_comment_end)
{
GtkTextIter at_start_of_line;
GtkTextIter leading_end;
GtkTextIter trailing_start;
GtkTextIter end_of_block_comment_start;
GtkTextIter start_of_block_comment_end;
gboolean block_comment_start_found;
gboolean block_comment_end_found;
gtk_text_buffer_get_iter_at_line (buffer, &at_start_of_line, line_num);
g_return_val_if_fail (gtk_text_iter_get_line (&at_start_of_line) == line_num, FALSE);
gtk_source_iter_get_leading_spaces_end_boundary (&at_start_of_line, &leading_end);
gtk_source_iter_get_trailing_spaces_start_boundary (&at_start_of_line, &trailing_start);
block_comment_start_found = gtk_source_iter_starts_string (&leading_end,
block_comment_start,
&end_of_block_comment_start);
block_comment_end_found = gtk_source_iter_ends_string (&trailing_start,
block_comment_end,
&start_of_block_comment_end);
if (block_comment_start_found && block_comment_end_found)
{
GtkTextMark *mark_for_start_of_block_comment_end;
GtkTextMark *mark_for_trailing_start;
g_return_val_if_fail (gtk_text_iter_get_line (&leading_end) == line_num, FALSE);
g_return_val_if_fail (gtk_text_iter_get_line (&end_of_block_comment_start) == line_num, FALSE);
g_return_val_if_fail (gtk_text_iter_get_line (&start_of_block_comment_end) == line_num, FALSE);
g_return_val_if_fail (gtk_text_iter_get_line (&trailing_start) == line_num, FALSE);
g_return_val_if_fail (gtk_text_iter_compare (&leading_end, &end_of_block_comment_start) < 0, FALSE);
g_return_val_if_fail (gtk_text_iter_compare (&start_of_block_comment_end, &trailing_start) < 0, FALSE);
// Overlap. This can happen with /*/ in C :-)
if (gtk_text_iter_compare (&end_of_block_comment_start, &start_of_block_comment_end) > 0)
{
return FALSE;
}
mark_for_start_of_block_comment_end = gtk_text_buffer_create_mark (buffer,
NULL,
&start_of_block_comment_end,
TRUE);
mark_for_trailing_start = gtk_text_buffer_create_mark (buffer,
NULL,
&trailing_start,
TRUE);
gtk_text_buffer_delete (buffer, &leading_end, &end_of_block_comment_start);
gtk_text_buffer_get_iter_at_mark (buffer,
&start_of_block_comment_end,
mark_for_start_of_block_comment_end);
gtk_text_buffer_get_iter_at_mark (buffer,
&trailing_start,
mark_for_trailing_start);
gtk_text_buffer_delete_mark (buffer, mark_for_start_of_block_comment_end);
gtk_text_buffer_delete_mark (buffer, mark_for_trailing_start);
gtk_text_buffer_delete (buffer, &start_of_block_comment_end, &trailing_start);
return TRUE;
}
return FALSE;
}
static gboolean
uncomment_line_by_line_with_block_comments (GtkTextBuffer *buffer,
gint start_line,
gint end_line,
const gchar *block_comment_start,
const gchar *block_comment_end)
{
gint line_num;
gboolean success = FALSE;
if (block_comment_start == NULL || block_comment_end == NULL)
{
return FALSE;
}
for (line_num = start_line; line_num <= end_line; line_num++)
{
if (uncomment_line_with_block_comments (buffer,
line_num,
block_comment_start,
block_comment_end))
{
success = TRUE;
}
}
return success;
}
/**
* tepl_code_comment_view_uncomment_selection:
* @code_comment_view: a #TeplCodeCommentView.
*
* Uncomments the selected text.
*
* Ensure that code comment is supported before calling this function. See
* tepl_code_comment_view_code_comment_is_supported().
*
* Since: 6.14
*/
void
tepl_code_comment_view_uncomment_selection (TeplCodeCommentView *code_comment_view)
{
GtkSourceBuffer *buffer;
GtkSourceLanguage *language;
const gchar *line_comment_start;
const gchar *block_comment_start;
const gchar *block_comment_end;
GtkTextIter selection_start;
GtkTextIter selection_end;
gint start_line;
gint end_line;
gboolean done;
g_return_if_fail (TEPL_IS_CODE_COMMENT_VIEW (code_comment_view));
if (code_comment_view->priv->source_view == NULL)
{
return;
}
g_return_if_fail (tepl_code_comment_view_code_comment_is_supported (code_comment_view));
buffer = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (code_comment_view->priv->source_view)));
language = gtk_source_buffer_get_language (buffer);
get_sanitized_metadata (language,
&line_comment_start,
&block_comment_start,
&block_comment_end);
gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (buffer), &selection_start, &selection_end);
start_line = gtk_text_iter_get_line (&selection_start);
end_line = gtk_text_iter_get_line (&selection_end);
gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));
done = uncomment_line_by_line_with_line_comment_start (GTK_TEXT_BUFFER (buffer),
start_line,
end_line,
line_comment_start);
if (!done)
{
uncomment_line_by_line_with_block_comments (GTK_TEXT_BUFFER (buffer),
start_line,
end_line,
block_comment_start,
block_comment_end);
}
gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
}
|