Author Topic: Source Formatter  (Read 23770 times)

Offline Ceniza

  • Developer
  • Lives here!
  • *****
  • Posts: 1442
    • CenizaSOFT
Re: Source Formatter
« Reply #30 on: January 18, 2006, 09:29:52 am »
thomas: by "full redisign" I meant doing it totally different. It could be, for example, an interface with all productions and some widget to choose what to do with every token there (put it in the next line, add space before/after, next line and indent, keep original style, ...). That'd be the Stiff GUI.

Heh, and I wouldn't feel like trying to fix bugs in, or adapt, a 5000+ lines project that wasn't written by me and where all that bunch of lines is in just a few files. Code::Blocks is huge, but at least it's well divided in many modules :)

Quote from: thomas
Hey! Why not an "indent between <regex> and <regex>" option? That might be good for other things, too (not just event tables).

See? That'd be a hack! The regex would apply to tokens! :?  :(  :cry:

What about a special IDs thingy? You could add BEGIN_EVENT_TABLE with something like "next line + indent" and END_EVENT_TABLE with "unindent".

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Source Formatter
« Reply #31 on: January 18, 2006, 09:35:55 am »
What about a special IDs thingy? You could add BEGIN_EVENT_TABLE with something like "next line + indent" and END_EVENT_TABLE with "unindent".
Sounds good. :)
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline Raindog

  • Multiple posting newcomer
  • *
  • Posts: 17
Re: Source Formatter
« Reply #32 on: January 19, 2006, 03:58:03 am »
Might I suggest looking at the features that ReSharper or IntelliJ IDEA offer for source code formatting?

www.jetbrains.com

IMO they offer a real complete solution to formatting source code, and instead of using a large screen space to display the formatted source, they use a scrollable text view.

Offline Ceniza

  • Developer
  • Lives here!
  • *****
  • Posts: 1442
    • CenizaSOFT
Re: Source Formatter
« Reply #33 on: January 19, 2006, 07:13:51 pm »
News: I'ven't coded any kind of parser :)

Anyway, I just wanted to comment you about a little source formatter I found and its configuration concept. I'm talking about NiceC, where the idea is to make it learn from existing formatted code. Then I came with the idea of a "learning session".

Here's a scenario (images are for illustrative purposes):

You have Code::Blocks opened, the plugin installed, and a source file loaded. You go to Plugins and select SourceFormatter.

It'll display a dialog showing you the current styles (predefined + user defined), a button to create a new empty style, a button to create a new style from an existing one, a button to apply the style and another button to learn.



You decide to create a new style, select it and click Learn.

Now tokens are found and a dialog pops-up. It'll allow you to configure some behavior based in the current scenario. Say it found a '{' that belongs to an if. Here you can select a set of actions based on some rules like: we're currently in an IF, previous token is, next token is, previous actions, next actions, ...



That one needs more "tuning". An option to add custom states could also be added.

States are pushed in a stack. The IF one would be pushed when a LPAREN token is found and the previous token is IF. Who removes it? :)

The configuration dialog should allow you to see, remove, modify and add actions (dialog not defined yet).

Anyway, what you'd get with that action would be (comments for clarification; dots mean space; indentation is 4 spaces):

