Beispiel #1
0
        protected override async Task Tokenize(
            SemanticTokensBuilder builder, ITextDocumentIdentifierParams identifier,
            CancellationToken cancellationToken
            )
        {
            var hash = Hash.StringHash(identifier.TextDocument.Uri.GetFileSystemPath());

            var    now            = DateTime.Now;
            IntPtr tokensPtr      = IntPtr.Zero;
            int    count          = 0;
            long   internalMicros = TreeSitter.GetTokens(hash, out tokensPtr, out count); // this can be async because it actually does work.
            var    then           = DateTime.Now;
            var    elapsed        = then - now;

            _logger.LogInformation("Elapsed time for C++ tokens: " + elapsed.TotalMilliseconds + " native time: " + internalMicros);

            List <Diagnostic> diagnostics = new List <Diagnostic>();

            unsafe
            {
                SemanticToken *ptr = (SemanticToken *)tokensPtr;
                for (int i = 0; i < count; i++)
                {
                    if ((int)ptr[i].type == 255)
                    {
                        Diagnostic diag = new Diagnostic();
                        diag.Severity    = DiagnosticSeverity.Error;
                        diag.Range       = new OmniSharp.Extensions.LanguageServer.Protocol.Models.Range();
                        diag.Range.Start = new Position(ptr[i].line, ptr[i].col);
                        diag.Range.End   = new Position(ptr[i].line, ptr[i].col + ptr[i].length);
                        diag.Message     = "undeclared identifer";
                        diagnostics.Add(diag);

                        continue;
                    }

                    builder.Push(ptr[i].line, ptr[i].col, ptr[i].length, (int)ptr[i].type, (int)ptr[i].modifier);
                }
            }

            diagnoser.Add(identifier.TextDocument.Uri, 0, diagnostics);
            diagnoser.Publish(identifier.TextDocument.Uri);
        }
Beispiel #2
0
        public override Task <SignatureHelp> Handle(SignatureHelpParams request, CancellationToken cancellationToken)
        {
            var currentHash = Hash.StringHash(request.TextDocument.Uri.GetFileSystemPath());

            var pos = request.Position;

            TreeSitter.GetSignature(currentHash, pos.Line, pos.Character, out var signatureArrayPtr, out var parameterCount, out var activeParameter, out var errorCount, out var errorRanges);

            if (signatureArrayPtr.ToInt64() == 0)
            {
                return(Task.FromResult(new SignatureHelp()));
            }

            var signaturePtr = System.Runtime.InteropServices.Marshal.ReadIntPtr(signatureArrayPtr);
            var signature    = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(signaturePtr);


            SignatureInformation info = new SignatureInformation();

            info.Label = signature;


            var paramList = new List <ParameterInformation>();

            if (parameterCount > 0)
            {
                for (int i = 0; i < parameterCount; i++)
                {
                    var paramInfo = new ParameterInformation();
                    var paramPtr  = System.Runtime.InteropServices.Marshal.ReadIntPtr(signatureArrayPtr + 8 * (i + 1));;
                    paramInfo.Label = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(paramPtr);
                    paramList.Add(paramInfo);
                }
            }

            info.Parameters = new Container <ParameterInformation>(paramList);


            SignatureHelp help = new SignatureHelp();

            help.Signatures      = new Container <SignatureInformation>(info);
            help.ActiveParameter = activeParameter;
            help.ActiveSignature = 0;


            if (errorCount > 0)
            {
                List <Diagnostic>        diagnostics       = new List <Diagnostic>();
                PublishDiagnosticsParams diagnosticsParams = new PublishDiagnosticsParams();
                diagnosticsParams.Uri = request.TextDocument.Uri;
                unsafe
                {
                    for (int i = 0; i < errorCount; i++)
                    {
                        Range *    errors     = (Range *)errorRanges;
                        var        error      = errors[i];
                        Diagnostic diagnostic = new Diagnostic();
                        diagnostic.Message = "Extra argument";
                        diagnostic.Range   = new OmniSharp.Extensions.LanguageServer.Protocol.Models.Range(
                            new Position(error.startLine, error.startCol),
                            new Position(error.endLine, error.endCol));
                        diagnostics.Add(diagnostic);
                    }
                }

                diagnoser.Add(request.TextDocument.Uri, 1, diagnostics);
            }
            else
            {
                diagnoser.Add(request.TextDocument.Uri, 1, new List <Diagnostic>());
            }

            return(Task.FromResult(help));
        }