#859: FLUID: Improves widget class code formatting

- categorizes multiple comments before a widget class member
correctly vs. before a widget member
- reintrodues newline characters for comment preview in the widget browser
This commit is contained in:
Matthias Melcher 2023-12-04 21:55:21 +01:00
parent c3849b0a97
commit 5b96fca1c1
2 changed files with 27 additions and 5 deletions

View File

@ -678,6 +678,26 @@ bool is_class_member(Fl_Type *t) {
// || t->is_a(ID_DeclBlock) // Declaration blocks are generally not handled well
}
/**
Return true, if this is a comment, and if it is followed by a class member.
This must only be called if q is inside a widget class.
Widget classes can have widgets and members (functions/methods, declarations,
etc.) intermixed.
\param[in] q should be a comment type
\return true if this comment is followed by a class member
\return false if it is followed by a widget or code
\see is_class_member(Fl_Type *t)
*/
bool is_comment_before_class_member(Fl_Type *q) {
if (q->is_a(ID_Comment) && q->next && q->next->level==q->level) {
if (q->next->is_a(ID_Comment))
return is_comment_before_class_member(q->next);
if (is_class_member(q->next))
return true;
}
return false;
}
/**
Recursively dump code, putting children between the two parts of the parent code.
\param[in] p write this type and all its children
@ -699,9 +719,7 @@ Fl_Type* Fd_Code_Writer::write_code(Fl_Type* p) {
// Handle widget classes specially
for (q = p->next; q && q->level > p->level;) {
// note: maybe declaration blocks should be handled like comments in the context
// note: we don't handle comments before a comment before a member
bool comment_before_member = (q->is_a(ID_Comment) && q->next && q->next->level==q->level && is_class_member(q->next));
if (!is_class_member(q) && !comment_before_member) {
if (!is_class_member(q) && !is_comment_before_class_member(q)) {
q = write_code(q);
} else {
int level = q->level;
@ -719,8 +737,7 @@ Fl_Type* Fd_Code_Writer::write_code(Fl_Type* p) {
if (write_sourceview) p->header2_end = (int)ftell(header_file);
for (q = p->next; q && q->level > p->level;) {
bool comment_before_member = (q->is_a(ID_Comment) && q->next && q->next->level==q->level && is_class_member(q->next));
if (is_class_member(q) || comment_before_member) {
if (is_class_member(q) || is_comment_before_class_member(q)) {
q = write_code(q);
} else {
int level = q->level;

View File

@ -146,6 +146,11 @@ static char *copy_trunc(char *p, const char *str, int maxl, int quote)
const char *end = str + strlen(str); // end of input string
if (quote) *p++ = '"'; // opening quote
while (size < maxl) { // maximum <maxl> characters
if (*str == '\n') {
*p++ = '\\'; *p++ = 'n';
str++; size++;
continue;
}
if (!(*str & (-32))) break; // end of string (0 or control char)
bs = fl_utf8len(*str); // size of next character
if (bs <= 0) break; // some error - leave