示例#1
0
            public override object VisitLocalVariableDeclaration(LocalVariableDeclaration localVariableDeclaration, object data)
            {
                //				Console.WriteLine ("LocalVariableDeclaration: " + localVariableDeclaration.StartLocation.ToString () + " - " + localVariableDeclaration.EndLocation.ToString ());
                localVariableDeclaration.TypeReference.AcceptVisitor(this, data);
                foreach (VariableDeclaration o in localVariableDeclaration.Variables)
                {
                    if (o.Name == ((IntegrateTemporaryVariableVisitorOptions)data).GetName())
                    {
                        IntegrateTemporaryVariableVisitorOptions options = (IntegrateTemporaryVariableVisitorOptions)data;
                        options.Initializer = localVariableDeclaration.GetVariableDeclaration(((LocalVariable)options.Options.SelectedItem).Name).Initializer;
                        if (localVariableDeclaration.Variables.Count == 1)
                        {
                            TextReplaceChange change = new TextReplaceChange();
                            change.Description = string.Format(GettextCatalog.GetString("Deleting local variable declaration {0}"), options.GetName());
                            change.FileName    = options.Options.Document.FileName;

                            change.Offset = options.Options.Document.TextEditor.GetPositionFromLineColumn(localVariableDeclaration.StartLocation.Line + ((LocalVariable)options.Options.SelectedItem).DeclaringMember.BodyRegion.Start.Line, localVariableDeclaration.StartLocation.Column);
                            int end = options.Options.Document.TextEditor.GetPositionFromLineColumn(localVariableDeclaration.EndLocation.Line + ((LocalVariable)options.Options.SelectedItem).DeclaringMember.BodyRegion.Start.Line, localVariableDeclaration.EndLocation.Column);

                            change.RemovedChars = end - change.Offset;
                            change.InsertedText = "";
                            ((IntegrateTemporaryVariableVisitorOptions)data).Changes.Add(change);
                        }
                        else
                        {
                            TextReplaceChange change = new TextReplaceChange();
                            change.Description = string.Format(GettextCatalog.GetString("Deleting local variable declaration {0}"), options.GetName());
                            change.FileName    = options.Options.Document.FileName;

                            change.Offset = options.Options.Document.TextEditor.GetPositionFromLineColumn(localVariableDeclaration.StartLocation.Line + ((LocalVariable)options.Options.SelectedItem).DeclaringMember.BodyRegion.Start.Line, localVariableDeclaration.StartLocation.Column);
                            int end = options.Options.Document.TextEditor.GetPositionFromLineColumn(localVariableDeclaration.EndLocation.Line + ((LocalVariable)options.Options.SelectedItem).DeclaringMember.BodyRegion.Start.Line, localVariableDeclaration.EndLocation.Column);

                            change.RemovedChars = end - change.Offset;
                            localVariableDeclaration.Variables.Remove(localVariableDeclaration.GetVariableDeclaration(options.GetName()));
                            INRefactoryASTProvider provider = options.Options.GetASTProvider();
                            change.InsertedText = options.Options.GetWhitespaces(change.Offset) + provider.OutputNode(options.Options.Dom, localVariableDeclaration);
                            ((IntegrateTemporaryVariableVisitorOptions)data).Changes.Add(change);
                        }
                    }
                    else
                    {
                        o.AcceptVisitor(this, data);
                    }
                }
                return(null);
            }
            public override object VisitLocalVariableDeclaration(LocalVariableDeclaration localVariableDeclaration, object data)
            {
                //				Console.WriteLine ("LocalVariableDeclaration: " + localVariableDeclaration.StartLocation.ToString () + " - " + localVariableDeclaration.EndLocation.ToString ());
                localVariableDeclaration.TypeReference.AcceptVisitor(this, data);
                foreach (VariableDeclaration o in localVariableDeclaration.Variables)
                {
                    if (o.Name == ((IntegrateTemporaryVariableVisitorOptions)data).GetName())
                    {
                        IntegrateTemporaryVariableVisitorOptions options = (IntegrateTemporaryVariableVisitorOptions)data;
                        options.Initializer = localVariableDeclaration.GetVariableDeclaration(((LocalVariable)options.Options.SelectedItem).Name).Initializer;
                        if (localVariableDeclaration.Variables.Count == 1)
                        {
                            TextReplaceChange change = new TextReplaceChange();
                            change.Description = string.Format(GettextCatalog.GetString("Deleting local variable declaration {0}"), options.GetName());
                            change.FileName    = options.Options.Document.FileName;
                            int lineNumber = localVariableDeclaration.StartLocation.Line + ((LocalVariable)options.Options.SelectedItem).DeclaringMember.BodyRegion.Start.Line;
                            change.Offset = options.Options.Document.Editor.Document.LocationToOffset(lineNumber, localVariableDeclaration.StartLocation.Column);
                            int end = options.Options.Document.Editor.Document.LocationToOffset(localVariableDeclaration.EndLocation.Line + ((LocalVariable)options.Options.SelectedItem).DeclaringMember.BodyRegion.Start.Line, localVariableDeclaration.EndLocation.Column);
                            change.RemovedChars = end - change.Offset;
                            // check if whole line can be removed.
                            var line = options.Options.Document.Editor.GetLine(lineNumber);
                            if (line.GetIndentation(options.Options.Document.Editor.Document).Length == localVariableDeclaration.StartLocation.Column - 1)
                            {
                                bool isEmpty = true;
                                for (int i = end; i < line.EndOffset; i++)
                                {
                                    if (!char.IsWhiteSpace(options.Options.Document.Editor.GetCharAt(i)))
                                    {
                                        isEmpty = false;
                                        break;
                                    }
                                }
                                if (isEmpty)
                                {
                                    change.Offset       = line.Offset;
                                    change.RemovedChars = line.Length;
                                }
                            }
                            change.InsertedText = "";
                            ((IntegrateTemporaryVariableVisitorOptions)data).Changes.Add(change);
                        }
                        else
                        {
                            TextReplaceChange change = new TextReplaceChange();
                            change.Description = string.Format(GettextCatalog.GetString("Deleting local variable declaration {0}"), options.GetName());
                            change.FileName    = options.Options.Document.FileName;

                            change.Offset = options.Options.Document.Editor.Document.LocationToOffset(localVariableDeclaration.StartLocation.Line + ((LocalVariable)options.Options.SelectedItem).DeclaringMember.BodyRegion.Start.Line, localVariableDeclaration.StartLocation.Column);
                            int end = options.Options.Document.Editor.Document.LocationToOffset(localVariableDeclaration.EndLocation.Line + ((LocalVariable)options.Options.SelectedItem).DeclaringMember.BodyRegion.Start.Line, localVariableDeclaration.EndLocation.Column);

                            change.RemovedChars = end - change.Offset;
                            localVariableDeclaration.Variables.Remove(localVariableDeclaration.GetVariableDeclaration(options.GetName()));
                            INRefactoryASTProvider provider = options.Options.GetASTProvider();
                            change.InsertedText = options.Options.GetWhitespaces(change.Offset) + provider.OutputNode(options.Options.Dom, localVariableDeclaration);
                            ((IntegrateTemporaryVariableVisitorOptions)data).Changes.Add(change);
                        }
                    }
                    else
                    {
                        o.AcceptVisitor(this, data);
                    }
                }
                return(null);
            }