未验证 提交 a3bcda86 编写于 作者: J Jinu 提交者: GitHub

Merge pull request #25574 from Neme12/optionPreview

Allowing multiple exposed line spans in VS style options preview
...@@ -232,12 +232,16 @@ class C ...@@ -232,12 +232,16 @@ class C
{{ {{
private string s; private string s;
public C(string s) void M1(string s)
{{ {{
//[ //[
// {ServicesVSResources.Prefer_colon} // {ServicesVSResources.Prefer_colon}
this.s = s ?? throw new ArgumentNullException(nameof(s)); this.s = s ?? throw new ArgumentNullException(nameof(s));
//]
}}
void M2(string s)
{{
//[
// {ServicesVSResources.Over_colon} // {ServicesVSResources.Over_colon}
if (s == null) if (s == null)
{{ {{
...@@ -257,12 +261,16 @@ class C ...@@ -257,12 +261,16 @@ class C
{{ {{
private string s; private string s;
public C(string s) void M1(string s)
{{ {{
//[ //[
// {ServicesVSResources.Prefer_colon} // {ServicesVSResources.Prefer_colon}
var v = x ?? y; var v = x ?? y;
//]
}}
void M2(string s)
{{
//[
// {ServicesVSResources.Over_colon} // {ServicesVSResources.Over_colon}
var v = x != null ? x : y; // {ServicesVSResources.or} var v = x != null ? x : y; // {ServicesVSResources.or}
var v = x == null ? y : x; var v = x == null ? y : x;
...@@ -278,12 +286,16 @@ class C ...@@ -278,12 +286,16 @@ class C
{{ {{
private string s; private string s;
public C(string s) void M1(string s)
{{ {{
//[ //[
// {ServicesVSResources.Prefer_colon} // {ServicesVSResources.Prefer_colon}
func?.Invoke(args); func?.Invoke(args);
//]
}}
void M2(string s)
{{
//[
// {ServicesVSResources.Over_colon} // {ServicesVSResources.Over_colon}
if (func != null) if (func != null)
{{ {{
...@@ -299,12 +311,16 @@ public C(string s) ...@@ -299,12 +311,16 @@ public C(string s)
class C class C
{{ {{
public C(object o) void M1(object o)
{{ {{
//[ //[
// {ServicesVSResources.Prefer_colon} // {ServicesVSResources.Prefer_colon}
var v = o?.ToString(); var v = o?.ToString();
//]
}}
void M2(object o)
{{
//[
// {ServicesVSResources.Over_colon} // {ServicesVSResources.Over_colon}
var v = o == null ? null : o.ToString(); // {ServicesVSResources.or} var v = o == null ? null : o.ToString(); // {ServicesVSResources.or}
var v = o != null ? o.ToString() : null; var v = o != null ? o.ToString() : null;
...@@ -316,14 +332,18 @@ public C(object o) ...@@ -316,14 +332,18 @@ public C(object o)
private static readonly string s_preferPatternMatchingOverAsWithNullCheck = $@" private static readonly string s_preferPatternMatchingOverAsWithNullCheck = $@"
class C class C
{{ {{
void M() void M1()
{{ {{
//[ //[
// {ServicesVSResources.Prefer_colon} // {ServicesVSResources.Prefer_colon}
if (o is string s) if (o is string s)
{{ {{
}} }}
//]
}}
void M2()
{{
//[
// {ServicesVSResources.Over_colon} // {ServicesVSResources.Over_colon}
var s = o as string; var s = o as string;
if (s != null) if (s != null)
...@@ -337,14 +357,18 @@ void M() ...@@ -337,14 +357,18 @@ void M()
private static readonly string s_preferPatternMatchingOverIsWithCastCheck = $@" private static readonly string s_preferPatternMatchingOverIsWithCastCheck = $@"
class C class C
{{ {{
void M() void M1()
{{ {{
//[ //[
// {ServicesVSResources.Prefer_colon} // {ServicesVSResources.Prefer_colon}
if (o is int i) if (o is int i)
{{ {{
}} }}
//]
}}
void M2()
{{
//[
// {ServicesVSResources.Over_colon} // {ServicesVSResources.Over_colon}
if (o is int) if (o is int)
{{ {{
...@@ -362,7 +386,7 @@ class Customer ...@@ -362,7 +386,7 @@ class Customer
{{ {{
private int Age; private int Age;
public Customer() void M1()
{{ {{
//[ //[
// {ServicesVSResources.Prefer_colon} // {ServicesVSResources.Prefer_colon}
...@@ -370,7 +394,11 @@ public Customer() ...@@ -370,7 +394,11 @@ public Customer()
{{ {{
Age = 21 Age = 21
}}; }};
//]
}}
void M2()
{{
//[
// {ServicesVSResources.Over_colon} // {ServicesVSResources.Over_colon}
var c = new Customer(); var c = new Customer();
c.Age = 21; c.Age = 21;
...@@ -386,7 +414,7 @@ class Customer ...@@ -386,7 +414,7 @@ class Customer
{{ {{
private int Age; private int Age;
public Customer() void M1()
{{ {{
//[ //[
// {ServicesVSResources.Prefer_colon} // {ServicesVSResources.Prefer_colon}
...@@ -396,7 +424,11 @@ public Customer() ...@@ -396,7 +424,11 @@ public Customer()
2, 2,
3 3
}}; }};
//]
}}
void M2()
{{
//[
// {ServicesVSResources.Over_colon} // {ServicesVSResources.Over_colon}
var list = new List<int>(); var list = new List<int>();
list.Add(1); list.Add(1);
...@@ -410,14 +442,18 @@ public Customer() ...@@ -410,14 +442,18 @@ public Customer()
private static readonly string s_preferExplicitTupleName = $@" private static readonly string s_preferExplicitTupleName = $@"
class Customer class Customer
{{ {{
public Customer() void M1()
{{ {{
//[ //[
// {ServicesVSResources.Prefer_colon} // {ServicesVSResources.Prefer_colon}
(string name, int age) customer = GetCustomer(); (string name, int age) customer = GetCustomer();
var name = customer.name; var name = customer.name;
var age = customer.age; var age = customer.age;
//]
}}
void M2()
{{
//[
// {ServicesVSResources.Over_colon} // {ServicesVSResources.Over_colon}
(string name, int age) customer = GetCustomer(); (string name, int age) customer = GetCustomer();
var name = customer.Item1; var name = customer.Item1;
...@@ -430,12 +466,16 @@ public Customer() ...@@ -430,12 +466,16 @@ public Customer()
private static readonly string s_preferSimpleDefaultExpression = $@" private static readonly string s_preferSimpleDefaultExpression = $@"
using System.Threading; using System.Threading;
class Customer class Customer1
{{ {{
//[ //[
// {ServicesVSResources.Prefer_colon} // {ServicesVSResources.Prefer_colon}
void DoWork(CancellationToken cancellationToken = default) {{ }} void DoWork(CancellationToken cancellationToken = default) {{ }}
//]
}}
class Customer2
{{
//[
// {ServicesVSResources.Over_colon} // {ServicesVSResources.Over_colon}
void DoWork(CancellationToken cancellationToken = default(CancellationToken)) {{ }} void DoWork(CancellationToken cancellationToken = default(CancellationToken)) {{ }}
//] //]
...@@ -447,12 +487,16 @@ class Customer ...@@ -447,12 +487,16 @@ class Customer
class Customer class Customer
{{ {{
public Customer(int age, string name) void M1(int age, string name)
{{ {{
//[ //[
// {ServicesVSResources.Prefer_colon} // {ServicesVSResources.Prefer_colon}
var tuple = (age, name); var tuple = (age, name);
//]
}}
void M2(int age, string name)
{{
//[
// {ServicesVSResources.Over_colon} // {ServicesVSResources.Over_colon}
var tuple = (age: age, name: name); var tuple = (age: age, name: name);
//] //]
...@@ -465,12 +509,16 @@ public Customer(int age, string name) ...@@ -465,12 +509,16 @@ public Customer(int age, string name)
class Customer class Customer
{{ {{
public Customer(int age, string name) void M1(int age, string name)
{{ {{
//[ //[
// {ServicesVSResources.Prefer_colon} // {ServicesVSResources.Prefer_colon}
var anon = new {{ age, name }}; var anon = new {{ age, name }};
//]
}}
void M2(int age, string name)
{{
//[
// {ServicesVSResources.Over_colon} // {ServicesVSResources.Over_colon}
var anon = new {{ age = age, name = name }}; var anon = new {{ age = age, name = name }};
//] //]
...@@ -483,14 +531,18 @@ public Customer(int age, string name) ...@@ -483,14 +531,18 @@ public Customer(int age, string name)
class Customer class Customer
{{ {{
public Customer(string value) void M1(string value)
{{ {{
//[ //[
// {ServicesVSResources.Prefer_colon} // {ServicesVSResources.Prefer_colon}
if (int.TryParse(value, out int i)) if (int.TryParse(value, out int i))
{{ {{
}} }}
//]
}}
void M2(string value)
{{
//[
// {ServicesVSResources.Over_colon} // {ServicesVSResources.Over_colon}
int i; int i;
if (int.TryParse(value, out i)) if (int.TryParse(value, out i))
...@@ -506,7 +558,7 @@ public Customer(string value) ...@@ -506,7 +558,7 @@ public Customer(string value)
class Customer class Customer
{{ {{
public Customer(string value) void M1(string value)
{{ {{
//[ //[
// {ServicesVSResources.Prefer_colon} // {ServicesVSResources.Prefer_colon}
...@@ -515,7 +567,11 @@ public Customer(string value) ...@@ -515,7 +567,11 @@ public Customer(string value)
(int x, int y) = GetPointTuple(); (int x, int y) = GetPointTuple();
Console.WriteLine($""{{x}} {{y}}""); Console.WriteLine($""{{x}} {{y}}"");
//]
}}
void M2(string value)
{{
//[
// {ServicesVSResources.Over_colon} // {ServicesVSResources.Over_colon}
var person = GetPersonTuple(); var person = GetPersonTuple();
Console.WriteLine($""{{person.name}} {{person.age}}""); Console.WriteLine($""{{person.name}} {{person.age}}"");
...@@ -534,7 +590,7 @@ class Customer ...@@ -534,7 +590,7 @@ class Customer
{{ {{
private int Age; private int Age;
public int GetAge() void M1()
{{ {{
//[ //[
// {ServicesVSResources.Prefer_colon} // {ServicesVSResources.Prefer_colon}
...@@ -542,7 +598,11 @@ public int GetAge() ...@@ -542,7 +598,11 @@ public int GetAge()
{{ {{
this.Display(); this.Display();
}} }}
//]
}}
void M2()
{{
//[
// {ServicesVSResources.Over_colon} // {ServicesVSResources.Over_colon}
if (test) if (test)
this.Display(); this.Display();
...@@ -554,12 +614,16 @@ public int GetAge() ...@@ -554,12 +614,16 @@ public int GetAge()
private static readonly string s_preferAutoProperties = $@" private static readonly string s_preferAutoProperties = $@"
using System; using System;
class Customer class Customer1
{{ {{
//[ //[
// {ServicesVSResources.Prefer_colon} // {ServicesVSResources.Prefer_colon}
public int Age {{ get; }} public int Age {{ get; }}
//]
}}
class Customer2
{{
//[
// {ServicesVSResources.Over_colon} // {ServicesVSResources.Over_colon}
private int age; private int age;
...@@ -579,7 +643,7 @@ public int Age ...@@ -579,7 +643,7 @@ public int Age
class Customer class Customer
{{ {{
public Customer(string value) void M1(string value)
{{ {{
//[ //[
// {ServicesVSResources.Prefer_colon} // {ServicesVSResources.Prefer_colon}
...@@ -587,7 +651,11 @@ int fibonacci(int n) ...@@ -587,7 +651,11 @@ int fibonacci(int n)
{{ {{
return n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2); return n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);
}} }}
//]
}}
void M2(string value)
{{
//[
// {ServicesVSResources.Over_colon} // {ServicesVSResources.Over_colon}
Func<int, int> fibonacci = null; Func<int, int> fibonacci = null;
fibonacci = (int n) => fibonacci = (int n) =>
...@@ -604,13 +672,17 @@ int fibonacci(int n) ...@@ -604,13 +672,17 @@ int fibonacci(int n)
class Customer class Customer
{{ {{
public Customer(string value) void M1(string value)
{{ {{
//[ //[
// {ServicesVSResources.Prefer_colon} // {ServicesVSResources.Prefer_colon}
if (value is null) if (value is null)
return; return;
//]
}}
void M2(string value)
{{
//[
// {ServicesVSResources.Over_colon} // {ServicesVSResources.Over_colon}
if (object.ReferenceEquals(value, null)) if (object.ReferenceEquals(value, null))
return; return;
...@@ -784,13 +856,17 @@ class List<T> ...@@ -784,13 +856,17 @@ class List<T>
"; ";
private static readonly string s_preferReadonly = $@" private static readonly string s_preferReadonly = $@"
class Customer class Customer1
{{ {{
//[ //[
// {ServicesVSResources.Prefer_colon} // {ServicesVSResources.Prefer_colon}
// '_value' can only be assigned in constructor // '_value' can only be assigned in constructor
private readonly int _value = 0; private readonly int _value = 0;
//]
}}
class Customer2
{{
//[
// {ServicesVSResources.Over_colon} // {ServicesVSResources.Over_colon}
// '_value' can be assigned anywhere // '_value' can be assigned anywhere
private int _value = 0; private int _value = 0;
......
...@@ -128,9 +128,6 @@ private set ...@@ -128,9 +128,6 @@ private set
public void UpdatePreview(string text) public void UpdatePreview(string text)
{ {
const string start = "//[";
const string end = "//]";
var service = MefV1HostServices.Create(_componentModel.DefaultExportProvider); var service = MefV1HostServices.Create(_componentModel.DefaultExportProvider);
var workspace = new PreviewWorkspace(service); var workspace = new PreviewWorkspace(service);
var fileName = string.Format("project.{0}", Language == "C#" ? "csproj" : "vbproj"); var fileName = string.Format("project.{0}", Language == "C#" ? "csproj" : "vbproj");
...@@ -160,17 +157,11 @@ public void UpdatePreview(string text) ...@@ -160,17 +157,11 @@ public void UpdatePreview(string text)
var container = textBuffer.AsTextContainer(); var container = textBuffer.AsTextContainer();
var documentBackedByTextBuffer = document.WithText(container.CurrentText); var documentBackedByTextBuffer = document.WithText(container.CurrentText);
var bufferText = textBuffer.CurrentSnapshot.GetText().ToString();
var startIndex = bufferText.IndexOf(start, StringComparison.Ordinal);
var endIndex = bufferText.IndexOf(end, StringComparison.Ordinal);
var startLine = textBuffer.CurrentSnapshot.GetLineNumberFromPosition(startIndex) + 1;
var endLine = textBuffer.CurrentSnapshot.GetLineNumberFromPosition(endIndex);
var projection = _projectionBufferFactory.CreateProjectionBufferWithoutIndentation(_contentTypeRegistryService, var projection = _projectionBufferFactory.CreateProjectionBufferWithoutIndentation(_contentTypeRegistryService,
_editorOptions.CreateOptions(), _editorOptions.CreateOptions(),
textBuffer.CurrentSnapshot, textBuffer.CurrentSnapshot,
"", separator: "",
LineSpan.FromBounds(startLine, endLine)); exposedLineSpans: GetExposedLineSpans(textBuffer.CurrentSnapshot).ToArray());
var textView = _textEditorFactoryService.CreateTextView(projection, var textView = _textEditorFactoryService.CreateTextView(projection,
_textEditorFactoryService.CreateTextViewRoleSet(PredefinedTextViewRoles.Interactive)); _textEditorFactoryService.CreateTextViewRoleSet(PredefinedTextViewRoles.Interactive));
...@@ -187,6 +178,36 @@ public void UpdatePreview(string text) ...@@ -187,6 +178,36 @@ public void UpdatePreview(string text)
}; };
} }
private static List<LineSpan> GetExposedLineSpans(ITextSnapshot textSnapshot)
{
const string start = "//[";
const string end = "//]";
var bufferText = textSnapshot.GetText().ToString();
var lineSpans = new List<LineSpan>();
var lastEndIndex = 0;
while (true)
{
var startIndex = bufferText.IndexOf(start, lastEndIndex, StringComparison.Ordinal);
if (startIndex == -1)
{
break;
}
var endIndex = bufferText.IndexOf(end, lastEndIndex, StringComparison.Ordinal);
var startLine = textSnapshot.GetLineNumberFromPosition(startIndex) + 1;
var endLine = textSnapshot.GetLineNumberFromPosition(endIndex);
lineSpans.Add(LineSpan.FromBounds(startLine, endLine));
lastEndIndex = endIndex + end.Length;
}
return lineSpans;
}
public void Dispose() public void Dispose()
{ {
if (_textViewHost != null) if (_textViewHost != null)
......
...@@ -152,13 +152,16 @@ Imports System ...@@ -152,13 +152,16 @@ Imports System
Class Customer Class Customer
Private Age As Integer Private Age As Integer
Sub New() Sub M1()
//[ //[
' {ServicesVSResources.Prefer_colon} ' {ServicesVSResources.Prefer_colon}
Dim c = New Customer() With {{ Dim c = New Customer() With {{
.Age = 21 .Age = 21
}} }}
//]
End Sub
Sub M2()
//[
' {ServicesVSResources.Over_colon} ' {ServicesVSResources.Over_colon}
Dim c = New Customer() Dim c = New Customer()
c.Age = 21 c.Age = 21
...@@ -171,7 +174,7 @@ End Class" ...@@ -171,7 +174,7 @@ End Class"
Class Customer Class Customer
Private Age As Integer Private Age As Integer
Sub New() Sub M1()
//[ //[
' {ServicesVSResources.Prefer_colon} ' {ServicesVSResources.Prefer_colon}
Dim list = New List(Of Integer) From {{ Dim list = New List(Of Integer) From {{
...@@ -179,7 +182,10 @@ Class Customer ...@@ -179,7 +182,10 @@ Class Customer
2, 2,
3 3
}} }}
//]
End Sub
Sub M2()
//[
' {ServicesVSResources.Over_colon} ' {ServicesVSResources.Over_colon}
Dim list = New List(Of Integer)() Dim list = New List(Of Integer)()
list.Add(1) list.Add(1)
...@@ -191,13 +197,16 @@ End Class" ...@@ -191,13 +197,16 @@ End Class"
Private Shared ReadOnly s_preferExplicitTupleName As String = $" Private Shared ReadOnly s_preferExplicitTupleName As String = $"
Class Customer Class Customer
Public Sub New() Sub M1()
//[ //[
' {ServicesVSResources.Prefer_colon} ' {ServicesVSResources.Prefer_colon}
Dim customer As (name As String, age As Integer) Dim customer As (name As String, age As Integer)
Dim name = customer.name Dim name = customer.name
Dim age = customer.age Dim age = customer.age
//]
End Sub
Sub M2()
//[
' {ServicesVSResources.Over_colon} ' {ServicesVSResources.Over_colon}
Dim customer As (name As String, age As Integer) Dim customer As (name As String, age As Integer)
Dim name = customer.Item1 Dim name = customer.Item1
...@@ -209,11 +218,14 @@ end class ...@@ -209,11 +218,14 @@ end class
Private Shared ReadOnly s_preferInferredTupleName As String = $" Private Shared ReadOnly s_preferInferredTupleName As String = $"
Class Customer Class Customer
Public Sub New(name as String, age As Integer) Sub M1(name as String, age As Integer)
//[ //[
' {ServicesVSResources.Prefer_colon} ' {ServicesVSResources.Prefer_colon}
Dim tuple = (name, age) Dim tuple = (name, age)
//]
End Sub
Sub M2(name as String, age As Integer)
//[
' {ServicesVSResources.Over_colon} ' {ServicesVSResources.Over_colon}
Dim tuple = (name:=name, age:=age) Dim tuple = (name:=name, age:=age)
//] //]
...@@ -223,11 +235,14 @@ end class ...@@ -223,11 +235,14 @@ end class
Private Shared ReadOnly s_preferInferredAnonymousTypeMemberName As String = $" Private Shared ReadOnly s_preferInferredAnonymousTypeMemberName As String = $"
Class Customer Class Customer
Public Sub New(name as String, age As Integer) Sub M1(name as String, age As Integer)
//[ //[
' {ServicesVSResources.Prefer_colon} ' {ServicesVSResources.Prefer_colon}
Dim anon = New With {{ name, age }} Dim anon = New With {{ name, age }}
//]
End Sub
Sub M2(name as String, age As Integer)
//[
' {ServicesVSResources.Over_colon} ' {ServicesVSResources.Over_colon}
Dim anon = New With {{ .name = name, .age = age }} Dim anon = New With {{ .name = name, .age = age }}
//] //]
...@@ -241,11 +256,14 @@ Imports System ...@@ -241,11 +256,14 @@ Imports System
Class Customer Class Customer
Private Age As Integer Private Age As Integer
Sub New() Sub M1()
//[ //[
' {ServicesVSResources.Prefer_colon} ' {ServicesVSResources.Prefer_colon}
Dim v = If(x, y) Dim v = If(x, y)
//]
End Sub
Sub M2()
//[
' {ServicesVSResources.Over_colon} ' {ServicesVSResources.Over_colon}
Dim v = If(x Is Nothing, y, x) ' {ServicesVSResources.or} Dim v = If(x Is Nothing, y, x) ' {ServicesVSResources.or}
Dim v = If(x IsNot Nothing, x, y) Dim v = If(x IsNot Nothing, x, y)
...@@ -259,11 +277,14 @@ Imports System ...@@ -259,11 +277,14 @@ Imports System
Class Customer Class Customer
Private Age As Integer Private Age As Integer
Sub New() Sub M1()
//[ //[
' {ServicesVSResources.Prefer_colon} ' {ServicesVSResources.Prefer_colon}
Dim v = o?.ToString() Dim v = o?.ToString()
//]
End Sub
Sub M2()
//[
' {ServicesVSResources.Over_colon} ' {ServicesVSResources.Over_colon}
Dim v = If(o Is Nothing, Nothing, o.ToString()) ' {ServicesVSResources.or} Dim v = If(o Is Nothing, Nothing, o.ToString()) ' {ServicesVSResources.or}
Dim v = If(o IsNot Nothing, o.ToString(), Nothing) Dim v = If(o IsNot Nothing, o.ToString(), Nothing)
...@@ -274,11 +295,14 @@ End Class" ...@@ -274,11 +295,14 @@ End Class"
Private Shared ReadOnly s_preferAutoProperties As String = $" Private Shared ReadOnly s_preferAutoProperties As String = $"
Imports System Imports System
Class Customer Class Customer1
//[ //[
' {ServicesVSResources.Prefer_colon} ' {ServicesVSResources.Prefer_colon}
Public ReadOnly Property Age As Integer Public ReadOnly Property Age As Integer
//]
End Class
Class Customer2
//[
' {ServicesVSResources.Over_colon} ' {ServicesVSResources.Over_colon}
Private _age As Integer Private _age As Integer
...@@ -295,13 +319,16 @@ End Class ...@@ -295,13 +319,16 @@ End Class
Imports System Imports System
Class Customer Class Customer
Sub New(value as object) Sub M1(value as object)
//[ //[
' {ServicesVSResources.Prefer_colon} ' {ServicesVSResources.Prefer_colon}
If value Is Nothing If value Is Nothing
Return Return
End If End If
//]
End Sub
Sub M2(value as object)
//[
' {ServicesVSResources.Over_colon} ' {ServicesVSResources.Over_colon}
If Object.ReferenceEquals(value, Nothing) If Object.ReferenceEquals(value, Nothing)
Return Return
...@@ -311,12 +338,15 @@ Class Customer ...@@ -311,12 +338,15 @@ Class Customer
End Class" End Class"
Private Shared ReadOnly s_preferReadonly As String = $" Private Shared ReadOnly s_preferReadonly As String = $"
Class Customer Class Customer1
//[ //[
' {ServicesVSResources.Prefer_colon} ' {ServicesVSResources.Prefer_colon}
' 'value' can only be assigned in constructor ' 'value' can only be assigned in constructor
Private ReadOnly value As Integer = 0 Private ReadOnly value As Integer = 0
//]
End Class
Class Customer2
//[
' {ServicesVSResources.Over_colon} ' {ServicesVSResources.Over_colon}
' 'value' can be assigned anywhere ' 'value' can be assigned anywhere
Private value As Integer = 0 Private value As Integer = 0
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册