Jump to content
PeterPanettone

Code expert for fixing very simple and obvious syntax errors?

Recommended Posts

Posted (edited)

Does anybody know an IDE Code expert for automatically fixing very simple and obvious non-ambiguous syntax errors, for example:

 

Transform this:

 

image.png.1134b1ac9927e3602a9ad2ff4ce7de4d.png

 

Into this:

 

image.png.8c111c98762c84c0d4c73768570197bd.png

Edited by PeterPanettone

Share this post


Link to post
4 minutes ago, David Heffernan said:

Is this the only error that you want to be fixed automatically? 

For now, yes.

Share this post


Link to post
Posted (edited)
48 minutes ago, David Heffernan said:

Is this the only error that you want to be fixed automatically? 

Look for ESLint, add-on for VSCode

 

 

Edited by Jacek Laskowski

Share this post


Link to post
36 minutes ago, Jacek Laskowski said:

Look for ESLint, add-on for VSCode

 

In the video, it fixes just one error at a time. Can it also fix multiple errors with one action?

 

But I doubt it can fix Delphi syntax errors... or is it configurable?

Share this post


Link to post
39 minutes ago, David Heffernan said:

Wouldn't you just add the commas? I mean, how many lines of code do you have with this mistake in? 

There would be many lines. An automatism would be faster than adding the commas manually. Ideally, I would select a block of code and the errors in this block would be fixed. Ideally, the compiler would help.

Share this post


Link to post
25 minutes ago, PeterPanettone said:

There would be many lines. An automatism would be faster than adding the commas manually

How are those lines entered? If by typing, why not just type the commas as the code is being typed? If they're generated, why not generate the lines with commas in the first place?

Share this post


Link to post
1 minute ago, Dave Nottage said:

How are those lines entered?

They come from an external source by pasting.

Share this post


Link to post

I could easily write a small script-tool which fixes the commas. But at a more general scope, it would be interesting if it was possible to fix these syntax errors with the help of the compiler?

Share this post


Link to post
47 minutes ago, PeterPanettone said:

They come from an external source by pasting.

Why don't you use MultiPaste in the first place and let it add the comma after each line. Then you end up with only one remaining error for the semicolon at the last line.

  • Like 3
  • Thanks 2

Share this post


Link to post
Posted (edited)
1 hour ago, Uwe Raabe said:

Why don't you use MultiPaste in the first place and let it add the comma after each line. Then you end up with only one remaining error for the semicolon at the last line.

Thanks, Uwe, this is a VERY GOOD idea!

 

image.thumb.png.6905fd152fcc0176c3f7c07665f43b70.png

 

In fact, this completely solves the problem with pasting many raw use-clause-items.

 

Didn't even know Multipaste before.

 

Maybe it should be called SmartPaste instead of MultiPaste, as it really is not a multiple paste action.

 

Maybe also the dialog should automatically remember its last settings.

Edited by PeterPanettone

Share this post


Link to post
Posted (edited)
9 hours ago, PeterPanettone said:

I could easily write a small script-tool which fixes the commas. But at a more general scope, it would be interesting if it was possible to fix these syntax errors with the help of the compiler?

Writing a tool is how I would approach this. Learn a language like Python so that task like this can be done very easily and quickly.

 

Of course, an even better solution is to fix the process that is generating erroneous code in the first place. 

Edited by David Heffernan

Share this post


Link to post

There is also the GExperts Convert Strings editor expert. Unfortunately I just realized that it does not cover this excact case. Hm, time for an improvement: Add a suffix in addition to the existing prefix option for each line.

 

Btw: Another solution would be to create a keyboard macro for this case. I would require one Shift+Ctrl+P keypress per line though (still better than repeating the required key sequences for every line). I have attached such a macro in case anybody is interested. It can be imported into the Keyboard Macro Manager.

 

AppendComma.gxm

Share this post


Link to post
Posted (edited)
On 4/7/2019 at 11:15 PM, PeterPanettone said:

I could easily write a small script-tool which fixes the commas. But at a more general scope, it would be interesting if it was possible to fix these syntax errors with the help of the compiler?

Has anyone ever used the macro recorder in the IDE? Just move the cursor to the first line, start recording, press end-of-line key, add comma, press down-arrow key, press start-of-line key, stop recording. Now just replay as many times as needed. Can't be for more than a few lines (and not hundreds of them, I hope).

 

I often do things like that, for instance when I'm converting a header to a Delphi unit. The macro recorder is extremely useful for often repeated actions (i.e. on many lines) that are not so easily done with a regexp search and replace. The latter is the other way to easily convert certain patterns.

 

Update: I see @dummzeuch had the same idea.

Edited by Rudy Velthuis

Share this post


Link to post
Posted (edited)

Just in case anybody is still interested: I have changed the GExperts Convert Strings Editor Expert to allow for prefixes and suffixes for all lines.

 

Example:

 

I want to create a string containing this code:

function FindClassForm(const AClassName: string): TForm;
var
  i: Integer;
  s: string;
begin
  Result := nil;
  s := 'Some String';
  for i := 0 to Screen.FormCount - 1 do
    if Screen.Forms[i].ClassNameIs(AClassName) then begin
      Result := Screen.Forms[i];
      Break;
    end;
end;

Which means:

  • Quote the lines and escape any existing quotes
  • Add a variable assignment as prefix to the first line
  • Add a "       " prefix to all lines to indent the whole thing
  • add a " + #13#10" suffix to each line but the last
  • add a ";" suffix to the last line.

The result looks like this:

s := 'function FindClassForm(const AClassName: string): TForm; ' + #13#10
      'var ' + #13#10
      '  i: Integer; ' + #13#10
      '  s: string; ' + #13#10
      'begin ' + #13#10
      '  Result := nil; ' + #13#10
      '  s := ''Some String''; ' + #13#10
      '  for i := 0 to Screen.FormCount - 1 do ' + #13#10
      '    if Screen.Forms[i].ClassNameIs(AClassName) then begin ' + #13#10
      '      Result := Screen.Forms[i]; ' + #13#10
      '      Break; ' + #13#10
      '    end; ' + #13#10
      'end; ';

There is no release for this yet, but you can always get the sources and compile your own dll:

https://blog.dummzeuch.de/gexperts-documentation/compiling-gexperts/

GExperts-Convert-Strings-Expert.png

Edited by dummzeuch

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×