public IEnumerable <ILspHandlerDescriptor> FindHandler(object parameters, IEnumerable <ILspHandlerDescriptor> descriptors)
        {
            switch (parameters)
            {
            case ITextDocumentIdentifierParams textDocumentIdentifierParams:
            {
                var attributes = GetTextDocumentAttributes(textDocumentIdentifierParams.TextDocument.Uri);

                _logger.LogTrace("Found attributes {Count}, {Attributes}", attributes.Count, attributes.Select(x => $"{x.LanguageId}:{x.Scheme}:{x.Uri}"));

                return(GetHandler(descriptors, attributes));
            }

            case DidOpenTextDocumentParams openTextDocumentParams:
            {
                var attributes = new TextDocumentAttributes(openTextDocumentParams.TextDocument.Uri, openTextDocumentParams.TextDocument.LanguageId);

                _logger.LogTrace("Created attribute {Attribute}", $"{attributes.LanguageId}:{attributes.Scheme}:{attributes.Uri}");

                return(GetHandler(descriptors, attributes));
            }

            case DidChangeTextDocumentParams didChangeDocumentParams:
            {
                // TODO: Do something with document version here?
                var attributes = GetTextDocumentAttributes(didChangeDocumentParams.TextDocument.Uri);

                _logger.LogTrace("Found attributes {Count}, {Attributes}", attributes.Count, attributes.Select(x => $"{x.LanguageId}:{x.Scheme}:{x.Uri}"));

                return(GetHandler(descriptors, attributes));
            }
            }

            return(Enumerable.Empty <ILspHandlerDescriptor>());
        }
        private ILspHandlerDescriptor FindDescriptor(string method, JToken @params)
        {
            _logger.LogDebug("Finding descriptor for {Method}", method);
            var descriptor = _collection.FirstOrDefault(x => x.Method == method);

            if (descriptor is null)
            {
                _logger.LogDebug("Unable to find {Method}, methods found include {Methods}", method, string.Join(", ", _collection.Select(x => x.Method + ":" + x.Handler.GetType().FullName)));
                return(null);
            }

            if (@params != null && descriptor.Params != null)
            {
                var paramsValue = @params.ToObject(descriptor.Params);
                if (paramsValue is ITextDocumentIdentifierParams textDocumentIdentifierParams)
                {
                    var attributes = GetTextDocumentAttributes(textDocumentIdentifierParams.TextDocument.Uri);

                    _logger.LogTrace("Found attributes {Count}, {Attributes}", attributes.Count, attributes.Select(x => $"{x.LanguageId}:{x.Scheme}:{x.Uri}"));

                    return(GetHandler(method, attributes));
                }
                else if (paramsValue is DidOpenTextDocumentParams openTextDocumentParams)
                {
                    var attributes = new TextDocumentAttributes(openTextDocumentParams.TextDocument.Uri, openTextDocumentParams.TextDocument.LanguageId);

                    _logger.LogTrace("Created attribute {Attribute}", $"{attributes.LanguageId}:{attributes.Scheme}:{attributes.Uri}");

                    return(GetHandler(method, attributes));
                }
                else if (paramsValue is DidChangeTextDocumentParams didChangeDocumentParams)
                {
                    // TODO: Do something with document version here?
                    var attributes = GetTextDocumentAttributes(didChangeDocumentParams.TextDocument.Uri);

                    _logger.LogTrace("Found attributes {Count}, {Attributes}", attributes.Count, attributes.Select(x => $"{x.LanguageId}:{x.Scheme}:{x.Uri}"));

                    return(GetHandler(method, attributes));
                }
            }

            // TODO: How to split these
            // Do they fork and join?
            return(descriptor);
        }
        private ILspHandlerDescriptor GetHandler(string method, TextDocumentAttributes attributes)
        {
            _logger.LogTrace("Looking for handler for method {Method}", method);
            foreach (var handler in _collection.Where(x => x.Method == method))
            {
                _logger.LogTrace("Checking handler {Method}:{Handler}", method, handler.Handler.GetType().FullName);
                var registrationOptions = handler.Registration.RegisterOptions as TextDocumentRegistrationOptions;

                _logger.LogTrace("Registration options {OptionsName}", registrationOptions.GetType().FullName);
                _logger.LogTrace("Document Selector {DocumentSelector}", registrationOptions.DocumentSelector.ToString());
                if (registrationOptions.DocumentSelector == null || registrationOptions.DocumentSelector.IsMatch(attributes))
                {
                    _logger.LogTrace("Handler Selected: {Handler} via {DocumentSelector} (targeting {HandlerInterface})", handler.Handler.GetType().FullName, registrationOptions.DocumentSelector.ToString(), handler.HandlerType.GetType().FullName);
                    return(handler);
                }
            }
            return(null);
        }
 public bool IsMatch(TextDocumentAttributes attributes)
 {
     return(this.Any(z => z.IsMatch(attributes)));
 }
Exemplo n.º 5
0
        private IEnumerable <ILspHandlerDescriptor> GetHandler(IEnumerable <ILspHandlerDescriptor> descriptors, TextDocumentAttributes attributes)
        {
            var method = descriptors.FirstOrDefault()?.Method;

            _logger.LogTrace("Looking for handler for descriptors {Method}", method);
            foreach (var descriptor in descriptors)
            {
                _logger.LogTrace("Checking handler {Method}:{Handler}", method, descriptor.ImplementationType.FullName);
                var registrationOptions = descriptor.RegistrationOptions as ITextDocumentRegistrationOptions;

                _logger.LogTrace("Registration options {OptionsName}", registrationOptions?.GetType().FullName);
                _logger.LogTrace("Document Selector {DocumentSelector}", registrationOptions?.DocumentSelector.ToString());
                if (registrationOptions?.DocumentSelector == null || registrationOptions.DocumentSelector.IsMatch(attributes))
                {
                    _logger.LogTrace(
                        "Handler Selected: {Handler} via {DocumentSelector} (targeting {HandlerInterface})", descriptor.ImplementationType.FullName,
                        registrationOptions?.DocumentSelector?.ToString(), descriptor.HandlerType.FullName
                        );
                    yield return(descriptor);
                }
            }
        }
Exemplo n.º 6
0
 public bool IsMatch(TextDocumentAttributes attributes) => this.Any(z => z.IsMatch(attributes));