Original code:
Code: C++
  1. if.(1.+.1.==.5).{

Formatted code:
Code: C++
  1. if.(1.+.1.==.5)// pre-action: Go to next line
  2. {// post-action: Go to next line
  3. ....// post-action: Indent

Now it's your turn to say how much this idea sucks :D

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Source Formatter
« Reply #34 on: January 19, 2006, 07:22:14 pm »
Look really cool :D. But before a final judgement I would like to see it in action... :D

Michael

[EDIT] @Ceniza: I cannot SVN update the SourceFormatter, because it seems that your server refuses the connection. Should I try later?
 
« Last Edit: January 19, 2006, 07:26:51 pm by Michael »

Offline Ceniza

  • Developer
  • Lives here!
  • *****
  • Posts: 1442
    • CenizaSOFT
Re: Source Formatter
« Reply #35 on: January 19, 2006, 07:48:02 pm »
Quote from: tiwag
98/1/1

Yep, that'd be a huge problem.

Quote from: Michael
Look really cool :D. But before a final judgement I would like to see it in action... :D

See it in action? I just came up with the idea! Those dialogs were made with wxSmith (I got wxSmith to crash many times creating the second one :P) with 0 functionality.

Quote from: Michael
@Ceniza: I cannot SVN update the SourceFormatter, because it seems that your server refuses the connection. Should I try later?

I rebooted the machine where that's stored two times today (I needed its CD-ROM drive :P) and it seems svnserve wasn't started.

Ok, found the problem: I decided to add the command to start svnserve at the end of /etc/init.d/bootmisc.sh, but /etc/rcS.d/S55bootmisc.sh is a file and not a symlink so it was useless. Now I added it to the one in rcS.d :)

I just started it manually so it should work now.

Sorry for the Linux talking :P

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Source Formatter
« Reply #36 on: January 19, 2006, 07:57:35 pm »
Quote from: Michael
Look really cool :D. But before a final judgement I would like to see it in action... :D

See it in action? I just came up with the idea! Those dialogs were made with wxSmith (I got wxSmith to crash many times creating the second one :P) with 0 functionality.

Ok. Anyway, your idea is very good :D. The Learn button gives an air of AI... :D

Concerning the wxSmith crashes (Windows, right?), you should may be post them.

Quote from: Michael
@Ceniza: I cannot SVN update the SourceFormatter, because it seems that your server refuses the connection. Should I try later?

I rebooted the machine where that's stored two times today (I needed its CD-ROM drive :P) and it seems svnserve wasn't started.

Ok, found the problem: I decided to add the command to start svnserve at the end of /etc/init.d/bootmisc.sh, but /etc/rcS.d/S55bootmisc.sh is a file and not a symlink so it was useless. Now I added it to the one in rcS.d :)

I just started it manually so it should work now.

Sorry for the Linux talking :P

No problem for the Linux talking :). The server works fine now. Thanks :).

Michael

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2180
Re: Source Formatter
« Reply #37 on: January 19, 2006, 08:08:02 pm »
I like the linux talking, how else are we gonna learn that greek??  :lol:

pecan

Offline Ceniza

  • Developer
  • Lives here!
  • *****
  • Posts: 1442
    • CenizaSOFT
Re: Source Formatter
« Reply #38 on: January 20, 2006, 09:12:20 pm »
Yesterday I committed the changes I made to the lexer a few days ago, and the first sketch of a formatter (nothing really).

So far the idea of the "rules" system sounds good. I'd need to define the default rules too.

It's a shame I didn't save those dialogs. More work to do again :(

There's a huge problem though: today is friday, 15:00+ here right now. My GF will surely want to come here saturday and sunday (and she won't help me with this plugin). Classes begin on monday.

Code: C++
  1. limit(free_time, busy_time, all_time) | free_time = all_time - busy_time

Quote from: Texas Instruments TI-89
0

