intelligent vscode "Go to Definition" - visual-studio-code

--- edit
Let's say that I have the following scenario in vscode:
I have 3 open files, with vscode showing two files at once.
on the left I have 2 tabs: a.js, in foreground, with
import { b } from './b';
import { c } from './c';
b();
c();
and b.js, with
export function b() {
// ...
}
and on the right I have c.js, with
export function c() {
// ...
}
My goal is:
starting from a.js as current focused editor
to have a shortcut - ideally ctrl-click - that
when I click on b() takes the tab with b.js in foreground and focuses on the function b() { definition
when I click on c(), move the focus on the right side, with c.js in foreground, and focuses on the function c() { definition.
keep in mind that I DO NOT want to use different shortcuts. This is a simplified example (normally I have multiple tabs AND multiple columns opened) but normally I don't know where the target is, and definitely I don't want the same file opened multiple times.
Is there a way to get it?
--- original
In vscode, according to https://code.visualstudio.com/docs/editor/editingevolved#_go-to-definition
You can jump to the definition with Ctrl+Click or open the definition to the side with Ctrl+Alt+Click.
Now, I use the side panel a lot, and sometimes the "target" where the definition is, is in another side, and sometimes is a different tab in the same side on where I am now.
If I do the appropriate choice I can go directly to the already opened buffer. If I choose the "wrong" shortcut a new buffer of the same file is opened.
Is there a way to make the "go to definition" smart? I just want to always go to the already opened file (if is available).

You should head to VS Code's settings and select the Reveal If Open option.
You'll be able to find it in the Workbench --> Editor Management section.

Related

Search/get original function/method location in visual studio code

Assuming I have this code
<?php
namespace App\Traits;
use App\Something\Class1;
use App\Something\Class2;
use App\Something\Class3;
trait Helpers {
public function someThingCool( $someVar) {
return $this->checkCoolness( $someVar );
}
}
is there some way I can easily determine and locate where that checkCoolness method originally define without going through each of inherited class?
Use Go to Definition in the context menu
Right click on a desired function and click on go to definition
Setting shortcut.
Press ctrl+shift+p and type in keyboard-shortcuts
Type in go to definition in the search bar
Set desired keybinding to Go to Definition. Say ctrl + B
Now use your keybinding (shortcut) to access the definition of checkCoolness click on checkCoolness and press ctrl + B

Is there a way to avoid holding the ALT key when cycling through input fields within a SAPUI5 table?

I have a SAPUI5 table which contains input fields. I have prepared an example at http://jsfiddle.net/bgerth/x8h92mz8/.
When you press ALT+TAB you can cycle through the input fields within the table (I only found that out by looking at sap.m.ListBase.prototype._startItemNavigation).
Only pressing TAB focuses the first element outside the table.
I consider that rather non-intuitive. Is there a way to make TAB alone work the same way?
Update:
Alt+Tab works in Chrome and Safari, but not Firefox (45.0.2) on my Mac. It doesn't work at all on Windows, as that combination is reserved to toggle through open application windows.
There are two solutions I discovered so far to address this problem
Proposal 1: Adapt tab key handling
I have found the blog SAPUI5 Table Navigation with Tab Key by Klaus Kronawetter which adds extra keyboard handling to a sap.ui.table.Table. I adapted his code for a sap.m.Table which you can find at http://jsfiddle.net/bgerth/os6r096y.
Unfortunately, the code is rather lenghty.
Proposal 2: Enable up/down arrow keys
After further investigation, I decided that the solution from proposal 1 above is too much hassle.
Instead, I adapted the class sap.ui.core.delegate.ItemNavigationmentioned above, which is internally employed by sap.m.ListBase. In essence, you can cycle through the input fields with up and down arrow keys.
I have prepared an example at http://jsfiddle.net/bgerth/0r9L30wd. The relevant code is
var fnPatchedItemNavigationsetItemDomRefs = sap.ui.core.delegate.ItemNavigation.prototype.setItemDomRefs;
sap.ui.core.delegate.ItemNavigation.prototype.setItemDomRefs = function setItemDomRefsPatched(aItemDomRefs) {
// 'this' is now the instance of sap.ui.core.delegate.ItemNavigation
jQuery.sap.log.debug("Patched version of sap.ui.core.delegate.ItemNavigation.setItemDomRefs");
var aInputFields = $(aItemDomRefs).find("input:enabled").get();
if (aInputFields[0]) {
// There is at least one enabled input field in this table
return fnPatchedItemNavigationsetItemDomRefs.call(this, aInputFields);
} else {
return fnPatchedItemNavigationsetItemDomRefs.call(this, aItemDomRefs);
}
}

Surround selection with method call

Often when I'm writing code I forget to surround a section of code with a method. For example, when printing an array, I realize that I forgot to pass the array into Arrays.toString().
String[] foo(){
return new String[3];
}
main() {
System.out.println(foo());
}
Is there a way in Eclipse that I can select foo() and then use auto complete or something to surround it with Arrays.toString()? So I want to end up with this:
main() {
System.out.println(Arrays.toString(foo()));
}
I know I could use templates, but I would have to make a template for each method I want to use. I'm looking for something like Eclipse's auto complete feature, which knows about every class and method in the build path.
Yes, you could use templates for that:
First, experiment with existing templates:
Go to the source editor and select "foo()".
Open the view General > Templates.
Select some template, for example, Java > toArray and see how it works.
Then, add your own template:
Windows > Preferences > Java > Editor > Templates > New.
I think the right context should be "Java".
Another way of accesing templates is through the content assist: In the source code, in a new line, start typing the first letters of your template, then press [CTRL][SPACE]. A selector will appear with the matching templates. You may find it useful to check the checkbox "Automatically inserted" in the template definition window.
And yet another way to access them is to select a line of code and then Context Menu > Surround With.
A quick way:
Double click or use select enclosing element and its cousins to select the expression you wish to wrap. ctrl-x to temporarily cut it. Type a few characters and ctrl-space to insert your method name and parentheses. Finally, ctrl-v to paste what you just cut.
with templates - under Java Statements: ${method}(${word_selection})${cursor}
You can make a template like the one described by #LittleSanti. If you use a fake template variable for the method name (like ${method} or ${name}) instead of a constant like foo, Eclipse will highlight it and let you paste or type or complete over it. Then when you hit return or tab, it will jump the cursor to the end (the position indicated by ${cursor}
Unfortunately I don't think Eclipse provides a "real" template variable for selecting methods in scope. It would be nice if it would let did completion for you on methods.

e4 dynamic menu contributions

This feature has been realized as of Kepler M4, for a detailed information on the usage see my blog
I want to realize a fully dynamic menu contribution to the menu of a handler located in a view toolbar. In Eclipse 3 it was possible to add "dynamic" as org.eclipse.ui.menus contribution to a Menu!
I already found out about www.vogella.com/blog/2010/10/26/processors-e4-model explaining on how to dynamically contribute to menus by means of processor model extensions but I am talking about a completely dynamic menu implementation which changes on every call of the resp. submenu. As mentioned this was no problem to realize in Eclipse 3.x via the dynamic menu contribution and the set of isDynamic() to true.
I already tried several approaches:
Registering a processor hooking to a menu => no dynamic add possible (new elements are simply not shown, also discussed in Eclipse Forum - Cannot replace menu items at runtime)
Listening to the UIEventTopic for the event of creating the menu, getting the widget for Modification => modifications to the swt.Menu gathered are simply ignored (every listener, element etc.) (for info RCP Event Model
Open, untried solutions
Inserting a ToolControl to try an SWT approach -> quite complicated but may work
I've been banging my head for some time now, but can't seem to understand the correct implementation of this problem within E4.
-- This question was also asked in Eclipse Forum - Dynamic menu contributions
----- UPDATE
I tried a different approach up to now:
I added a HandledToolItem to the Menu (please see the following image)
and with the following code I am trying to interfere with the menus way to build, where the code is called by the resp. command handleer as referenced in the image.
#CanExecute
public boolean canExecute(#Optional MApplication application) {
System.out.println("CanExecute Counter="+counter);
// --- 1 ---
// Find the required MMenu Entry in the Application Model
if(application == null) return true;
EModelService modelService = (EModelService) application.getContext().get(EModelService.class.getName());
MPart part = (MPart) modelService.find("at.medevit.emr.contacts.ui.contactselector", application);
List<MToolBarElement> lmte = part.getToolbar().getChildren();
HandledToolItemImpl htil = null;
for (MToolBarElement mToolBarElement : lmte) {
if(mToolBarElement.getElementId().equals("at.medevit.emr.contacts.ui.contactselector.toolbar.handledtoolitem.filter")) htil = (HandledToolItemImpl) mToolBarElement;
}
if(htil != null) {
MMenu elemMenu = htil.getMenu();
// --- 2 ---
// Found it hopefully, let's start the real work, simply add a new item
MDirectMenuItem mdi = MMenuFactory.INSTANCE.createDirectMenuItem();
mdi.setLabel("Counter "+counter);
counter++;
// --- 3 ---
elemMenu.getChildren().add(mdi); // ConcurrentModificationException
}
As one can see, this code is queried once the menu is created, to determine whether the command is executable or not. All the code from 1 - 2 is to find the correct MMenu element to work on. The code from 2 - 3 creates a MenuItem and increments a counter in the field.
BUT at 3 I face a java.util.ConcurrentModificationException the first time the menu is opened! I assume that at this very point the menu is iterating over elemMenu.getChildren() and I am not allowed to enable!
So whats all the fuzz about the entire e4 model being changeable all the time ;) (just kiddin' I know this is a baaaad hack!!!)
Thing is: I really think that the possibility to add fully dynamic menu parts is one of the best usability tools, and if it is not possible to realize it in E4 as it was in E3 this is a very serious degradation of possibilities!!!
-- UPDATE
An Eclipse Bug has been filed for this https://bugs.eclipse.org/bugs/show_bug.cgi?id=389063
Proper dynamic model updates should be handled in the bug you've opened. As a workaround in Eclipse4 in Juno, a MRenderedMenuItem can be created in Eclipse4 to provide the equivalent functionality to the dynamic element (although if you are using 4.2, you would just use org.eclipse.ui.menus).
ex:
ContextFunction generator = new ContextFunction() {
#Override
public Object compute(IEclipseContext context) {
return new MyCompoundContributionItem(context);
}
};
MRenderedMenuItem menuItem = MenuFactoryImpl.eINSTANCE.createRenderedMenuItem();
menuItem.setElementId(id);
menuItem.setContributionItem(generator);
container.getChildren().add(menuItem);
This effectively provides a CompoundContributionItem directly to the Eclipse4 menu renderer.

How to minimize code area in Eclipse?

There is a nice feature in Visual Studio: you can create special code areas which can be minimized just as class methods in Eclipse are minimized. Like:
#region
//some code
#endregion
Is there a way do make such pleasant feature in Eclipse?
It's called "collapse all", click on the editor view that you want to collapse all your methods in go to
help>
key assist...>
double click "collapse all">, everything is collapsed
Click on ' - (minus)' symbol on the side of the editor, Right click on minus symbol , Go to folding / Collapse All
Keyboard shortcut : Ctrl+Shift+NumbPad_Divide
First check Folding is enable or disable if enabled then you can minimize or collapse code and expand code. You can check Folding through
Folding is configured under Window -> Preferences -> Java -> Editor ->Folding
Please check Enable Folding if its unchecked
Now you can minimize your code
Collapse All (all functions on page) : Ctrl+Shift+NumbPad_Divide
Expand All (all function on page) : Ctrl+Shift+NumbPad_Mulitply
Collapse One function : Ctrl+NumbPad_Minus
Expand One Function : Ctrl+NumbPad_Plus
If you mean by minimizing folding, then I don't think Eclipse has the folding option you want. When I look at the eclipse folding options it tells me that it can fold
Comments
Header Comments
Inner Types
Members
Imports
If you don't mean folding, then I'm sorry for the confusion.
There are some plugins for this, like "Coffee Bytes Java Folding" for Eclipse.
Other IDE like Netbeans as an native equivalent.
It's really IDE dependent in Java, not implemented in the language like in C# for example.
Java Equivalent to #region in c#
Check the second answer
//region MY REGION
code here
//endregion
Heres how I do it:
Create an empty class called Blank
Use this outline for your code:
Blank b = new Blank()
{
public void myCode()
{
/Insert code here/
}
};
b.myCode();