Extend Jython auto-completion to instantiated classes#51
Conversation
Currently, if there is e.g., a typo in an import declaration completion will fail with a ClassNotFoundException. This commit adds placeholders to the completion list to warn user about such cases,
|
This pull request has been mentioned on Image.sc Forum. There might be relevant details there: https://forum.image.sc/t/auto-code-completion-for-python-and-groovy-in-imagej-fiji/20515/22 |
|
@acardona , there are still two things that are missing in this PR:
from ij import ImagePlus
imp = ImagePlus()But no completions are retrieved if you use: from ij import IJ
imp = IJ.getImage()The latter would be the most important issue. I will wait for your feedback on this. |
|
This pull request has been mentioned on Image.sc Forum. There might be relevant details there: https://forum.image.sc/t/auto-code-completion-for-python-and-groovy-in-imagej-fiji/20515/25 |
|
Thanks a lot @tferr, this is great. On
or even: |
|
On this use case: If it's from a static method, the object class can be retrieved from the return type of the static method. If it's from a function, like: ... then no cookie: it's the perennial problem in non-strongly typed languages. Python is fixing this with annotations, which seem to be a part of the language in 3.8. There is currently an effort to bring jython to python 3+, but it's very understaffed (one very part-time developer). |
(not just those which start with it)
| // System.out.println("Hit: line #" + i + ": " + line); | ||
| // System.out.println("Matcher g1: " + matcher.group(1)); | ||
| // System.out.println("Matcher g2: " + matcher.group(2)); | ||
| if (variable.equals(matcher.group(1))) { |
There was a problem hiding this comment.
This approach is dangerous in that it doesn't consider the scope of the variable. I would do it differently, by parsing the code like jython's interpreter with a ParserFacade, see e.g. https://github.com/acardona/scripts/blob/master/python/imagej/jython/parse_code.py
There was a problem hiding this comment.
Good point. Are you able to work on this? To me the priority would be to support he return type of the static method so I'd prefer to work on that instead (unless you say otherwise)
There was a problem hiding this comment.
Yes, proper jython code parsing is something I've been working on but haven't committed yet. Will lead to:
- autocompletion for variable names within scope, in addition to possible java class imports;
- autocompletion and auto-import for python modules included in the jython jar or discoverable from its PYTHONPATH-equivalent;
- type resolution for variables within scope, in order to autocomplete their methods. Won't always work (e.g. when an object derives from a call on a function argument), but it will work in many cases.
For the latter case, the AutoCompletionListener interface will allow e.g. @haesleinhuepf's update site to add variable name-based imports (e.g. for imp, ip, fp, etc.)
There was a problem hiding this comment.
@acardona, Great! On a second thought I will hold for now, as you are likely to revamp a bunch of things. But do let me know if there is something you want me too look into. I won't work on this until I hear from you (so feel free to merge), but do let me know if you want me to look into something. I will post below a summary of what this (snowballed) PR now has.
| static private final Pattern importPattern = Pattern.compile("^(from[ \\t]+([a-zA-Z_][a-zA-Z0-9._]*)[ \\t]+|)import[ \\t]+([a-zA-Z_][a-zA-Z0-9_]*[ \\ta-zA-Z0-9_,]*)[ \\t]*([\\\\]*|)[ \\t]*(#.*|)$"), | ||
| tripleQuotePattern = Pattern.compile("\"\"\""); | ||
| tripleQuotePattern = Pattern.compile("\"\"\""), | ||
| variableDeclarationPattern = Pattern.compile("([a-zA-Z_][a-zA-Z0-9._]*)\\s*=\\s*([A-Z_][a-zA-Z0-9._]*)(?:\\()"); // E.g., in 'imp=ImagePlus()' grou1: imp; group2: ImagePlus |
There was a problem hiding this comment.
Not all kinds of whitespace are valid, for example a line break isn't valid if the line doesn't end with a backslash. That's why I used [ \t] instead of \s.
There was a problem hiding this comment.
Good point. will fix.
| String packageName = m2.group(3), | ||
| className = m2.group(4); // incomplete or empty, or multiple separated by commas with the last one incomplete or empty | ||
| final String packageName = m2.group(3); // incomplete or empty, or multiple separated by commas with the last one incomplete or empty | ||
| String className = m2.group(4); |
There was a problem hiding this comment.
Not sure why this code was changed here? Seems like you have an opinionated code formatter. Now the comment is in the wrong place.
There was a problem hiding this comment.
Yes. My Eclipse has a will of its own. I hate it when it does that. Will fix.
|
|
||
| // a call to an instantiated class | ||
| final String[] varAndSeed = getVariableAnSeedAtCaretLocation(); | ||
| if (varAndSeed == null) return Collections.emptyList();; |
…ontain_term auto-complete words that contain a term
This reverts commit b7d5201.
Import as statement (Adds support for lower case aliases)
- Include parameters type in replacement text (`imp.draw(int, int, int, int)` vs `imp.draw(` - Improve summary - Display type of fields - Include javadoc link in description - Fix dup. text
...This facilitates access to other future providers (Groovy, etc.)
(also fixes missing .html suffix in URL)
At least for now. Somehow breaks variable completion This reverts commit 9a584df.
|
For clarity, here is what this PR implements:
Here are some snapshots: Fuzzy string search (methods): I have to say, I find it quite functional even at this stage.
|
At least for now. Somehow breaks variable completion This reverts commit 9a584df.
|
Hi @tferr, the code may have changed significantly. Would appreciate if you could revise your commits and see which can be applied. The branch I pushed forward is "fix-autocompletion-listener". And @haesleinhuepf: notice I've had to change the AutoCompletionListener interface significantly. Please have a look, should be straightforward to adapt for CLIJ to insert expansions for commonly used variable names. |
Currently, if there is e.g., a typo in an import declaration completion will fail with a ClassNotFoundException. This commit adds placeholders to the completion list to warn user about such cases,
(not just those which start with it)
This reverts commit b7d5201.
- Include parameters type in replacement text (`imp.draw(int, int, int, int)` vs `imp.draw(` - Improve summary - Display type of fields - Include javadoc link in description - Fix dup. text
...This facilitates access to other future providers (Groovy, etc.)
(also fixes missing .html suffix in URL)
At least for now. Somehow breaks variable completion This reverts commit 9a584df.
|
@acardona, to include JavaDoc URLs and list types in the completions, I would have to modify DotAutoCompletions#get() to return both the completion String and the completion description (also a String). Are you OK with it? |
|
Hi @tferr, sounds good, should then return a new object that has both as fields rather than just a String. |
|
By the way @tferr, when done, after review/test, I'd appreciate very much if you could release it all: the jython-autocompletion and the script-editor, so that it all appears in the Fiji updater. |
|
@acardona, sincere apologies for the delay. 'Real work' got in the way. I think I have it working, but still need to test it more seriously. Hyperlinks seem to be working, and the completion lists the type for both fields and methods. If there are no major issues I will push and release tonight. There are still some (minor) issues like the |
|
Sounds great! On the subclasses: a one-liner filtering for dollar signs '$', indicative of subclasses, would do, at the level of the returned stream or list of autocompletions. |
... in https://github.com/fiji/jython-autocompletion While at it, do some code cleanup
|
@acardona, I don't seem to have write access to this repository. Could you please merge this AND release version 5.0.10 of the script editor? Otherwise, give me write permissions and I will do so. Thanks! |




@acardona , @haesleinhuepf , This implements the following changes by extending Albert's recent work: