public void RenameVariable(ILuaIntellisenseDocument document)
        {
            VariableExpression expression;
            List<LuatValue.IReference> definitions;
            if (!CanRenameVariable(document, out expression, out definitions))
                return;

            using (var dialog = new RenameVariableForm())
            {
                dialog.InputText = expression.DisplayText;

                var se = (ActiproSoftware.SyntaxEditor.SyntaxEditor)document.SyntaxEditorControl;
                if (dialog.ShowDialog(se) != DialogResult.OK)
                    return;

                if (CheckVariableNameInUse(expression, dialog.InputText))
                {
                    const string message = "A variable with this name already exists. Do you want to continue?";
                    const string caption = "Warning";

                    if (MessageBox.Show(message, caption, MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.Cancel)
                        return;
                }

                try
                {
                    // save cursor and scroll pos to set it back to where it was after we are done replacing
                    //int caretPos = se.Caret.Offset;
                    //int scrollPos = se.SelectedView.FirstVisibleDisplayLineIndex;

                    var options =
                        new FindReplaceOptions
                            {
                                FindText = expression.DisplayText,
                                MatchCase = true,
                                MatchWholeWord = true,
                                ReplaceText = dialog.InputText,
                                SearchHiddenText = true,
                                SearchType = FindReplaceSearchType.Normal
                            };

                    se.Document.FindReplace.ReplaceAll(options);

                    //// remove duplicates (for instance, if scripts are referenced multiple times in a project)
                    //for (var i = 0; i < definitions.Count; ++i)
                    //{
                    //    var original = definitions[i];

                    //    var duplicates = new List<LuatValue.IReference>();
                    //    for (var j = i + 1; j < definitions.Count; ++j)
                    //    {
                    //        var potentialDuplicate = definitions[j];

                    //        if ((original.Line == potentialDuplicate.Line) &&
                    //            (original.TextRange.StartOffset == potentialDuplicate.TextRange.StartOffset) &&
                    //            (original.TextRange.EndOffset == potentialDuplicate.TextRange.EndOffset) &&
                    //            (string.Compare(original.Path, potentialDuplicate.Path, true) == 0))
                    //        {
                    //            duplicates.Add(potentialDuplicate);
                    //        }
                    //    }

                    //    foreach (LuatValue.IReference duplicate in duplicates)
                    //        definitions.Remove(duplicate);
                    //}

                    //// remove spans that are included in larger spans
                    //for (var i = 0; i < definitions.Count; ++i)
                    //{
                    //    var original = definitions[i];

                    //    var duplicates = new List<LuatValue.IReference>();
                    //    foreach (var potentialDuplicate in definitions)
                    //    {
                    //        if (ReferenceEquals(original, potentialDuplicate))
                    //            continue;

                    //        if (string.Compare(original.Path, potentialDuplicate.Path, true) != 0)
                    //            continue;

                    //        if ((potentialDuplicate.TextRange.StartOffset >= original.TextRange.StartOffset) &&
                    //            (potentialDuplicate.TextRange.EndOffset <= original.TextRange.EndOffset))
                    //        {
                    //            duplicates.Add(potentialDuplicate);
                    //        }
                    //    }

                    //    foreach (LuatValue.IReference duplicate in duplicates)
                    //        definitions.Remove(duplicate);
                    //}

                    //// sort the references so we replace the text bottom to top to avoid invalidating the
                    //// other references text range
                    //definitions.Sort(
                    //    (a, b) =>
                    //    {
                    //        TextRange aRange = ((SyntaxEditorTextRange)a.TextRange).ToTextRange();
                    //        TextRange bRange = ((SyntaxEditorTextRange)b.TextRange).ToTextRange();

                    //        return bRange.FirstOffset.CompareTo(aRange.FirstOffset);
                    //    });

                    //foreach (LuatValue.IReference reference in definitions)
                    //{
                    //    TextRange textRange = ((SyntaxEditorTextRange)reference.TextRange).ToTextRange();
                    //    se.Document.ReplaceText(DocumentModificationType.Replace, textRange, dialog.InputText);
                    //}

                    //// adjust the position of the cursor based on changes made to the document
                    //int diff = dialog.InputText.Length - expression.DisplayText.Length;
                    //caretPos += (diff * (definitions.Count - 1));

                    //se.Caret.Offset = caretPos;
                    //se.SelectedView.FirstVisibleDisplayLineIndex = scrollPos;
                }
                catch (Exception ex)
                {
                    ex.ToString();
                }
            }
        }
        public void RenameVariable(ILuaIntellisenseDocument document)
        {
            VariableExpression          expression;
            List <LuatValue.IReference> definitions;

            if (!CanRenameVariable(document, out expression, out definitions))
            {
                return;
            }

            using (var dialog = new RenameVariableForm())
            {
                dialog.InputText = expression.DisplayText;

                var se = (ActiproSoftware.SyntaxEditor.SyntaxEditor)document.SyntaxEditorControl;
                if (dialog.ShowDialog(se) != DialogResult.OK)
                {
                    return;
                }

                if (CheckVariableNameInUse(expression, dialog.InputText))
                {
                    const string message = "A variable with this name already exists. Do you want to continue?";
                    const string caption = "Warning";

                    if (MessageBox.Show(message, caption, MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.Cancel)
                    {
                        return;
                    }
                }

                try
                {
                    // save cursor and scroll pos to set it back to where it was after we are done replacing
                    //int caretPos = se.Caret.Offset;
                    //int scrollPos = se.SelectedView.FirstVisibleDisplayLineIndex;

                    var options =
                        new FindReplaceOptions
                    {
                        FindText         = expression.DisplayText,
                        MatchCase        = true,
                        MatchWholeWord   = true,
                        ReplaceText      = dialog.InputText,
                        SearchHiddenText = true,
                        SearchType       = FindReplaceSearchType.Normal
                    };

                    se.Document.FindReplace.ReplaceAll(options);

                    //// remove duplicates (for instance, if scripts are referenced multiple times in a project)
                    //for (var i = 0; i < definitions.Count; ++i)
                    //{
                    //    var original = definitions[i];

                    //    var duplicates = new List<LuatValue.IReference>();
                    //    for (var j = i + 1; j < definitions.Count; ++j)
                    //    {
                    //        var potentialDuplicate = definitions[j];

                    //        if ((original.Line == potentialDuplicate.Line) &&
                    //            (original.TextRange.StartOffset == potentialDuplicate.TextRange.StartOffset) &&
                    //            (original.TextRange.EndOffset == potentialDuplicate.TextRange.EndOffset) &&
                    //            (string.Compare(original.Path, potentialDuplicate.Path, true) == 0))
                    //        {
                    //            duplicates.Add(potentialDuplicate);
                    //        }
                    //    }

                    //    foreach (LuatValue.IReference duplicate in duplicates)
                    //        definitions.Remove(duplicate);
                    //}

                    //// remove spans that are included in larger spans
                    //for (var i = 0; i < definitions.Count; ++i)
                    //{
                    //    var original = definitions[i];

                    //    var duplicates = new List<LuatValue.IReference>();
                    //    foreach (var potentialDuplicate in definitions)
                    //    {
                    //        if (ReferenceEquals(original, potentialDuplicate))
                    //            continue;

                    //        if (string.Compare(original.Path, potentialDuplicate.Path, true) != 0)
                    //            continue;

                    //        if ((potentialDuplicate.TextRange.StartOffset >= original.TextRange.StartOffset) &&
                    //            (potentialDuplicate.TextRange.EndOffset <= original.TextRange.EndOffset))
                    //        {
                    //            duplicates.Add(potentialDuplicate);
                    //        }
                    //    }

                    //    foreach (LuatValue.IReference duplicate in duplicates)
                    //        definitions.Remove(duplicate);
                    //}

                    //// sort the references so we replace the text bottom to top to avoid invalidating the
                    //// other references text range
                    //definitions.Sort(
                    //    (a, b) =>
                    //    {
                    //        TextRange aRange = ((SyntaxEditorTextRange)a.TextRange).ToTextRange();
                    //        TextRange bRange = ((SyntaxEditorTextRange)b.TextRange).ToTextRange();

                    //        return bRange.FirstOffset.CompareTo(aRange.FirstOffset);
                    //    });

                    //foreach (LuatValue.IReference reference in definitions)
                    //{
                    //    TextRange textRange = ((SyntaxEditorTextRange)reference.TextRange).ToTextRange();
                    //    se.Document.ReplaceText(DocumentModificationType.Replace, textRange, dialog.InputText);
                    //}

                    //// adjust the position of the cursor based on changes made to the document
                    //int diff = dialog.InputText.Length - expression.DisplayText.Length;
                    //caretPos += (diff * (definitions.Count - 1));

                    //se.Caret.Offset = caretPos;
                    //se.SelectedView.FirstVisibleDisplayLineIndex = scrollPos;
                }
                catch (Exception ex)
                {
                    ex.ToString();
                }
            }
        }