提交 1e20b0a3 编写于 作者: C Cyrus Najmabadi

Use simple usings

上级 21272885
......@@ -580,12 +580,12 @@ private async Task<XElement> TryDownloadFileAsync(IRemoteControlClient client)
DtdProcessing = DtdProcessing.Prohibit,
XmlResolver = null
};
using (var reader = XmlReader.Create(stream, settings))
{
var result = XElement.Load(reader);
await _service.LogInfoAsync("Converting data to XElement completed").ConfigureAwait(false);
return result;
}
using var reader = XmlReader.Create(stream, settings);
var result = XElement.Load(reader);
await _service.LogInfoAsync("Converting data to XElement completed").ConfigureAwait(false);
return result;
}
}
......
......@@ -49,72 +49,71 @@ public bool ExecuteCommand(EncapsulateFieldCommandArgs args, CommandExecutionCon
private bool Execute(EncapsulateFieldCommandArgs args, IUIThreadOperationScope waitScope)
{
using (var token = _listener.BeginAsyncOperation("EncapsulateField"))
using var token = _listener.BeginAsyncOperation("EncapsulateField");
var cancellationToken = waitScope.Context.UserCancellationToken;
var document = args.SubjectBuffer.CurrentSnapshot.GetFullyLoadedOpenDocumentInCurrentContextWithChangesAsync(
waitScope.Context).WaitAndGetResult(cancellationToken);
if (document == null)
{
var cancellationToken = waitScope.Context.UserCancellationToken;
var document = args.SubjectBuffer.CurrentSnapshot.GetFullyLoadedOpenDocumentInCurrentContextWithChangesAsync(
waitScope.Context).WaitAndGetResult(cancellationToken);
if (document == null)
{
return false;
}
return false;
}
var spans = args.TextView.Selection.GetSnapshotSpansOnBuffer(args.SubjectBuffer);
var spans = args.TextView.Selection.GetSnapshotSpansOnBuffer(args.SubjectBuffer);
var service = document.GetLanguageService<AbstractEncapsulateFieldService>();
var service = document.GetLanguageService<AbstractEncapsulateFieldService>();
var result = service.EncapsulateFieldAsync(document, spans.First().Span.ToTextSpan(), true, cancellationToken).WaitAndGetResult(cancellationToken);
var result = service.EncapsulateFieldAsync(document, spans.First().Span.ToTextSpan(), true, cancellationToken).WaitAndGetResult(cancellationToken);
// We are about to show a modal UI dialog so we should take over the command execution
// wait context. That means the command system won't attempt to show its own wait dialog
// and also will take it into consideration when measuring command handling duration.
waitScope.Context.TakeOwnership();
// We are about to show a modal UI dialog so we should take over the command execution
// wait context. That means the command system won't attempt to show its own wait dialog
// and also will take it into consideration when measuring command handling duration.
waitScope.Context.TakeOwnership();
var workspace = document.Project.Solution.Workspace;
if (result == null)
{
var notificationService = workspace.Services.GetService<INotificationService>();
notificationService.SendNotification(EditorFeaturesResources.Please_select_the_definition_of_the_field_to_encapsulate, severity: NotificationSeverity.Error);
return false;
}
var workspace = document.Project.Solution.Workspace;
if (result == null)
{
var notificationService = workspace.Services.GetService<INotificationService>();
notificationService.SendNotification(EditorFeaturesResources.Please_select_the_definition_of_the_field_to_encapsulate, severity: NotificationSeverity.Error);
return false;
}
waitScope.AllowCancellation = false;
cancellationToken = waitScope.Context.UserCancellationToken;
waitScope.AllowCancellation = false;
cancellationToken = waitScope.Context.UserCancellationToken;
var finalSolution = result.GetSolutionAsync(cancellationToken).WaitAndGetResult(cancellationToken);
var finalSolution = result.GetSolutionAsync(cancellationToken).WaitAndGetResult(cancellationToken);
var previewService = workspace.Services.GetService<IPreviewDialogService>();
if (previewService != null)
{
finalSolution = previewService.PreviewChanges(
string.Format(EditorFeaturesResources.Preview_Changes_0, EditorFeaturesResources.Encapsulate_Field),
"vs.csharp.refactoring.preview",
EditorFeaturesResources.Encapsulate_Field_colon,
result.GetNameAsync(cancellationToken).WaitAndGetResult(cancellationToken),
result.GetGlyphAsync(cancellationToken).WaitAndGetResult(cancellationToken),
finalSolution,
document.Project.Solution);
}
var previewService = workspace.Services.GetService<IPreviewDialogService>();
if (previewService != null)
{
finalSolution = previewService.PreviewChanges(
string.Format(EditorFeaturesResources.Preview_Changes_0, EditorFeaturesResources.Encapsulate_Field),
"vs.csharp.refactoring.preview",
EditorFeaturesResources.Encapsulate_Field_colon,
result.GetNameAsync(cancellationToken).WaitAndGetResult(cancellationToken),
result.GetGlyphAsync(cancellationToken).WaitAndGetResult(cancellationToken),
finalSolution,
document.Project.Solution);
}
if (finalSolution == null)
{
// User clicked cancel.
return true;
}
if (finalSolution == null)
{
// User clicked cancel.
return true;
}
using (var undoTransaction = _undoManager.GetTextBufferUndoManager(args.SubjectBuffer).TextBufferUndoHistory.CreateTransaction(EditorFeaturesResources.Encapsulate_Field))
using (var undoTransaction = _undoManager.GetTextBufferUndoManager(args.SubjectBuffer).TextBufferUndoHistory.CreateTransaction(EditorFeaturesResources.Encapsulate_Field))
{
if (!workspace.TryApplyChanges(finalSolution))
{
if (!workspace.TryApplyChanges(finalSolution))
{
undoTransaction.Cancel();
return false;
}
undoTransaction.Complete();
undoTransaction.Cancel();
return false;
}
return true;
undoTransaction.Complete();
}
return true;
}
public VSCommanding.CommandState GetCommandState(EncapsulateFieldCommandArgs args)
......
......@@ -304,31 +304,30 @@ private ImmutableArray<string> GetFolderNamesForFolder(uint folderItemID)
{
AssertIsForeground();
using (var pooledObject = SharedPools.Default<List<string>>().GetPooledObject())
{
var newFolderNames = pooledObject.Object;
using var pooledObject = SharedPools.Default<List<string>>().GetPooledObject();
var newFolderNames = pooledObject.Object;
if (!_folderNameMap.TryGetValue(folderItemID, out var folderNames))
if (!_folderNameMap.TryGetValue(folderItemID, out var folderNames))
{
ComputeFolderNames(folderItemID, newFolderNames, Hierarchy);
folderNames = newFolderNames.ToImmutableArray();
_folderNameMap.Add(folderItemID, folderNames);
}
else
{
// verify names, and change map if we get a different set.
// this is necessary because we only get document adds/removes from the project system
// when a document name or folder name changes.
ComputeFolderNames(folderItemID, newFolderNames, Hierarchy);
if (!Enumerable.SequenceEqual(folderNames, newFolderNames))
{
ComputeFolderNames(folderItemID, newFolderNames, Hierarchy);
folderNames = newFolderNames.ToImmutableArray();
_folderNameMap.Add(folderItemID, folderNames);
}
else
{
// verify names, and change map if we get a different set.
// this is necessary because we only get document adds/removes from the project system
// when a document name or folder name changes.
ComputeFolderNames(folderItemID, newFolderNames, Hierarchy);
if (!Enumerable.SequenceEqual(folderNames, newFolderNames))
{
folderNames = newFolderNames.ToImmutableArray();
_folderNameMap[folderItemID] = folderNames;
}
_folderNameMap[folderItemID] = folderNames;
}
return folderNames;
}
return folderNames;
}
// Different hierarchies are inconsistent on whether they return ints or uints for VSItemIds.
......
......@@ -328,19 +328,18 @@ private bool WhitespaceOnEdges(SourceText text, TextSpan visibleTextSpan, TextCh
private IEnumerable<TextChange> GetSubTextChanges(SourceText originalText, TextChange changeInOriginalText, TextSpan visibleSpanInOriginalText)
{
using (var changes = SharedPools.Default<List<TextChange>>().GetPooledObject())
{
var leftText = originalText.ToString(changeInOriginalText.Span);
var rightText = changeInOriginalText.NewText;
var offsetInOriginalText = changeInOriginalText.Span.Start;
using var changes = SharedPools.Default<List<TextChange>>().GetPooledObject();
if (TryGetSubTextChanges(originalText, visibleSpanInOriginalText, leftText, rightText, offsetInOriginalText, changes.Object))
{
return changes.Object.ToList();
}
var leftText = originalText.ToString(changeInOriginalText.Span);
var rightText = changeInOriginalText.NewText;
var offsetInOriginalText = changeInOriginalText.Span.Start;
return GetSubTextChanges(originalText, visibleSpanInOriginalText, leftText, rightText, offsetInOriginalText);
if (TryGetSubTextChanges(originalText, visibleSpanInOriginalText, leftText, rightText, offsetInOriginalText, changes.Object))
{
return changes.Object.ToList();
}
return GetSubTextChanges(originalText, visibleSpanInOriginalText, leftText, rightText, offsetInOriginalText);
}
private bool TryGetSubTextChanges(
......@@ -766,24 +765,22 @@ private void AdjustIndentation(IProjectionBuffer subjectBuffer, IEnumerable<int>
.WithChangedOption(FormattingOptions.TabSize, root.Language, editorOptions.GetTabSize())
.WithChangedOption(FormattingOptions.IndentationSize, root.Language, editorOptions.GetIndentSize());
using (var pooledObject = SharedPools.Default<List<TextSpan>>().GetPooledObject())
{
var spans = pooledObject.Object;
using var pooledObject = SharedPools.Default<List<TextSpan>>().GetPooledObject();
spans.AddRange(this.GetEditorVisibleSpans());
using (var edit = subjectBuffer.CreateEdit(s_venusEditOptions, reiteratedVersionNumber: null, editTag: null))
{
foreach (var spanIndex in visibleSpanIndex)
{
var rule = GetBaseIndentationRule(root, originalText, spans, spanIndex);
var spans = pooledObject.Object;
var visibleSpan = spans[spanIndex];
AdjustIndentationForSpan(document, edit, visibleSpan, rule, options);
}
spans.AddRange(this.GetEditorVisibleSpans());
using var edit = subjectBuffer.CreateEdit(s_venusEditOptions, reiteratedVersionNumber: null, editTag: null);
edit.Apply();
}
foreach (var spanIndex in visibleSpanIndex)
{
var rule = GetBaseIndentationRule(root, originalText, spans, spanIndex);
var visibleSpan = spans[spanIndex];
AdjustIndentationForSpan(document, edit, visibleSpan, rule, options);
}
edit.Apply();
}
private void AdjustIndentationForSpan(
......
......@@ -25,34 +25,32 @@ public CSharpNavigateTo(VisualStudioInstanceFactory instanceFactory, ITestOutput
[WpfFact, Trait(Traits.Feature, Traits.Features.NavigateTo)]
public void NavigateTo()
{
using (var telemetry = VisualStudio.EnableTestTelemetryChannel())
{
using var telemetry = VisualStudio.EnableTestTelemetryChannel();
var project = new ProjectUtils.Project(ProjectName);
VisualStudio.SolutionExplorer.AddFile(project, "test1.cs", open: false, contents: @"
var project = new ProjectUtils.Project(ProjectName);
VisualStudio.SolutionExplorer.AddFile(project, "test1.cs", open: false, contents: @"
class FirstClass
{
void FirstMethod() { }
}");
VisualStudio.SolutionExplorer.AddFile(project, "test2.cs", open: true, contents: @"
VisualStudio.SolutionExplorer.AddFile(project, "test2.cs", open: true, contents: @"
");
VisualStudio.Editor.InvokeNavigateTo("FirstMethod", VirtualKey.Enter);
VisualStudio.Editor.WaitForActiveView("test1.cs");
Assert.Equal("FirstMethod", VisualStudio.Editor.GetSelectedText());
VisualStudio.Editor.InvokeNavigateTo("FirstMethod", VirtualKey.Enter);
VisualStudio.Editor.WaitForActiveView("test1.cs");
Assert.Equal("FirstMethod", VisualStudio.Editor.GetSelectedText());
// Add a VB project and verify that VB files are found when searching from C#
var vbProject = new ProjectUtils.Project("VBProject");
VisualStudio.SolutionExplorer.AddProject(vbProject, WellKnownProjectTemplates.ClassLibrary, LanguageNames.VisualBasic);
VisualStudio.SolutionExplorer.AddFile(vbProject, "vbfile.vb", open: true);
// Add a VB project and verify that VB files are found when searching from C#
var vbProject = new ProjectUtils.Project("VBProject");
VisualStudio.SolutionExplorer.AddProject(vbProject, WellKnownProjectTemplates.ClassLibrary, LanguageNames.VisualBasic);
VisualStudio.SolutionExplorer.AddFile(vbProject, "vbfile.vb", open: true);
VisualStudio.Editor.InvokeNavigateTo("FirstClass", VirtualKey.Enter);
VisualStudio.Editor.WaitForActiveView("test1.cs");
Assert.Equal("FirstClass", VisualStudio.Editor.GetSelectedText());
telemetry.VerifyFired("vs/ide/vbcs/navigateto/search", "vs/platform/goto/launch");
}
VisualStudio.Editor.InvokeNavigateTo("FirstClass", VirtualKey.Enter);
VisualStudio.Editor.WaitForActiveView("test1.cs");
Assert.Equal("FirstClass", VisualStudio.Editor.GetSelectedText());
telemetry.VerifyFired("vs/ide/vbcs/navigateto/search", "vs/platform/goto/launch");
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册