eclipse-jdt / eclipse-jdt/eclipse.jdt.core

Suggest casted receiver completions in `while` and `for`

Open
#3,737 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 10h
Merged PRs (30d)
49

Description

If you complete:

```java
if (node instanceof MethodDeclaration) {
node.getB|
}
```

one completion available is:

```java
if (node instanceof MethodDeclaration) {
((MethodDeclaration)node).getBody()|
}
```

However, the same doesn't work for `while` and `for`. `while` is probably more interesting to be honest:

```java
int count = 0; // 🧛
while (node instanceof Block) {
// add the number of statements in the block
count += node.stateme|
node = node.getParent();
}
```

```java
for (;node instanceof MethodDeclaration;) {
node.getBod|
}
```

As a side note, this casted receiver completion has some limitations: https://bugs.eclipse.org/bugs/show_bug.cgi?id=193909 . For instance, casting to `MethodDeclaration` isn't suggested here, to avoid flow analysis calculations that were deemed to expensive:

```java
if (node != null && node instanceof MethodDeclaration) {
node.getB|
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.