I`m looking for a way to create an inspection rule in Intellij that identifies when there are inline comments and moves them to their own line above.
Example: Find
someCode() // someComment
and replace with
// someComment
someCode()
I was able to find the correct regexp to find and replace:
Find: (.\S. )(//)(.\S.)
Replace: $2$3\n$1
But I can`t find a way to make a rule of it.
I have read the documentation on
https://www.jetbrains.com/idea/help/creating-custom-inspections.html
But can`t find any examples that uses regexp for search and replace.
Your regexp seems to be wrong. It does not work the way you think.
This one should work ^(.*)//(.*)
However, I'm not completelly sure that you can use it as a custom inspection.
According to documentation. You need to define some structure based on "placeholders". The placeholders seem to allow regular expressions, but the main structure doesn't seem to do so as it requires an specific syntax.
Related
I'm trying to implement the Lesk Algorithm for word sense disambiguation using Wordnet and it's Java API JWI. One of the steps requires to build a bag of words from the gloss and example sentences of the target word. I can easily get the gloss from the method getGloss() in class ISynset, but I don't see a method to get the example sentences. I'm sure I'm missing something obvious since JWI is described as "full-featured" on wordnet's site, but i can't find anything useful in the documentation or the internet. How do I get those sentences?
It may not be there. Examples are attached to synsets (e.g. they are a sibling function to getting lemmas and definitions in the NLTK API), but the 2.4.0 JWI docs for synset only have functions for getGloss() and getWords().
(If it turns out there is a way to get them from JWI, can someone leave me a comment, and I'll then delete this answer.)
I have a Java (lucene 4) based application and a set of keywords fed into the application as a search query (the terms may include more than one words, eg it can be: “memory”, “old house”, “European Union law”, etc).
I need a way to get the list of matched keywords out of an indexed document and possibly also get keyword positions in the document (also for the multi-word keywords).
I tried with the lucene highlight package but I need to get only the keywords without any surrounding portion of text. It also returns multi-word keywords in separate fragments.
I would greatly appreciate any help.
There's a similar (possibly same) question here:
Get matched terms from Lucene query
Did you see this?
The solution suggested there is to disassemble a complicated query into a more simple query, until you get a TermQuery, and then check via searcher.explain(query, docId) (because if it matches, you know that's the term).
I think It's not very efficient, but
it worked for me until I ran into SpanQueries. it might be enough for you.
I'm looking for a parser that can extract methods from a java class (static source code -> .java file) and method signature, comments / documentation, variables of each of the methods. Preferably in Java programming language.
Could someone please advise?
Thanks.
You can use ASTParser by eclipse. Its super simple to use.
Find a quick standalone example here.
Here is what I do to extract the method signatures from a java file/s:
I use Sublime Text 2, to the file I want to get the signatures from and the do a find Ctrl+F with regular expression set for the following Regex I made (I tested it on my code and it works, I hope it will work for you too)
((synchronized +)?(public|private|protected) +(static [a-Z\[\]]+|[a-Z\[\]]+) [a-Z]+\([a-Z ,\[\]]*\)\n?[a-Z ,\t\n]*\{)
After Sublime Text 2 highlight my results I click on "Find All" then copy Ctrl+C, open a new tab Ctrl+N and paste Ctrl+V.
You will then see all your methods signatures.
I hope it helped.
If all you want is the exact text of each method, and the exact text of the variables inside methods, you could get by with a parser that produces a CST, walking the CST to find the right nodes, and then prettyprinting the found subtrees. ANTLR has a Java parser that would work for this. I don't know if it will capture comments. I think the main distribution of ANTLR is coded in Java.
You can likely do this more hackily, in Java, with a lexer for Java, implementing what amounts to a bad island parser that looks for the key phrases. ("After 'class', find '{' and print out everything you find up to the matching '}'" would give you all the methods and fields).
If you want more precise detail (e.g, you want to know the actual type of an argument rather than just its name, or where the type is actually defined) you'll need a parser with a full front end and name resolution. (ANTLR won't do this.) The Eclipse JDT certainly builds trees; it likely does name resolution. Our DMS Software Reengineering Toolkit with its Java Front End can provide everything necessary for this task, including comment capture and extraction. DMS isn't coded in Java.
You objected to Javadoc as being inadequate, because it doesn't give you the content of methods. Perhaps our Java Source Browser, which does give you that code, would serve better. It integrates name resolution data from our DMS/Java Front End to hyperlink JavaDoc-type information into browsable source text; all fields as well as local variables are explicitly indexed. The Source Browser isn't coded in Java, but then presumably you simply want to run it and scrape your result. Such scraping might be harder than it appears staring at the screen; there's a lot of HTML behind such a display.
I want to search my whole project for a certain string, but I only want results which are uncommented. It would be great if there was just an option to not include comments in the search results, but I don't see how to do this.
Is there a hidden option for this, or a trick? I could go back to the command line and use grep, but I prefer to have the results within eclipse so I can easily jump to the line number.
In "File Search", you can tick "Regular expressions" and use this regex to search for "some text" but exclude one-line (ie // ...) java comments:
^(?!\s*//).*some text
There's probably a better way to express this. It would be difficult (impossible?) to to write a regex for multi-line comments ie /* ...many lines... */
EDIT:
This regex will also exclude all lines whose first character is * - ie javadoc comments:
^(?!\s*(//|\*)).*some text
While lines that start with * are not necessarily comments, it is rare indeed for java code.
You can limit your search results, from the Java Search tab, if you are employing Java Search in the first place.
Searches can be limited to References, Declarations, Implementors and selected other areas of the source code, instead of All ocurrences (which might be the default in your case).
If you are referring to plain text searches (the File Search tab), then you are out of luck.
I'm working in a java project where a big part of the code was written with a formatting style that I don't like (and is also non standard), namely all method parameters are in uppercase (and also all local variables).
On IntellJ I am able to use "Analyze -> Inspect Code" and actually find all occurrences of uppercase method parameters (over 1000).
To fix one occurrence I can do "refactor > rename parameter" and it works fine (let's assume there is no overlapping).
Is there a way to automagically doing this refactor (e.g: rename method parameter starting with uppercase to same name starting with lowercase)?
Use a Source Parser
I think what you need to do is use a source code parser like javaparser to do this.
For every java source file, parse it to a CompilationUnit, create a Visitor, probably using ModifierVisitorAdapter as base class, and override (at least) visit(MethodDeclaration, arg). Then write the changed CompilationUnit to a new File and do a diff afterwards.
I would advise against changing the original source file, but creating a shadow file tree may me a good idea (e.g. old file: src/main/java/com/mycompany/MyClass.java, new file src/main/refactored/com/mycompany/MyClass.java, that way you can diff the entire directories).
I'd advise that you think about a few things before you do anything:
If this is a team effort, inform your team.
If this is for an employer, inform your boss.
If this is checked into a version control system, realize that you'll have diffs coming out the wazoo.
If it's not checked into a version control system, check it in.
Take a backup before you make any changes.
See if you have some tests to check before & after behavior hasn't changed.
This is a dangerous refactoring. Be careful.
I am not aware of any direct support for such refactoring out of the box in IDEs. As most IDEs would support name refactoring (which is regularly used). You may need to write some IDE plugin that could browse through source code (AST) and invoke rename refactoring behind the scene for such parameter names matching such format.
I have done a lot of such refactorings on a rather large scale of files, using TextPad or WildPad, and a bunch of reg-ex replace-all. Always worked for me!
I'm confident that if the code is first formatted using an IDE like Eclipse (if it is not properly formatted), and then a reg-ex involving the methods' signature (scope, return-type, name, bracket, arg list, bracket) can be devised, your job will be done in seconds with these tools. You might need more than one replace-all sets of reg-ex.
The only time-taking activity would be to come up with such a set of reg-ex.
Hope this helps!