|
| 1 | +using System.Collections.ObjectModel; |
| 2 | +using System.Reflection.Metadata; |
| 3 | +using System; |
| 4 | +using System.Security.Cryptography.Xml; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | +using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities; |
| 7 | +using OmniSharp.Extensions.LanguageServer.Protocol.Document; |
| 8 | +using OmniSharp.Extensions.LanguageServer.Protocol.Models; |
| 9 | +using OmniSharp.Extensions.LanguageServer.Protocol.Server; |
| 10 | +using ShaderlabVS.Data; |
| 11 | +using ShaderLS.Management; |
| 12 | + |
| 13 | +namespace ShaderLS.Handlers |
| 14 | +{ |
| 15 | + public class SignatureHelpHandler : SignatureHelpHandlerBase |
| 16 | + { |
| 17 | + private readonly ILogger<SignatureHelpHandler> _logger; |
| 18 | + private readonly ILanguageServerConfiguration _configuration; |
| 19 | + private readonly Workspace _workspace; |
| 20 | + private readonly DocumentSelector _documentSelector; |
| 21 | + |
| 22 | + public SignatureHelpHandler( |
| 23 | + ILogger<SignatureHelpHandler> logger, |
| 24 | + ILanguageServerConfiguration configuration, |
| 25 | + DocumentSelector documentSelector, |
| 26 | + Workspace workspace) |
| 27 | + { |
| 28 | + this._logger = logger; |
| 29 | + this._configuration = configuration; |
| 30 | + this._documentSelector = documentSelector; |
| 31 | + this._workspace = workspace; |
| 32 | + } |
| 33 | + |
| 34 | + public override async Task<SignatureHelp?> Handle(SignatureHelpParams request, CancellationToken cancellationToken) |
| 35 | + { |
| 36 | + var uri = request.TextDocument.Uri; |
| 37 | + Position position = request.Position; |
| 38 | + |
| 39 | + string current = _workspace.BufferService.GetWordAtPosition(uri, position); |
| 40 | + |
| 41 | + int newChar = Math.Max(position.Character - current.Length, 0); |
| 42 | + Position newPos = new Position(position.Line, newChar); |
| 43 | + |
| 44 | + // Attempt to get function name. |
| 45 | + string word = _workspace.BufferService.GetWordAtPosition(uri, newPos); |
| 46 | + |
| 47 | + _logger.LogWarning("word: " + word); |
| 48 | + |
| 49 | + var signatures = new Container<SignatureInformation>(); |
| 50 | + |
| 51 | + ShaderlabDataManager.Instance.HLSLCGFunctions.ForEach(f => |
| 52 | + { |
| 53 | + foreach (var item in f.Synopsis) |
| 54 | + { |
| 55 | + if (f.Name.Equals(word)) |
| 56 | + { |
| 57 | + var sign = CreateSignature(item, f.Description); |
| 58 | + signatures.Append(sign); |
| 59 | + } |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + ShaderlabDataManager.Instance.UnityBuiltinFunctions.ForEach(f => |
| 64 | + { |
| 65 | + foreach (var item in f.Synopsis) |
| 66 | + { |
| 67 | + if (f.Name.Equals(word)) |
| 68 | + { |
| 69 | + var sign = CreateSignature(item, f.Description); |
| 70 | + signatures.Append(sign); |
| 71 | + } |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + return new SignatureHelp |
| 76 | + { |
| 77 | + Signatures = signatures |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + protected override SignatureHelpRegistrationOptions CreateRegistrationOptions(SignatureHelpCapability capability, ClientCapabilities clientCapabilities) |
| 82 | + { |
| 83 | + return new SignatureHelpRegistrationOptions() |
| 84 | + { |
| 85 | + DocumentSelector = _documentSelector, |
| 86 | + TriggerCharacters = new[] { "(", ",", "<", "{", "[", "," } |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + private SignatureInformation CreateSignature(string sign, string documentation) |
| 91 | + { |
| 92 | + var signature = new SignatureInformation |
| 93 | + { |
| 94 | + Documentation = documentation, |
| 95 | + Label = sign, |
| 96 | + Parameters = new() |
| 97 | + }; |
| 98 | + |
| 99 | + //find the parameters in the method signature (expect methodname(one, two) |
| 100 | + string[] pars = sign.Split(new char[] { '(', ',', ')', ';' }, StringSplitOptions.RemoveEmptyEntries); |
| 101 | + |
| 102 | + int locusSearchStart = 0; |
| 103 | + for (int i = 1; i < pars.Length; ++i) |
| 104 | + { |
| 105 | + string param = pars[i].Trim(); |
| 106 | + |
| 107 | + if (string.IsNullOrEmpty(param)) |
| 108 | + continue; |
| 109 | + |
| 110 | + int locusStart = sign.IndexOf(param, locusSearchStart); |
| 111 | + |
| 112 | + if (locusStart >= 0) |
| 113 | + { |
| 114 | + _logger.LogWarning("param: " + param); |
| 115 | + |
| 116 | + var newPararm = new ParameterInformation() |
| 117 | + { |
| 118 | + Documentation = "", |
| 119 | + Label = param |
| 120 | + }; |
| 121 | + |
| 122 | + signature.Parameters.Append(newPararm); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return signature; |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments