Ejemplo n.º 1
0
        private void ExtractMethod(ExtractMethodModel model)
        {
            var selection = model.Selection.Selection;

            _editor.DeleteLines(selection);
            _editor.InsertLines(selection.StartLine, GetMethodCall(model));

            var insertionLine = model.SourceMember.Context.GetSelection().EndLine - selection.LineCount + 2;

            _editor.InsertLines(insertionLine, GetExtractedMethod(model));

            // assumes these are declared *before* the selection...
            var offset = 0;

            foreach (var declaration in model.DeclarationsToMove.OrderBy(e => e.Selection.StartLine))
            {
                var target = new Selection(
                    declaration.Selection.StartLine - offset,
                    declaration.Selection.StartColumn,
                    declaration.Selection.EndLine - offset,
                    declaration.Selection.EndColumn);

                _editor.DeleteLines(target);
                offset += declaration.Selection.LineCount;
            }
        }
Ejemplo n.º 2
0
        private void RemoveVariable(Declaration target)
        {
            Selection selection;
            var       declarationText      = target.Context.GetText();
            var       multipleDeclarations = target.HasMultipleDeclarationsInStatement();

            var variableStmtContext = target.GetVariableStmtContext();

            if (!multipleDeclarations)
            {
                declarationText = variableStmtContext.GetText();
                selection       = target.GetVariableStmtContextSelection();
            }
            else
            {
                selection = new Selection(target.Context.Start.Line, target.Context.Start.Column,
                                          target.Context.Stop.Line, target.Context.Stop.Column);
            }

            var oldLines = _editor.GetLines(selection);

            var newLines = oldLines.Replace(" _" + Environment.NewLine, string.Empty)
                           .Remove(selection.StartColumn, declarationText.Length);

            if (multipleDeclarations)
            {
                selection = target.GetVariableStmtContextSelection();
                newLines  = RemoveExtraComma(_editor.GetLines(selection).Replace(oldLines, newLines),
                                             target.CountOfDeclarationsInStatement(), target.IndexOfVariableDeclarationInStatement());
            }

            var newLinesWithoutExcessSpaces = newLines.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

            for (var i = 0; i < newLinesWithoutExcessSpaces.Length; i++)
            {
                newLinesWithoutExcessSpaces[i] = newLinesWithoutExcessSpaces[i].RemoveExtraSpacesLeavingIndentation();
            }

            for (var i = newLinesWithoutExcessSpaces.Length - 1; i >= 0; i--)
            {
                if (newLinesWithoutExcessSpaces[i].Trim() == string.Empty)
                {
                    continue;
                }

                if (newLinesWithoutExcessSpaces[i].EndsWith(" _"))
                {
                    newLinesWithoutExcessSpaces[i] =
                        newLinesWithoutExcessSpaces[i].Remove(newLinesWithoutExcessSpaces[i].Length - 2);
                }
                break;
            }

            _editor.DeleteLines(selection);
            _editor.InsertLines(selection.StartLine, string.Join(Environment.NewLine, newLinesWithoutExcessSpaces));
        }
Ejemplo n.º 3
0
        private void SetFieldToPrivate(CodeModule module)
        {
            if (_model.TargetDeclaration.Accessibility == Accessibility.Private)
            {
                return;
            }

            RemoveField(_model.TargetDeclaration);

            var newField = "Private " + _model.TargetDeclaration.IdentifierName + " As " +
                           _model.TargetDeclaration.AsTypeName;

            module.InsertLines(module.CountOfDeclarationLines + 1, newField);

            _editor.SetSelection(_model.TargetDeclaration.QualifiedSelection);
            for (var index = 1; index <= module.CountOfDeclarationLines; index++)
            {
                if (module.Lines[index, 1].Trim() == string.Empty)
                {
                    _editor.DeleteLines(new Selection(index, 0, index, 0));
                }
            }
        }