Eclipse shortcut to insert variable in string literal - java

When I need to interpolate a String variable into a quoted string literal in the Eclipse java editor, I generally type "++" and eclipse usually inserts another " after the latter one, so I then have to go past it with arrow keys and then backspace to remove the duplicate, then go insert some spaces in between the "++" and type my variable name.
This seems like a lot of work. I have found Practically Macro, but I thought there might be something built in for this situation which is very common for me.

I'm not sure if this shortcut exists, try doing CTRL+SHIFT+L when the string is selected.
Also there is this link which has a whole bunch of nice keyboard shortcuts, I know it may not answer the question completely but maybe it'll get you one step closer. :)
**** Edit: In that link the "Ctrl-2 something" post looks promising.****

The most relevant built-in feature is maybe the "Pick out part of a string" quick assist:
insert a space (or anything else) where you want to add a variable,
select it,
press Ctrl+1,
select the "Pick out part of a string" quick assist,
insert your variable name.
Reference: "Pick out part of a string" section in this post.

Related

How to set color of Java comment in latex/Lyx?

I need to insert Java/Android code into Latex/Lyx, and I would like to highlight Java comments with a certain colour. I have done it earlier in the document, but now when I try the same code, it is not working for some reason. One difference between the codes are, that the one I'm now trying to insert is Android Java-code, and the former was pure Java, could that have an affect? The settings I'm using in the both cases are these:
\def\listingsfont{\ttfamily}
\lstset{language=Java}
\lstset{basicstyle=\ttfamily\scriptsize}
\lstset{keywordstyle=\color{blue}\ttfamily}
\lstset{stringstyle=\color{red}\ttfamily}
\lstset{commentstyle=\color{green}\ttfamily}
\lstset{breaklines=true}
\lstset{tabsize=2}
\begin{lstlisting}[frame=single] % Start your code-block
So first time working, second time not any more. What happens is, that all comment marks (//) appear as blue, but only the marks, not the whole comments with them. Strings are highlighted correctly.
EDIT: I have gained some new information what is happening. After the first time, when this code was working, I have a latex code where language is set as R. If I insert this code before the code that is written in R, all is working as it should. But if I insert the code after the code that is written in R, then it is no longer working. So something weird happens there.
EDIT2: For testing purposes, I changed the settings for the R-code as (language=Java). What happened was, that after that, all the Java code in the latex appeared correctly. So apparently, setting language as R breaks Lyx/Latex somehow to not being able to highlight other languages.
If you use \lstset, this is the default for the whole document. Try setting the language of each listing in LyX separate instead (through the settings you reach in the context menu of the program listing)

Alter order of content assist eclipse for android development

I am working on an Android app, using Eclipse 4.2. I have content assist set up almost as I want it. However, when I, for example, start typing int to create a Java int variable, the Android.R.Integer entry in content assist is the primary entry.
I have content assist set up by relevance. This is just an annoyance because every time I want to make an int variable, I have to either hit Esc or hit enter and delete "eger" from the end of the type.
Thanks in advance.
None of the solutions posted here really worked, so what I did was to go to
Java -> Appearance -> Type Filters
and add *int* (yes, it's an asterisk followed by int and then another asterisk).
There's a checkbox next to the filter you just added, make sure it is checked.
Problem solved (finally!)
You can use Preferences->Java->Appearance->Type Filters to filter out the suggestions you want to ignore.
I'm wondering why you really need content assist to create an int variable, it would be easier to simply type it. But if you really want to, you can type the integer value and then use the Ctrl-2-L shortcut to create the local variable.
In
Java->Appearance>Type Filters
click add
type 'int*' -> click ok.
Then when you type in int , the first is int instead of integer .
Cren I am in the same boat. I have the content assist set to auto activate with every letter because I find it extremely useful that way, but have the same int(eger) problem that you do.
I have come up with a few ways to deal with it and thought I would share them in case you find one of them useful.
You could use a Type Filter as ralph suggested works just fine, you just don't get the assist when you actually want Integer, which isn't that big of a deal.
You could delay the content assist long enough so that you can type "int" and enter before it pops up, giving you a delay always. (In Preferences->Java->Editor->Content Assist there is an Auto activation delay setting)
Or remove 'i', 'n', and 't' from the Auto activation triggers (again in Preferences->Java->Editor->Content Assist). This is the method I am currently trying out, and it seems to work with everything, even Integer, saving me from having to type "ger" :).

IDEA: "Assign statement to new local variable"?

As a long time Eclipse user, I'm playing around a little bit with IntelliJ IDEA 10. I can't seem to find out how to perform an "Assign statement to new local variable" code completion.
Feature explanation:
I type something like
new BufferedOutputStream(out)
and then hit Cmd (or Ctrl)+1 and enter, and Eclipse changes the line into:
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(out);
At the same time, I can type over "bufferedOutputStream" immediately to rename it (or select from the options "bufferedOutputStream", "outputStream" and "stream" from a dropdown).
I use this feature for absolutely every assignment, and it's an enormous time saver - this must be available in IDEA, too - but where is it hidden?
Refactor | Introduce Variable (Ctrl+Alt+V on Windows). Note that you don't need to select the text if it's the only text in the current line. Then you can change the variable name in-line just like you've described and press Enter to complete editing.
Another way is to use the Postfix Completion:
Type .var (or just .v to select it from the list) and confirm it with Enter.
As CrazyCoder mentions you can use Ctrl+Alt+V. Also instead of selecting the expression, clicking into somewhere in your expression and using Ctrl+W to expand scope is very useful while using introduce refactorings. Extract refactorings are:
Extract variable: Ctrl+Alt+V
Extract field: Ctrl+Alt+F
Extract method: Ctrl+Alt+M
Extract parameter: Ctrl+Alt+P
Extract constant: Ctrl+Alt+C
Also, Idea is a polygot editor so you can use these extract refactorings for other file types like js or html also (not all refactorings work in all file types but Ctrl+W works mostly).
There are more extract refactorings which do not have shortcuts which you can access from Refactor|Extract menu (both menu bar and context menu). To quick access all refactorings you can use Ctrl+Alt+Shift+T for a popup menu.
As a last word, I highly encourage you using "Tip of the Day" (Help|Tip of the Day). It is a fast way to learn many helpful features of Idea.
It's not as nice as Eclipse, but you can try the following:
new BufferedOutputStream(out)
Select the expression above, either with your mouse (or by using Ctrl+W).
Then hit Ctrl+Alt+V to Introduce a Variable or (Ctrl+Alt+F to Introduce a Field)
Easiest is, hit Alt+Enter, you will be offered with a list of options, and just select "Introduce local Variable".

Search for commented-out code across files in Eclipse

Is there a quick way to find all the commented-out code across Java files in Eclipse?
Any option in Search, perhaps, or any add-on that can do this?
It should be able to find only code which is commented out, but not ordinary comments.
In Eclipse, I just do a file search with the regular expression checkbox turned on:
(/\*.*;.*\*/)|(//.*;)
It will find semicolons in
// These;
and /* these; */
Works for me.
Sonar can do it: http://www.sonarsource.org/commented-out-code-eradication-with-sonar/
You can mark your own commented code with a task tag. You can create your own task tags in Eclipse.
From the menu, go to Window -> Preferences. In the Preferences dialog, go to General -> Editors -> Structured Text Editors -> Task Tags.
Add an appropriate task tag, like COMMENTED. Set the priority to Low.
Then, any code you comment out, you can mark with the COMMENTED task tag. A list of these task tags, along with their locations, appears in the Tasks view.
#Jorn said:
I think [the OP] wants to find code that is commented out, not code that has a comment.
If the intention is to find commented out code, then I don't think it is possible in general. The problem is that it is impossible to distinguish between comments that were written as code or pseudo-code, and code that is commented out. Making that distinction requires human intelligence.
Now IDE's typically have a "toggle comments" function that comments out code in a particular way. It would be feasible to write a tool / plugin that matches the style produced by a
particular IDE. But that's probably not good enough, especially since reformatting the code typically gets rid of the characteristics that made the commented out code recognizable.
If the problem is to find commented-out code, what is needed is a way to find comments, and way to decide if a comment might contain code.
A simple way to do this is to search for comment that contain code-like things. I'd be tempted to hunt for comments containing a ";" character (or some other rare indicator such as "="); it will be pretty hard to have any interesting commented code that doesn't contain this and in my experience with comments, I don't see many that people write that contain this. A regexp search for this should be pretty straightforward, even if it picked up a few addtional false positives (e.g. // in a string literal).
A more sophisticated way to accomplish this is to use a Java lexer or parser. If you have a lexer that returns comments at tokens (not all of them do, Java compilers aren't interested in comments), then you can simply scan the lexemes for a comment and do the semicolon check I described above. You won't get any false positives hits for comment like things in string literals with this approach.
If you have a re-engineering parser that captures comments as part of the AST ( such as our SD Java Front End),
you can mechanically scan the parse tree for comments, feed the comment context back to the parser
to see if the content is code like, and report any that passes that test modulo some size-depedent error rate
(10 errors in 15 characters implies "really is a comment"). Now the "code-like" test requires
the reengineering parser be willing to recognize any substring of the (Java) language.
Our DMS Software Reengineering Toolkit underlying the Java Front End can actually do that, using access to the grammar buried in the front end, as it is willing to start a parse for any language (non)terminal,
and this question is "can you find a sequuence of (non)terminals that consumes the string?".
The lexer and parser approaches are small and big sledgehammers respectively. If OP is going to do this just once, he can stick to the manual regex search. If the problem is to vet the code base repeatedly (needed in big organizations), he'd want a tool that can be run on regular basis.
You can do a search in Eclipse.
All you need to search for is /* and //
However, you will only find the files which contain that expression, and not the actual content which I believe you are after.
However, if you are using Linux you can easily get all the comments with a one liner.

Eclipse IDE search a function with given name

How to search a partifular function in a particular project in Eclipse
e.g. if I want to search a function with name xyz how do I do it?
Use "Java Search" in the search menu or move the caret on the function name in the source and press Ctrl+Shift+G (-> search for reference in Workspace).
[EDIT] If you don't know in which class the function is, then use the text search (which is called "File search"). Enter the name in the search field and then limit the search to "*.java" files.
If you don't know where the method is located, use text search with a root of your whole project, working set or even the whole workspace if you've really got no idea where it might be; Search → File… brings up a dialog which lets you enter exactly what you are looking for. You'll have to sort through all the things it finds (using the Search window) to identify the real match from among the false positives, but that's better than nothing.
You can write some code that uses that function and then use the [Ctrl] + [Click] trick to go to its implementations.
Check out nWire for Java. It has a quick search feature: start typing and it will instantly show results which include types, interfaces, methods, fields and other artifacts. It has a lot more features.
In Eclipse Neon 4.6 there's CTRL+H (from the menu Search -> Search) then we can use either the "File search" or "Java Search" tabs. both are very useful

Categories