I'm trying to write a plug-in that among other things adds a function header. Unfortunately this is also my first C program so it has been more of a learning experience then anything, but I'm finally at the point where it sort of works.
So far it has been a trial and error sort of thing using devhelp and various web sites, but now I am at the point where I need some guidance that is Geany specific. Currently my plug-in reads the current line and breaks it down, however it's my understanding that some C programmers may break a function definition over several lines, in which case my plug-in would not work properly. I would also like to make it so the function definition for whatever the current function is would be used.
So this brings me to my questions, is there an easy way to determine if the current position in the editor is part of a function or not? If so is there an easy way to get the function definition? If not could someone maybe point me to some tutorials about parsing text in c.
Also I was wondering if it's possible to use Valgrind to profile a plug-in?
Also currently the header that is generated by my plug-in c style multi line hard coded, and I was wondering if at some time more of the methods in templates.c would be opened up to the API?
-Jordan
Hi Jordan,
jordan a écrit :
I'm trying to write a plug-in that among other things adds a function header. Unfortunately this is also my first C program so it has been more of a learning experience then anything, but I'm finally at the point where it sort of works.
What does your plugin, apart adding a function header? I'm curious. And hum, I'm working on something similar to this part: a "generic" documentation generator [1].
Currently my plug-in reads the current line and breaks it down, however it's my understanding that some C programmers may break a function definition over several lines, in which case my plug-in would not work properly.
Yes. Many programmers (I'm of these) breaks function definition in multiple line, actually I do something like this:
return_type function_name (type arg1, type arg2, ...)
…and of course it is a little harder to naively parse (not so much in facts, but doesn't allow what you do).
I would also like to make it so the function definition for whatever the current function is would be used.
So this brings me to my questions, is there an easy way to determine if the current position in the editor is part of a function or not? If so is there an easy way to get the function definition? If not could someone maybe point me to some tutorials about parsing text in c.
As Enzo said, there is the scope to determine if a particular line is part of a function or not, but its is not supported by every tag generator used by Geany. What I personally chose was to walk the tag list and find the tag closest to the current line. Tweaking this way a bit gives quite good results.
And for the function definition, see the tag list, there is everything you probably want (or almost). One missing thing is the argument list – I personally chose to fix this with a regular expression, but there might be better solutions; for example if hard-coded things are acceptable, a custom argument parser is a somewhat easy task.
Also currently the header that is generated by my plug-in c style multi line hard coded, and I was wondering if at some time more of the methods in templates.c would be opened up to the API?
I don't know exactly your goals, but Geany's templates are not complete. if I'm right: they are very simple, which is good, but doesn't support things like loops and so (as far as I know, tell me if I'm wrong). Since I didn't find any template parser in C, I written my own [2] for the needs of the plugin I write. It is heavily more complex, but OTOH more powerful… choice is not easy, is it? :D If you're interested, feel free to use it.
Regards, Colomban
[1] no it's not dead, you'll see soon (for those who understand ^^) [2] http://ctpl.tuxfamily.org/ ; but if you're interested, wait for the upcoming 0.2 release that will be somewhat different in its API. It's almost ready and can be found in the git master.
On 04/14/2010 01:56 PM, Colomban Wendling wrote:
Hi Jordan,
jordan a écrit :
I'm trying to write a plug-in that among other things adds a function header. Unfortunately this is also my first C program so it has been more of a learning experience then anything, but I'm finally at the point where it sort of works.
What does your plugin, apart adding a function header? I'm curious. And hum, I'm working on something similar to this part: a "generic" documentation generator [1].
Currently just function header, but as I figure things out I'd like to do a function generator as well, that would in turn call the header generator if that was selected. Currently it works for the most part if the defiinition is on a single line. It displays a dialog with the method name, and labels and GtkEntrys to enter a description for the method and if applicable any parameters, and return value.
Currently my plug-in reads the current line and breaks it down, however it's my understanding that some C programmers may break a function definition over several lines, in which case my plug-in would not work properly.
Yes. Many programmers (I'm of these) breaks function definition in multiple line, actually I do something like this:
return_type function_name (type arg1, type arg2, ...)
…and of course it is a little harder to naively parse (not so much in facts, but doesn't allow what you do).
I would also like to make it so the function definition for whatever the current function is would be used.
So this brings me to my questions, is there an easy way to determine if the current position in the editor is part of a function or not? If so is there an easy way to get the function definition? If not could someone maybe point me to some tutorials about parsing text in c.
As Enzo said, there is the scope to determine if a particular line is part of a function or not, but its is not supported by every tag generator used by Geany. What I personally chose was to walk the tag list and find the tag closest to the current line. Tweaking this way a bit gives quite good results.
And for the function definition, see the tag list, there is everything you probably want (or almost). One missing thing is the argument list – I personally chose to fix this with a regular expression, but there might be better solutions; for example if hard-coded things are acceptable, a custom argument parser is a somewhat easy task.
How do I access the scope, or this tag list you speak of, I noticed nothing like that in plug-in api documentation?
Also currently the header that is generated by my plug-in c style multi line hard coded, and I was wondering if at some time more of the methods in templates.c would be opened up to the API?
I don't know exactly your goals, but Geany's templates are not complete. if I'm right: they are very simple, which is good, but doesn't support things like loops and so (as far as I know, tell me if I'm wrong). Since I didn't find any template parser in C, I written my own [2] for the needs of the plugin I write. It is heavily more complex, but OTOH more powerful… choice is not easy, is it? :D If you're interested, feel free to use it.
I was only thinking templates so that coding styles would be easy to enforce, by easily allowing the user to change the style of the header.
Regards, Colomban
[1] no it's not dead, you'll see soon (for those who understand ^^) [2] http://ctpl.tuxfamily.org/ ; but if you're interested, wait for the upcoming 0.2 release that will be somewhat different in its API. It's almost ready and can be found in the git master. _______________________________________________ Geany-devel mailing list Geany-devel@uvena.de http://lists.uvena.de/cgi-bin/mailman/listinfo/geany-devel
jordan a écrit :
As Enzo said, there is the scope to determine if a particular line is part of a function or not, but its is not supported by every tag generator used by Geany. What I personally chose was to walk the tag list and find the tag closest to the current line. Tweaking this way a bit gives quite good results.
And for the function definition, see the tag list, there is everything you probably want (or almost). One missing thing is the argument list – I personally chose to fix this with a regular expression, but there might be better solutions; for example if hard-coded things are acceptable, a custom argument parser is a somewhat easy task.
How do I access the scope, or this tag list you speak of, I noticed nothing like that in plug-in api documentation?
See the tm_file field of GeanyDocument. I don't know exactly for the scope, but the tag list is doc->tm_file->tags_array.
On 04/16/2010 02:39 PM, Colomban Wendling wrote:
jordan a écrit :
As Enzo said, there is the scope to determine if a particular line is part of a function or not, but its is not supported by every tag generator used by Geany. What I personally chose was to walk the tag list and find the tag closest to the current line. Tweaking this way a bit gives quite good results.
And for the function definition, see the tag list, there is everything you probably want (or almost). One missing thing is the argument list – I personally chose to fix this with a regular expression, but there might be better solutions; for example if hard-coded things are acceptable, a custom argument parser is a somewhat easy task.
How do I access the scope, or this tag list you speak of, I noticed nothing like that in plug-in api documentation?
See the tm_file field of GeanyDocument. I don't know exactly for the scope, but the tag list is doc->tm_file->tags_array.
Thanks for the pointer, I have figured out how to access the tags, and have started re-writing my code to use tags instead of the current line, and it is now able to handle multiline function definitions, however this method seems broken as after the first comment block is inserted any tags below the insertion no longer have a valid line number until the application is saved.
I notice this behavior is also present in the symbol browser of Geany, if you add some lines and try to go to a symbol using the symbol browser it will take you to the wrong line, is there any way to force an update of the tags after my plugin inserts the text? -Jordan
jordan a écrit :
[…] however this method seems broken as after the first comment block is inserted any tags below the insertion no longer have a valid line number until the application is saved.
I notice this behavior is also present in the symbol browser of Geany, if you add some lines and try to go to a symbol using the symbol browser it will take you to the wrong line, is there any way to force an update of the tags after my plugin inserts the text? -Jordan
The only way (for now) is to save the file, which will update the tag list. See http://lists.uvena.de/pipermail/geany-devel/2009-March/000535.html for other details (yay, I had the same problem). To fix this it would need to make tm_source_file_buffer_update() work, which according to Enrico is not the case for now.
I trick if you want to do insertion sequences at once would be to start inserting from the end of the file, which would not modify the upper tags… yep, kinda workaround; doesn't work if the user is involved beteen the insertions.
Regards, Colomban
On Sat, 17 Apr 2010 14:55:40 +0200, Colomban wrote:
jordan a écrit :
[…] however this method seems broken as after the first comment block is inserted any tags below the insertion no longer have a valid line number until the application is saved.
I notice this behavior is also present in the symbol browser of Geany, if you add some lines and try to go to a symbol using the symbol browser it will take you to the wrong line, is there any way to force an update of the tags after my plugin inserts the text? -Jordan
The only way (for now) is to save the file, which will update the tag list. See http://lists.uvena.de/pipermail/geany-devel/2009-March/000535.html for other details (yay, I had the same problem). To fix this it would need to make tm_source_file_buffer_update() work, which according to Enrico is not the case for now.
Some time ago, I started working on this but it never really worked and additionally, it could work currently only for a few parsers (some of those are C, Fortran, SQL IIRC). To get it working reliably, some more work is needed and we would have to adjust *all* existing parsers which is by no means an easy task.
Regards, Enrico
On Fri, 16 Apr 2010 20:39:59 +0200, Colomban wrote:
jordan a écrit :
As Enzo said, there is the scope to determine if a particular line is part of a function or not, but its is not supported by every tag generator used by Geany. What I personally chose was to walk the tag list and find the tag closest to the current line. Tweaking this way a bit gives quite good results.
And for the function definition, see the tag list, there is everything you probably want (or almost). One missing thing is the argument list – I personally chose to fix this with a regular expression, but there might be better solutions; for example if hard-coded things are acceptable, a custom argument parser is a somewhat easy task.
How do I access the scope, or this tag list you speak of, I noticed nothing like that in plug-in api documentation?
See the tm_file field of GeanyDocument. I don't know exactly for the scope, but the tag list is doc->tm_file->tags_array.
An alternative way is using symbols_get_current_function() but this is currently not part of the plugin API. But we could add it if requested.
Regards, Enrico