Does Eclipse have a shortcut to split a line and introduce a variable? - eclipse

During typing in code, I often have code lines that become long:
String result = new MyObject(foo, bar).getBaz().getFoo(new Config(new File(f))).get(key).get(0);
I frequently break up such a line manually, making two of them:
Foo foo = new MyObject(foo, bar).getBaz().getFoo(new Config(new File(f)));
String result = foo.get(key).get(0);
This means positioning the cursor after new File(f))), pressing Enter, then select the String result = part, cut and paste it from the first to the second line, create a new variable named like the getter, type it in at the beginning of the second line and at the beginning of the first line, the latter as an assignment, with declaration. A lot of manual typework if you do it often enaugh.
I wonder again and again if there is a help, like a keyboard short cut, which does this for me: I position the cursor after new File(f))), press Ctrl+something and the result is the two lines. A second one I wonder if it exists is if I would position the cursor on Config at the first line, Ctrl+something and it would form these lines:
Config config = new Config(new File(f));
String result = new MyObject(foo, bar).getBaz().getFoo(config).get(key).get(0);

I finally found it. Long version:
Position the cursor as described in the question, press Shift+Alt+L. A dialog will appear to configure the variable name, whether all occurrences shall be replaced or the variable should be declared final. Press Enter as you aggree to the settings.
Short version:
Position the cursor as described in the question, press Ctrl+1 and the quick fix menu will appear (even if there is nothing marked red or yellow). Select Extract to local variable (or one of the related options, as you need) and press Enter.
For the second example to work, the cursor must be positioned either after the expression you want to cut out
….getBaz().getFoo(new Config(new File(f))<CURSOR HERE>).get(key).get(0);
or on the new keyword.

Related

Get current Highlight in VS Code Extension

I am not talking about the current selection, which can be accessed by vscode.window.activeTextEditor.selection.
When the cursor is inside an identifier, variable name, etc, it becomes highlighted, as shown in this screenshot:
What is this highlight object called? How do I access it?
Searching for everything from "highlight" to "identifier" and whatever else, the answer was more obvious. TextDocument has a getWordRangeAtPosition method, which takes a position and returns the range of the word.
const editor = vscode.window.activeTextEditor;
let cursorPosition = editor.selection.start;
let wordRange = editor.document.getWordRangeAtPosition(cursorPosition);
let highlight = editor.document.getText(wordRange);
// highlight will now contain the currently highlighted word
The thing you are showing is called a Document Highlight. Other instances (of in this case colorData) will also be highlighted. The answer by #Rene Roth is probably answering what you want. And as #Gama11 wrote, using getWordRangeAtPosition without a second parameter uses the "word pattern" of a language. However a document highlight doesn't have to be a single word.
As far as I know you can't get a list of all the highlights? (I've only just figured them out for use in my first extension.)
To highlight like that you need to use registerDocumentHighlightProvider and provideDocumentHighlights. The DocumentHighlightKind can be used to provide different highlight colors (say when an instance of a highlighted variable is on the right or left of an equal sign, i.e. read or written, or for some other reason for distinction). I've used DocumentHighlightKind successfully to show a highlight that's contained within another.

JTextArea appending and deleting problems

I have a jTextArea that displays the clicked item from a jTable. I have a running code already and I am able to display the strings into my jTextArea. However, I have an issue whenever I try to remove a string.
So far, below is my code for getting the string value from the clicked item in jTable:
c = jTable2.getModel().getValueAt(jTable2.convertRowIndexToModel(selectedRow), 1).toString(); // this will get the name of product from a table and store it into C variable
The string from above code will be displayed on my jTextArea, as shown below:
jTextArea1.append(c + "\n");
Now, whenever I click an item to my jTable this will be stored into c variable, and the new item will be appended on my jTextArea with next line.
My sample output in jTextArea of this will be:
Apple
Mango
Now, I want to delete a specific string from that jTextArea, which I am able to do so in my current code. I am using this line of code to do that:
jTextArea1.setText(jTextArea1.getText().replaceAll(c, ""));
But then, whenever I clicked my jTable once again to append a new item, it will be appended next to the empty string, sample output is like this:
// from here is the beginning of the jTextArea
Mango
Apple
Now, I got empty strings before the new text is displayed.
In conclusion, I have understood that I never deleted an item from my jTextArea, but what it did was only to replace the string into an empty one. My problem is that I want to be able to append a string, then delete it whenever I need to do so without affecting the other appended strings, and without having an empty string.
Is there any other way to achieve this in replacement of my "replaceAll" line of code?
What you are asking for is analogous to editing a line of text to delete something in the middle. Naturally, you usually do not want a gap. One way to do this would be to copy the line over character by character but with a rule that if a space was preceded by a space it will not be copied (or, if a space comes up and is copied, then only characters will be copied that are not a space in that instance).
You could copy a series of records by reading each string, checking whether it had only spaces in it, and adding that string to the second screen only if it was other than white space.

Vim duplicate line multiple times with 2 keypresses

