Skip to content

Commit

Permalink
Converted to use the new abstract base class to simplify some of the …
Browse files Browse the repository at this point in the history
…plumbing code
  • Loading branch information
david-driscoll committed Jul 19, 2019
1 parent 78e0a23 commit 1471c79
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 196 deletions.
1 change: 1 addition & 0 deletions codealike.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"projectId":"14125030-a9b5-11e9-91f0-17def6569726","projectName":"omnisharp-roslyn"}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace OmniSharp.LanguageServerProtocol.Handlers
{
internal sealed class OmniSharpCodeLensHandler : ICodeLensHandler, ICodeLensResolveHandler
internal sealed class OmniSharpCodeLensHandler : CodeLensHandler
{
public static IEnumerable<IJsonRpcHandler> Enumerate(RequestHandlers handlers)
{
Expand All @@ -28,22 +28,24 @@ public static IEnumerable<IJsonRpcHandler> Enumerate(RequestHandlers handlers)
}
}

private CodeLensCapability _capability;
private readonly Mef.IRequestHandler<MembersTreeRequest, FileMemberTree> _membersAsTreeHandler;
private readonly Mef.IRequestHandler<FindUsagesRequest, QuickFixResponse> _findUsagesHandler;
private readonly DocumentSelector _documentSelector;

public OmniSharpCodeLensHandler(
Mef.IRequestHandler<MembersTreeRequest, FileMemberTree> membersAsTreeHandler,
Mef.IRequestHandler<FindUsagesRequest, QuickFixResponse> findUsagesHandler,
DocumentSelector documentSelector)
: base(new CodeLensRegistrationOptions()
{
DocumentSelector = documentSelector,
ResolveProvider = true
})
{
_membersAsTreeHandler = membersAsTreeHandler;
_findUsagesHandler = findUsagesHandler;
_documentSelector = documentSelector;
}

public async Task<CodeLensContainer> Handle(CodeLensParams request, CancellationToken token)
public async override Task<CodeLensContainer> Handle(CodeLensParams request, CancellationToken token)
{
var omnisharpRequest = new MembersTreeRequest()
{
Expand All @@ -61,13 +63,13 @@ public async Task<CodeLensContainer> Handle(CodeLensParams request, Cancellation
return codeLenseContainer;
}

public async Task<CodeLens> Handle(CodeLens request, CancellationToken token)
public async override Task<CodeLens> Handle(CodeLens request, CancellationToken token)
{
var omnisharpRequest = new FindUsagesRequest
{
FileName = Helpers.FromUri(request.Data.ToObject<Uri>()),
Column = (int) request.Range.Start.Character,
Line = (int) request.Range.Start.Line,
Column = (int)request.Range.Start.Character,
Line = (int)request.Range.Start.Line,
OnlyThisFile = false,
ExcludeDefinition = true
};
Expand All @@ -85,21 +87,6 @@ public async Task<CodeLens> Handle(CodeLens request, CancellationToken token)
return request;
}


public CodeLensRegistrationOptions GetRegistrationOptions()
{
return new CodeLensRegistrationOptions()
{
DocumentSelector = _documentSelector,
ResolveProvider = true
};
}

public void SetCapability(CodeLensCapability capability)
{
_capability = capability;
}

private static void ToCodeLens(TextDocumentIdentifier textDocument, FileMemberElement node, List<CodeLens> codeLensContainer)
{
var codeLens = new CodeLens
Expand All @@ -121,12 +108,12 @@ private static void ToCodeLens(TextDocumentIdentifier textDocument, FileMemberEl
}
}

public bool CanResolve(CodeLens value)
public override bool CanResolve(CodeLens value)
{
var textDocumentUri = value.Data.ToObject<Uri>();

return textDocumentUri != null &&
_documentSelector.IsMatch(new TextDocumentAttributes(textDocumentUri, string.Empty));
GetRegistrationOptions().DocumentSelector.IsMatch(new TextDocumentAttributes(textDocumentUri, string.Empty));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace OmniSharp.LanguageServerProtocol.Handlers
{
class OmniSharpCompletionHandler : ICompletionHandler
class OmniSharpCompletionHandler : CompletionHandler
{
public static IEnumerable<IJsonRpcHandler> Enumerate(RequestHandlers handlers)
{
Expand All @@ -21,9 +21,7 @@ public static IEnumerable<IJsonRpcHandler> Enumerate(RequestHandlers handlers)
yield return new OmniSharpCompletionHandler(handler, selector);
}

private CompletionCapability _capability;
private readonly Mef.IRequestHandler<AutoCompleteRequest, IEnumerable<AutoCompleteResponse>> _autoCompleteHandler;
private readonly DocumentSelector _documentSelector;

private static readonly IDictionary<string, CompletionItemKind> _kind = new Dictionary<string, CompletionItemKind>{
// types
Expand Down Expand Up @@ -58,20 +56,26 @@ private static CompletionItemKind GetCompletionItemKind(string key)
{
return CompletionItemKind.Property;
}
if(_kind.TryGetValue(key, out var completionItemKind))
if (_kind.TryGetValue(key, out var completionItemKind))
{
return completionItemKind;
}
return CompletionItemKind.Property;
}

public OmniSharpCompletionHandler(Mef.IRequestHandler<AutoCompleteRequest, IEnumerable<AutoCompleteResponse>> autoCompleteHandler, DocumentSelector documentSelector)
: base(new CompletionRegistrationOptions()
{
DocumentSelector = documentSelector,
// TODO: Come along and add a service for getting autocompletion details after the fact.
ResolveProvider = false,
TriggerCharacters = new[] { ".", },
})
{
_autoCompleteHandler = autoCompleteHandler;
_documentSelector = documentSelector;
}

public async Task<CompletionList> Handle(CompletionParams request, CancellationToken token)
public async override Task<CompletionList> Handle(CompletionParams request, CancellationToken token)
{
var omnisharpRequest = new AutoCompleteRequest()
{
Expand All @@ -81,19 +85,20 @@ public async Task<CompletionList> Handle(CompletionParams request, CancellationT
WantKind = true,
WantDocumentationForEveryCompletionResult = true,
WantReturnType = true,
WantSnippet =_capability.CompletionItem?.SnippetSupport ?? false
WantSnippet = Capability.CompletionItem?.SnippetSupport ?? false
};

var omnisharpResponse = await _autoCompleteHandler.Handle(omnisharpRequest);

var completions = new Dictionary<string, List<CompletionItem>>();
foreach (var response in omnisharpResponse)
{
var isSnippet = !string.IsNullOrEmpty(response.Snippet);
var text = isSnippet ? response.Snippet : response.CompletionText;
var isSnippet = !string.IsNullOrEmpty(response.Snippet);
var text = isSnippet ? response.Snippet : response.CompletionText;
var textFormat = isSnippet ? InsertTextFormat.Snippet : InsertTextFormat.PlainText;

var completionItem = new CompletionItem {
var completionItem = new CompletionItem
{
Label = response.CompletionText,
Detail = !string.IsNullOrEmpty(response.ReturnType) ?
response.DisplayText :
Expand All @@ -104,7 +109,7 @@ public async Task<CompletionList> Handle(CompletionParams request, CancellationT
InsertTextFormat = textFormat,
};

if(!completions.ContainsKey(completionItem.Label))
if (!completions.ContainsKey(completionItem.Label))
{
completions[completionItem.Label] = new List<CompletionItem>();
}
Expand All @@ -129,18 +134,14 @@ public async Task<CompletionList> Handle(CompletionParams request, CancellationT
return new CompletionList(result);
}

public CompletionRegistrationOptions GetRegistrationOptions()
public override Task<CompletionItem> Handle(CompletionItem request, CancellationToken cancellationToken)
{
return new CompletionRegistrationOptions()
{
DocumentSelector = _documentSelector,
TriggerCharacters = new[] { "." },
};
throw new NotImplementedException();
}

public void SetCapability(CompletionCapability capability)
public override bool CanResolve(CompletionItem value)
{
_capability = capability;
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace OmniSharp.LanguageServerProtocol.Handlers
{
class OmniSharpDefinitionHandler : IDefinitionHandler
class OmniSharpDefinitionHandler : DefinitionHandler
{
public static IEnumerable<IJsonRpcHandler> Enumerate(RequestHandlers handlers)
{
Expand All @@ -20,25 +20,18 @@ public static IEnumerable<IJsonRpcHandler> Enumerate(RequestHandlers handlers)
yield return new OmniSharpDefinitionHandler(handler, selector);
}

private DefinitionCapability _capability;
private readonly Mef.IRequestHandler<GotoDefinitionRequest, GotoDefinitionResponse> _definitionHandler;
private readonly DocumentSelector _documentSelector;

public OmniSharpDefinitionHandler(Mef.IRequestHandler<GotoDefinitionRequest, GotoDefinitionResponse> definitionHandler, DocumentSelector documentSelector)
: base(new TextDocumentRegistrationOptions()
{
DocumentSelector = documentSelector
})
{
_definitionHandler = definitionHandler;
_documentSelector = documentSelector;
}

public TextDocumentRegistrationOptions GetRegistrationOptions()
{
return new TextDocumentRegistrationOptions()
{
DocumentSelector = _documentSelector
};
}

public async Task<LocationOrLocationLinks> Handle(DefinitionParams request, CancellationToken token)
public async override Task<LocationOrLocationLinks> Handle(DefinitionParams request, CancellationToken token)
{
var omnisharpRequest = new GotoDefinitionRequest()
{
Expand All @@ -60,10 +53,5 @@ public async Task<LocationOrLocationLinks> Handle(DefinitionParams request, Canc
Range = ToRange((omnisharpResponse.Column, omnisharpResponse.Line))
});
}

public void SetCapability(DefinitionCapability capability)
{
_capability = capability;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace OmniSharp.LanguageServerProtocol.Handlers
{
internal sealed class OmniSharpDocumentSymbolHandler : IDocumentSymbolHandler
internal sealed class OmniSharpDocumentSymbolHandler : DocumentSymbolHandler
{
public static IEnumerable<IJsonRpcHandler> Enumerate(RequestHandlers handlers)
{
Expand All @@ -22,9 +22,7 @@ public static IEnumerable<IJsonRpcHandler> Enumerate(RequestHandlers handlers)
yield return new OmniSharpDocumentSymbolHandler(handler, selector);
}

private DocumentSymbolCapability _capability;
private readonly Mef.IRequestHandler<CodeStructureRequest, CodeStructureResponse> _codeStructureHandler;
private readonly DocumentSelector _documentSelector;

private static readonly IDictionary<string, SymbolKind> Kinds = new Dictionary<string, SymbolKind>
{
Expand All @@ -47,12 +45,15 @@ public static IEnumerable<IJsonRpcHandler> Enumerate(RequestHandlers handlers)
};

public OmniSharpDocumentSymbolHandler(Mef.IRequestHandler<CodeStructureRequest, CodeStructureResponse> codeStructureHandler, DocumentSelector documentSelector)
: base(new TextDocumentRegistrationOptions()
{
DocumentSelector = documentSelector
})
{
_codeStructureHandler = codeStructureHandler;
_documentSelector = documentSelector;
}

public async Task<SymbolInformationOrDocumentSymbolContainer> Handle(DocumentSymbolParams request, CancellationToken token)
public async override Task<SymbolInformationOrDocumentSymbolContainer> Handle(DocumentSymbolParams request, CancellationToken token)
{
var omnisharpRequest = new CodeStructureRequest()
{
Expand All @@ -65,19 +66,6 @@ public async Task<SymbolInformationOrDocumentSymbolContainer> Handle(DocumentSym
Array.Empty<SymbolInformationOrDocumentSymbol>();
}

public TextDocumentRegistrationOptions GetRegistrationOptions()
{
return new TextDocumentRegistrationOptions()
{
DocumentSelector = _documentSelector
};
}

public void SetCapability(DocumentSymbolCapability capability)
{
_capability = capability;
}

private static SymbolInformationOrDocumentSymbol ToDocumentSymbolInformationOrDocumentSymbol(CodeElement node)
{
return new SymbolInformationOrDocumentSymbol(ToDocumentSymbol(node));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace OmniSharp.LanguageServerProtocol.Handlers
{
class OmniSharpHoverHandler : IHoverHandler
class OmniSharpHoverHandler : HoverHandler
{
public static IEnumerable<IJsonRpcHandler> Enumerate(RequestHandlers handlers)
{
Expand All @@ -20,25 +20,18 @@ public static IEnumerable<IJsonRpcHandler> Enumerate(RequestHandlers handlers)
yield return new OmniSharpHoverHandler(handler, selector);
}

private HoverCapability _capability;
private readonly Mef.IRequestHandler<TypeLookupRequest, TypeLookupResponse> _definitionHandler;
private readonly DocumentSelector _documentSelector;

public OmniSharpHoverHandler(Mef.IRequestHandler<TypeLookupRequest, TypeLookupResponse> definitionHandler, DocumentSelector documentSelector)
: base(new TextDocumentRegistrationOptions()
{
DocumentSelector = documentSelector
})
{
_definitionHandler = definitionHandler;
_documentSelector = documentSelector;
}

public TextDocumentRegistrationOptions GetRegistrationOptions()
{
return new TextDocumentRegistrationOptions()
{
DocumentSelector = _documentSelector
};
}

public async Task<Hover> Handle(HoverParams request, CancellationToken token)
public async override Task<Hover> Handle(HoverParams request, CancellationToken token)
{
var omnisharpRequest = new TypeLookupRequest()
{
Expand All @@ -57,10 +50,5 @@ public async Task<Hover> Handle(HoverParams request, CancellationToken token)
Contents = new MarkedStringsOrMarkupContent(new MarkedStringContainer(omnisharpResponse.Type, omnisharpResponse.Documentation))
};
}

public void SetCapability(HoverCapability capability)
{
_capability = capability;
}
}
}
Loading

0 comments on commit 1471c79

Please sign in to comment.