Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
硅谷海盗
kotlin
提交
68c7e0d9
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,发现更多精彩内容 >>
提交
68c7e0d9
编写于
5月 03, 2012
作者:
E
Evgeny Gerashchenko
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Made SpecifyTypeExplicitlyAction stateless.
上级
e4983484
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
47 addition
and
29 deletion
+47
-29
idea/src/org/jetbrains/jet/plugin/intentions/SpecifyTypeExplicitlyAction.java
...ns/jet/plugin/intentions/SpecifyTypeExplicitlyAction.java
+47
-29
未找到文件。
idea/src/org/jetbrains/jet/plugin/intentions/SpecifyTypeExplicitlyAction.java
浏览文件 @
68c7e0d9
...
...
@@ -46,9 +46,7 @@ import java.util.Collections;
* @since 4/20/12
*/
public
class
SpecifyTypeExplicitlyAction
extends
PsiElementBaseIntentionAction
{
private
JetType
targetType
;
@NotNull
@NotNull
@Override
public
String
getFamilyName
()
{
return
JetBundle
.
message
(
"specify.type.explicitly.action.family.name"
);
...
...
@@ -57,16 +55,21 @@ public class SpecifyTypeExplicitlyAction extends PsiElementBaseIntentionAction {
@Override
public
void
invoke
(
@NotNull
Project
project
,
Editor
editor
,
@NotNull
PsiElement
element
)
{
PsiElement
parent
=
element
.
getParent
();
JetType
type
=
getTypeForDeclaration
((
JetNamedDeclaration
)
parent
);
if
(
ErrorUtils
.
isErrorType
(
type
))
{
return
;
}
if
(
parent
instanceof
JetProperty
)
{
JetProperty
property
=
(
JetProperty
)
parent
;
if
(
targetType
==
null
)
{
removeTypeAnnotation
(
project
,
property
);
if
(
property
.
getPropertyTypeRef
()
==
null
)
{
addTypeAnnotation
(
project
,
property
,
type
);
}
else
{
addTypeAnnotation
(
project
,
property
,
targetType
);
removeTypeAnnotation
(
project
,
property
);
}
}
else
if
(
parent
instanceof
JetNamedFunction
)
{
assert
targetType
!=
null
;
addTypeAnnotation
(
project
,
(
JetFunction
)
parent
,
targetType
);
JetNamedFunction
function
=
(
JetNamedFunction
)
parent
;
assert
function
.
getReturnTypeRef
()
==
null
;
addTypeAnnotation
(
project
,
function
,
type
);
}
else
{
assert
false
;
}
...
...
@@ -74,9 +77,13 @@ public class SpecifyTypeExplicitlyAction extends PsiElementBaseIntentionAction {
@Override
public
boolean
isAvailable
(
@NotNull
Project
project
,
Editor
editor
,
@NotNull
PsiElement
element
)
{
if
(
element
.
getParent
()
instanceof
JetProperty
&&
!
PsiTreeUtil
.
isAncestor
(((
JetProperty
)
element
.
getParent
()).
getInitializer
(),
element
,
false
))
{
if
(((
JetProperty
)
element
.
getParent
()).
getPropertyTypeRef
()
!=
null
)
{
targetType
=
null
;
PsiElement
parent
=
element
.
getParent
();
if
(!(
parent
instanceof
JetNamedDeclaration
))
{
return
false
;
}
JetNamedDeclaration
declaration
=
(
JetNamedDeclaration
)
parent
;
if
(
declaration
instanceof
JetProperty
&&
!
PsiTreeUtil
.
isAncestor
(((
JetProperty
)
declaration
).
getInitializer
(),
element
,
false
))
{
if
(((
JetProperty
)
declaration
).
getPropertyTypeRef
()
!=
null
)
{
setText
(
JetBundle
.
message
(
"specify.type.explicitly.remove.action.name"
));
return
true
;
}
...
...
@@ -84,38 +91,49 @@ public class SpecifyTypeExplicitlyAction extends PsiElementBaseIntentionAction {
setText
(
JetBundle
.
message
(
"specify.type.explicitly.add.action.name"
));
}
}
else
if
(
element
.
getParent
()
instanceof
JetNamedFunction
&&
((
JetNamedFunction
)
element
.
getParent
()
).
getReturnTypeRef
()
==
null
&&
!((
JetNamedFunction
)
element
.
getParent
()
).
hasBlockBody
())
{
else
if
(
declaration
instanceof
JetNamedFunction
&&
((
JetNamedFunction
)
declaration
).
getReturnTypeRef
()
==
null
&&
!((
JetNamedFunction
)
declaration
).
hasBlockBody
())
{
setText
(
JetBundle
.
message
(
"specify.type.explicitly.add.return.type.action.name"
));
}
else
{
return
false
;
}
BindingContext
bindingContext
=
AnalyzeSingleFileUtil
.
getContextForSingleFile
((
JetFile
)
element
.
getContainingFile
());
DeclarationDescriptor
descriptor
=
bindingContext
.
get
(
BindingContext
.
DECLARATION_TO_DESCRIPTOR
,
element
.
getParent
());
if
(
ErrorUtils
.
isErrorType
(
getTypeForDeclaration
(
declaration
)))
{
return
false
;
}
return
!
isDisabledForError
()
||
!
hasPublicMemberDiagnostic
(
declaration
);
}
private
static
boolean
hasPublicMemberDiagnostic
(
@NotNull
JetNamedDeclaration
declaration
)
{
BindingContext
bindingContext
=
AnalyzeSingleFileUtil
.
getContextForSingleFile
((
JetFile
)
declaration
.
getContainingFile
());
for
(
Diagnostic
diagnostic
:
bindingContext
.
getDiagnostics
())
{
//noinspection ConstantConditions
if
(
Errors
.
PUBLIC_MEMBER_SHOULD_SPECIFY_TYPE
==
diagnostic
.
getFactory
()
&&
declaration
==
diagnostic
.
getPsiElement
())
{
return
true
;
}
}
return
false
;
}
@NotNull
private
static
JetType
getTypeForDeclaration
(
@NotNull
JetNamedDeclaration
declaration
)
{
BindingContext
bindingContext
=
AnalyzeSingleFileUtil
.
getContextForSingleFile
((
JetFile
)
declaration
.
getContainingFile
());
DeclarationDescriptor
descriptor
=
bindingContext
.
get
(
BindingContext
.
DECLARATION_TO_DESCRIPTOR
,
declaration
);
JetType
type
;
if
(
descriptor
instanceof
VariableDescriptor
)
{
t
argetT
ype
=
((
VariableDescriptor
)
descriptor
).
getType
();
type
=
((
VariableDescriptor
)
descriptor
).
getType
();
}
else
if
(
descriptor
instanceof
SimpleFunctionDescriptor
)
{
t
argetT
ype
=
((
SimpleFunctionDescriptor
)
descriptor
).
getReturnType
();
type
=
((
SimpleFunctionDescriptor
)
descriptor
).
getReturnType
();
}
else
{
assert
false
;
return
ErrorUtils
.
createErrorType
(
"unknown declaration type"
)
;
}
if
(
targetType
==
null
||
ErrorUtils
.
isErrorType
(
targetType
))
{
return
false
;
}
if
(
isDisabledForError
())
{
for
(
Diagnostic
diagnostic
:
bindingContext
.
getDiagnostics
())
{
if
(
Errors
.
PUBLIC_MEMBER_SHOULD_SPECIFY_TYPE
==
diagnostic
.
getFactory
()
&&
element
.
getParent
()
==
diagnostic
.
getPsiElement
())
{
return
false
;
}
}
}
return
true
;
return
type
==
null
?
ErrorUtils
.
createErrorType
(
"null type"
)
:
type
;
}
protected
boolean
isDisabledForError
()
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录