Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
硅谷海盗
kotlin
提交
2ac8e5b4
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,发现更多精彩内容 >>
提交
2ac8e5b4
编写于
12月 05, 2016
作者:
M
Mikhail Glukhikh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
"Change return type fix" now handles data class properties correctly #KT-15028 Fixed
上级
0994d460
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
65 addition
and
32 deletion
+65
-32
idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeCallableReturnTypeFix.kt
...rains/kotlin/idea/quickfix/ChangeCallableReturnTypeFix.kt
+30
-19
idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeFunctionLiteralReturnTypeFix.kt
...otlin/idea/quickfix/ChangeFunctionLiteralReturnTypeFix.kt
+1
-1
idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeVariableTypeFix.kt
...g/jetbrains/kotlin/idea/quickfix/ChangeVariableTypeFix.kt
+1
-1
idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixFactoryForTypeMismatchError.kt
...tlin/idea/quickfix/QuickFixFactoryForTypeMismatchError.kt
+3
-3
idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt
...c/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt
+6
-6
idea/testData/quickfix/override/typeMismatchOnOverride/cantChangeReturnTypeOfOverriddenFunction.kt
...tchOnOverride/cantChangeReturnTypeOfOverriddenFunction.kt
+1
-1
idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/dataClass.kt
...Mismatch/componentFunctionReturnTypeMismatch/dataClass.kt
+8
-0
idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/dataClass.kt.after
...ch/componentFunctionReturnTypeMismatch/dataClass.kt.after
+8
-0
idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/notApplicableToConstructor.kt
...ismatchOnReturnedExpression/notApplicableToConstructor.kt
+1
-1
idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java
...jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java
+6
-0
未找到文件。
idea/src/org/jetbrains/kotlin/idea/quickfix/Change
Function
ReturnTypeFix.kt
→
idea/src/org/jetbrains/kotlin/idea/quickfix/Change
Callable
ReturnTypeFix.kt
浏览文件 @
2ac8e5b4
...
...
@@ -48,7 +48,10 @@ import org.jetbrains.kotlin.types.typeUtil.isUnit
import
org.jetbrains.kotlin.utils.addToStdlib.check
import
java.util.*
abstract
class
ChangeFunctionReturnTypeFix
(
element
:
KtFunction
,
type
:
KotlinType
)
:
KotlinQuickFixAction
<
KtFunction
>(
element
)
{
abstract
class
ChangeCallableReturnTypeFix
(
element
:
KtCallableDeclaration
,
type
:
KotlinType
)
:
KotlinQuickFixAction
<
KtCallableDeclaration
>(
element
)
{
private
val
changeFunctionLiteralReturnTypeFix
:
ChangeFunctionLiteralReturnTypeFix
?
...
...
@@ -73,32 +76,36 @@ abstract class ChangeFunctionReturnTypeFix(element: KtFunction, type: KotlinType
if
(
name
!=
null
)
{
val
container
=
element
.
resolveToDescriptor
().
containingDeclaration
as
?
ClassDescriptor
val
containerName
=
container
?.
name
?.
check
{
!
it
.
isSpecial
}
?.
asString
()
return
"function "
+
(
if
(
containerName
!=
null
)
"'$containerName.$name'"
else
"'$name'"
)
val
fullName
=
if
(
containerName
!=
null
)
"'$containerName.$name'"
else
"'$name'"
if
(
element
is
KtParameter
)
{
return
"property $fullName"
}
return
"function $fullName"
}
else
{
return
null
}
}
class
OnType
(
element
:
KtFunction
,
type
:
KotlinType
)
:
Change
Function
ReturnTypeFix
(
element
,
type
),
HighPriorityAction
{
class
OnType
(
element
:
KtFunction
,
type
:
KotlinType
)
:
Change
Callable
ReturnTypeFix
(
element
,
type
),
HighPriorityAction
{
override
fun
functionPresentation
()
=
null
}
class
ForEnclosing
(
element
:
KtFunction
,
type
:
KotlinType
)
:
Change
Function
ReturnTypeFix
(
element
,
type
),
HighPriorityAction
{
class
ForEnclosing
(
element
:
KtFunction
,
type
:
KotlinType
)
:
Change
Callable
ReturnTypeFix
(
element
,
type
),
HighPriorityAction
{
override
fun
functionPresentation
():
String
?
{
val
presentation
=
super
.
functionPresentation
()
?:
return
"enclosing function"
return
"enclosing $presentation"
}
}
class
ForCalled
(
element
:
Kt
Function
,
type
:
KotlinType
)
:
ChangeFunction
ReturnTypeFix
(
element
,
type
)
{
class
ForCalled
(
element
:
Kt
CallableDeclaration
,
type
:
KotlinType
)
:
ChangeCallable
ReturnTypeFix
(
element
,
type
)
{
override
fun
functionPresentation
():
String
?
{
val
presentation
=
super
.
functionPresentation
()
?:
return
"called function"
return
"called $presentation"
return
if
(
element
is
KtParameter
)
"accessed $presentation"
else
"called $presentation"
}
}
class
ForOverridden
(
element
:
KtFunction
,
type
:
KotlinType
)
:
Change
Function
ReturnTypeFix
(
element
,
type
)
{
class
ForOverridden
(
element
:
KtFunction
,
type
:
KotlinType
)
:
Change
Callable
ReturnTypeFix
(
element
,
type
)
{
override
fun
functionPresentation
():
String
?
{
val
presentation
=
super
.
functionPresentation
()
?:
return
null
return
"base $presentation"
...
...
@@ -114,17 +121,19 @@ abstract class ChangeFunctionReturnTypeFix(element: KtFunction, type: KotlinType
val
functionPresentation
=
functionPresentation
()
if
(
isUnitType
&&
element
.
hasBlockBody
())
{
if
(
isUnitType
&&
element
is
KtFunction
&&
element
.
hasBlockBody
())
{
return
if
(
functionPresentation
==
null
)
"Remove explicitly specified return type"
else
"Remove explicitly specified return type of $functionPresentation"
}
val
typeName
=
if
(
element
is
KtFunction
)
"return type"
else
"type"
return
if
(
functionPresentation
==
null
)
"Change
return typ
e to '$typePresentation'"
"Change
$typeNam
e to '$typePresentation'"
else
"Change
return typ
e of $functionPresentation to '$typePresentation'"
"Change
$typeNam
e of $functionPresentation to '$typePresentation'"
}
override
fun
getFamilyName
()
=
KotlinBundle
.
message
(
"change.type.family"
)
...
...
@@ -142,7 +151,7 @@ abstract class ChangeFunctionReturnTypeFix(element: KtFunction, type: KotlinType
changeFunctionLiteralReturnTypeFix
.
invoke
(
project
,
editor
!!
,
file
)
}
else
{
if
(!(
isUnitType
&&
element
.
hasBlockBody
()))
{
if
(!(
isUnitType
&&
element
is
KtFunction
&&
element
.
hasBlockBody
()))
{
var
newTypeRef
=
KtPsiFactory
(
project
).
createType
(
typeSourceCode
)
newTypeRef
=
element
.
setTypeReference
(
newTypeRef
)
!!
ShortenReferences
.
DEFAULT
.
process
(
newTypeRef
)
...
...
@@ -158,9 +167,11 @@ abstract class ChangeFunctionReturnTypeFix(element: KtFunction, type: KotlinType
val
entry
=
getDestructuringDeclarationEntryThatTypeMismatchComponentFunction
(
diagnostic
)
val
context
=
entry
.
analyze
(
BodyResolveMode
.
PARTIAL
)
val
resolvedCall
=
context
.
get
(
BindingContext
.
COMPONENT_RESOLVED_CALL
,
entry
)
?:
return
null
val
componentFunction
=
DescriptorToSourceUtils
.
descriptorToDeclaration
(
resolvedCall
.
candidateDescriptor
)
as
KtFunction
?
?:
return
null
val
componentFunction
=
DescriptorToSourceUtils
.
descriptorToDeclaration
(
resolvedCall
.
candidateDescriptor
)
as
?
KtCallableDeclaration
?:
return
null
val
expectedType
=
context
[
BindingContext
.
TYPE
,
entry
.
typeReference
!!
]
?:
return
null
return
Change
Function
ReturnTypeFix
.
ForCalled
(
componentFunction
,
expectedType
)
return
Change
Callable
ReturnTypeFix
.
ForCalled
(
componentFunction
,
expectedType
)
}
}
...
...
@@ -172,7 +183,7 @@ abstract class ChangeFunctionReturnTypeFix(element: KtFunction, type: KotlinType
val
resolvedCall
=
context
[
BindingContext
.
LOOP_RANGE_HAS_NEXT_RESOLVED_CALL
,
expression
]
?:
return
null
val
hasNextDescriptor
=
resolvedCall
.
candidateDescriptor
val
hasNextFunction
=
DescriptorToSourceUtils
.
descriptorToDeclaration
(
hasNextDescriptor
)
as
KtFunction
?
?:
return
null
return
Change
Function
ReturnTypeFix
.
ForCalled
(
hasNextFunction
,
hasNextDescriptor
.
builtIns
.
booleanType
)
return
Change
Callable
ReturnTypeFix
.
ForCalled
(
hasNextFunction
,
hasNextDescriptor
.
builtIns
.
booleanType
)
}
}
...
...
@@ -183,7 +194,7 @@ abstract class ChangeFunctionReturnTypeFix(element: KtFunction, type: KotlinType
val
resolvedCall
=
expression
.
getResolvedCall
(
context
)
?:
return
null
val
compareToDescriptor
=
resolvedCall
.
candidateDescriptor
val
compareTo
=
DescriptorToSourceUtils
.
descriptorToDeclaration
(
compareToDescriptor
)
as
?
KtFunction
?:
return
null
return
Change
Function
ReturnTypeFix
.
ForCalled
(
compareTo
,
compareToDescriptor
.
builtIns
.
intType
)
return
Change
Callable
ReturnTypeFix
.
ForCalled
(
compareTo
,
compareToDescriptor
.
builtIns
.
intType
)
}
}
...
...
@@ -197,7 +208,7 @@ abstract class ChangeFunctionReturnTypeFix(element: KtFunction, type: KotlinType
val
matchingReturnType
=
QuickFixUtil
.
findLowerBoundOfOverriddenCallablesReturnTypes
(
descriptor
)
if
(
matchingReturnType
!=
null
)
{
actions
.
add
(
Change
Function
ReturnTypeFix
.
OnType
(
function
,
matchingReturnType
))
actions
.
add
(
Change
Callable
ReturnTypeFix
.
OnType
(
function
,
matchingReturnType
))
}
val
functionType
=
descriptor
.
returnType
?:
return
actions
...
...
@@ -213,7 +224,7 @@ abstract class ChangeFunctionReturnTypeFix(element: KtFunction, type: KotlinType
if
(
overriddenMismatchingFunctions
.
size
==
1
)
{
val
overriddenFunction
=
DescriptorToSourceUtils
.
descriptorToDeclaration
(
overriddenMismatchingFunctions
[
0
])
if
(
overriddenFunction
is
KtFunction
)
{
actions
.
add
(
Change
Function
ReturnTypeFix
.
ForOverridden
(
overriddenFunction
,
functionType
))
actions
.
add
(
Change
Callable
ReturnTypeFix
.
ForOverridden
(
overriddenFunction
,
functionType
))
}
}
...
...
@@ -224,14 +235,14 @@ abstract class ChangeFunctionReturnTypeFix(element: KtFunction, type: KotlinType
object
ChangingReturnTypeToUnitFactory
:
KotlinSingleIntentionActionFactory
()
{
override
fun
createAction
(
diagnostic
:
Diagnostic
):
IntentionAction
?
{
val
function
=
QuickFixUtil
.
getParentElementOfType
(
diagnostic
,
KtFunction
::
class
.
java
)
?:
return
null
return
Change
Function
ReturnTypeFix
.
ForEnclosing
(
function
,
function
.
builtIns
.
unitType
)
return
Change
Callable
ReturnTypeFix
.
ForEnclosing
(
function
,
function
.
builtIns
.
unitType
)
}
}
object
ChangingReturnTypeToNothingFactory
:
KotlinSingleIntentionActionFactory
()
{
override
fun
createAction
(
diagnostic
:
Diagnostic
):
IntentionAction
?
{
val
function
=
QuickFixUtil
.
getParentElementOfType
(
diagnostic
,
KtFunction
::
class
.
java
)
?:
return
null
return
Change
Function
ReturnTypeFix
.
ForEnclosing
(
function
,
function
.
builtIns
.
nothingType
)
return
Change
Callable
ReturnTypeFix
.
ForEnclosing
(
function
,
function
.
builtIns
.
nothingType
)
}
}
...
...
idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeFunctionLiteralReturnTypeFix.kt
浏览文件 @
2ac8e5b4
...
...
@@ -96,7 +96,7 @@ class ChangeFunctionLiteralReturnTypeFix(
val
parentFunctionReturnTypeRef
=
parentFunction
.
typeReference
val
parentFunctionReturnType
=
context
.
get
(
BindingContext
.
TYPE
,
parentFunctionReturnTypeRef
)
return
if
(
parentFunctionReturnType
!=
null
&&
!
KotlinTypeChecker
.
DEFAULT
.
isSubtypeOf
(
eventualFunctionLiteralType
,
parentFunctionReturnType
))
Change
Function
ReturnTypeFix
.
ForEnclosing
(
parentFunction
,
eventualFunctionLiteralType
)
Change
Callable
ReturnTypeFix
.
ForEnclosing
(
parentFunction
,
eventualFunctionLiteralType
)
else
null
}
...
...
idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeVariableTypeFix.kt
浏览文件 @
2ac8e5b4
...
...
@@ -113,7 +113,7 @@ open class ChangeVariableTypeFix(element: KtVariableDeclaration, type: KotlinTyp
object
ComponentFunctionReturnTypeMismatchFactory
:
KotlinSingleIntentionActionFactory
()
{
override
fun
createAction
(
diagnostic
:
Diagnostic
):
IntentionAction
?
{
val
entry
=
Change
Function
ReturnTypeFix
.
getDestructuringDeclarationEntryThatTypeMismatchComponentFunction
(
diagnostic
)
val
entry
=
Change
Callable
ReturnTypeFix
.
getDestructuringDeclarationEntryThatTypeMismatchComponentFunction
(
diagnostic
)
val
context
=
entry
.
analyze
()
val
resolvedCall
=
context
.
get
(
BindingContext
.
COMPONENT_RESOLVED_CALL
,
entry
)
?:
return
null
if
(
DescriptorToSourceUtils
.
descriptorToDeclaration
(
resolvedCall
.
candidateDescriptor
)
==
null
)
return
null
...
...
idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixFactoryForTypeMismatchError.kt
浏览文件 @
2ac8e5b4
...
...
@@ -141,7 +141,7 @@ class QuickFixFactoryForTypeMismatchError : KotlinIntentionActionsFactory() {
if
(
function
is
KtFunction
&&
QuickFixUtil
.
canFunctionOrGetterReturnExpression
(
function
,
diagnosticElement
))
{
val
scope
=
function
.
getResolutionScope
(
context
,
function
.
getResolutionFacade
())
val
typeToInsert
=
expressionType
.
approximateWithResolvableType
(
scope
,
false
)
actions
.
add
(
Change
Function
ReturnTypeFix
.
ForEnclosing
(
function
,
typeToInsert
))
actions
.
add
(
Change
Callable
ReturnTypeFix
.
ForEnclosing
(
function
,
typeToInsert
))
}
// Fixing overloaded operators:
...
...
@@ -150,7 +150,7 @@ class QuickFixFactoryForTypeMismatchError : KotlinIntentionActionsFactory() {
if
(
resolvedCall
!=
null
)
{
val
declaration
=
getFunctionDeclaration
(
resolvedCall
)
if
(
declaration
!=
null
)
{
actions
.
add
(
Change
Function
ReturnTypeFix
.
ForCalled
(
declaration
,
expectedType
))
actions
.
add
(
Change
Callable
ReturnTypeFix
.
ForCalled
(
declaration
,
expectedType
))
}
}
}
...
...
@@ -161,7 +161,7 @@ class QuickFixFactoryForTypeMismatchError : KotlinIntentionActionsFactory() {
if
(
resolvedCall
!=
null
)
{
val
declaration
=
getFunctionDeclaration
(
resolvedCall
)
if
(
declaration
!=
null
)
{
actions
.
add
(
Change
Function
ReturnTypeFix
.
ForCalled
(
declaration
,
expectedType
))
actions
.
add
(
Change
Callable
ReturnTypeFix
.
ForCalled
(
declaration
,
expectedType
))
}
}
}
...
...
idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt
浏览文件 @
2ac8e5b4
...
...
@@ -264,14 +264,14 @@ class QuickFixRegistrar : QuickFixContributor {
VAR_TYPE_MISMATCH_ON_OVERRIDE
.
registerFactory
(
changeVariableTypeFix
)
COMPONENT_FUNCTION_RETURN_TYPE_MISMATCH
.
registerFactory
(
ChangeVariableTypeFix
.
ComponentFunctionReturnTypeMismatchFactory
)
val
changeFunctionReturnTypeFix
=
Change
Function
ReturnTypeFix
.
ChangingReturnTypeToUnitFactory
val
changeFunctionReturnTypeFix
=
Change
Callable
ReturnTypeFix
.
ChangingReturnTypeToUnitFactory
RETURN_TYPE_MISMATCH
.
registerFactory
(
changeFunctionReturnTypeFix
)
NO_RETURN_IN_FUNCTION_WITH_BLOCK_BODY
.
registerFactory
(
changeFunctionReturnTypeFix
)
RETURN_TYPE_MISMATCH_ON_OVERRIDE
.
registerFactory
(
Change
Function
ReturnTypeFix
.
ReturnTypeMismatchOnOverrideFactory
)
COMPONENT_FUNCTION_RETURN_TYPE_MISMATCH
.
registerFactory
(
Change
Function
ReturnTypeFix
.
ComponentFunctionReturnTypeMismatchFactory
)
HAS_NEXT_FUNCTION_TYPE_MISMATCH
.
registerFactory
(
Change
Function
ReturnTypeFix
.
HasNextFunctionTypeMismatchFactory
)
COMPARE_TO_TYPE_MISMATCH
.
registerFactory
(
Change
Function
ReturnTypeFix
.
CompareToTypeMismatchFactory
)
IMPLICIT_NOTHING_RETURN_TYPE
.
registerFactory
(
Change
Function
ReturnTypeFix
.
ChangingReturnTypeToNothingFactory
)
RETURN_TYPE_MISMATCH_ON_OVERRIDE
.
registerFactory
(
Change
Callable
ReturnTypeFix
.
ReturnTypeMismatchOnOverrideFactory
)
COMPONENT_FUNCTION_RETURN_TYPE_MISMATCH
.
registerFactory
(
Change
Callable
ReturnTypeFix
.
ComponentFunctionReturnTypeMismatchFactory
)
HAS_NEXT_FUNCTION_TYPE_MISMATCH
.
registerFactory
(
Change
Callable
ReturnTypeFix
.
HasNextFunctionTypeMismatchFactory
)
COMPARE_TO_TYPE_MISMATCH
.
registerFactory
(
Change
Callable
ReturnTypeFix
.
CompareToTypeMismatchFactory
)
IMPLICIT_NOTHING_RETURN_TYPE
.
registerFactory
(
Change
Callable
ReturnTypeFix
.
ChangingReturnTypeToNothingFactory
)
TOO_MANY_ARGUMENTS
.
registerFactory
(
ChangeFunctionSignatureFix
)
NO_VALUE_FOR_PARAMETER
.
registerFactory
(
ChangeFunctionSignatureFix
)
...
...
idea/testData/quickfix/override/typeMismatchOnOverride/cantChangeReturnTypeOfOverriddenFunction.kt
浏览文件 @
2ac8e5b4
// "class org.jetbrains.kotlin.idea.quickfix.Change
Function
ReturnTypeFix" "false"
// "class org.jetbrains.kotlin.idea.quickfix.Change
Callable
ReturnTypeFix" "false"
// ERROR: Return type of 'foo' is not a subtype of the return type of the overridden member 'public abstract fun foo(): Int defined in A'
interface
A
{
fun
foo
():
Int
...
...
idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/dataClass.kt
0 → 100644
浏览文件 @
2ac8e5b4
// "Change type of accessed property 'A.x' to 'String'" "true"
// See KT-15028 (CCE)
data class
A
(
val
x
:
Int
,
val
y
:
String
)
fun
f1
(
a
:
A
)
{
val
(
x
:
String
,
y
)
=
a
<
caret
>
}
\ No newline at end of file
idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/dataClass.kt.after
0 → 100644
浏览文件 @
2ac8e5b4
// "Change type of accessed property 'A.x' to 'String'" "true"
// See KT-15028 (CCE)
data class A(val x: String, val y: String)
fun f1(a: A) {
val (x: String, y) = a<caret>
}
\ No newline at end of file
idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/notApplicableToConstructor.kt
浏览文件 @
2ac8e5b4
// "class org.jetbrains.kotlin.idea.quickfix.Change
Function
ReturnTypeFix" "false"
// "class org.jetbrains.kotlin.idea.quickfix.Change
Callable
ReturnTypeFix" "false"
// ACTION: Change type of 'b' to 'A'
// ACTION: Convert property initializer to getter
// ACTION: Let 'A' implement interface 'B'
...
...
idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java
浏览文件 @
2ac8e5b4
...
...
@@ -9346,6 +9346,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/componentFunctionReturnTypeMismatch5.kt");
doTest(fileName);
}
@TestMetadata("dataClass.kt")
public void testDataClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/dataClass.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/quickfix/typeMismatch/fixOverloadedOperator")
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录