IDE Scripting Question

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.

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.

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.

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

[quote=454526:@]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.[/quote]
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.

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.

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 :wink:

Here’s the base:

[code]
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[/code]

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

And customize it. :wink:

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.

@Greg O’Lone : 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…

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).

[quote=454567:@Emile Schwarz]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.[/quote]
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.

@Greg O’Lone You are a star! This looks great. I’m going to play around with this.

Possibly related to what @Jean-Yves Pochez 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.

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

[quote=454549:@Greg O’Lone]…This script is for macOS and you’ll probably have to tweak the chr(13) for other platforms…
[/quote]
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.

[quote=454598:@Garry Pettet]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.[/quote]
Legacy reasons.

[quote=454577:@Garry Pettet]@Greg O’Lone You are a star! This looks great. I’m going to play around with this.

Possibly related to what @Jean-Yves Pochez 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.[/quote]
Same issue as writing. It was just never added.

Thanks Greg.

Feedback case for adding the ability to manipulate the description field on properties and methods via an IDE script: <https://xojo.com/issue/57497>