Esempio n. 1
0
static void draw_brackets(SpaceText *st, ARegion *ar)
{
	TextLine *startl, *endl, *linep;
	Text *text = st->text;
	int b, fc, find, stack, viewc, viewl, offl, offc, x, y;
	int startc, endc, c;
	
	char ch;

	// showsyntax must be on or else the format string will be null
	if (!text->curl || !st->showsyntax) return;

	startl = text->curl;
	startc = text->curc;
	b = text_check_bracket(startl->line[startc]);
	if (b == 0 && startc > 0) b = text_check_bracket(startl->line[--startc]);
	if (b == 0) return;

	linep = startl;
	c = startc;
	fc = txt_utf8_offset_to_index(linep->line, startc);
	endl = NULL;
	endc = -1;
	find = -b;
	stack = 0;
	
	/* Don't highlight backets if syntax HL is off or bracket in string or comment. */
	if (!linep->format || linep->format[fc] == FMT_TYPE_STRING || linep->format[fc] == FMT_TYPE_COMMENT)
		return;

	if (b > 0) {
		/* opening bracket, search forward for close */
		fc++;
		c += BLI_str_utf8_size_safe(linep->line + c);
		while (linep) {
			while (c < linep->len) {
				if (linep->format && linep->format[fc] != FMT_TYPE_STRING && linep->format[fc] != FMT_TYPE_COMMENT) {
					b = text_check_bracket(linep->line[c]);
					if (b == find) {
						if (stack == 0) {
							endl = linep;
							endc = c;
							break;
						}
						stack--;
					}
					else if (b == -find) {
						stack++;
					}
				}
				fc++;
				c += BLI_str_utf8_size_safe(linep->line + c);
			}
			if (endl) break;
			linep = linep->next;
			c = 0;
			fc = 0;
		}
	}
	else {
		/* closing bracket, search backward for open */
		fc--;
		if (c > 0) c -= linep->line + c - BLI_str_prev_char_utf8(linep->line + c);
		while (linep) {
			while (fc >= 0) {
				if (linep->format && linep->format[fc] != FMT_TYPE_STRING && linep->format[fc] != FMT_TYPE_COMMENT) {
					b = text_check_bracket(linep->line[c]);
					if (b == find) {
						if (stack == 0) {
							endl = linep;
							endc = c;
							break;
						}
						stack--;
					}
					else if (b == -find) {
						stack++;
					}
				}
				fc--;
				if (c > 0) c -= linep->line + c - BLI_str_prev_char_utf8(linep->line + c);
			}
			if (endl) break;
			linep = linep->prev;
			if (linep) {
				if (linep->format) fc = strlen(linep->format) - 1;
				else fc = -1;
				if (linep->len) c = BLI_str_prev_char_utf8(linep->line + linep->len) - linep->line;
				else fc = -1;
			}
		}
	}

	if (!endl || endc == -1)
		return;

	UI_ThemeColor(TH_HILITE);
	x = st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET;
	y = ar->winy - st->lheight_dpi;

	/* draw opening bracket */
	ch = startl->line[startc];
	wrap_offset(st, ar, startl, startc, &offl, &offc);
	viewc = text_get_char_pos(st, startl->line, startc) - st->left + offc;

	if (viewc >= 0) {
		viewl = txt_get_span(text->lines.first, startl) - st->top + offl;

		text_font_draw_character(st, x + viewc * st->cwidth, y - viewl * (st->lheight_dpi + TXT_LINE_SPACING), ch);
		text_font_draw_character(st, x + viewc * st->cwidth + 1, y - viewl * (st->lheight_dpi + TXT_LINE_SPACING), ch);
	}

	/* draw closing bracket */
	ch = endl->line[endc];
	wrap_offset(st, ar, endl, endc, &offl, &offc);
	viewc = text_get_char_pos(st, endl->line, endc) - st->left + offc;

	if (viewc >= 0) {
		viewl = txt_get_span(text->lines.first, endl) - st->top + offl;

		text_font_draw_character(st, x + viewc * st->cwidth, y - viewl * (st->lheight_dpi + TXT_LINE_SPACING), ch);
		text_font_draw_character(st, x + viewc * st->cwidth + 1, y - viewl * (st->lheight_dpi + TXT_LINE_SPACING), ch);
	}
}
Esempio n. 2
0
static void draw_brackets(SpaceText *st, ARegion *ar)
{
	TextLine *startl, *endl, *linep;
	Text *text = st->text;
	int b, c, startc, endc, find, stack;
	int viewc, viewl, offl, offc, x, y;
	char ch;

	// showsyntax must be on or else the format string will be null
	if(!text->curl || !st->showsyntax) return;

	startl= text->curl;
	startc= text->curc;
	b= text_check_bracket(startl->line[startc]);
	if(b==0 && startc>0) b = text_check_bracket(startl->line[--startc]);
	if(b==0) return;
	
	linep= startl;
	c= startc;
	endl= NULL;
	endc= -1;
	find= -b;
	stack= 0;
	
	/* Dont highlight backets if syntax HL is off or bracket in string or comment. */
	if(!linep->format || linep->format[c] == 'l' || linep->format[c] == '#')
		return;

	if(b>0) {
		/* opening bracket, search forward for close */
		c++;
		while(linep) {
			while(c<linep->len) {
				if(linep->format && linep->format[c] != 'l' && linep->format[c] != '#') {
					b= text_check_bracket(linep->line[c]);
					if(b==find) {
						if(stack==0) {
							endl= linep;
							endc= c;
							break;
						}
						stack--;
					}
					else if(b==-find) {
						stack++;
					}
				}
				c++;
			}
			if(endl) break;
			linep= linep->next;
			c= 0;
		}
	}
	else {
		/* closing bracket, search backward for open */
		c--;
		while(linep) {
			while(c>=0) {
				if(linep->format && linep->format[c] != 'l' && linep->format[c] != '#') {
					b= text_check_bracket(linep->line[c]);
					if(b==find) {
						if(stack==0) {
							endl= linep;
							endc= c;
							break;
						}
						stack--;
					}
					else if(b==-find) {
						stack++;
					}
				}
				c--;
			}
			if(endl) break;
			linep= linep->prev;
			if(linep) c= linep->len-1;
		}
	}

	if(!endl || endc==-1)
		return;

	UI_ThemeColor(TH_HILITE);	
	x= st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET;
	y= ar->winy - st->lheight;

	/* draw opening bracket */
	ch= startl->line[startc];
	wrap_offset(st, ar, startl, startc, &offl, &offc);
	viewc= text_get_char_pos(st, startl->line, startc) - st->left + offc;

	if(viewc >= 0){
		viewl= txt_get_span(text->lines.first, startl) - st->top + offl;

		text_font_draw_character(st, x+viewc*st->cwidth, y-viewl*st->lheight, ch);
		text_font_draw_character(st, x+viewc*st->cwidth+1, y-viewl*st->lheight, ch);
	}

	/* draw closing bracket */
	ch= endl->line[endc];
	wrap_offset(st, ar, endl, endc, &offl, &offc);
	viewc= text_get_char_pos(st, endl->line, endc) - st->left + offc;

	if(viewc >= 0) {
		viewl= txt_get_span(text->lines.first, endl) - st->top + offl;

		text_font_draw_character(st, x+viewc*st->cwidth, y-viewl*st->lheight, ch);
		text_font_draw_character(st, x+viewc*st->cwidth+1, y-viewl*st->lheight, ch);
	}
}