コード例 #1
0
        private void RemoveVariable(Declaration target)
        {
            Selection selection;
            var       declarationText      = target.Context.GetText().Replace(" _" + Environment.NewLine, string.Empty);
            var       multipleDeclarations = target.HasMultipleDeclarationsInStatement();

            var variableStmtContext = target.GetVariableStmtContext();

            if (!multipleDeclarations)
            {
                declarationText = variableStmtContext.GetText().Replace(" _" + Environment.NewLine, string.Empty);
                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 = _vbe.ActiveCodePane.CodeModule.GetLines(selection);

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

            if (multipleDeclarations)
            {
                selection = target.GetVariableStmtContextSelection();
                newLines  = RemoveExtraComma(_vbe.ActiveCodePane.CodeModule.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;
            }

            var pane   = _vbe.ActiveCodePane;
            var module = pane.CodeModule;
            {
                module.DeleteLines(selection);
                module.InsertLines(selection.StartLine, string.Join(Environment.NewLine, newLinesWithoutExcessSpaces));
            }
        }
コード例 #2
0
        private static void RemoveDeclarationOnly(this ICodeModule module, Declaration target)
        {
            var multipleDeclarations = target.DeclarationType == DeclarationType.Variable && target.HasMultipleDeclarationsInStatement();
            var context         = GetStmtContext(target);
            var declarationText = context.GetText().Replace(" _" + Environment.NewLine, Environment.NewLine);
            var selection       = GetStmtContextSelection(target);

            Debug.Assert(selection.StartColumn > 0);

            var oldLines = module.GetLines(selection);
            var indent   = oldLines.IndexOf(oldLines.FirstOrDefault(c => c != ' ')) + 1;

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

            if (multipleDeclarations)
            {
                selection = GetStmtContextSelection(target);
                newLines  = RemoveExtraComma(module.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;
            }

            // remove all lines with only whitespace
            newLinesWithoutExcessSpaces = newLinesWithoutExcessSpaces.Where(str => str.Any(c => !char.IsWhiteSpace(c))).ToArray();

            module.DeleteLines(selection);
            if (newLinesWithoutExcessSpaces.Any())
            {
                module.InsertLines(selection.StartLine, string.Join(Environment.NewLine, newLinesWithoutExcessSpaces));
            }
        }
コード例 #3
0
        private void RemoveField(Declaration target)
        {
            Selection selection;
            var       declarationText      = target.Context.GetText().Replace(" _" + Environment.NewLine, string.Empty);
            var       multipleDeclarations = target.HasMultipleDeclarationsInStatement();

            var variableStmtContext = target.GetVariableStmtContext();

            if (!multipleDeclarations)
            {
                declarationText = variableStmtContext.GetText().Replace(" _" + Environment.NewLine, string.Empty);
                selection       = target.GetVariableStmtContextSelection();
            }
            else
            {
                selection = new Selection(target.Context.Start.Line, target.Context.Start.Column,
                                          target.Context.Stop.Line, target.Context.Stop.Column);
            }

            var module = target.QualifiedName.QualifiedModuleName.Component.CodeModule;
            {
                var oldLines = module.GetLines(selection.StartLine, selection.LineCount);
                var newLines = oldLines.Replace(" _" + Environment.NewLine, string.Empty)
                               .Remove(selection.StartColumn, declarationText.Length);

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

                var adjustedLines =
                    newLines.Split(new[] { Environment.NewLine }, StringSplitOptions.None)
                    .Select(s => s.EndsWith(" _") ? s.Remove(s.Length - 2) : s)
                    .Where(s => s.Trim() != string.Empty)
                    .ToList();

                module.DeleteLines(selection.StartLine, selection.LineCount);

                if (adjustedLines.Any())
                {
                    module.InsertLines(selection.StartLine, string.Join(string.Empty, adjustedLines));
                }
            }
        }
コード例 #4
0
        private void RemoveField(Declaration target)
        {
            Selection selection;
            var       declarationText      = target.Context.GetText().Replace(" _" + Environment.NewLine, string.Empty);
            var       multipleDeclarations = target.HasMultipleDeclarationsInStatement();

            var variableStmtContext = target.GetVariableStmtContext();

            if (!multipleDeclarations)
            {
                declarationText = variableStmtContext.GetText().Replace(" _" + Environment.NewLine, string.Empty);
                selection       = target.GetVariableStmtContextSelection();
            }
            else
            {
                selection = new Selection(target.Context.Start.Line, target.Context.Start.Column,
                                          target.Context.Stop.Line, target.Context.Stop.Column);
            }

            var pane   = _vbe.ActiveCodePane;
            var module = pane.CodeModule;
            {
                var oldLines = module.GetLines(selection);

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

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

                newLines = newLines.Replace(" _" + Environment.NewLine, string.Empty);

                module.DeleteLines(selection);

                if (newLines.Trim() != string.Empty)
                {
                    module.InsertLines(selection.StartLine, newLines);
                }
            }
        }
コード例 #5
0
        private void RemoveVariable(Declaration target)
        {
            Selection selection;
            var       declarationText      = target.Context.GetText().Replace(" _" + Environment.NewLine, string.Empty);
            var       multipleDeclarations = target.DeclarationType == DeclarationType.Variable && target.HasMultipleDeclarationsInStatement();

            if (!multipleDeclarations)
            {
                declarationText = GetStmtContext(target).GetText().Replace(" _" + Environment.NewLine, string.Empty);
                selection       = GetStmtContextSelection(target);
            }
            else
            {
                selection = new Selection(target.Context.Start.Line, target.Context.Start.Column,
                                          target.Context.Stop.Line, target.Context.Stop.Column);
            }

            var codeModule = target.QualifiedName.QualifiedModuleName.Component.CodeModule;
            var oldLines   = codeModule.GetLines(selection);

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

            if (multipleDeclarations)
            {
                selection = GetStmtContextSelection(target);
                newLines  = RemoveExtraComma(codeModule.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;
            }

            // remove all lines with only whitespace
            newLinesWithoutExcessSpaces = newLinesWithoutExcessSpaces.Where(str => str.Any(c => !char.IsWhiteSpace(c))).ToArray();

            codeModule.DeleteLines(selection);
            if (newLinesWithoutExcessSpaces.Any())
            {
                codeModule.InsertLines(selection.StartLine,
                                       string.Join(Environment.NewLine, newLinesWithoutExcessSpaces));
            }
        }