IDE Scripting Question

  1. 8 months ago

    Garry P

    17 Sep 2019 Testers, Xojo Pro Europe (Torquay, UK)

    I'd like to have a standardised comment header at the top of all of my methods. Specifically, I would like to document each parameter, whether it throws an exception and what the return value means. Up until now I have been using Dash's snippets function. Whilst this works OK, I have to manually enter the parameter names and types, etc. I figured this might be something I could do with an IDE script.

    Are there IDE scripting commands to do the following?

    1. Get the name of the current method
    2. Get the names of the current method's parameters (if any)
    3. Get the return type (if any).

    I have read the IDE scripting section of the user guide but it's not particularly helpful.

    Absolutely doable. This script is for macOS and you'll probably have to tweak the chr(13) for other platforms...

    DoCommand("Copy")
    
    Dim lines() As String
    lines = split(clipboard, chr(13))
    Dim sigline As String = lines(0)
    
    Dim parts() As String
    Dim p1 As Integer = instr(sigline, "(")
    Dim name, paramstr As String
    
    name = left(sigline, p1 - 1)
    Dim p2 As Integer = instr(p1 + 1, sigline, ")")
    paramstr = mid(sigline, p1 + 1, p2 - p1 - 1)
    
    Dim scope As String = nthfield(name, " ", 1)
    Dim fnType As String = nthfield(name, " ", 2)
    name = nthfield(name, " ", 3)
    
    Dim params() As String = split(paramstr, ",")
    // If  you want only parameter names, strip them here
    For i As Integer = 0 To UBound(params)
      params(i) = trim(params(i))
    Next
    
    Dim returntype As String = nthfield(sigline, " ", countfields(sigline, " "))
    If instr(returntype, ")") > 0 Or returntype = name Then 
      returntype = ""
    End If
    
    Redim lines(-1)
    lines.append "//===================================="
    lines.append "//"
    lines.append "// Name: " + name
    lines.append "// Params: "
    For i As Integer = 0 To UBound(params)
      lines.append "//    " + params(i)
    Next
    lines.append "// Returns: " + returntype
    lines.append "//"
    lines.append "//===================================="
    lines.append ""
    lines.append ""
    
    Text = join(lines, endofline) + Text

    To use it, select a method and run the script.

  2. Beatrix W

    17 Sep 2019 Testers, Third Party Store Europe (Germany)

    That sounds like way too much work.

    1 is "currentmethodname"
    2 + 3 probably with introspection

    Arbed has some sort of scripting language which may help.

  3. Julian S

    17 Sep 2019 Xojo Pro UK

    I don't think so Garry. I looked into this a while back and found that I couldn't even traverse the project in script. IDE Scripting seems very one sided it seems to have been written as a tool for build automation rather than controlling the IDE.

    The best bet would probably be to parse your your xml/text saves and inject it that way via an external app.

  4. Garry P

    17 Sep 2019 Testers, Xojo Pro Europe (Torquay, UK)

    @Beatrix W 2 + 3 probably with introspection

    I'm not sure if you can use introspection from within a Xojo script. Does anyone know if you can?

    @Julian S I don't think so Garry. I looked into this a while back and found that I couldn't even traverse the project in script. IDE Scripting seems very one sided it seems to have been written as a tool for build automation rather than controlling the IDE.

    The best bet would probably be to parse your your xml/text saves and inject it that way via an external app.

    That's my fear. I'm not keen on altering the source code files though. That seems like a sure fire way to corrupt a project.

  5. Kem T

    17 Sep 2019 Testers, Xojo Pro, XDC Speakers, MVP Connecticut

    I've done things similar to this with Keyboard Maestro. Not perfect, but it worked.

    In this case, perhaps something that chooses the contextual menu that inserts pragmas for unused parameters, then an ide script that alters that text into what you want?

    I have to think on how to get the method name.

  6. Emile S

    17 Sep 2019 Europe (France, Strasbourg)

    @Julian S I don't think so Garry. I looked into this a while back and found that I couldn't even traverse the project in script. IDE Scripting seems very one sided it seems to have been written as a tool for build automation rather than controlling the IDE.

    This was possible to do far before Introspection was added.

    Now, what I use is a “comment block” with the current date and my name/location: simple IDE Script.

    Garry’s idea to automate the Method (Function) Name Parameters (in / out) is a good complement.

    I do not think at that. But since I have to explain what the Method/Function do… Copy/Paste is used.

    BTW: one thing to not forget is to move the scripts from the current (now old) version to the (new) version at new version install time. You do not understand ? sorry: hard to explain ;)

  7. Emile S

    17 Sep 2019 Europe (France, Strasbourg)

    Here’s the base:

    Dim header As String
    
    Dim theResult As String
    // Dim Today As New Date
    // theResult = DoShellCommand(date)
    
    Dim command As String
    Dim result  As String
    
    command = "date '+%Y-%m-%d; %H:%M:%S'"
    
    result = DoShellCommand(command)
    result = Left(result, Len(result)-1)
    
    header = "// " + EndOfLine
    header =  header + "// ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------" + EndOfLine
    header =  header + "// " + EndOfLine
    header =  header + "// " + EndOfLine
    header =  header + "// " + EndOfLine
    header =  header + "// " + EndOfLine
    header =  header + "// " + EndOfLine
    header =  header + "// " + EndOfLine
    header =  header + "// ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------" + EndOfLine
    header =  header + "// " + EndOfLine + EndOfLine
    
    // Display the Comments block
    Text = header + EndOfLine + EndOfLine + Text

    Set your name, etc. in the line(s) before the long one.

    And customize it. ;)

  8. Greg O

    17 Sep 2019 Xojo Inc Answer scout.galaxy.barn
    Edited 8 months ago

    Absolutely doable. This script is for macOS and you'll probably have to tweak the chr(13) for other platforms...

    DoCommand("Copy")
    
    Dim lines() As String
    lines = split(clipboard, chr(13))
    Dim sigline As String = lines(0)
    
    Dim parts() As String
    Dim p1 As Integer = instr(sigline, "(")
    Dim name, paramstr As String
    
    name = left(sigline, p1 - 1)
    Dim p2 As Integer = instr(p1 + 1, sigline, ")")
    paramstr = mid(sigline, p1 + 1, p2 - p1 - 1)
    
    Dim scope As String = nthfield(name, " ", 1)
    Dim fnType As String = nthfield(name, " ", 2)
    name = nthfield(name, " ", 3)
    
    Dim params() As String = split(paramstr, ",")
    // If  you want only parameter names, strip them here
    For i As Integer = 0 To UBound(params)
      params(i) = trim(params(i))
    Next
    
    Dim returntype As String = nthfield(sigline, " ", countfields(sigline, " "))
    If instr(returntype, ")") > 0 Or returntype = name Then 
      returntype = ""
    End If
    
    Redim lines(-1)
    lines.append "//===================================="
    lines.append "//"
    lines.append "// Name: " + name
    lines.append "// Params: "
    For i As Integer = 0 To UBound(params)
      lines.append "//    " + params(i)
    Next
    lines.append "// Returns: " + returntype
    lines.append "//"
    lines.append "//===================================="
    lines.append ""
    lines.append ""
    
    Text = join(lines, endofline) + Text

    To use it, select a method and run the script.

  9. Jean-Yves P

    17 Sep 2019 Testers, Xojo Pro Europe (France, Besançon)

    @Greg OLone : could you store this in the description field of the method ?

  10. Greg O

    17 Sep 2019 Xojo Inc scout.galaxy.barn

    @Jean-YvesPochez @Greg OLone : could you store this in the description field of the method ?

    I don't think we have a way to get it into the description at this time in an IDE script. That would make a good feature request though...

  11. Emile S

    17 Sep 2019 Europe (France, Strasbourg)

    Greg Thank you:
    WORKS FINE within Xojo 2019r1.1.

    To all: make extensive tests 'caus I have troubles (both with the script run many times and with my eyes).

    On Xojo 2015r1:
    The Method name is not here
    If run twice, it report an error in line 0 // Add a blank line at the top of the Editor Code and all is OK.

    Nota: I have to stop here, now, my vision is really too bad (a brand new one).

  12. Greg O

    17 Sep 2019 Xojo Inc scout.galaxy.barn

    @Emile S On Xojo 2015r1:
    The Method name is not here
    If run twice, it report an error in line 0 // Add a blank line at the top of the Editor Code and all is OK.

    That doesn't surprise me. There have been a number of fixes to the IDE scripting over the years and 2015 was one of the times when a lot of things got fixed because that summer we implemented a Continuous Integration pipeline. That process brought a lot of little subtle bugs to light.

  13. Garry P

    17 Sep 2019 Testers, Xojo Pro Europe (Torquay, UK)

    @Greg OLone You are a star! This looks great. I'm going to play around with this.

    Possibly related to what @Jean-YvesPochez said but rather than putting the formatted header into the description field, is there any way of getting the contents of the description field? What I tend to do is create a method and then add the description. If it was possible to get the contents of the description field then I could add that to the header of my method comment.

    If not, I'll add a Feedback request.

  14. Dave S

    17 Sep 2019 San Diego, California USA

    At one time I had begun a self-documenter for Xojo projects. It would read certain specially formatted comments, method signatures as well as description fields and create a linkable webpage document.

    XCode does something like this in real time, and there is a program called JAZZY that creates this same document

  15. Garry P

    17 Sep 2019 Testers, Xojo Pro Europe (Torquay, UK)

    @Greg OLone ...This script is for macOS and you'll probably have to tweak the chr(13) for other platforms...

    I'm on macOS.
    Splitting on Chr(13) works but why? I thought the newline character was Chr(10) on macOS? That's what EndOfLine returns in the script at least.

  16. Greg O

    17 Sep 2019 Xojo Inc scout.galaxy.barn

    @Garry P I'm on macOS.
    Splitting on Chr(13) works but why? I thought the newline character was Chr(10) on macOS? That's what EndOfLine returns in the script at least.

    Legacy reasons.

  17. Greg O

    17 Sep 2019 Xojo Inc scout.galaxy.barn

    @Garry P @Greg OLone You are a star! This looks great. I'm going to play around with this.

    Possibly related to what @Jean-YvesPochez said but rather than putting the formatted header into the description field, is there any way of getting the contents of the description field? What I tend to do is create a method and then add the description. If it was possible to get the contents of the description field then I could add that to the header of my method comment.

    If not, I'll add a Feedback request.

    Same issue as writing. It was just never added.

  18. Garry P

    17 Sep 2019 Testers, Xojo Pro Europe (Torquay, UK)
    Edited 8 months ago

    Thanks Greg.

    Feedback case for adding the ability to manipulate the description field on properties and methods via an IDE script: Feedback Case #57497

or Sign Up to reply!