Jump to content
PeterPanettone

Open Url From Current Line

Recommended Posts

Put the caret on a source code line containing the URL of a web page:

 

image.thumb.png.821af822e722e42f2f7a5c45f4ace060.png

 

Then press Alt+T -> U to run this IDE tool Open URL From Current Line:

 

image.thumb.png.4c4881b5faa410e78ef97bc4186e45c2.png

 

Here is the source code:

 

program OpenUrlFromCurrentLine;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  Winapi.Windows, Winapi.ShellAPI,
  System.Classes,
  System.RegularExpressions,
  System.SysUtils;

var
  ThisLine: Integer;
  ThisLineStr, ThisURL: string;
  ThisSource: TStringList;

begin // This is explained at: https://en.delphipraxis.net/
  try
    if ParamCount > 0 then
    begin
      if ParamCount = 2 then
      begin
        if FileExists(ParamStr(1)) then
        begin
          if System.SysUtils.TryStrToInt(ParamStr(2), ThisLine) then
          begin
            ThisSource := TStringList.Create;
            try
              ThisSource.LoadFromFile(ParamStr(1));
              if ThisSource.Count >= (ThisLine) then
              begin
                ThisLineStr := ThisSource[ThisLine - 1];
                ThisURL := TRegEx.Match(ThisLineStr,
                  '\b(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&@#/%=~_|$?!:,.]*[A-Z0-9+&@#/%=~_|$]',
                  [roIgnoreCase]).Value;
                if ThisURL <> '' then
                  Winapi.ShellAPI.ShellExecute(0, 'open', PChar(ThisURL), nil, nil, SW_SHOW);
              end;
            finally
              ThisSource.Free;
            end;
          end;
        end;
      end;
    end;
    //Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

 

Build it.

 

Then create a new IDE tool and configure it like this:

 

image.thumb.png.59c17de90398b170a2d7f48cec2428f9.png

 

Now you are ready to have fun. Merry Christmas!

Edited by PeterPanettone
  • Thanks 1

Share this post


Link to post

Nice.

But: Early Return

...
  if ParamCount < 2 then
    Exit;
    
  if not FileExists(ParamStr(1)) then
    Exit;

  if not System.SysUtils.TryStrToInt(ParamStr(2), ThisLine) then
    Exit;
...

 

 

  • Like 1
  • Haha 1

Share this post


Link to post
4 minutes ago, Anders Melander said:

Nice.

But: Early Return


...
  if ParamCount < 2 then
    Exit;
    
  if not FileExists(ParamStr(1)) then
    Exit;

  if not System.SysUtils.TryStrToInt(ParamStr(2), ThisLine) then
    Exit;
...

 

 

 

Thanks. But how could this improve the tool?

Share this post


Link to post
17 minutes ago, PeterPanettone said:

But how could this improve the tool?

It's a refactoring; It improves the readability of the code without changing the functionality.

  • Like 1
  • Haha 1

Share this post


Link to post
12 minutes ago, Anders Melander said:

It's a refactoring; It improves the readability of the code without changing the functionality.

IMO it complicates the readability. But that's a personal view. Did you get this from a book or is this your own idea?

 

BTW, Pascal Expert found 0 issues:

 

image.thumb.png.8d3f806235bd21ecc49fae3a3030d6dd.png

Edited by PeterPanettone
  • Confused 1

Share this post


Link to post
2 hours ago, PeterPanettone said:

Did you get this from a book or is this your own idea?

That's a strange question.

It's based on the experience that unnecessary nesting makes the code harder to read.

Sure it's a personal preference, some like it, some don't (just google Early Return - I'm sure you will find one or two that doesn't advocate it), but at least one of your "if" blocks are complete superfluous and would be removed by the optimizer if it was worth anything.

 

2 hours ago, PeterPanettone said:

BTW, Pascal Expert found 0 issues

What's the relevance? Are you saying that because some simple rule based validator doesn't flag your code then it can't be improved? - Because I can spot several goofs in it.

  • Like 1
  • Haha 1

Share this post


Link to post
5 hours ago, Anders Melander said:

It's a refactoring; It improves the readability of the code without changing the functionality.

I'm a fan of early returns, I use them all the times. For me it does improve readability but my colleagues complain that it makes it harder for them.

This is a matter of preference IMO, nothing else.

  • Like 1

Share this post


Link to post
11 minutes ago, PeterPanettone said:

I too, if they are LOGICAL. But the above are not.

For me the changes make sense what @Anders Melander mentioned.

But please - do not be OFFENDED. We are NOT saying it's bad code. As stated above - it's only PREFERENCE. 🙂

Share this post


Link to post
7 hours ago, PeterPanettone said:

Did you get this from a book or is this your own idea?

I doub you'll find a single book written in the past 25 years that would advocate nesting over early return.

 

All the most respected experts agree on this and have done for years. 

 

Which books are you reading? 

  • Like 1
  • Haha 1

Share this post


Link to post
7 hours ago, David Heffernan said:

Which books are you reading? 

Science Fiction 😉

 

SCNR

  • Haha 1

Share this post


Link to post
1 hour ago, dummzeuch said:

Science Fiction

Thomas, didn't you say you are a fan of MANGA PORN COMICS? :classic_love:

Edited by PeterPanettone

Share this post


Link to post
13 hours ago, Anders Melander said:

It's based on the experience that unnecessary nesting makes the code harder to read.

That's not an "experience" but a FALSE ASSUMPTION (like e.g. climate change caused by cow farts).

Edited by PeterPanettone
  • Confused 1

Share this post


Link to post
1 minute ago, Anders Melander said:

Code style to tentacles in three posts. I didn't see that coming. :classic_ohmy:

There's always a connection. It's not spaghetti, it's tentacle code!

  • Haha 1

Share this post


Link to post
4 minutes ago, PeterPanettone said:

That's not an "experience" but a FALSE ASSUMPTION.

No, it's not. @Anders Melander said: "It's based on the experience that unnecessary nesting makes the code harder to read." which is not false and not an assumption. It's a general statement.

I only can quote myself. Don't get offended. We are talking about personal preferences here.

  • Like 1

Share this post


Link to post
1 minute ago, PeterPanettone said:

Define "unnecessary nesting".

I'm no expert, but I'd say it's... nesting, which is... unnecessary? 😐

  • Like 2

Share this post


Link to post
1 minute ago, aehimself said:

I'm no expert, but I'd say it's... nesting, which is... unnecessary? 😐

That's not a definition. Try harder.

Share this post


Link to post
1 minute ago, PeterPanettone said:

That's not a definition. Try harder.

There you go, I found it: https://dictionary.cambridge.org/us/dictionary/english/sarcasm

 

I'll try to act like an adult and stop this discussion right here. If you would like to learn more about coding styles, Google is - as always - your friend. In the mean time, please learn how not to get offended when people say their favorite color is not the same as yours.

  • Like 2

Share this post


Link to post

Let's make an intelligent conclusion and let's agree on it: Good code styles are very important and we should follow them. But do not try to apply code styles to your home-baked Christmas cookies!

Edited by PeterPanettone

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

×