Skip to content

Commit

Permalink
[closes gh-44] Added command to force signature help on Ctrl+Shift+Sp…
Browse files Browse the repository at this point in the history
…ace.
  • Loading branch information
ashmind committed Dec 30, 2016
1 parent bcaba09 commit 731f61e
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 15 deletions.
1 change: 1 addition & 0 deletions MirrorSharp.Common/Advanced/MiddlewareBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ internal IReadOnlyCollection<ICommandHandler> CreateHandlers() {
new ReplaceTextHandler(signatureHelp, completion, typedCharEffects),
new RequestSelfDebugDataHandler(),
new SetOptionsHandler(_languages, _options?.SetOptionsFromClient),
new SignatureHelpStateHandler(signatureHelp),
new SlowUpdateHandler(_options?.SlowUpdate),
new TypeCharHandler(typedCharEffects)
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using MirrorSharp.Internal.Results;

namespace MirrorSharp.Internal.Handlers.Shared {
internal interface ISignatureHelpSupport {
Task ApplyCursorPositionChangeAsync(WorkSession session, ICommandResultSender sender, CancellationToken cancellationToken);
Task ApplyTypedCharAsync(char @char, WorkSession session, ICommandResultSender sender, CancellationToken cancellationToken);
[NotNull] Task ApplyCursorPositionChangeAsync([NotNull] WorkSession session, [NotNull] ICommandResultSender sender, CancellationToken cancellationToken);
[NotNull] Task ApplyTypedCharAsync(char @char, [NotNull] WorkSession session, [NotNull] ICommandResultSender sender, CancellationToken cancellationToken);
[NotNull] Task ForceSignatureHelpAsync([NotNull] WorkSession session, [NotNull] ICommandResultSender sender, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,27 @@ public Task ApplyCursorPositionChangeAsync(WorkSession session, ICommandResultSe
return TryApplySignatureHelpAsync(currentHelp.Value.Provider, new SignatureHelpTriggerInfoData(SignatureHelpTriggerReason.RetriggerCommand), session, sender, cancellationToken, true);
}

public async Task ApplyTypedCharAsync(char @char, WorkSession session, ICommandResultSender sender, CancellationToken cancellationToken) {
public Task ApplyTypedCharAsync(char @char, WorkSession session, ICommandResultSender sender, CancellationToken cancellationToken) {
var trigger = new SignatureHelpTriggerInfoData(SignatureHelpTriggerReason.TypeCharCommand, @char);
if (session.CurrentSignatureHelp != null) {
var provider = session.CurrentSignatureHelp.Value.Provider;
if (provider.IsRetriggerCharacter(@char)) {
session.CurrentSignatureHelp = null;
await SendSignatureHelpAsync(null, sender, cancellationToken).ConfigureAwait(false);
return;
return SendSignatureHelpAsync(null, sender, cancellationToken);
}

await TryApplySignatureHelpAsync(provider, trigger, session, sender, cancellationToken, sendIfEmpty: true).ConfigureAwait(false);
return;
return TryApplySignatureHelpAsync(provider, trigger, session, sender, cancellationToken, sendIfEmpty: true);
}

return TryApplySignatureHelpAsync(session, sender, cancellationToken, trigger);
}

public Task ForceSignatureHelpAsync(WorkSession session, ICommandResultSender sender, CancellationToken cancellationToken) {
var trigger = new SignatureHelpTriggerInfoData(SignatureHelpTriggerReason.InvokeSignatureHelpCommand);
return TryApplySignatureHelpAsync(session, sender, cancellationToken, trigger);
}

private async Task TryApplySignatureHelpAsync(WorkSession session, ICommandResultSender sender, CancellationToken cancellationToken, SignatureHelpTriggerInfoData trigger) {
foreach (var provider in session.SignatureHelpProviders) {
if (await TryApplySignatureHelpAsync(provider, trigger, session, sender, cancellationToken).ConfigureAwait(false))
return;
Expand Down
25 changes: 25 additions & 0 deletions MirrorSharp.Common/Internal/Handlers/SignatureHelpStateHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using MirrorSharp.Internal.Handlers.Shared;
using MirrorSharp.Internal.Results;

namespace MirrorSharp.Internal.Handlers {
internal class SignatureHelpStateHandler : ICommandHandler {
public char CommandId => 'P';
[NotNull] private readonly ISignatureHelpSupport _signatureHelp;

public SignatureHelpStateHandler([NotNull] ISignatureHelpSupport signatureHelp) {
_signatureHelp = signatureHelp;
}

public Task ExecuteAsync(ArraySegment<byte> data, WorkSession session, ICommandResultSender sender, CancellationToken cancellationToken) {
var @char = FastConvert.Utf8ByteArrayToChar(data);
if (@char != 'F')
throw new FormatException($"Unknown SignatureHelp command '{@char}'.");

return _signatureHelp.ForceSignatureHelpAsync(session, sender, cancellationToken);
}
}
}
5 changes: 3 additions & 2 deletions MirrorSharp.Tests/Internal/Results/SignaturesResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public class ResultSignature {
public bool Selected { get; set; }
public IList<ResultSignaturePart> Parts { get; } = new List<ResultSignaturePart>();

public override string ToString() {
return string.Join("", Parts.GroupAdjacentBy(p => p.Selected ? "*" : "").Select(g => g.Key + string.Join("", g.Select(p => p.Text)) + g.Key));
public override string ToString() => ToString(true);
public string ToString(bool markSelected) {
return string.Join("", Parts.GroupAdjacentBy(p => markSelected && p.Selected ? "*" : "").Select(g => g.Key + string.Join("", g.Select(p => p.Text)) + g.Key));
}
}

Expand Down
28 changes: 28 additions & 0 deletions MirrorSharp.Tests/SignatureHelpStateHandlerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using MirrorSharp.Internal.Handlers;
using MirrorSharp.Tests.Internal;
using MirrorSharp.Tests.Internal.Results;
using Xunit;

namespace MirrorSharp.Tests {
using static TestHelper;

public class SignatureHelpStateHandlerTests {
[Fact]
public async Task ExecuteAsync_ProducesExpectedSignatureHelp_WhenForceIsRequested() {
var session = SessionFromTextWithCursor(@"
class C {
void M(int a) {}
void T() { M(1|) }
}
");
var result = await ExecuteHandlerAsync<SignatureHelpStateHandler, SignaturesResult>(session, 'F');
Assert.Equal(
new[] { "void C.M(*int a*)" },
result.Signatures.Select(s => s.ToString())
);
}
}
}
5 changes: 1 addition & 4 deletions MirrorSharp.Tests/TypeCharHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ void T() { M| }
}
");
var result = await ExecuteHandlerAsync<TypeCharHandler, SignaturesResult>(session, '(');
Assert.Equal(
expected,
result.Signatures.Select(s => string.Join("", s.Parts.Select(p => p.Text)))
);
Assert.Equal(expected, result.Signatures.Select(s => s.ToString(markSelected: false)));
}

[Theory]
Expand Down
9 changes: 7 additions & 2 deletions MirrorSharp.WebAssets/wwwroot/js/mirrorsharp.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,16 @@
return sendWhenOpen('C' + char);
};

const completionCommandMap = { cancel: 'X', force: 'F' };
const stateCommandMap = { cancel: 'X', force: 'F' };
this.sendCompletionState = function(indexOrCommand) {
const argument = completionCommandMap[indexOrCommand] || indexOrCommand;
const argument = stateCommandMap[indexOrCommand] || indexOrCommand;
return sendWhenOpen('S' + argument);
};

this.sendSignatureHelpState = function(command) {
return sendWhenOpen('P' + stateCommandMap[command]);
};

this.sendSlowUpdate = function() {
return sendWhenOpen('U');
};
Expand Down Expand Up @@ -424,6 +428,7 @@
});
cmOptions.extraKeys = assign({
'Ctrl-Space': function() { connection.sendCompletionState('force'); },
'Shift-Ctrl-Space': function() { connection.sendSignatureHelpState('force'); },
'Ctrl-.': 'lintFixShow',
'Shift-Ctrl-Y': selfDebug ? function() { selfDebug.requestData(connection); } : null
}, cmOptions.extraKeys);
Expand Down
2 changes: 2 additions & 0 deletions MirrorSharp.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
<s:String x:Key="/Default/CodeInspection/Browsers/Browsers/@EntryValue">C27+,E12+,FF21+,IE11+,S9+</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=70BCCC0E_002D6EEF_002D40D0_002D932A_002D87F9C42BD67B_002Fd_003Awwwroot_002Fd_003Abower_005Fcomponents/@EntryIndexedValue">ExplicitlyExcluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=D2EB1CDD_002D12B5_002D4FDC_002DA56C_002D8A327200E759_002Fd_003Abower_005Fcomponents/@EntryIndexedValue">ExplicitlyExcluded</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ArgumentsStyleLiteral/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ArgumentsStyleStringLiteral/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=Html_002EEventNotResolved/@EntryIndexedValue">HINT</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=MissingHasOwnPropertyInForeach/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=RedundantDefaultMemberInitializer/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ThisInGlobalContext/@EntryIndexedValue">HINT</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=UnusedMember_002EGlobal/@EntryIndexedValue">WARNING</s:String>
<s:String x:Key="/Default/CodeInspection/JsInspections/LanguageLevel/@EntryValue">ECMAScript 2016</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/ACCESSOR_DECLARATION_BRACES/@EntryValue">END_OF_LINE</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/ACCESSOR_OWNER_DECLARATION_BRACES/@EntryValue">END_OF_LINE</s:String>
Expand Down

0 comments on commit 731f61e

Please sign in to comment.