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)
            {
                return(descriptor);
            }

            var lspHandlerDescriptors = _collection.Where(handler => handler.Method == method).ToList();

            if (lspHandlerDescriptors.Count == 1)
            {
                return(descriptor);
            }

            var paramsValue = @params.ToObject(descriptor.Params, _serializer.JsonSerializer);

            return(_handlerMatchers.SelectMany(strat => strat.FindHandler(paramsValue, lspHandlerDescriptors)).FirstOrDefault() ?? descriptor);
        }
Exemple #2
0
        private IRequestDescriptor <ILspHandlerDescriptor> FindDescriptor(string method, JToken? @params)
        {
            _logger.LogDebug("Finding descriptors 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(new RequestDescriptor <ILspHandlerDescriptor>());
            }

            if (@params == null || descriptor.Params == null)
            {
                return(new RequestDescriptor <ILspHandlerDescriptor>(descriptor));
            }

            object?paramsValue;

            if (descriptor.IsDelegatingHandler)
            {
                var o = @params.ToObject(descriptor.Params.GetGenericArguments()[0], _serializer.JsonSerializer);
                paramsValue = Activator.CreateInstance(descriptor.Params, o);
            }
            else
            {
                paramsValue = @params.ToObject(descriptor.Params, _serializer.JsonSerializer);
            }

            var lspHandlerDescriptors = _collection.Where(handler => handler.Method == method).ToList();

            var matchDescriptor = _handlerMatchers.SelectMany(strat => strat.FindHandler(paramsValue, lspHandlerDescriptors)).ToArray();

            if (matchDescriptor.Length > 0)
            {
                return(new RequestDescriptor <ILspHandlerDescriptor>(matchDescriptor));
            }
            // execute command is a special case
            // if no command was found to execute this must error
            // this is not great coupling but other options require api changes
            if (paramsValue is IExecuteCommandParams)
            {
                return(new RequestDescriptor <ILspHandlerDescriptor>());
            }
            if (lspHandlerDescriptors.Count > 0)
            {
                return(new RequestDescriptor <ILspHandlerDescriptor>(lspHandlerDescriptors));
            }
            return(new RequestDescriptor <ILspHandlerDescriptor>());
        }
        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);
        }