如何在 Eclipse 的任何地方获取“for 循环”的内容帮助?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11845995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 18:46:35  来源:igfitidea点击:

How to get content assist for "for loop" anywhere in Eclipse?

eclipse

提问by James Fu

In Eclipse, after a line like this:

在 Eclipse 中,在这样的一行之后:

List list = new ArrayList();

列表列表 = 新的 ArrayList();

Typing "for" just beneath, and followed by "ctrl-space" (by default), will bring several options that can help completing this "for loop": assist

在下方键入“for”,然后键入“ctrl-space”(默认情况下),将带来几个有助于完成“for 循环”的选项: 助攻

But if the variable "list" is declared far from here (e.g. as a class field) which may not be directly inferred from this context, or there are many Lists declared,then the assistance doesn't work well:

但是,如果变量“list”声明的离这里很远(例如作为类字段),这可能不能从这个上下文中直接推断出来,或者声明了许多列表,那么辅助就不能很好地工作:

not work@@ split line ---

不行@@分割线---

enter image description here

在此处输入图片说明

In some cases, Eclipse can assist, but just don't work for member variable. E.g. manually type "another" and ENTER after the ":" didn't persuade Eclipse to guess about it....

在某些情况下,Eclipse 可以提供帮助,但不适用于成员变量。例如,在“:”之后手动输入“another”并回车并没有说服 Eclipse 猜测它....

(P.S. workable case:

(PS可行案例:

Auto guessed

自动猜测

auto guessed

自动猜测

Entered wanted name, and ENTER, works great

输入想要的名字,然后回车,效果很好

entered wanted name, and ENTER, works great)

输入想要的名字,然后回车,效果很好)

Does anyone have any tip to make this assistance work under such scenarios?

有没有人有任何提示可以使这种帮助在这种情况下发挥作用?

采纳答案by Gediminas Aleknavi?ius

What I usually do to solve the content assist with for loop is the following:

我通常用 for 循环解决内容辅助的方法如下:

  • create a local variable by typing collection variable that is declared far above and a semicolon:

    list;
    
  • press Ctrl+2 L

  • Eclipse generates a new local variable declaration which looks like this:

    List list2 = list;
    
  • type my foreach and autocomplete with Ctrl+space getting the following:

    List list2 = list;
    for (Object object : list2) {
    }
    
  • place cursor on list2 in the for loop declaration and press Alt+Shift+I which stands for inline variable.

  • this results in what you want to achieve. The effort is minimal after some practicing:

    for (Object object : list) {
    }
    
  • 通过键入远在上面声明的集合变量和分号来创建局部变量:

    list;
    
  • 按 Ctrl+2 L

  • Eclipse 生成一个新的局部变量声明,如下所示:

    List list2 = list;
    
  • 键入我的 foreach 并使用 Ctrl+space 自动完成,得到以下内容:

    List list2 = list;
    for (Object object : list2) {
    }
    
  • 将光标放在 for 循环声明中的 list2 上,然后按 Alt+Shift+I(代表内联变量)。

  • 这会导致您想要实现的目标。经过一些练习后,努力是最小的:

    for (Object object : list) {
    }
    

回答by summerian

I followed Ashutosh Jindal's tip and I managed to configure the template that works (tested with Kepler release). Here it is:

我遵循了 Ashutosh Jindal 的提示,并设法配置了有效的模板(使用 Kepler 版本进行了测试)。这里是:

for (${iterable_type:elemType(iterable)} ${iterable_element:newName(iterable_type)} :  ${iterable:var(java.lang.Iterable)}) {
    ${cursor}
}

The main point was to change localVarto varin the template definition (the Eclipse docsclearly explain this).

要点是在模板定义中将localVar更改为varEclipse 文档清楚地解释了这一点)。

How to use it:

如何使用它:

  1. Print the template name (foreachfor default template) and hit Enter. The template will be used with default collection selected by Eclipse (the latest collection declared)
  2. Hit TAB twice to jump to collection element. You will get drop down list with all iterable collections that apply.
  3. Use UP/DOWN arrows to select desired collection, hit Enter. Eclipse will adjust element type and name (very cool).
  1. 打印模板名称(默认模板为foreach)并按 Enter。该模板将与 Eclipse 选择的默认集合(声明的最新集合)一起使用
  2. 按 T​​AB 两次以跳转到集合元素。您将获得包含所有适用的可迭代集合的下拉列表。
  3. 使用向上/向下箭头选择所需的集合,按 Enter。Eclipse 会调整元素类型和名称(非常酷)。

Click for the screenshot

点击查看截图

This works almost as good as Intellij templates. The drawbacks are:

这几乎和 Intellij 模板一样好。缺点是:

  • template does not include arrays (as opposed to default foreachtemplate). For arrays you have to define another template.
  • 模板不包括数组(与默认的foreach模板相反)。对于数组,您必须定义另一个模板。

回答by Ashutosh Jindal

I have not tried this myself, but take a look at the code template definition. For example, the foreachcode template is defined in Preferences -> Java -> Editor -> Templates as follows :

我自己没有尝试过,但请查看代码模板定义。例如,foreach代码模板在 Preferences -> Java -> Editor -> Templates 中定义如下:

Definition of foreach

foreach的定义

The definition is as follows :

定义如下:

for (${iterable_type} ${iterable_element} : ${iterable}) {
    ${cursor}
}

Notice the variables being used such as iterable_type.

请注意正在使用的变量,例如iterable_type.

Now take a look at thisEclipse help page.

现在看看这个Eclipse 帮助页面。

There is a variable there called ${id:localVar(type[,type]*)}which is described as follows :

那里有一个变量,${id:localVar(type[,type]*)}其描述如下:

Evaluates to a local variable or parameter visible in the current scope that is a subtype of any of the given type. If no type is specified, any non-primitive local variable matches.
${array} is a shortcut for ${array:localVar(java.lang.Object[])}, but also matches arrays of primitive types.
${collection} is a shortcut for ${collection:localVar(java.util.Collection)}.
${iterable} is a shortcut for ${iterable:localVar(java.lang.Iterable)}, but also matches arrays. 

A screenshot of the same :

相同的屏幕截图:

Variable

多变的

I believe that if you wanted to increase the scope from which the foreachtemplate infers it's variables, you may have to edit the template definition with the appropriate variable.

我相信,如果您想增加foreach模板推断其变量的范围,您可能必须使用适当的变量编辑模板定义。

Let me know if this helps. Unfortunately, I have not delved into editing the code templates before so shall not be able to give a concrete example.

如果这有帮助,请告诉我。遗憾的是,我之前没有深入研究代码模板的编辑,因此无法给出具体示例。