Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
5a9fecd7
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,发现更多精彩内容 >>
提交
5a9fecd7
编写于
1月 19, 2017
作者:
R
Remy Willems
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rabase onto post-dev15
上级
94c1483b
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
365 addition
and
24 deletion
+365
-24
src/EditorFeatures/CSharpTest/ImplementInterface/ImplementInterfaceTests.cs
.../CSharpTest/ImplementInterface/ImplementInterfaceTests.cs
+63
-6
src/EditorFeatures/Test/CodeGeneration/CodeGenerationTests.VisualBasic.cs
...es/Test/CodeGeneration/CodeGenerationTests.VisualBasic.cs
+107
-5
src/EditorFeatures/Test/CodeGeneration/CodeGenerationTests.cs
...EditorFeatures/Test/CodeGeneration/CodeGenerationTests.cs
+4
-4
src/EditorFeatures/VisualBasicTest/ImplementInterface/ImplementInterfaceTests.vb
...alBasicTest/ImplementInterface/ImplementInterfaceTests.vb
+45
-0
src/Features/Core/Portable/ImplementInterface/AbstractImplementInterfaceService.CodeAction.cs
...Interface/AbstractImplementInterfaceService.CodeAction.cs
+24
-3
src/Workspaces/CSharp/Portable/CodeGeneration/CSharpSyntaxGenerator.cs
...s/CSharp/Portable/CodeGeneration/CSharpSyntaxGenerator.cs
+13
-3
src/Workspaces/CSharpTest/CodeGeneration/SyntaxGeneratorTests.cs
...kspaces/CSharpTest/CodeGeneration/SyntaxGeneratorTests.cs
+16
-0
src/Workspaces/Core/Portable/Editing/SyntaxGenerator.cs
src/Workspaces/Core/Portable/Editing/SyntaxGenerator.cs
+10
-0
src/Workspaces/Core/Portable/PublicAPI.Unshipped.txt
src/Workspaces/Core/Portable/PublicAPI.Unshipped.txt
+2
-0
src/Workspaces/VisualBasic/Portable/CodeGeneration/EventGenerator.vb
...ces/VisualBasic/Portable/CodeGeneration/EventGenerator.vb
+60
-1
src/Workspaces/VisualBasic/Portable/CodeGeneration/VisualBasicSyntaxGenerator.vb
...sic/Portable/CodeGeneration/VisualBasicSyntaxGenerator.vb
+21
-2
未找到文件。
src/EditorFeatures/CSharpTest/ImplementInterface/ImplementInterfaceTests.cs
浏览文件 @
5a9fecd7
...
...
@@ -22,7 +22,7 @@ public partial class ImplementInterfaceTests : AbstractCSharpDiagnosticProviderB
internal
override
(
DiagnosticAnalyzer
,
CodeFixProvider
)
CreateDiagnosticProviderAndFixer
(
Workspace
workspace
)
=>
(
null
,
new
CSharpImplementInterfaceCodeFixProvider
());
private
static
readonly
Dictionary
<
OptionKey
,
object
>
A
llOptionsOff
=
private
static
readonly
Dictionary
<
OptionKey
,
object
>
s_a
llOptionsOff
=
new
Dictionary
<
OptionKey
,
object
>
{
{
CSharpCodeStyleOptions
.
PreferExpressionBodiedConstructors
,
CodeStyleOptions
.
FalseWithNoneEnforcement
},
...
...
@@ -33,7 +33,7 @@ internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProvider
{
CSharpCodeStyleOptions
.
PreferExpressionBodiedOperators
,
CodeStyleOptions
.
FalseWithNoneEnforcement
}
};
private
static
readonly
Dictionary
<
OptionKey
,
object
>
A
llOptionsOn
=
private
static
readonly
Dictionary
<
OptionKey
,
object
>
s_a
llOptionsOn
=
new
Dictionary
<
OptionKey
,
object
>
{
{
CSharpCodeStyleOptions
.
PreferExpressionBodiedConstructors
,
CodeStyleOptions
.
TrueWithNoneEnforcement
},
...
...
@@ -44,7 +44,7 @@ internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProvider
{
CSharpCodeStyleOptions
.
PreferExpressionBodiedOperators
,
CodeStyleOptions
.
TrueWithNoneEnforcement
}
};
private
static
readonly
Dictionary
<
OptionKey
,
object
>
A
ccessorOptionsOn
=
private
static
readonly
Dictionary
<
OptionKey
,
object
>
s_a
ccessorOptionsOn
=
new
Dictionary
<
OptionKey
,
object
>
{
{
CSharpCodeStyleOptions
.
PreferExpressionBodiedConstructors
,
CodeStyleOptions
.
FalseWithNoneEnforcement
},
...
...
@@ -62,7 +62,7 @@ internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProvider
bool
withScriptOption
=
false
)
{
await
TestAsync
(
initialMarkup
,
expectedMarkup
,
parseOptions
,
null
,
index
,
compareTokens
,
options
:
A
llOptionsOff
,
withScriptOption
:
withScriptOption
);
index
,
compareTokens
,
options
:
s_a
llOptionsOff
,
withScriptOption
:
withScriptOption
);
}
internal
async
Task
TestWithAllCodeStyleOptionsOnAsync
(
...
...
@@ -72,7 +72,7 @@ internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProvider
bool
withScriptOption
=
false
)
{
await
TestAsync
(
initialMarkup
,
expectedMarkup
,
parseOptions
,
null
,
index
,
compareTokens
,
options
:
A
llOptionsOn
,
withScriptOption
:
withScriptOption
);
index
,
compareTokens
,
options
:
s_a
llOptionsOn
,
withScriptOption
:
withScriptOption
);
}
internal
async
Task
TestWithAccessorCodeStyleOptionsOnAsync
(
...
...
@@ -82,7 +82,7 @@ internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProvider
bool
withScriptOption
=
false
)
{
await
TestAsync
(
initialMarkup
,
expectedMarkup
,
parseOptions
,
null
,
index
,
compareTokens
,
options
:
A
ccessorOptionsOn
,
withScriptOption
:
withScriptOption
);
index
,
compareTokens
,
options
:
s_a
ccessorOptionsOn
,
withScriptOption
:
withScriptOption
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsImplementInterface
)]
...
...
@@ -2453,6 +2453,63 @@ A a
count
:
2
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsImplementInterface
)]
public
async
Task
TestImplementEventThroughMember
()
{
await
TestAsync
(
@"
interface IFoo
{
event System.EventHandler E;
}
class CanFoo : IFoo
{
public event EventHandler E;
}
class HasCanFoo : [|IFoo|]
{
CanFoo canFoo;
}"
,
@"
using System;
interface IFoo
{
event System.EventHandler E;
}
class CanFoo : IFoo
{
public event EventHandler E;
}
class HasCanFoo : IFoo
{
CanFoo canFoo;
public event EventHandler E
{
add
{
((IFoo)canFoo).E += value;
}
remove
{
((IFoo)canFoo).E -= value;
}
}
}"
,
index
:
1
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsImplementInterface
)]
public
async
Task
TestImplementEventThroughExplicitMember
()
{
await
TestAsync
(
@"interface IFoo { event System . EventHandler E ; } class CanFoo : IFoo { event IFoo.EventHandler E; } class HasCanFoo : [|IFoo|] { CanFoo canFoo; } "
,
@"using System ; interface IFoo { event System . EventHandler E ; } class CanFoo : IFoo { event IFoo.EventHandler E; } class HasCanFoo : IFoo { CanFoo canFoo; public event EventHandler E { add { ((IFoo)canFoo).E += value; } remove { ((IFoo)canFoo).E -= value; } } } "
,
index
:
1
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsImplementInterface
)]
public
async
Task
TestImplementEvent
()
{
...
...
src/EditorFeatures/Test/CodeGeneration/CodeGenerationTests.VisualBasic.cs
浏览文件 @
5a9fecd7
...
...
@@ -7,15 +7,11 @@
using
System.Threading.Tasks
;
using
Microsoft.CodeAnalysis
;
using
Microsoft.CodeAnalysis.CodeGeneration
;
using
Microsoft.CodeAnalysis.CSharp
;
using
Microsoft.CodeAnalysis.Editing
;
using
Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
;
using
Microsoft.CodeAnalysis.Text
;
using
Microsoft.CodeAnalysis.VisualBasic
;
using
Microsoft.CodeAnalysis.VisualBasic.Syntax
;
using
Roslyn.Test.Utilities
;
using
Roslyn.Utilities
;
using
Xunit
;
using
CS
=
Microsoft
.
CodeAnalysis
.
CSharp
;
using
VB
=
Microsoft
.
CodeAnalysis
.
VisualBasic
;
namespace
Microsoft.CodeAnalysis.Editor.UnitTests.CodeGeneration
...
...
@@ -272,6 +268,112 @@ public async Task AddSealedDelegateType()
parameters
:
Parameters
(
Parameter
(
typeof
(
string
),
"s"
)));
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeGeneration
)]
public
async
Task
AddEvent
()
{
var
input
=
"Class [|C|] \n End Class"
;
var
expected
=
"Class C \n Public Event E As Action \n End Class"
;
await
TestAddEventAsync
(
input
,
expected
,
codeGenerationOptions
:
new
CodeGenerationOptions
(
addImports
:
false
));
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeGeneration
)]
public
async
Task
AddEventWithAccessorAndImplementsClause
()
{
var
input
=
"Class [|C|] \n End Class"
;
var
expected
=
@"
Class C
Public Custom Event E As ComponentModel.PropertyChangedEventHandler Implements ComponentModel.INotifyPropertyChanged.PropertyChanged
AddHandler ( value As ComponentModel . PropertyChangedEventHandler )
End AddHandler
RemoveHandler ( value As ComponentModel . PropertyChangedEventHandler )
End RemoveHandler
RaiseEvent ( sender As Object , e As ComponentModel . PropertyChangedEventArgs )
End RaiseEvent
End Event
End Class"
;
Func
<
SemanticModel
,
IEventSymbol
>
getExplicitInterfaceEvent
=
semanticModel
=>
{
var
parameterSymbols
=
SpecializedCollections
.
EmptyList
<
AttributeData
>();
return
new
CodeGenerationEventSymbol
(
GetTypeSymbol
(
typeof
(
System
.
ComponentModel
.
INotifyPropertyChanged
))(
semanticModel
),
null
,
Accessibility
.
Public
,
default
(
DeclarationModifiers
),
GetTypeSymbol
(
typeof
(
System
.
ComponentModel
.
PropertyChangedEventHandler
))(
semanticModel
),
null
,
nameof
(
System
.
ComponentModel
.
INotifyPropertyChanged
.
PropertyChanged
),
null
,
null
,
null
);
};
await
TestAddEventAsync
(
input
,
expected
,
addMethod
:
CodeGenerationSymbolFactory
.
CreateAccessorSymbol
(
SpecializedCollections
.
EmptyList
<
AttributeData
>(),
Accessibility
.
NotApplicable
,
SpecializedCollections
.
EmptyList
<
SyntaxNode
>()),
explicitInterfaceSymbol
:
getExplicitInterfaceEvent
,
type
:
typeof
(
System
.
ComponentModel
.
PropertyChangedEventHandler
),
codeGenerationOptions
:
new
CodeGenerationOptions
(
addImports
:
false
));
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeGeneration
)]
public
async
Task
AddEventWithAddAccessor
()
{
var
input
=
@"
Class [|C|]
End Class"
;
var
expected
=
@"
Class C
Public Custom Event E As Action
AddHandler(value As Action)
End AddHandler
RemoveHandler(value As Action)
End RemoveHandler
RaiseEvent()
End RaiseEvent
End Event
End Class"
;
await
TestAddEventAsync
(
input
,
expected
,
addMethod
:
CodeGenerationSymbolFactory
.
CreateAccessorSymbol
(
SpecializedCollections
.
EmptyList
<
AttributeData
>(),
Accessibility
.
NotApplicable
,
SpecializedCollections
.
EmptyList
<
SyntaxNode
>()),
codeGenerationOptions
:
new
CodeGenerationOptions
(
addImports
:
false
));
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeGeneration
)]
public
async
Task
AddEventWithAccessors
()
{
var
input
=
@"
Class [|C|]
End Class"
;
var
expected
=
@"
Class C
Public Custom Event E As Action
AddHandler(value As Action)
Console.WriteLine(0)
End AddHandler
RemoveHandler(value As Action)
Console.WriteLine(1)
End RemoveHandler
RaiseEvent()
Console.WriteLine(2)
End RaiseEvent
End Event
End Class"
;
var
addStatements
=
new
List
<
SyntaxNode
>()
{
VB
.
SyntaxFactory
.
ParseExecutableStatement
(
"Console.WriteLine(0)"
)
};
var
removeStatements
=
new
List
<
SyntaxNode
>()
{
VB
.
SyntaxFactory
.
ParseExecutableStatement
(
"Console.WriteLine(1)"
)
};
var
raiseStatements
=
new
List
<
SyntaxNode
>()
{
VB
.
SyntaxFactory
.
ParseExecutableStatement
(
"Console.WriteLine(2)"
)
};
await
TestAddEventAsync
(
input
,
expected
,
addMethod
:
CodeGenerationSymbolFactory
.
CreateAccessorSymbol
(
SpecializedCollections
.
EmptyList
<
AttributeData
>(),
Accessibility
.
NotApplicable
,
addStatements
),
removeMethod
:
CodeGenerationSymbolFactory
.
CreateAccessorSymbol
(
SpecializedCollections
.
EmptyList
<
AttributeData
>(),
Accessibility
.
NotApplicable
,
removeStatements
),
raiseMethod
:
CodeGenerationSymbolFactory
.
CreateAccessorSymbol
(
SpecializedCollections
.
EmptyList
<
AttributeData
>(),
Accessibility
.
NotApplicable
,
raiseStatements
),
codeGenerationOptions
:
new
CodeGenerationOptions
(
addImports
:
false
));
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeGeneration
)]
public
async
Task
AddMethodToClass
()
{
...
...
src/EditorFeatures/Test/CodeGeneration/CodeGenerationTests.cs
浏览文件 @
5a9fecd7
...
...
@@ -333,7 +333,7 @@ public partial class CodeGenerationTests
DeclarationModifiers
modifiers
=
default
(
DeclarationModifiers
),
IList
<
Func
<
SemanticModel
,
IParameterSymbol
>>
parameters
=
null
,
Type
type
=
null
,
IEventSymbol
explicitInterfaceSymbol
=
null
,
Func
<
SemanticModel
,
IEventSymbol
>
explicitInterfaceSymbol
=
null
,
IMethodSymbol
addMethod
=
null
,
IMethodSymbol
removeMethod
=
null
,
IMethodSymbol
raiseMethod
=
null
,
...
...
@@ -351,7 +351,7 @@ public partial class CodeGenerationTests
accessibility
,
modifiers
,
typeSymbol
,
explicitInterfaceSymbol
,
explicitInterfaceSymbol
?.
Invoke
(
context
.
SemanticModel
)
,
name
,
addMethod
,
removeMethod
,
...
...
@@ -664,12 +664,12 @@ private static IList<ISymbol> GetSymbols(IList<Func<SemanticModel, ISymbol>> mem
null
,
accessibility
,
modifiers
,
GetTypeSymbol
(
type
)(
s
),
name
);
}
private
static
Func
<
SemanticModel
,
ITypeSymbol
>
GetTypeSymbol
(
Type
type
)
private
static
Func
<
SemanticModel
,
I
Named
TypeSymbol
>
GetTypeSymbol
(
Type
type
)
{
return
GetTypeSymbol
(
type
.
FullName
);
}
private
static
Func
<
SemanticModel
,
ITypeSymbol
>
GetTypeSymbol
(
string
typeMetadataName
)
private
static
Func
<
SemanticModel
,
I
Named
TypeSymbol
>
GetTypeSymbol
(
string
typeMetadataName
)
{
return
s
=>
s
==
null
?
null
:
s
.
Compilation
.
GetTypeByMetadataName
(
typeMetadataName
);
}
...
...
src/EditorFeatures/VisualBasicTest/ImplementInterface/ImplementInterfaceTests.vb
浏览文件 @
5a9fecd7
...
...
@@ -1594,6 +1594,51 @@ Class C
End Class"
)
End
Function
<
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsImplementInterface
)
>
Public
Async
Function
TestImplementThroughMemberEvent
()
As
Task
Await
TestAsync
(
"
Imports System.ComponentModel
Class Worker
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
End Class
Class Boss
Implements [|INotifyPropertyChanged|]
Private worker As Worker
End Class"
,
"
Imports System.ComponentModel
Class Worker
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
End Class
Class Boss
Implements INotifyPropertyChanged
Private worker As Worker
Public Custom Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
AddHandler(value As PropertyChangedEventHandler)
AddHandler DirectCast(worker, INotifyPropertyChanged).PropertyChanged, value
End AddHandler
RemoveHandler(value As PropertyChangedEventHandler)
RemoveHandler DirectCast(worker, INotifyPropertyChanged).PropertyChanged, value
End RemoveHandler
RaiseEvent(sender As Object, e As PropertyChangedEventArgs)
End RaiseEvent
End Event
End Class"
,
index
:
=
1
)
End
Function
<
WorkItem
(
543588
,
"http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/543588"
)
>
<
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsImplementInterface
)
>
Public
Async
Function
TestNameSimplifyGenericType
()
As
Task
...
...
src/Features/Core/Portable/ImplementInterface/AbstractImplementInterfaceService.CodeAction.cs
浏览文件 @
5a9fecd7
...
...
@@ -389,7 +389,7 @@ private static bool IsUnexpressibleTypeParameter(ITypeParameterSymbol typeParame
var
useExplicitInterfaceSymbol
=
generateInvisibly
||
!
Service
.
CanImplementImplicitly
;
var
accessibility
=
member
.
Name
==
memberName
||
generateAbstractly
?
Accessibility
.
Public
?
Accessibility
.
Public
:
Accessibility
.
Private
;
if
(
member
.
Kind
==
SymbolKind
.
Method
)
...
...
@@ -419,13 +419,34 @@ private static bool IsUnexpressibleTypeParameter(ITypeParameterSymbol typeParame
modifiers
:
modifiers
,
explicitInterfaceSymbol
:
useExplicitInterfaceSymbol
?
@event
:
null
,
name
:
memberName
,
addMethod
:
generateInvisibly
?
accessor
:
null
,
removeMethod
:
generateInvisibly
?
accessor
:
null
);
addMethod
:
GetAddOrRemoveMethod
(
generateInvisibly
,
accessor
,
memberName
,
factory
.
AddEventHandler
)
,
removeMethod
:
GetAddOrRemoveMethod
(
generateInvisibly
,
accessor
,
memberName
,
factory
.
RemoveEventHandler
)
);
}
return
null
;
}
private
IMethodSymbol
GetAddOrRemoveMethod
(
bool
generateInvisibly
,
IMethodSymbol
accessor
,
string
memberName
,
Func
<
SyntaxNode
,
SyntaxNode
,
SyntaxNode
>
createAddOrRemoveHandler
)
{
if
(
ThroughMember
!=
null
)
{
var
factory
=
Document
.
GetLanguageService
<
SyntaxGenerator
>();
var
throughExpression
=
CreateThroughExpression
(
factory
);
var
statement
=
factory
.
ExpressionStatement
(
createAddOrRemoveHandler
(
factory
.
MemberAccessExpression
(
throughExpression
,
memberName
),
factory
.
IdentifierName
(
"value"
)));
return
CodeGenerationSymbolFactory
.
CreateAccessorSymbol
(
attributes
:
null
,
accessibility
:
Accessibility
.
NotApplicable
,
statements
:
SpecializedCollections
.
SingletonList
(
statement
));
}
return
generateInvisibly
?
accessor
:
null
;
}
private
SyntaxNode
CreateThroughExpression
(
SyntaxGenerator
factory
)
{
var
through
=
ThroughMember
.
IsStatic
...
...
src/Workspaces/CSharp/Portable/CodeGeneration/CSharpSyntaxGenerator.cs
浏览文件 @
5a9fecd7
...
...
@@ -2543,7 +2543,7 @@ private static TNode AddMissingTokens<TNode>(TNode node, bool recurse)
private
class
AddMissingTokensRewriter
:
CSharpSyntaxRewriter
{
private
readonly
bool
_recurse
;
private
bool
firstVisit
=
true
;
private
bool
_
firstVisit
=
true
;
public
AddMissingTokensRewriter
(
bool
recurse
)
{
...
...
@@ -2552,12 +2552,12 @@ public AddMissingTokensRewriter(bool recurse)
public
override
SyntaxNode
Visit
(
SyntaxNode
node
)
{
if
(!
_recurse
&&
!
firstVisit
)
if
(!
_recurse
&&
!
_
firstVisit
)
{
return
node
;
}
firstVisit
=
false
;
_
firstVisit
=
false
;
return
base
.
Visit
(
node
);
}
...
...
@@ -3454,6 +3454,16 @@ internal override bool IsRegularOrDocComment(SyntaxTrivia trivia)
#
region
Statements
and
Expressions
public
override
SyntaxNode
AddEventHandler
(
SyntaxNode
@event
,
SyntaxNode
handler
)
{
return
SyntaxFactory
.
AssignmentExpression
(
SyntaxKind
.
AddAssignmentExpression
,
(
ExpressionSyntax
)
@event
,
Parenthesize
(
handler
));
}
public
override
SyntaxNode
RemoveEventHandler
(
SyntaxNode
@event
,
SyntaxNode
handler
)
{
return
SyntaxFactory
.
AssignmentExpression
(
SyntaxKind
.
SubtractAssignmentExpression
,
(
ExpressionSyntax
)
@event
,
Parenthesize
(
handler
));
}
public
override
SyntaxNode
AwaitExpression
(
SyntaxNode
expression
)
{
return
SyntaxFactory
.
AwaitExpression
((
ExpressionSyntax
)
expression
);
...
...
src/Workspaces/CSharpTest/CodeGeneration/SyntaxGeneratorTests.cs
浏览文件 @
5a9fecd7
...
...
@@ -434,6 +434,22 @@ public void TestLocalDeclarationStatements()
VerifySyntax
<
LocalDeclarationStatementSyntax
>(
_g
.
LocalDeclarationStatement
(
"y"
,
_g
.
IdentifierName
(
"z"
)),
"var y = z;"
);
}
[
Fact
]
public
void
TestAddHandlerExpressions
()
{
VerifySyntax
<
AssignmentExpressionSyntax
>(
_g
.
AddEventHandler
(
_g
.
IdentifierName
(
"@event"
),
_g
.
IdentifierName
(
"handler"
)),
"@event += (handler)"
);
}
[
Fact
]
public
void
TestSubtractHandlerExpressions
()
{
VerifySyntax
<
AssignmentExpressionSyntax
>(
_g
.
RemoveEventHandler
(
_g
.
IdentifierName
(
"@event"
),
_g
.
IdentifierName
(
"handler"
)),
"@event -= (handler)"
);
}
[
Fact
]
public
void
TestAwaitExpressions
()
{
...
...
src/Workspaces/Core/Portable/Editing/SyntaxGenerator.cs
浏览文件 @
5a9fecd7
...
...
@@ -309,6 +309,16 @@ public SyntaxNode ParameterDeclaration(IParameterSymbol symbol, SyntaxNode initi
setAccessorStatements
);
}
/// <summary>
/// Creates a statement that adds the given handler to the given event.
/// </summary>
public
abstract
SyntaxNode
AddEventHandler
(
SyntaxNode
@event
,
SyntaxNode
handler
);
/// <summary>
/// Creates a statement that removes the given handler from the given event.
/// </summary>
public
abstract
SyntaxNode
RemoveEventHandler
(
SyntaxNode
@event
,
SyntaxNode
handler
);
/// <summary>
/// Creates an event declaration.
/// </summary>
...
...
src/Workspaces/Core/Portable/PublicAPI.Unshipped.txt
浏览文件 @
5a9fecd7
...
...
@@ -38,8 +38,10 @@ Microsoft.CodeAnalysis.Workspace.DocumentActiveContextChanged -> System.EventHan
Microsoft.CodeAnalysis.Workspace.RaiseDocumentActiveContextChangedEventAsync(Microsoft.CodeAnalysis.Text.SourceTextContainer sourceTextContainer, Microsoft.CodeAnalysis.DocumentId oldActiveContextDocumentId, Microsoft.CodeAnalysis.DocumentId newActiveContextDocumentId) -> System.Threading.Tasks.Task
Microsoft.CodeAnalysis.XmlDocumentationProvider
Microsoft.CodeAnalysis.XmlDocumentationProvider.XmlDocumentationProvider() -> void
abstract Microsoft.CodeAnalysis.Editing.SyntaxGenerator.AddEventHandler(Microsoft.CodeAnalysis.SyntaxNode event, Microsoft.CodeAnalysis.SyntaxNode handler) -> Microsoft.CodeAnalysis.SyntaxNode
abstract Microsoft.CodeAnalysis.Editing.SyntaxGenerator.GetSwitchSections(Microsoft.CodeAnalysis.SyntaxNode switchStatement) -> System.Collections.Generic.IReadOnlyList<Microsoft.CodeAnalysis.SyntaxNode>
abstract Microsoft.CodeAnalysis.Editing.SyntaxGenerator.InsertSwitchSections(Microsoft.CodeAnalysis.SyntaxNode switchStatement, int index, System.Collections.Generic.IEnumerable<Microsoft.CodeAnalysis.SyntaxNode> switchSections) -> Microsoft.CodeAnalysis.SyntaxNode
abstract Microsoft.CodeAnalysis.Editing.SyntaxGenerator.RemoveEventHandler(Microsoft.CodeAnalysis.SyntaxNode event, Microsoft.CodeAnalysis.SyntaxNode handler) -> Microsoft.CodeAnalysis.SyntaxNode
abstract Microsoft.CodeAnalysis.Editing.SyntaxGenerator.ThrowExpression(Microsoft.CodeAnalysis.SyntaxNode expression) -> Microsoft.CodeAnalysis.SyntaxNode
abstract Microsoft.CodeAnalysis.XmlDocumentationProvider.GetSourceStream(System.Threading.CancellationToken cancellationToken) -> System.IO.Stream
const Microsoft.CodeAnalysis.Tags.WellKnownTags.Assembly = "Assembly" -> string
...
...
src/Workspaces/VisualBasic/Portable/CodeGeneration/EventGenerator.vb
浏览文件 @
5a9fecd7
...
...
@@ -4,6 +4,7 @@ Imports System.Collections.Immutable
Imports
Microsoft.CodeAnalysis
Imports
Microsoft.CodeAnalysis.CodeGeneration
Imports
Microsoft.CodeAnalysis.CodeGeneration.CodeGenerationHelpers
Imports
Microsoft.CodeAnalysis.Editing
Imports
Microsoft.CodeAnalysis.VisualBasic.Symbols
Imports
Microsoft.CodeAnalysis.VisualBasic.Syntax
...
...
@@ -74,7 +75,65 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
Private
Function
GenerateEventDeclarationWorker
(
[
event
]
As
IEventSymbol
,
destination
As
CodeGenerationDestination
,
options
As
CodeGenerationOptions
)
As
DeclarationStatementSyntax
' TODO(cyrusn): Handle Add/Remove/Raise events
If
options
.
GenerateMethodBodies
AndAlso
(
[
event
]
.
AddMethod
IsNot
Nothing
OrElse
[
event
]
.
RemoveMethod
IsNot
Nothing
OrElse
[
event
]
.
RaiseMethod
IsNot
Nothing
)
Then
Return
GenerateCustomEventDeclarationWorker
(
[
event
]
,
destination
,
options
)
Else
Return
GenerateNotCustomEventDeclarationWorker
(
[
event
]
,
destination
,
options
)
End
If
End
Function
Private
Function
GenerateCustomEventDeclarationWorker
(
[
event
]
As
IEventSymbol
,
destination
As
CodeGenerationDestination
,
options
As
CodeGenerationOptions
)
As
DeclarationStatementSyntax
Dim
addStatements
=
If
(
[
event
]
.
AddMethod
Is
Nothing
,
New
SyntaxList
(
Of
StatementSyntax
),
GenerateStatements
(
[
event
]
.
AddMethod
))
Dim
removeStatements
=
If
(
[
event
]
.
RemoveMethod
Is
Nothing
,
New
SyntaxList
(
Of
StatementSyntax
),
GenerateStatements
(
[
event
]
.
RemoveMethod
))
Dim
raiseStatements
=
If
(
[
event
]
.
RaiseMethod
Is
Nothing
,
New
SyntaxList
(
Of
StatementSyntax
),
GenerateStatements
(
[
event
]
.
RaiseMethod
))
Dim
generator
As
VisualBasicSyntaxGenerator
=
New
VisualBasicSyntaxGenerator
()
Dim
invoke
=
DirectCast
(
[
event
]
.
Type
,
INamedTypeSymbol
)
?
.
DelegateInvokeMethod
Dim
parameters
=
If
(
invoke
IsNot
Nothing
,
invoke
.
Parameters
.
Select
(
Function
(
p
)
generator
.
ParameterDeclaration
(
p
)),
Nothing
)
Dim
result
=
DirectCast
(
generator
.
CustomEventDeclarationWithRaise
(
[
event
]
.
Name
,
generator
.
TypeExpression
(
[
event
]
.
Type
),
[
event
]
.
DeclaredAccessibility
,
DeclarationModifiers
.
From
(
[
event
]
),
parameters
,
addStatements
,
removeStatements
,
raiseStatements
),
EventBlockSyntax
)
result
=
DirectCast
(
result
.
WithAttributeLists
(
GenerateAttributeBlocks
(
[
event
]
.
GetAttributes
(),
options
)),
EventBlockSyntax
)
result
=
DirectCast
(
result
.
WithModifiers
(
GenerateModifiers
(
[
event
]
,
destination
,
options
)),
EventBlockSyntax
)
Dim
explicitInterface
=
[
event
]
.
ExplicitInterfaceImplementations
.
FirstOrDefault
()
If
(
explicitInterface
IsNot
Nothing
)
result
=
result
.
WithEventStatement
(
result
.
EventStatement
.
WithImplementsClause
(
GenerateImplementsClause
(
explicitInterface
)))
End
If
Return
result
End
Function
Private
Function
GenerateNotCustomEventDeclarationWorker
(
[
event
]
As
IEventSymbol
,
destination
As
CodeGenerationDestination
,
options
As
CodeGenerationOptions
)
As
EventStatementSyntax
Dim
eventType
=
TryCast
(
[
event
]
.
Type
,
INamedTypeSymbol
)
If
eventType
.
IsDelegateType
()
AndAlso
eventType
.
AssociatedSymbol
IsNot
Nothing
Then
' This is a declaration style event like "Event E(x As String)". This event will
...
...
src/Workspaces/VisualBasic/Portable/CodeGeneration/VisualBasicSyntaxGenerator.vb
浏览文件 @
5a9fecd7
...
...
@@ -22,6 +22,14 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
#Region "Expressions and Statements"
Public
Overrides
Function
AddEventHandler
(
[
event
]
As
SyntaxNode
,
handler
As
SyntaxNode
)
As
SyntaxNode
Return
SyntaxFactory
.
AddHandlerStatement
(
CType
(
[
event
]
,
ExpressionSyntax
),
CType
(
handler
,
ExpressionSyntax
))
End
Function
Public
Overrides
Function
RemoveEventHandler
(
[
event
]
As
SyntaxNode
,
handler
As
SyntaxNode
)
As
SyntaxNode
Return
SyntaxFactory
.
RemoveHandlerStatement
(
CType
(
[
event
]
,
ExpressionSyntax
),
CType
(
handler
,
ExpressionSyntax
))
End
Function
Public
Overrides
Function
AwaitExpression
(
expression
As
SyntaxNode
)
As
SyntaxNode
Return
SyntaxFactory
.
AwaitExpression
(
DirectCast
(
expression
,
ExpressionSyntax
))
End
Function
...
...
@@ -3415,16 +3423,27 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
End
Function
Public
Overrides
Function
CustomEventDeclaration
(
name
As
String
,
type
As
SyntaxNode
,
Optional
accessibility
As
Accessibility
=
Accessibility
.
NotApplicable
,
Optional
modifiers
As
DeclarationModifiers
=
Nothing
,
Optional
parameters
As
IEnumerable
(
Of
SyntaxNode
)
=
Nothing
,
Optional
addAccessorStatements
As
IEnumerable
(
Of
SyntaxNode
)
=
Nothing
,
Optional
removeAccessorStatements
As
IEnumerable
(
Of
SyntaxNode
)
=
Nothing
)
As
SyntaxNode
Return
CustomEventDeclarationWithRaise
(
name
,
type
,
accessibility
,
modifiers
,
parameters
,
addAccessorStatements
,
removeAccessorStatements
)
End
Function
Public
Function
CustomEventDeclarationWithRaise
(
name
As
String
,
type
As
SyntaxNode
,
Optional
accessibility
As
Accessibility
=
Accessibility
.
NotApplicable
,
Optional
modifiers
As
DeclarationModifiers
=
Nothing
,
Optional
parameters
As
IEnumerable
(
Of
SyntaxNode
)
=
Nothing
,
Optional
addAccessorStatements
As
IEnumerable
(
Of
SyntaxNode
)
=
Nothing
,
Optional
removeAccessorStatements
As
IEnumerable
(
Of
SyntaxNode
)
=
Nothing
)
As
SyntaxNode
Optional
removeAccessorStatements
As
IEnumerable
(
Of
SyntaxNode
)
=
Nothing
,
Optional
raiseAccessorStatements
As
IEnumerable
(
Of
SyntaxNode
)
=
Nothing
)
As
SyntaxNode
Dim
accessors
=
New
List
(
Of
AccessorBlockSyntax
)()
Dim
raiseAccessorStatements
As
IEnumerable
(
Of
SyntaxNode
)
=
Nothing
If
modifiers
.
IsAbstract
Then
addAccessorStatements
=
Nothing
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录