:(

Offline Ceniza

  • Developer
  • Lives here!
  • *****
  • Posts: 1442
    • CenizaSOFT
Re: Source Formatter
« Reply #39 on: January 21, 2006, 09:12:19 am »
Naming things... how time consuming can this task be?

Anyway, here is a preliminar version of the basic structures for the rules:

Code: C++
  1. struct Condition
  2. {
  3.   TokenType cur_token;
  4.   TokenType pre_token;
  5.   TokenType next_token;
  6.   State cur_state;
  7. };
  8.  
  9. struct Actions
  10. {
  11.   std::vector<SingleAction> pre_actions;
  12.   std::vector<SingleAction> post_actions;
  13. };
  14.  
  15. struct Rule // version 1
  16. {
  17.   Condition cond;
  18.   Actions acts;
  19. };
  20.  
  21. struct Rule : Condition, Actions // version 2 (I prefer this one)
  22. {
  23.   // What else?
  24. };

So far the function name could be something like matches. Overloading operator == wouldn't be that a good idea.

The function could look something like this:

Code: C++
  1. // kids: don't code like this :P
  2. inline int Condition::matches(const Condition &other)
  3. {
  4.   return cur_token == other.cur_token ? 1 + (pre_token == other.pre_token) + (next_token == other.next_token) + (cur_state == other.cur_state) : 0;
  5. }

Quite simple: if the current token is the same, count it and all others that match, else 0.

What if two rules return the same value (there wouldn't be duplicates, just same number of matches)?

If many return values != 0 but the one with biggest return value is unique, that one would be chosen.

Even though, there sould be another evaluation like this one (var == other.var || var == NOTHING || other.var == NOTHING). So, matches must return two values: exact matches and matches with "empty" variables. A struct, vector or pair<> would do that.

Now, the way to decide which one to apply would be (order is quite important when coding this, not here :P):
* The one with biggest return value for exact matches.
* If there's more than one with the same value, then the one of those with biggest total matches (those counting "empty" cases).
* If even that way there's more than one with both number of matches the same, what to do?
* If all returned 0 for exact matches, return the one with biggest total matches.
* If there's more than one with the same value, what to do?
* If both exact and total matches are 0, it's just a "NO MATCH". Apply default rule (easy to say, but what's the default rule?).

Feedback anyone? :)

Emmanuel Delahaye

  • Guest
Re: Source Formatter
« Reply #40 on: January 21, 2006, 09:36:27 am »
The ~good news: I've started another Source Formatter plugin only for C/C++..
Good Idea. I don't like the A-style formatter. I suggest GNU-Indent 1.91 that is free and has nice features (the more recent versions don't have them).

Actually, the easy way should be to allow to install an external tool with auto-reload (I do that with UltraEdit and Dev-C++).
« Last Edit: January 21, 2006, 09:39:56 am by Emmanuel Delahaye »

Offline Ceniza

  • Developer
  • Lives here!
  • *****
  • Posts: 1442
    • CenizaSOFT
Re: Source Formatter
« Reply #41 on: January 21, 2006, 10:38:15 am »
There's a big problem with GNU-Indent though: it's for C only.

Quote from: Emmanuel Delahaye
Actually, the easy way should be to allow to install an external tool with auto-reload (I do that with UltraEdit and Dev-C++).

Just like using Code::Blocks' Tool menu...

I still wonder if this SourceFormatter based on those basic rules will get somewhere...

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Source Formatter
« Reply #42 on: January 21, 2006, 09:00:45 pm »
I still wonder if this SourceFormatter based on those basic rules will get somewhere...

I think yes :D. May be you can provide a small example that shows some basic functionalities. That would help both you and users. And you will get valuable feedback (IMHO :D).

Michael

Offline takeshi miya

  • Lives here!
  • ****
  • Posts: 1487
Re: Source Formatter
« Reply #43 on: January 21, 2006, 10:38:19 pm »
Good Idea. I don't like the A-style formatter. I suggest GNU-Indent 1.91 that is free and has nice features (the more recent versions don't have them).

Yeah, just clone GNU-indent, it is a lot more configurable than AStyle, but it's designed only for C, not C++.
Anyways, AStyle is great for common cases. I don't think there is an urgent need for another source formatter.

Offline Game_Ender

  • Lives here!
  • ****
  • Posts: 551
Re: Source Formatter
« Reply #44 on: January 22, 2006, 12:07:43 am »
Ok, found the problem: I decided to add the command to start svnserve at the end of /etc/init.d/bootmisc.sh, but /etc/rcS.d/S55bootmisc.sh is a file and not a symlink so it was useless. Now I added it to the one in rcS.d :)

I just started it manually so it should work now.

Sorry for the Linux talking :P

If you are looking for a cleaner way to do that you can use the place the attached script in the /etc/init.d and then make the proper sym links in /etc/rc3.d.  It is a full working init script with start, stop, reload, and force-reload.  You will need to edit the settings for to init svnserve with at the top.

[attachment deleted by admin]