Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Intellij Community
Commits
cc6b79a3
Commit
cc6b79a3
authored
6 years ago
by
Anna.Kozlova
Browse files
Options
Download
Email Patches
Plain Diff
preserve comments: invert if (IDEA-205835)
parent
c518058b
Branches unavailable
Tags unavailable
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
java/java-impl/src/com/intellij/codeInsight/intention/impl/InvertIfConditionAction.java
+10
-7
...j/codeInsight/intention/impl/InvertIfConditionAction.java
java/java-impl/src/com/intellij/codeInsight/intention/impl/UnwrapElseBranchAction.java
+4
-2
...ij/codeInsight/intention/impl/UnwrapElseBranchAction.java
java/java-tests/testData/codeInsight/invertIfCondition/afterCommentsInsideThen.java
+14
-0
...odeInsight/invertIfCondition/afterCommentsInsideThen.java
java/java-tests/testData/codeInsight/invertIfCondition/beforeCommentsInsideThen.java
+13
-0
...deInsight/invertIfCondition/beforeCommentsInsideThen.java
java/java-tests/testData/codeInsight/unwrapElseBranch/afterBlock3.java
+2
-0
...ts/testData/codeInsight/unwrapElseBranch/afterBlock3.java
java/java-tests/testData/codeInsight/unwrapElseBranch/beforeBlock3.java
+1
-1
...s/testData/codeInsight/unwrapElseBranch/beforeBlock3.java
with
44 additions
and
10 deletions
+44
-10
java/java-impl/src/com/intellij/codeInsight/intention/impl/InvertIfConditionAction.java
+
10
-
7
View file @
cc6b79a3
...
...
@@ -227,7 +227,7 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
}
}
else
{
if
(!(
thenBranch
instanceof
PsiReturnStatement
))
{
ifStatement
=
addAfterWithinCodeBlock
(
ifStatement
,
ct
.
markUnchanged
(
thenBranch
));
ifStatement
=
addAfterWithinCodeBlock
(
ifStatement
,
ct
.
markUnchanged
(
thenBranch
)
,
ct
);
}
}
ct
.
replaceAndRestoreComments
(
Objects
.
requireNonNull
(
ifStatement
.
getThenBranch
()),
statement
);
...
...
@@ -241,14 +241,14 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
element
instanceof
PsiForeachStatement
&&
flow
.
getStartOffset
(
element
)
+
1
==
endOffset
)
{
PsiStatement
statement
=
factory
.
createStatementFromText
(
"continue;"
,
ifStatement
);
statement
=
(
PsiStatement
)
codeStyle
.
reformat
(
statement
);
ifStatement
=
addAfterWithinCodeBlock
(
ifStatement
,
ct
.
markUnchanged
(
thenBranch
));
ifStatement
=
addAfterWithinCodeBlock
(
ifStatement
,
ct
.
markUnchanged
(
thenBranch
)
,
ct
);
Objects
.
requireNonNull
(
ifStatement
.
getThenBranch
()).
replace
(
statement
);
return
ifStatement
;
}
if
(
element
instanceof
PsiReturnStatement
)
{
PsiReturnStatement
returnStatement
=
(
PsiReturnStatement
)
element
;
ifStatement
=
addAfterWithinCodeBlock
(
ifStatement
,
thenBranch
);
ifStatement
=
addAfterWithinCodeBlock
(
ifStatement
,
thenBranch
,
ct
);
ct
.
replaceAndRestoreComments
(
Objects
.
requireNonNull
(
ifStatement
.
getThenBranch
()),
ct
.
markUnchanged
(
returnStatement
).
copy
());
ControlFlow
flow2
=
buildControlFlow
(
findCodeBlock
(
ifStatement
));
...
...
@@ -356,27 +356,30 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
return
stmt
.
getStatements
()[
0
];
}
private
static
PsiIfStatement
addAfterWithinCodeBlock
(
@NotNull
PsiIfStatement
ifStatement
,
@NotNull
PsiStatement
branch
)
{
private
static
PsiIfStatement
addAfterWithinCodeBlock
(
@NotNull
PsiIfStatement
ifStatement
,
@NotNull
PsiStatement
branch
,
CommentTracker
ct
)
{
final
PsiElement
parent
=
ifStatement
.
getParent
();
if
(
parent
!=
null
&&
!(
parent
instanceof
PsiCodeBlock
))
{
branch
=
(
PsiStatement
)
branch
.
copy
();
ifStatement
=
(
PsiIfStatement
)
wrapWithCodeBlock
(
ifStatement
);
}
addAfter
(
ifStatement
,
branch
);
addAfter
(
ifStatement
,
branch
,
ct
);
return
ifStatement
;
}
static
void
addAfter
(
PsiIfStatement
ifStatement
,
PsiStatement
branch
)
throws
IncorrectOperationException
{
static
void
addAfter
(
PsiIfStatement
ifStatement
,
PsiStatement
branch
,
CommentTracker
ct
)
throws
IncorrectOperationException
{
if
(
branch
instanceof
PsiBlockStatement
)
{
PsiBlockStatement
blockStatement
=
(
PsiBlockStatement
)
branch
;
final
PsiCodeBlock
block
=
blockStatement
.
getCodeBlock
();
final
PsiElement
firstBodyElement
=
block
.
getFirstBodyElement
();
final
PsiElement
lastBodyElement
=
block
.
getLastBodyElement
();
if
(
firstBodyElement
!=
null
&&
lastBodyElement
!=
null
)
{
ct
.
markRangeUnchanged
(
firstBodyElement
,
lastBodyElement
);
ifStatement
.
getParent
().
addRangeAfter
(
firstBodyElement
,
lastBodyElement
,
ifStatement
);
}
}
else
{
ifStatement
.
getParent
().
addAfter
(
branch
,
ifStatement
);
ifStatement
.
getParent
().
addAfter
(
ct
.
markUnchanged
(
branch
)
,
ifStatement
);
}
}
...
...
This diff is collapsed.
Click to expand it.
java/java-impl/src/com/intellij/codeInsight/intention/impl/UnwrapElseBranchAction.java
+
4
-
2
View file @
cc6b79a3
...
...
@@ -24,6 +24,7 @@ import com.intellij.psi.*;
import
com.intellij.psi.util.PsiTreeUtil
;
import
com.intellij.util.IncorrectOperationException
;
import
com.intellij.util.ObjectUtils
;
import
com.siyeh.ig.psiutils.CommentTracker
;
import
com.siyeh.ig.psiutils.ControlFlowUtils
;
import
org.jetbrains.annotations.Nls
;
import
org.jetbrains.annotations.NotNull
;
...
...
@@ -51,8 +52,9 @@ public class UnwrapElseBranchAction extends PsiElementBaseIntentionAction {
elseBranch
=
ifStatement
.
getElseBranch
();
LOG
.
assertTrue
(
elseBranch
!=
null
);
}
InvertIfConditionAction
.
addAfter
(
ifStatement
,
elseBranch
);
elseBranch
.
delete
();
CommentTracker
ct
=
new
CommentTracker
();
InvertIfConditionAction
.
addAfter
(
ifStatement
,
elseBranch
,
ct
);
ct
.
deleteAndRestoreComments
(
elseBranch
);
}
}
}
...
...
This diff is collapsed.
Click to expand it.
java/java-tests/testData/codeInsight/invertIfCondition/afterCommentsInsideThen.java
0 → 100644
+
14
-
0
View file @
cc6b79a3
// "Invert 'if' condition" "true"
public
class
C
{
public
boolean
isAcceptable
(
Object
element
)
{
if
(
element
==
null
)
{
return
false
;
}
System
.
out
.
println
();
//c1
if
(
true
)
{
//c2
}
return
false
;
}
}
This diff is collapsed.
Click to expand it.
java/java-tests/testData/codeInsight/invertIfCondition/beforeCommentsInsideThen.java
0 → 100644
+
13
-
0
View file @
cc6b79a3
// "Invert 'if' condition" "true"
public
class
C
{
public
boolean
isAcceptable
(
Object
element
)
{
i
<
caret
>
f
(
element
!=
null
)
{
System
.
out
.
println
();
//c1
if
(
true
)
{
//c2
}
}
return
false
;
}
}
This diff is collapsed.
Click to expand it.
java/java-tests/testData/codeInsight/unwrapElseBranch/afterBlock3.java
+
2
-
0
View file @
cc6b79a3
...
...
@@ -4,6 +4,8 @@ class T {
void
f
(
boolean
b
)
{
if
(
b
)
throw
new
RuntimeException
(
"When true"
);
//c1
System
.
out
.
println
(
"Otherwise"
);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
java/java-tests/testData/codeInsight/unwrapElseBranch/beforeBlock3.java
+
1
-
1
View file @
cc6b79a3
...
...
@@ -6,6 +6,6 @@ class T {
throw
new
RuntimeException
(
"When true"
);
<
caret
>
else
{
System
.
out
.
println
(
"Otherwise"
);
}
}
//c1
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help