Skip to content

Commit

Permalink
Undo changes made in PR #2218 and optimize copying query results (#2385)
Browse files Browse the repository at this point in the history
* Copy query results to the clipboard

* Add TextCopy NuGet package.

* Conditionally copies directly to clipboard

* Rename copy in backend request property

* Rename property for copying results request.

* Stop sending result string for copying

* Improve check for new line at builder end

* Correctly checks if row end needs line break
  • Loading branch information
lewis-sanchez authored Sep 12, 2024
1 parent 272e117 commit 4646cd3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
1 change: 1 addition & 0 deletions Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<PackageReference Update="Microsoft.SqlServer.XEvent.XELite" Version="2023.1.30.3" />
<PackageReference Update="SkiaSharp" Version="2.88.6" />
<PackageReference Update="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.6" Condition="$([MSBuild]::IsOsPlatform('Linux'))" />
<PackageReference Update="TextCopy" Version="6.2.1" />
</ItemGroup>

<!-- When updating version of Dependencies in the below section, please also update the version in the following files:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<PackageReference Include="Microsoft.SqlServer.TransactSql.ScriptDom.NRT">
<Aliases>ASAScriptDom</Aliases>
</PackageReference>
<PackageReference Include="TextCopy" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../Microsoft.SqlTools.Hosting/Microsoft.SqlTools.Hosting.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public class CopyResultsRequestParams : SubsetParams
/// </summary>
public class CopyResultsRequestResult
{
public string Results { get; set; }
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
Expand Down Expand Up @@ -29,6 +29,7 @@
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
using Microsoft.SqlTools.ServiceLayer.Workspace;
using Microsoft.SqlTools.Utility;
using TextCopy;

namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
{
Expand Down Expand Up @@ -863,25 +864,33 @@ internal async Task HandleCopyResultsRequest(CopyResultsRequestParams requestPar
}
}
// Add line break if this is not the last row in all selections.
if (rowIndex + pageStartRowIndex != lastRowIndex && (!builder.ToString().EndsWith(Environment.NewLine) || (!Settings?.QueryEditorSettings?.Results?.SkipNewLineAfterTrailingLineBreak ?? true)))
if (rowIndex + pageStartRowIndex != lastRowIndex && (!StringBuilderEndsWith(builder, Environment.NewLine) || (!Settings?.QueryEditorSettings?.Results?.SkipNewLineAfterTrailingLineBreak ?? true)))
{
builder.Append(Environment.NewLine);
}
}
pageStartRowIndex += rowsToFetch;
} while (pageStartRowIndex < rowRange.End);
}
CopyResultsRequestResult result = new CopyResultsRequestResult
{
Results = builder.ToString()
};
await requestContext.SendResult(result);
await ClipboardService.SetTextAsync(builder.ToString());
await requestContext.SendResult(new CopyResultsRequestResult());
}

#endregion

#region Private Helpers

private bool StringBuilderEndsWith(StringBuilder sb, string target)
{
if (sb.Length < target.Length)
{
return false;
}

// Calling ToString like this only converts the last few characters of the StringBuilder to a string
return sb.ToString(sb.Length - target.Length, target.Length).EndsWith(target);
}

private Query CreateQuery(
ExecuteRequestParamsBase executeParams,
ConnectionInfo connInfo,
Expand Down

0 comments on commit 4646cd3

Please sign in to comment.