Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
硅谷海盗
kotlin
提交
67788e39
K
kotlin
项目概览
硅谷海盗
/
kotlin
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
K
kotlin
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
67788e39
编写于
12月 20, 2016
作者:
M
Mikhail Glukhikh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fold initializer & if to elvis: search for variable usage inside if first #KT-14889 Fixed
上级
ee1b741e
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
51 addition
and
13 deletion
+51
-13
idea/src/org/jetbrains/kotlin/idea/intentions/FoldInitializerAndIfToElvisIntention.kt
...n/idea/intentions/FoldInitializerAndIfToElvisIntention.kt
+13
-13
idea/testData/intentions/foldInitializerAndIfToElvis/UsedInside.kt
...Data/intentions/foldInitializerAndIfToElvis/UsedInside.kt
+13
-0
idea/testData/intentions/foldInitializerAndIfToElvis/UsedInsideInTemplate.kt
...tions/foldInitializerAndIfToElvis/UsedInsideInTemplate.kt
+13
-0
idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java
...brains/kotlin/idea/intentions/IntentionTestGenerated.java
+12
-0
未找到文件。
idea/src/org/jetbrains/kotlin/idea/intentions/FoldInitializerAndIfToElvisIntention.kt
浏览文件 @
67788e39
...
...
@@ -19,6 +19,8 @@ package org.jetbrains.kotlin.idea.intentions
import
com.intellij.openapi.editor.Editor
import
com.intellij.openapi.util.TextRange
import
com.intellij.psi.PsiWhiteSpace
import
com.intellij.psi.search.LocalSearchScope
import
com.intellij.psi.search.searches.ReferencesSearch
import
org.jetbrains.kotlin.idea.caches.resolve.analyze
import
org.jetbrains.kotlin.idea.core.replaced
import
org.jetbrains.kotlin.idea.core.setType
...
...
@@ -27,10 +29,7 @@ import org.jetbrains.kotlin.idea.intentions.branchedTransformations.expressionCo
import
org.jetbrains.kotlin.idea.util.CommentSaver
import
org.jetbrains.kotlin.lexer.KtTokens
import
org.jetbrains.kotlin.psi.*
import
org.jetbrains.kotlin.psi.psiUtil.PsiChildRange
import
org.jetbrains.kotlin.psi.psiUtil.endOffset
import
org.jetbrains.kotlin.psi.psiUtil.siblings
import
org.jetbrains.kotlin.psi.psiUtil.startOffset
import
org.jetbrains.kotlin.psi.psiUtil.*
import
org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import
org.jetbrains.kotlin.types.typeUtil.isNothing
import
org.jetbrains.kotlin.types.typeUtil.makeNotNullable
...
...
@@ -107,21 +106,22 @@ class FoldInitializerAndIfToElvisIntention : SelfTargetingRangeIntention<KtIfExp
}
as
?
KtNameReferenceExpression
?:
return
null
if
(
ifExpression
.
parent
!
is
KtBlockExpression
)
return
null
val
prevStatement
=
ifExpression
.
siblings
(
forward
=
false
,
withItself
=
false
)
.
firstIsInstanceOrNull
<
KtExpression
>()
?:
return
null
if
(
prevStatement
!
is
KtVariableDeclaration
)
return
null
val
prevStatement
=
(
ifExpression
.
siblings
(
forward
=
false
,
withItself
=
false
)
.
firstIsInstanceOrNull
<
KtExpression
>()
?:
return
null
)
as
?
KtVariableDeclaration
prevStatement
?:
return
null
if
(
prevStatement
.
nameAsName
!=
value
.
getReferencedNameAsName
())
return
null
val
initializer
=
prevStatement
.
initializer
?:
return
null
val
then
=
ifExpression
.
then
?:
return
null
val
typeReference
=
(
operationExpression
as
?
KtIsExpression
)
?.
typeReference
if
(
then
is
KtBlockExpression
)
{
val
statement
=
then
.
statements
.
singleOrNull
()
?:
return
null
return
Data
(
initializer
,
prevStatement
,
statement
,
typeReference
)
}
else
{
return
Data
(
initializer
,
prevStatement
,
then
,
typeReference
)
val
statement
=
if
(
then
is
KtBlockExpression
)
then
.
statements
.
singleOrNull
()
else
then
statement
?:
return
null
if
(
ReferencesSearch
.
search
(
prevStatement
,
LocalSearchScope
(
statement
)).
findFirst
()
!=
null
)
{
return
null
}
return
Data
(
initializer
,
prevStatement
,
statement
,
typeReference
)
}
private
fun
PsiChildRange
.
withoutLastStatement
():
PsiChildRange
{
...
...
idea/testData/intentions/foldInitializerAndIfToElvis/UsedInside.kt
0 → 100644
浏览文件 @
67788e39
// IS_APPLICABLE: false
interface
A
{
val
s
:
String
}
fun
foo
()
=
Any
()
fun
test
():
String
{
val
y
=
foo
()
<
caret
>
if
(
y
!
is
A
)
return
y
.
toString
()
return
y
.
s
}
\ No newline at end of file
idea/testData/intentions/foldInitializerAndIfToElvis/UsedInsideInTemplate.kt
0 → 100644
浏览文件 @
67788e39
// IS_APPLICABLE: false
interface
A
{
val
s
:
String
}
fun
foo
()
=
Any
()
fun
test
():
String
{
val
y
=
foo
()
<
caret
>
if
(
y
!
is
A
)
return
"Expected A: $y"
return
y
.
s
}
\ No newline at end of file
idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java
浏览文件 @
67788e39
...
...
@@ -7397,6 +7397,18 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("UsedInside.kt")
public void testUsedInside() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/foldInitializerAndIfToElvis/UsedInside.kt");
doTest(fileName);
}
@TestMetadata("UsedInsideInTemplate.kt")
public void testUsedInsideInTemplate() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/foldInitializerAndIfToElvis/UsedInsideInTemplate.kt");
doTest(fileName);
}
@TestMetadata("Var.kt")
public void testVar() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/foldInitializerAndIfToElvis/Var.kt");
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录