Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
ba60d323
R
roslyn
项目概览
lwm1986
/
roslyn
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
roslyn
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ba60d323
编写于
10月 20, 2018
作者:
C
Cyrus Najmabadi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add support for ranges.
上级
7bc67891
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
53 addition
and
17 deletion
+53
-17
src/Features/CSharp/Portable/UseIndexOperator/CSharpUseRangeOperatorCodeFixProvider.cs
...UseIndexOperator/CSharpUseRangeOperatorCodeFixProvider.cs
+13
-6
src/Features/CSharp/Portable/UseIndexOperator/CSharpUseRangeOperatorDiagnosticAnalyzer.cs
...IndexOperator/CSharpUseRangeOperatorDiagnosticAnalyzer.cs
+40
-11
未找到文件。
src/Features/CSharp/Portable/UseIndexOperator/CSharpUseRangeOperatorCodeFixProvider.cs
浏览文件 @
ba60d323
...
...
@@ -47,14 +47,21 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
Diagnostic
diagnostic
,
SyntaxEditor
editor
,
CancellationToken
cancellationToken
)
{
var
invocation
=
(
InvocationExpressionSyntax
)
diagnostic
.
Location
.
FindNode
(
getInnermostNodeForTie
:
true
,
cancellationToken
);
var
start
=
(
ExpressionSyntax
)
diagnostic
.
AdditionalLocations
[
0
].
FindNode
(
getInnermostNodeForTie
:
true
,
cancellationToken
);
var
end
=
(
ExpressionSyntax
)
diagnostic
.
AdditionalLocations
[
1
].
FindNode
(
getInnermostNodeForTie
:
true
,
cancellationToken
);
ExpressionSyntax
start
=
null
,
end
=
null
;
var
startFromEnd
=
diagnostic
.
Properties
.
ContainsKey
(
CSharpUseRangeOperatorDiagnosticAnalyzer
.
StartFromEnd
);
var
endFromEnd
=
diagnostic
.
Properties
.
ContainsKey
(
CSharpUseRangeOperatorDiagnosticAnalyzer
.
EndFromEnd
);
if
(!
diagnostic
.
Properties
.
ContainsKey
(
CSharpUseRangeOperatorDiagnosticAnalyzer
.
OmitStart
))
{
start
=(
ExpressionSyntax
)
diagnostic
.
AdditionalLocations
[
0
].
FindNode
(
getInnermostNodeForTie
:
true
,
cancellationToken
);
start
=
MakeIndexExpression
(
start
,
diagnostic
.
Properties
.
ContainsKey
(
CSharpUseRangeOperatorDiagnosticAnalyzer
.
StartFromEnd
));
}
start
=
MakeIndexExpression
(
start
,
startFromEnd
);
end
=
MakeIndexExpression
(
end
,
endFromEnd
);
if
(!
diagnostic
.
Properties
.
ContainsKey
(
CSharpUseRangeOperatorDiagnosticAnalyzer
.
OmitEnd
))
{
end
=
(
ExpressionSyntax
)
diagnostic
.
AdditionalLocations
[
1
].
FindNode
(
getInnermostNodeForTie
:
true
,
cancellationToken
);
end
=
MakeIndexExpression
(
end
,
diagnostic
.
Properties
.
ContainsKey
(
CSharpUseRangeOperatorDiagnosticAnalyzer
.
EndFromEnd
));
}
var
argList
=
invocation
.
ArgumentList
;
var
elementAccess
=
SyntaxFactory
.
ElementAccessExpression
(
...
...
src/Features/CSharp/Portable/UseIndexOperator/CSharpUseRangeOperatorDiagnosticAnalyzer.cs
浏览文件 @
ba60d323
...
...
@@ -15,6 +15,8 @@ internal class CSharpUseRangeOperatorDiagnosticAnalyzer : AbstractCodeStyleDiagn
{
public
const
string
StartFromEnd
=
nameof
(
StartFromEnd
);
public
const
string
EndFromEnd
=
nameof
(
EndFromEnd
);
public
const
string
OmitStart
=
nameof
(
OmitStart
);
public
const
string
OmitEnd
=
nameof
(
OmitEnd
);
public
CSharpUseRangeOperatorDiagnosticAnalyzer
()
:
base
(
IDEDiagnosticIds
.
UseRangeOperatorDiagnosticId
,
...
...
@@ -124,16 +126,28 @@ protected override void InitializeWorker(AnalysisContext context)
// var end = range.End.FromEnd ? array.Length - range.End.Value : range.End.Value;
var
properties
=
ImmutableDictionary
<
string
,
string
>.
Empty
;
if
(
IsFromEnd
(
invocation
.
Instance
,
startOperation
,
stringLength
))
if
(
IsFromEnd
(
stringLength
,
invocation
.
Instance
,
ref
startOperation
))
{
properties
=
properties
.
Add
(
StartFromEnd
,
StartFromEnd
);
}
if
(
IsFromEnd
(
invocation
.
Instance
,
endOperation
,
stringLength
))
if
(
IsFromEnd
(
stringLength
,
invocation
.
Instance
,
ref
endOperation
))
{
properties
=
properties
.
Add
(
EndFromEnd
,
EndFromEnd
);
}
if
(
IsInstanceLengthCheck
(
stringLength
,
invocation
.
Instance
,
endOperation
))
{
properties
=
properties
.
Add
(
OmitEnd
,
OmitEnd
);
}
if
(
startOperation
.
ConstantValue
.
HasValue
&&
startOperation
.
ConstantValue
.
Value
is
0
)
{
properties
=
properties
.
Add
(
OmitStart
,
OmitStart
);
}
var
additionalLocations
=
ImmutableArray
.
Create
(
startOperation
.
Syntax
.
GetLocation
(),
endOperation
.
Syntax
.
GetLocation
());
...
...
@@ -147,18 +161,33 @@ protected override void InitializeWorker(AnalysisContext context)
properties
));
}
private
bool
IsFromEnd
(
IOperation
instance
,
IOperation
rangeOperation
,
IPropertySymbol
stringLength
)
private
bool
IsFromEnd
(
IPropertySymbol
stringLength
,
IOperation
stringInstance
,
ref
IOperation
rangeOperation
)
{
var
syntaxFacts
=
CSharpSyntaxFactsService
.
Instance
;
// check if its the form: `stringExpr.Length - value`
return
rangeOperation
is
IBinaryOperation
binaryOperation
&&
if
(
rangeOperation
is
IBinaryOperation
binaryOperation
&&
binaryOperation
.
OperatorKind
==
BinaryOperatorKind
.
Subtract
&&
binaryOperation
.
LeftOperand
is
IPropertyReferenceOperation
leftPropertyRef
&&
stringLength
.
Equals
(
leftPropertyRef
.
Property
)
&&
leftPropertyRef
.
Instance
!=
null
&&
syntaxFacts
.
AreEquivalent
(
instance
.
Syntax
,
leftPropertyRef
.
Instance
.
Syntax
);
IsInstanceLengthCheck
(
stringLength
,
stringInstance
,
binaryOperation
.
LeftOperand
))
{
rangeOperation
=
binaryOperation
.
RightOperand
;
return
true
;
}
return
false
;
}
/// <summary>
/// Checks if this is an expression `expr.Length` where `expr` is equivalent to
/// the instance we were calling .Substring off of.
/// </summary>
private
bool
IsInstanceLengthCheck
(
IPropertySymbol
stringLength
,
IOperation
stringInstance
,
IOperation
operation
)
{
var
syntaxFacts
=
CSharpSyntaxFactsService
.
Instance
;
return
operation
is
IPropertyReferenceOperation
propertyRef
&&
stringLength
.
Equals
(
propertyRef
.
Property
)
&&
propertyRef
.
Instance
!=
null
&&
syntaxFacts
.
AreEquivalent
(
stringInstance
.
Syntax
,
propertyRef
.
Instance
.
Syntax
);
}
private
static
bool
IsStringIndexer
(
IPropertySymbol
property
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录