Example #1
0
        public static Dictionary <string, TextEdit[]> Rename(int index, string new_text, Document doc)
        {
            IEnumerable <Location>          locations = LanguageServer.Module.FindRefsAndDefs(index, doc);
            Dictionary <string, TextEdit[]> result    = new Dictionary <string, TextEdit[]>();
            IEnumerable <Document>          documents = locations.Select(r => r.Uri).OrderBy(q => q).Distinct();

            foreach (Document f in documents)
            {
                string fn = f.FullPath;
                IOrderedEnumerable <Location> per_file_changes = locations.Where(z => z.Uri == f).OrderBy(q => q.Range.Start.Value);
                StringBuilder sb       = new StringBuilder();
                int           previous = 0;
                string        code     = f.Code;
                foreach (Location l in per_file_changes)
                {
                    Document d   = l.Uri;
                    string   xx  = d.FullPath;
                    Range    r   = l.Range;
                    string   pre = code.Substring(previous, r.Start.Value - previous);
                    sb.Append(pre);
                    sb.Append(new_text);
                    previous = r.End.Value + 1;
                }
                string rest = code.Substring(previous);
                sb.Append(rest);
                string           new_code = sb.ToString();
                List <TextEdit>  edits    = new List <TextEdit>();
                diff_match_patch diff     = new diff_match_patch();
                List <Diff>      diffs    = diff.diff_main(code, new_code);
                List <Patch>     patch    = diff.patch_make(diffs);
                int times = 0;
                int delta = 0;
                foreach (Patch p in patch)
                {
                    times++;
                    int start  = p.start1 - delta;
                    int offset = 0;
                    foreach (Diff ed in p.diffs)
                    {
                        if (ed.operation == Operation.EQUAL)
                        {
                            //// Let's verify that.
                            int len = ed.text.Length;
                            //var tokenSpan = new SnapshotSpan(buffer.CurrentSnapshot,
                            //  new Span(start + offset, len));
                            //var tt = tokenSpan.GetText();
                            //if (ed.text != tt)
                            //{ }
                            offset = offset + len;
                        }
                        else if (ed.operation == Operation.DELETE)
                        {
                            int len = ed.text.Length;
                            //var tokenSpan = new SnapshotSpan(buffer.CurrentSnapshot,
                            //  new Span(start + offset, len));
                            //var tt = tokenSpan.GetText();
                            //if (ed.text != tt)
                            //{ }
                            TextEdit edit = new TextEdit()
                            {
                                range = new Workspaces.Range(
                                    new Workspaces.Index(start + offset),
                                    new Workspaces.Index(start + offset + len)),
                                NewText = ""
                            };
                            offset = offset + len;
                            edits.Add(edit);
                        }
                        else if (ed.operation == Operation.INSERT)
                        {
                            int      len  = ed.text.Length;
                            TextEdit edit = new TextEdit()
                            {
                                range = new Workspaces.Range(
                                    new Workspaces.Index(start + offset),
                                    new Workspaces.Index(start + offset)),
                                NewText = ed.text
                            };
                            edits.Add(edit);
                        }
                    }
                    delta = delta + (p.length2 - p.length1);
                }
                TextEdit[] e = edits.ToArray();
                result.Add(fn, e);
            }
            return(result);
        }
Example #2
0
        public static TextEdit[] Reformat(Document doc)
        {
            ParserDetails ref_pd          = ParserDetailsFactory.Create(doc);
            string        code            = doc.Code;
            string        corpus_location = Options.Option.GetString("CorpusLocation");

            if (corpus_location == null)
            {
                TextEdit[] result = new TextEdit[] { };
                return(result);
            }

            string ffn = doc.FullPath;

            if (ffn == null)
            {
                TextEdit[] result = new TextEdit[] { };
                return(result);
            }
            IGrammarDescription grammar_description = LanguageServer.GrammarDescriptionFactory.Create(ffn);

            if (grammar_description == null)
            {
                TextEdit[] result = new TextEdit[] { };
                return(result);
            }
            org.antlr.codebuff.Tool.unformatted_input = code;
            try
            {
                string result = org.antlr.codebuff.Tool.Main(
                    new object[]
                {
                    "-g", grammar_description.Name,
                    "-lexer", grammar_description.Lexer,
                    "-parser", grammar_description.Parser,
                    "-rule", grammar_description.StartRule,
                    "-files", grammar_description.FileExtension,
                    "-corpus", corpus_location,
                    "-inoutstring",
                    ""
                });
                List <TextEdit>  edits = new List <TextEdit>();
                diff_match_patch diff  = new diff_match_patch();
                List <Diff>      diffs = diff.diff_main(code, result);
                List <Patch>     patch = diff.patch_make(diffs);
                //patch.Reverse();

                // Start edit session.
                int times = 0;
                int delta = 0;
                foreach (Patch p in patch)
                {
                    times++;
                    int start = p.start1 - delta;

                    int offset = 0;
                    foreach (Diff ed in p.diffs)
                    {
                        if (ed.operation == Operation.EQUAL)
                        {
                            //// Let's verify that.
                            int len = ed.text.Length;
                            //var tokenSpan = new SnapshotSpan(buffer.CurrentSnapshot,
                            //  new Span(start + offset, len));
                            //var tt = tokenSpan.GetText();
                            //if (ed.text != tt)
                            //{ }
                            offset = offset + len;
                        }
                        else if (ed.operation == Operation.DELETE)
                        {
                            int len = ed.text.Length;
                            //var tokenSpan = new SnapshotSpan(buffer.CurrentSnapshot,
                            //  new Span(start + offset, len));
                            //var tt = tokenSpan.GetText();
                            //if (ed.text != tt)
                            //{ }
                            TextEdit edit = new TextEdit()
                            {
                                range = new Workspaces.Range(
                                    new Workspaces.Index(start + offset),
                                    new Workspaces.Index(start + offset + len)),
                                NewText = ""
                            };
                            offset = offset + len;
                            edits.Add(edit);
                        }
                        else if (ed.operation == Operation.INSERT)
                        {
                            int      len  = ed.text.Length;
                            TextEdit edit = new TextEdit()
                            {
                                range = new Workspaces.Range(
                                    new Workspaces.Index(start + offset),
                                    new Workspaces.Index(start + offset)),
                                NewText = ed.text
                            };
                            edits.Add(edit);
                        }
                    }
                    delta = delta + (p.length2 - p.length1);
                }
                return(edits.ToArray());
            }
            catch (Exception)
            {
                TextEdit[] result = new TextEdit[] { };
                return(result);
            }
        }