示例#1
0
        public void TestReplaceParserLiterals()
        {
            var      cwd      = Directory.GetCurrentDirectory();
            Document document = CheckDoc("../../../../corpus-for-codebuff/keywordfun.g4"); // purposefully erroneously all lc.
            // Convert all string literals on RHS of lexer rule into uc/lc equivalent.
            int line      = 0;
            int character = 0;
            int index     = new LanguageServer.Module().GetIndex(line, character, document);
            var found     = LanguageServer.Transform.ReplaceLiterals(index, index, document);

            if (found.Count != 1)
            {
                throw new Exception();
            }
            var should_be = @"grammar KeywordFun;

a : A;
b : B;

A: 'abc';
B: 'def';
C: 'uvw' 'xyz'?;
D: 'uvw' 'xyz'+;
";
            var got       = found.First().Value;

            if (got != should_be)
            {
                throw new Exception();
            }
        }
示例#2
0
        public static Document CheckDoc(string path)
        {
            string   file_name = path;
            Document document  = Workspaces.Workspace.Instance.FindDocument(file_name);

            if (document == null)
            {
                document = new Workspaces.Document(file_name);
                try
                {   // Open the text file using a stream reader.
                    using (StreamReader sr = new StreamReader(file_name))
                    {
                        // Read the stream to a string, and write the string to the console.
                        string str = sr.ReadToEnd();
                        document.Code = str;
                    }
                }
                catch (IOException)
                {
                }
                Project project = Workspaces.Workspace.Instance.FindProject("Misc");
                if (project == null)
                {
                    project = new Project("Misc", "Misc", "Misc");
                    Workspaces.Workspace.Instance.AddChild(project);
                }
                project.AddDocument(document);
            }
            document.Changed = true;
            _ = ParsingResultsFactory.Create(document);
            var workspace = document.Workspace;

            _ = new LanguageServer.Module().Compile(workspace);
            return(document);
        }
示例#3
0
        public void TestFindDef3()
        {
            var      cwd      = Directory.GetCurrentDirectory();
            Document document = CheckDoc("../../../../UnitTestProject1/ANTLRv3.g3");
            // Position at the "grammarSpec" rule, beginning of RHS symbol "grammarDecl".
            // All lines and columns are zero based in LSP.
            int line      = 88;
            int character = 40;
            int index     = new LanguageServer.Module().GetIndex(line, character, document);

            (int, int)back = new LanguageServer.Module().GetLineColumn(index, document);
            if (back.Item1 != line || back.Item2 != character)
            {
                throw new Exception();
            }
            IList <Location> found = new LanguageServer.Module().FindDefs(index, document);

            if (found.Count != 1)
            {
                throw new Exception();
            }
            (int, int)back_start = new LanguageServer.Module().GetLineColumn(found.First().Range.Start.Value, document);
            if (back_start.Item1 != 96 || back_start.Item2 != 0)
            {
                throw new Exception();
            }
            (int, int)back_end = new LanguageServer.Module().GetLineColumn(found.First().Range.End.Value, document);
            if (back_end.Item1 != 96 || back_end.Item2 != 9)
            {
                throw new Exception();
            }
        }
示例#4
0
        public void TestFindDefBison()
        {
            var      cwd       = Directory.GetCurrentDirectory();
            Document document  = CheckDoc("../../../../UnitTestProject1/calc.y");
            int      line      = 8;
            int      character = 7;
            int      index     = new LanguageServer.Module().GetIndex(line, character, document);

            (int, int)back = new LanguageServer.Module().GetLineColumn(index, document);
            if (back.Item1 != line || back.Item2 != character)
            {
                throw new Exception();
            }
            IList <Location> found = new LanguageServer.Module().FindDefs(index, document);

            if (found.Count != 1)
            {
                throw new Exception();
            }
            (int, int)back_start = new LanguageServer.Module().GetLineColumn(found.First().Range.Start.Value, document);
            if (back_start.Item1 != 8 || back_start.Item2 != 7)
            {
                throw new Exception();
            }
            (int, int)back_end = new LanguageServer.Module().GetLineColumn(found.First().Range.End.Value, document);
            if (back_end.Item1 != 8 || back_end.Item2 != 9)
            {
                throw new Exception();
            }
        }
