Copy node source text with whitespace collapsed, capped with a … marker. */
| 125 | |
| 126 | /* Copy node source text with whitespace collapsed, capped with a … marker. */ |
| 127 | static void why_node_text(const char *source, int source_len, TSNode node, char *out, size_t cap) { |
| 128 | uint32_t start = ts_node_start_byte(node); |
| 129 | uint32_t end = ts_node_end_byte(node); |
| 130 | if (end > (uint32_t)source_len) |
| 131 | end = (uint32_t)source_len; |
| 132 | size_t w = 0; |
| 133 | bool in_space = false; |
| 134 | for (uint32_t i = start; i < end && w + 4 < cap; i++) { |
| 135 | char c = source[i]; |
| 136 | if (c == '\n' || c == '\t' || c == '\r' || c == ' ') { |
| 137 | in_space = true; |
| 138 | continue; |
| 139 | } |
| 140 | if (in_space && w > 0) |
| 141 | out[w++] = ' '; |
| 142 | in_space = false; |
| 143 | out[w++] = c; |
| 144 | } |
| 145 | if (w + 4 >= cap && end > start) |
| 146 | memcpy(out + w, "…", strlen("…") + 1); |
| 147 | else |
| 148 | out[w] = '\0'; |
| 149 | /* Strip one layer of wrapping parens for readability. */ |
| 150 | size_t len = strlen(out); |
| 151 | if (len >= 2 && out[0] == '(' && out[len - 1] == ')') { |
| 152 | memmove(out, out + 1, len - 2); |
| 153 | out[len - 2] = '\0'; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | static bool why_type_has(const char *type, const char *needle) { |
| 158 | return strstr(type, needle) != NULL; |