I use this key mapping to duplicate a line, and go to the same cursor position on this newly created line:
nnoremap , mqYp`qj
What this does:
Create mark 'q'
Yank line
Paste/put line (below current line, cursor is now at start of new line)
Go back to mark at previous line
Move one line down. (cursor is now on new line at the same place of start of previous line)
This works perfectly fine, however I see a flaw when putting a number in front of the command. Imagine I want to duplicate this line 10 times. It would try to create 10 marks basically. I could do Y10p for this, I do understand that. My problem with that approach is that I'm not on the same cursor position as I was in the first line, the one I'm duplicating.
So I'm looking for a way to do basically 10,, using my previously made mapping, and ending on the last line, at the same cursor position of where I was in the first line. Note that I am using IdeaVIM exclusively to code, which means I can't make any functions for this.
Is it possible to get this 10, working in this situation?
Edit #1:
Example text
# Start
# Initialize new variables
new_invoice_name_one = 'New Name One'
new_invoice_name_two = 'New Name Two'
new_invoice_address_one = 'New Address One'
Command executed: 3, with the cursor being on the first I of line 2
Desired output
# Start
# Initialize new variables
# Initialize new variables
# Initialize new variables
# Initialize new variables
new_invoice_name_one = 'New Name One'
new_invoice_name_two = 'New Name Two'
new_invoice_address_one = 'New Address One'
with the cursor being on the first I of line 5 `
Edit #2:
I see some potential at the LetHandler.java here, however I can not seem to figure out how to use it to match the use case. On the other hand, here it says it is not supported at all.
When I play vimgolf, I use a trick [count]#='... sometimes. It could be used for your requirement.
you can map:
nnoremap , #='mqYp`q'<cr>
Then you can just simple press 200, to achieve what you want.
You can achieve this by pasting to the above of current line. Do this nnoremap , YmqP`q
update
You cannot do that since vim's key mapping is just string concatenation. One way to achieve this is to predefine a macro. Put this in the related rc file,
let #q="mqYP`q"
nnoremap , #q

Can I make a macro in n++ that does a search/replace?

I'm new to n++, but I have been most impressed with this tool so far. I've been trying to record a macro that do a search/replace, but the 'search' part seems to have the initial search text from the recording 'hard-coded' in the macro.
What I want is:
Manually locate the cursor at the beginning of the first line of a fixed format code segment, then Macro actions:
move cursor two lines down
move cursor right x characters
mark charters from pos x to x+n
search and replace all occurrences of the selected text with "{p_'selected text'}"
In an more advanced version, I'd like to add some logic to step 4: only execute the replace part if the # of occurrences are > 1 (e.g. by first adding a count statement, but I'm not sure how to obtain the returned count # from the dialog box)
Is this possible?
While I'm a big fan of Notepad++, this sounds like something I would accomplish with AutoHotKey. You would select the text and copy it to the clipboard. AutoHotKey would read the clipboard, replace the text as you desire, and either replace the clipboard contents, or send it back to your document. Let me know if you would like to go that route.

How to search and replace 2 lines (together) in Eclipse?

I would like to search multiple files via eclipse for the following 2 lines:
#Length(max = L_255)
private String description;
and replace them with these two:
#Length(max = L_255, message="{validator.description.len}")
private String description;
Another tip on how to get the regex for a selected block.
Open one of the files that contains the multiple lines (multiline) to search or replace.
Click Ctrl+F and select "Regular expression". Close the Find/Replace window.
Select the block you need and click again Ctrl+F to open the Find/Replace window.
Now in the Find text box you have the regular expression that exactly matches your selection block.
(I discovered this, only after creating manually a regexp for very long block :)
Search are multi-line by default in Eclipse when you are using regex:
(\#Length\(max = L_255)\)([\r\n\s]+private)
I would like to add "private String description;"
(\#Length\(max = L_255)\)([\r\n\s]+private\s+?String\s+description\s*?;)
replaced by:
\1, message="{validator.description.len}")\2
It works perfectly in a File Search triggered by a CTRL-H.
As mentioned in Tika's answer, you can directly copy the two lines selected in the "Containing Text" field: those lines will be converted as a regexp for you by Eclipse.
CTRL+H does take two lines if you use regexp (and you don't have to write the regexp by yourself, eclipse does that for you).
Select your lines.
Click CTRL+H. The search dialog opens up.
If "Regular expression" is already checked, eclipse will have converted the two lines you search for into regexp for you, click Search.
If "Regular expression" if not already checked", check it and click Cancel (eclipse remembers your choice).
Select your lines again.
Click CTRL+H. The search dialog opens up. This time "Regular expression" is already selected. eclipse will have converted the two lines you search for into regexp for you, click Search.
A quick tip for including multiple lines as part of a manually constructed regular expression:
Where you would normally use .* to match any character zero or more times, instead consider using something like (?:.|\r?\n)*. Or put an extra ? at the end to make it non-greedy.
Explanation: . doesn't match new lines so need to do an "either-or": The parentheses match either the . before the pipe or the new line after it. The ? after \r makes the carriage return before the line feed optional to allow Windows or Unix new lines. The ?: excludes the whole thing as a capturing group (which helps to avoid a stack overflow).
Click Ctrl + F and select "Regular Expression" and then search the lines. In case to perform the same on multiple files, click Ctrl + H, click on 'File Search' and perform the same.
Select the folder that contains all your files and press Ctrl+H.