示例#5
0
        public void TestFindDef4()
        {
            var      cwd       = Directory.GetCurrentDirectory();
            Document lexer_doc = CheckDoc("../../../../LanguageServer/ANTLRv4Lexer.g4");
            Document document  = CheckDoc("../../../../LanguageServer/ANTLRv4Parser.g4");
            int      line      = 50;
            int      character = 18;
            int      index     = new LanguageServer.Module().GetIndex(line, character, document);

            (int, int)back = new LanguageServer.Module().GetLineColumn(index, document);
            if (back.Item1 != line || back.Item2 != character)
            {
                throw new Exception();
            }
            IList <Location> found = new LanguageServer.Module().FindDefs(index, document);

            if (found.Count != 1)
            {
                throw new Exception();
            }
            (int, int)back_start = new LanguageServer.Module().GetLineColumn(found.First().Range.Start.Value, document);
            if (back_start.Item1 != 53 || back_start.Item2 != 0)
            {
                throw new Exception();
            }
            (int, int)back_end = new LanguageServer.Module().GetLineColumn(found.First().Range.End.Value, document);
            if (back_end.Item1 != 53 || back_end.Item2 != 10)
            {
                throw new Exception();
            }
        }
示例#6
0
        public void TestIndexQuickInfo2()
        {
            var      cwd      = Directory.GetCurrentDirectory();
            Document document = CheckDoc("../../../../UnitTestProject1/ANTLRv2.g2");
            // Position at the "grammarSpec" rule, beginning of LHS symbol.
            // All lines and columns are zero based in LSP.
            int line      = 94;
            int character = 10;
            int index     = new LanguageServer.Module().GetIndex(line, character, document);

            (int, int)back = new LanguageServer.Module().GetLineColumn(index, document);
            if (back.Item1 != line || back.Item2 != character)
            {
                throw new Exception();
            }
            QuickInfo quick_info = new LanguageServer.Module().GetQuickInfo(index, document);

            if (quick_info == null)
            {
                throw new Exception();
            }
            (int, int)back_start = new LanguageServer.Module().GetLineColumn(quick_info.Range.Start.Value, document);
            if (back_start.Item1 != line || back_start.Item2 != character)
            {
                throw new Exception();
            }
            (int, int)back_end = new LanguageServer.Module().GetLineColumn(quick_info.Range.End.Value, document);
            if (back_end.Item1 != line || back_end.Item2 != character + 8)
            {
                throw new Exception();
            }
        }
