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