Esempio n. 1
0
        /// <summary>
        /// Converts output from a command-line diff tool into LSP compatible text edits.
        /// </summary>
        public static TextEdit[] GetEdits(string documentText, string diffOutputText)
        {
            if (documentText == null)
            {
                throw new ArgumentNullException(nameof(documentText));
            }

            if (diffOutputText == null)
            {
                throw new ArgumentNullException(nameof(diffOutputText));
            }

            // Diff tools may print a header with the original/modified file names
            // and that header must be removed for diff_match_patch.
            if (diffOutputText.StartsWithOrdinal("---"))
            {
                var startIndex = diffOutputText.IndexOfOrdinal("@@");
                if (startIndex >= 0)
                {
                    diffOutputText = diffOutputText.Substring(startIndex);
                }

                // TODO: needed?
                // Remove the text added by unified_diff
                // # Work around missing newline (http://bugs.python.org/issue2142).
                //patch = patch.replace(/\\ No newline at end of file[\r\n] /, '');
            }

            var patches = new diff_match_patch().patch_fromText(diffOutputText);

            var edits = patches.SelectMany(p => GetEdits(documentText, p));

            return(edits.ToArray());
        }