Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
硅谷海盗
kotlin
提交
e904e56d
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,发现更多精彩内容 >>
提交
e904e56d
编写于
12月 02, 2016
作者:
M
Mikhail Glukhikh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Convert reference to lambda: correct handling of static method references #KT-14982 Fixed
上级
ea8548c5
变更
13
隐藏空白更改
内联
并排
Showing
13 changed file
with
83 addition
and
5 deletion
+83
-5
idea/src/org/jetbrains/kotlin/idea/intentions/ConvertReferenceToLambdaIntention.kt
...tlin/idea/intentions/ConvertReferenceToLambdaIntention.kt
+12
-5
idea/testData/intentions/convertReferenceToLambda/companion.kt
...testData/intentions/convertReferenceToLambda/companion.kt
+11
-0
idea/testData/intentions/convertReferenceToLambda/companion.kt.after
...ta/intentions/convertReferenceToLambda/companion.kt.after
+11
-0
idea/testData/intentions/convertReferenceToLambda/object.kt
idea/testData/intentions/convertReferenceToLambda/object.kt
+7
-0
idea/testData/intentions/convertReferenceToLambda/static.1.java
...estData/intentions/convertReferenceToLambda/static.1.java
+3
-0
idea/testData/intentions/convertReferenceToLambda/static.1.java.after
...a/intentions/convertReferenceToLambda/static.1.java.after
+3
-0
idea/testData/intentions/convertReferenceToLambda/static.kt
idea/testData/intentions/convertReferenceToLambda/static.kt
+2
-0
idea/testData/intentions/convertReferenceToLambda/static.kt.after
...tData/intentions/convertReferenceToLambda/static.kt.after
+2
-0
idea/testData/intentions/convertReferenceToLambda/staticTwoParameters.1.java
...tions/convertReferenceToLambda/staticTwoParameters.1.java
+3
-0
idea/testData/intentions/convertReferenceToLambda/staticTwoParameters.1.java.after
...convertReferenceToLambda/staticTwoParameters.1.java.after
+3
-0
idea/testData/intentions/convertReferenceToLambda/staticTwoParameters.kt
...ntentions/convertReferenceToLambda/staticTwoParameters.kt
+1
-0
idea/testData/intentions/convertReferenceToLambda/staticTwoParameters.kt.after
...ons/convertReferenceToLambda/staticTwoParameters.kt.after
+1
-0
idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java
...brains/kotlin/idea/intentions/IntentionTestGenerated.java
+24
-0
未找到文件。
idea/src/org/jetbrains/kotlin/idea/intentions/ConvertReferenceToLambdaIntention.kt
浏览文件 @
e904e56d
...
...
@@ -30,7 +30,6 @@ import org.jetbrains.kotlin.resolve.BindingContext.DOUBLE_COLON_LHS
import
org.jetbrains.kotlin.resolve.BindingContext.REFERENCE_TARGET
import
org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import
org.jetbrains.kotlin.types.expressions.DoubleColonLHS
import
org.jetbrains.kotlin.utils.singletonOrEmptyList
class
ConvertReferenceToLambdaInspection
:
IntentionBasedInspection
<
KtCallableReferenceExpression
>(
ConvertReferenceToLambdaIntention
::
class
)
...
...
@@ -51,23 +50,31 @@ class ConvertReferenceToLambdaIntention : SelfTargetingOffsetIndependentIntentio
val
receiverNameAndType
=
receiverType
?.
let
{
KotlinNameSuggester
.
suggestNamesByType
(
it
,
validator
=
{
name
->
name
!
in
parameterNamesAndTypes
.
map
{
it
.
first
}
},
defaultName
=
"receiver"
).
first
()
to
it
}
val
acceptsReceiverAsParameter
=
receiverNameAndType
!=
null
&&
(
targetDescriptor
.
dispatchReceiverParameter
!=
null
||
targetDescriptor
.
extensionReceiverParameter
!=
null
)
val
referenceParent
=
element
.
parent
val
insideCall
=
referenceParent
is
KtValueArgument
val
factory
=
KtPsiFactory
(
element
)
val
targetName
=
reference
.
text
val
lambdaParameterNamesAndTypes
=
receiverNameAndType
.
singletonOrEmptyList
()
+
parameterNamesAndTypes
val
receiverPrefix
=
receiverNameAndType
?.
let
{
it
.
first
+
"."
}
?:
""
val
lambdaParameterNamesAndTypes
=
if
(
acceptsReceiverAsParameter
)
listOf
(
receiverNameAndType
!!
)
+
parameterNamesAndTypes
else
parameterNamesAndTypes
val
receiverPrefix
=
if
(
acceptsReceiverAsParameter
)
receiverNameAndType
!!
.
first
+
"."
else
receiverExpression
?.
let
{
it
.
text
+
"."
}
?:
""
val
lambdaExpression
=
if
(
insideCall
&&
lambdaParameterNamesAndTypes
.
size
==
1
)
{
factory
.
createLambdaExpression
(
parameters
=
""
,
body
=
when
{
receiverNameAndType
!=
null
->
acceptsReceiverAsParameter
->
if
(
targetDescriptor
is
PropertyDescriptor
)
"it.$targetName"
else
"it.$targetName()"
else
->
"$targetName(it)"
"$
receiverPrefix$
targetName(it)"
}
)
}
...
...
idea/testData/intentions/convertReferenceToLambda/companion.kt
0 → 100644
浏览文件 @
e904e56d
// WITH_RUNTIME
import
Utils.Companion.foo
val
list
=
listOf
(
1
,
2
,
3
).
map
(<
caret
>
::
foo
)
class
Utils
{
companion
object
{
fun
foo
(
x
:
Int
)
=
x
}
}
\ No newline at end of file
idea/testData/intentions/convertReferenceToLambda/companion.kt.after
0 → 100644
浏览文件 @
e904e56d
// WITH_RUNTIME
import Utils.Companion.foo
val list = listOf(1, 2, 3).map { foo(it) }
class Utils {
companion object {
fun foo(x: Int) = x
}
}
\ No newline at end of file
idea/testData/intentions/convertReferenceToLambda/object.kt
0 → 100644
浏览文件 @
e904e56d
// IS_APPLICABLE: false
// WITH_RUNTIME
val
list
=
listOf
(
1
,
2
,
3
).
map
(<
caret
>
Utils
::
foo
)
object
Utils
{
fun
foo
(
x
:
Int
)
=
x
}
\ No newline at end of file
idea/testData/intentions/convertReferenceToLambda/static.1.java
0 → 100644
浏览文件 @
e904e56d
class
Utils
{
public
static
int
foo
(
int
arg
)
{
return
arg
;
}
}
\ No newline at end of file
idea/testData/intentions/convertReferenceToLambda/static.1.java.after
0 → 100644
浏览文件 @
e904e56d
class Utils {
public static int foo(int arg) { return arg; }
}
\ No newline at end of file
idea/testData/intentions/convertReferenceToLambda/static.kt
0 → 100644
浏览文件 @
e904e56d
// WITH_RUNTIME
val
list
=
listOf
(
1
,
2
,
3
).
map
(<
caret
>
Utils
::
foo
)
\ No newline at end of file
idea/testData/intentions/convertReferenceToLambda/static.kt.after
0 → 100644
浏览文件 @
e904e56d
// WITH_RUNTIME
val list = listOf(1, 2, 3).map { Utils.foo(it) }
\ No newline at end of file
idea/testData/intentions/convertReferenceToLambda/staticTwoParameters.1.java
0 → 100644
浏览文件 @
e904e56d
class
Utils
{
public
static
int
foo
(
int
x
,
int
y
)
{
return
x
+
y
;
}
}
\ No newline at end of file
idea/testData/intentions/convertReferenceToLambda/staticTwoParameters.1.java.after
0 → 100644
浏览文件 @
e904e56d
class Utils {
public static int foo(int x, int y) { return x + y; }
}
\ No newline at end of file
idea/testData/intentions/convertReferenceToLambda/staticTwoParameters.kt
0 → 100644
浏览文件 @
e904e56d
val
x
=
<
caret
>
Utils
::
foo
\ No newline at end of file
idea/testData/intentions/convertReferenceToLambda/staticTwoParameters.kt.after
0 → 100644
浏览文件 @
e904e56d
val x = { x: Int, y: Int -> Utils.foo(x, y) }
\ No newline at end of file
idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java
浏览文件 @
e904e56d
...
...
@@ -4967,6 +4967,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("companion.kt")
public void testCompanion() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/companion.kt");
doTest(fileName);
}
@TestMetadata("constructor.kt")
public void testConstructor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/constructor.kt");
...
...
@@ -5015,6 +5021,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("object.kt")
public void testObject() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/object.kt");
doTest(fileName);
}
@TestMetadata("receiverParameter.kt")
public void testReceiverParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/receiverParameter.kt");
...
...
@@ -5027,6 +5039,18 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("static.kt")
public void testStatic() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/static.kt");
doTest(fileName);
}
@TestMetadata("staticTwoParameters.kt")
public void testStaticTwoParameters() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/staticTwoParameters.kt");
doTest(fileName);
}
@TestMetadata("threeParameters.kt")
public void testThreeParameters() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/threeParameters.kt");
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录