示例#7
0
        public void TestFindAllRefs()
        {
            var      cwd      = Directory.GetCurrentDirectory();
            Document document = CreateStringDocument(@"grammar A;

s
    : e
    ;

e
    : e '*' e       # Mult
    | INT           # primary
    ;

INT
    : [0-9]+
    ;

WS
    : [ \t\n]+ -> skip
    ;
");
            // Position at the "grammarSpec" rule, beginning of RHS symbol "grammarDecl".
            // All lines and columns are zero based in LSP.
            int line      = 3;
            int character = 6;
            int index     = new LanguageServer.Module().GetIndex(line, character, document);

            (int, int)back = new LanguageServer.Module().GetLineColumn(index, document);
            if (back.Item1 != line || back.Item2 != character)
            {
                throw new Exception();
            }
            var found = new LanguageServer.Module().FindRefsAndDefs(index, document).ToList();

            if (found.Count != 4)
            {
                throw new Exception();
            }
            List <Pair <int, int> > r = new List <Pair <int, int> >()
            {
                new Pair <int, int>(3, 6),
                new Pair <int, int>(6, 0),
                new Pair <int, int>(7, 6),
                new Pair <int, int>(7, 12),
            };
            var ordered_found = found.Select(t => t.Range.Start.Value).OrderBy(t => t).ToList();

            for (int i = 0; i < ordered_found.Count; ++i)
            {
                var start = ordered_found[i];
                (int, int)back_start = new LanguageServer.Module().GetLineColumn(start, document);
                if (back_start.Item1 != r[i].a || back_start.Item2 != r[i].b)
                {
                    throw new Exception();
                }
            }
        }
示例#8
0
        public void TestUnfold()
        {
            var      cwd      = Directory.GetCurrentDirectory();
            Document document = CreateStringDocument(@"grammar A;

s
    : e
    ;

e
    : e '*' e       # Mult
    | INT           # primary
    ;

INT
    : [0-9]+
    ;

WS
    : [ \t\n]+ -> skip
    ;
");
            int      start    = new LanguageServer.Module().GetIndex(2, 0, document);
            int      end      = new LanguageServer.Module().GetIndex(5, 0, document);
            var      found    = LanguageServer.Transform.Unfold(start, end, document);

            if (found.Count != 1)
            {
                throw new Exception();
            }
            var should_be = @"grammar A;

s
    : ( e '*' e | INT )
    ;

e
    : e '*' e       # Mult
    | INT           # primary
    ;

INT
    : [0-9]+
    ;

WS
    : [ \t\n]+ -> skip
    ;
";
            var got       = found.First().Value;

            if (got != should_be)
            {
                throw new Exception();
            }
        }
示例#9
0
        public void ParseDoc(Document document, int quiet_after, string grammar = null)
        {
            document.Changed = true;
            document.ParseAs = grammar;
            var pd = ParsingResultsFactory.Create(document);

            pd.QuietAfter = quiet_after;
            var workspace = document.Workspace;

            _ = new LanguageServer.Module().Compile(workspace);
        }
示例#10
0
        public void TestFold()
        {
            var      cwd      = Directory.GetCurrentDirectory();
            var      original = @"grammar A;

s
    : ( e '*' e | INT )
    ;

e
    : e '*' e       # Mult
    | INT           # primary
    ;

INT
    : [0-9]+
    ;

WS
    : [ \t\n]+ -> skip
    ;
";
            Document document = CreateStringDocument(original);
            int      start    = new LanguageServer.Module().GetIndex(6, 0, document);
            int      end      = new LanguageServer.Module().GetIndex(6, 0, document);
            var      found    = LanguageServer.Transform.Fold(start, end, document);
            //           if (found.Count != 1) throw new Exception();
//            var should_be = @"grammar A;

//s
//    : e
//    ;

//e
//    : e '*' e         # Mult
//    | INT             # primary
//    ;

//INT
//    : [0-9]+
//    ;

//WS
//    : [ \t\n]+ -> skip
//    ;
//";
//           var got = found.First().Value;
//           if (got != should_be) throw new Exception();
        }
示例#11
0
        public void TestIndexQuickInfo4a()
        {
            var      cwd       = Directory.GetCurrentDirectory();
            Document lexer_doc = CheckDoc("../../../../LanguageServer/ANTLRv4Lexer.g4");
            Document document  = CheckDoc("../../../../LanguageServer/ANTLRv4Parser.g4");
            int      line      = 1;
            int      character = 1;
            int      index     = new LanguageServer.Module().GetIndex(line, character, document);

            (int, int)back = new LanguageServer.Module().GetLineColumn(index, document);
            if (back.Item1 != line || back.Item2 != character)
            {
                throw new Exception();
            }
            QuickInfo quick_info = new LanguageServer.Module().GetQuickInfo(index, document);

            if (quick_info != null)
            {
                throw new Exception();
            }
        }
示例#12
0
        public void TestKeywordFun()
        {
            var      cwd      = Directory.GetCurrentDirectory();
            Document document = CheckDoc("../../../../corpus-for-codebuff/keywordfun.g4"); // purposefully erroneously all lc.
            // Convert all string literals on RHS of lexer rule into uc/lc equivalent.
            int line      = 5;
            int character = 0;
            int index     = new LanguageServer.Module().GetIndex(line, character, document);

            (int, int)back = new LanguageServer.Module().GetLineColumn(index, document);
            if (back.Item1 != line || back.Item2 != character)
            {
                throw new Exception();
            }
            int indexs = new LanguageServer.Module().GetIndex(5, 0, document);
            int indexe = new LanguageServer.Module().GetIndex(6, 0, document);
            var found  = LanguageServer.Transform.UpperLowerCaseLiteral(indexs, indexe, document);

            if (found.Count != 1)
            {
                throw new Exception();
            }
            var should_be = @"grammar KeywordFun;

a : 'abc';
b : 'def';

A: [aA] [bB] [cC];
B: 'def';
C: 'uvw' 'xyz'?;
D: 'uvw' 'xyz'+;
";
            var got       = found.First().Value;

            if (got != should_be)
            {
                throw new Exception();
            }
        }
示例#13
0
        public static Document CreateStringDocument(string input)
        {
            string   file_name = "Dummy" + random_number + ".g4";
            Document document  = Workspaces.Workspace.Instance.FindDocument(file_name);

            if (document == null)
            {
                document      = new Workspaces.Document(file_name);
                document.Code = input;
                Project project = Workspaces.Workspace.Instance.FindProject("Misc");
                if (project == null)
                {
                    project = new Project("Misc", "Misc", "Misc");
                    Workspaces.Workspace.Instance.AddChild(project);
                }
                project.AddDocument(document);
            }
            document.Changed = true;
            _ = ParsingResultsFactory.Create(document);
            var workspace = document.Workspace;

            _ = new LanguageServer.Module().Compile(workspace);
            return(document);
        }
示例#14
0
        private void MenuItemCallback(bool forward)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            try
            {
                ////////////////////////
                /// Next rule.
                ////////////////////////

                if (!(((IServiceProvider)ServiceProvider).GetService(typeof(VsTextManagerClass)) is IVsTextManager manager))
                {
                    return;
                }
                manager.GetActiveView(1, null, out IVsTextView view);
                if (view == null)
                {
                    return;
                }
                view.GetCaretPos(out int l, out int c);
                view.GetBuffer(out IVsTextLines buf);
                if (buf == null)
                {
                    return;
                }
                ITextBuffer buffer = AntlrLanguageClient.AdaptersFactory.GetWpfTextView(view)?.TextBuffer;
                string      ffn    = buffer.GetFFN();
                if (ffn == null)
                {
                    return;
                }
                Workspaces.Document document = Workspaces.Workspace.Instance.FindDocument(ffn);
                if (document == null)
                {
                    return;
                }
                int pos     = new LanguageServer.Module().GetIndex(l, c, document);
                int new_pos = AntlrLanguageClient.CMNextSymbol(ffn, pos, forward);
                if (new_pos < 0)
                {
                    return;
                }
                List <IToken> where = new List <IToken>();
                List <ParsingResults> where_details = new List <ParsingResults>();
                IVsTextView           vstv          = IVsTextViewExtensions.FindTextViewFor(ffn);
                if (vstv == null)
                {
                    return;
                }
                IWpfTextView wpftv = AntlrLanguageClient.AdaptersFactory.GetWpfTextView(vstv);
                if (wpftv == null)
                {
                    return;
                }
                vstv.GetLineAndColumn(new_pos, out int line_number, out int colum_number);
                ITextSnapshot cc = wpftv.TextBuffer.CurrentSnapshot;
                SnapshotSpan  ss = new SnapshotSpan(cc, new_pos, 1);
                SnapshotPoint sp = ss.Start;
                // Put cursor on symbol.
                wpftv.Caret.MoveTo(sp);
                if (line_number > 0)
                {
                    vstv.CenterLines(line_number - 1, 2);
                }
                else
                {
                    vstv.CenterLines(line_number, 1);
                }
            }
            catch (Exception exception)
            {
                Logger.Log.Notify(exception.StackTrace);
            }
        }
示例#15
0
        public void SendDiagnostics(List <DiagnosticInfo> list)
        {
            var files = list.Select(l => l.Document).OrderBy(q => q).Distinct().ToList();

            // If the computed set is empty it has to push the empty array to clear former diagnostics.
            foreach (var file in files)
            {
                PublishDiagnosticParams parameter = new PublishDiagnosticParams
                {
                    Uri         = file,
                    Diagnostics = Array.Empty <Diagnostic>()
                };
                _ = rpc.NotifyWithParameterObjectAsync(Methods.TextDocumentPublishDiagnosticsName, parameter);
            }
            foreach (var file in files)
            {
                List <Diagnostic> diagnostics = new List <Diagnostic>();
                foreach (var info in list)
                {
                    DiagnosticSeverity severity = default;
                    switch (info.Severify)
                    {
                    case DiagnosticInfo.Severity.Info:
                        severity = DiagnosticSeverity.Information;
                        break;

                    case DiagnosticInfo.Severity.Warning:
                        severity = DiagnosticSeverity.Warning;
                        break;

                    case DiagnosticInfo.Severity.Error:
                        severity = DiagnosticSeverity.Error;
                        break;
                    }
                    var document = target.CheckDoc(info.Document);
                    (int, int)bs = new LanguageServer.Module().GetLineColumn(info.Start, document);
                    (int, int)be = new LanguageServer.Module().GetLineColumn(info.End, document);
                    Diagnostic diagnostic = new Diagnostic
                    {
                        Message  = info.Message,
                        Severity = severity,
                        Range    = new LspTypes.Range
                        {
                            Start = new Position(bs.Item1, bs.Item2),
                            End   = new Position(be.Item1, be.Item2)
                        },
                        Code = "Test" + Enum.GetName(typeof(DiagnosticSeverity), severity)
                    };
                    diagnostics.Add(diagnostic);
                }

                PublishDiagnosticParams parameter = new PublishDiagnosticParams
                {
                    Uri         = file,
                    Diagnostics = diagnostics.ToArray()
                };
                if (maxProblems > -1)
                {
                    parameter.Diagnostics = parameter.Diagnostics.Take(maxProblems).ToArray();
                }
                _ = rpc.NotifyWithParameterObjectAsync(Methods.TextDocumentPublishDiagnosticsName, parameter);
            }
        }
示例#16
0
        private void MenuItemCallback(bool visitor)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            try
            {
                ////////////////////////
                /// Go to visitor.
                ////////////////////////

                if (!(((IServiceProvider)ServiceProvider).GetService(typeof(VsTextManagerClass)) is IVsTextManager manager))
                {
                    return;
                }
                manager.GetActiveView(1, null, out IVsTextView view);
                if (view == null)
                {
                    return;
                }
                view.GetCaretPos(out int l, out int c);
                view.GetBuffer(out IVsTextLines buf);
                if (buf == null)
                {
                    return;
                }
                ITextBuffer buffer   = AntlrLanguageClient.AdaptersFactory.GetWpfTextView(view)?.TextBuffer;
                bool        is_enter = CtrlKeyState.GetStateForView(AntlrLanguageClient.AdaptersFactory.GetWpfTextView(view)).Enabled;
                string      orig_ffn = buffer.GetFFN();
                if (orig_ffn == null)
                {
                    return;
                }
                Workspaces.Document document = Workspaces.Workspace.Instance.FindDocument(orig_ffn);
                if (document == null)
                {
                    return;
                }
                int          pos    = new LanguageServer.Module().GetIndex(l, c, document);
                CMGotoResult symbol = null;
                symbol = visitor ? AntlrLanguageClient.CMGotoVisitor(orig_ffn, pos, is_enter)
                    : AntlrLanguageClient.CMGotoListener(orig_ffn, pos, is_enter);
                if (symbol == null)
                {
                    return;
                }

                {
                    string class_file_path = symbol.TextDocument.LocalPath;
                    int    index           = symbol.Start;
                    // Open to this line in editor.
                    IVsTextView vstv = IVsTextViewExtensions.FindTextViewFor(class_file_path);
                    {
                        IVsTextViewExtensions.ShowFrame(class_file_path);
                        vstv = IVsTextViewExtensions.FindTextViewFor(class_file_path);
                    }

                    IWpfTextView wpftv = AntlrLanguageClient.AdaptersFactory.GetWpfTextView(vstv);
                    if (wpftv == null)
                    {
                        return;
                    }

                    // Create new span in the appropriate view.
                    ITextSnapshot cc = wpftv.TextBuffer.CurrentSnapshot;
                    vstv.GetLineAndColumn(index, out int line_number, out int colum_number);

                    // Put cursor on symbol.
                    wpftv.Caret.MoveTo(new SnapshotPoint(cc, index)); // This sets cursor, bot does not center.
                                                                      // Center on cursor.
                                                                      //wpftv.Caret.EnsureVisible(); // This works, sort of. It moves the scroll bar, but it does not CENTER! Does not really work!
                    if (line_number > 0)
                    {
                        vstv.CenterLines(line_number - 1, 2);
                    }
                    else
                    {
                        vstv.CenterLines(line_number, 1);
                    }

                    return;
                }
            }
            catch (Exception exception)
            {
                Logger.Log.Notify(exception.StackTrace);
            }
        }