Example #1
0
        internal IReadOnlyList <DocumentChange> RemoveImports()
        {
            ScopeStatement targetStmt = null;

            if (!_allScopes)
            {
                var enclosingNodeWalker = new EnclosingNodeWalker(_ast, _index, _index);
                _ast.Walk(enclosingNodeWalker);
                targetStmt = enclosingNodeWalker.Target.Parents[enclosingNodeWalker.Target.Parents.Count - 1];
            }

            var walker = new ImportWalker(_ast, targetStmt);

            _ast.Walk(walker);

            var changes = new List <DocumentChange>();

            foreach (var removeInfo in walker.GetToRemove())
            {
                // see if we're removing some or all of the
                //var node = removeInfo.Node;
                var removing = removeInfo.ToRemove;
                var removed  = removeInfo.Statement;
                UpdatedStatement updatedStatement = removed.InitialStatement;

                int removeCount = 0;
                for (int i = 0, curRemoveIndex = 0; i < removed.NameCount; i++)
                {
                    if (removed.IsRemoved(i, removing))
                    {
                        removeCount++;
                        updatedStatement = updatedStatement.RemoveName(_ast, curRemoveIndex);
                    }
                    else
                    {
                        curRemoveIndex++;
                    }
                }

                var span = removed.Node.GetSpan(_ast);
                if (removeCount == removed.NameCount)
                {
                    removeInfo.SiblingCount.Value--;

                    DeleteStatement(changes, span, removeInfo.SiblingCount.Value == 0);
                }
                else
                {
                    var newCode = updatedStatement.ToCodeString(_ast);

                    int proceedingLength = (removed.LeadingWhitespace ?? "").Length;

                    changes.Add(DocumentChange.Replace(new SourceSpan(
                                                           _ast.IndexToLocation(removed.Node.StartIndex - proceedingLength),
                                                           span.End
                                                           ), newCode));
                }
            }
            return(changes.ToArray());
        }
Example #2
0
        internal void RemoveImports()
        {
            ScopeStatement targetStmt = null;

            if (!_allScopes)
            {
                var enclosingNodeWalker = new EnclosingNodeWalker(_ast, _view.Caret.Position.BufferPosition, _view.Caret.Position.BufferPosition);
                _ast.Walk(enclosingNodeWalker);
                targetStmt = enclosingNodeWalker.Target.Parents[enclosingNodeWalker.Target.Parents.Length - 1];
            }

            var walker = new ImportWalker(targetStmt);

            _ast.Walk(walker);

            using (var edit = _view.TextBuffer.CreateEdit()) {
                foreach (var removeInfo in walker.GetToRemove())
                {
                    // see if we're removing some or all of the
                    //var node = removeInfo.Node;
                    var removing = removeInfo.ToRemove;
                    var removed  = removeInfo.Statement;
                    UpdatedStatement updatedStatement = removed.InitialStatement;

                    int removeCount = 0;
                    for (int i = 0, curRemoveIndex = 0; i < removed.NameCount; i++)
                    {
                        if (removed.IsRemoved(i, removing))
                        {
                            removeCount++;
                            updatedStatement = updatedStatement.RemoveName(_ast, curRemoveIndex);
                        }
                        else
                        {
                            curRemoveIndex++;
                        }
                    }

                    var span = removed.Node.GetSpan(_ast);
                    if (removeCount == removed.NameCount)
                    {
                        removeInfo.SiblingCount.Value--;

                        DeleteStatement(edit, span, removeInfo.SiblingCount.Value == 0);
                    }
                    else
                    {
                        var newCode = updatedStatement.ToCodeString(_ast);

                        int proceedingLength = (removed.Node.GetLeadingWhiteSpace(_ast) ?? "").Length;
                        int start            = span.Start.Index - proceedingLength;
                        int length           = span.Length + proceedingLength;

                        edit.Delete(start, length);
                        edit.Insert(span.Start.Index, newCode);
                    }
                }

                edit.Apply();
            }
        }
Example #3
0
        internal AP.ChangeInfo[] RemoveImports()
        {
            ScopeStatement targetStmt = null;

            if (!_allScopes)
            {
                var enclosingNodeWalker = new EnclosingNodeWalker(_ast, _index, _index);
                _ast.Walk(enclosingNodeWalker);
                targetStmt = enclosingNodeWalker.Target.Parents[enclosingNodeWalker.Target.Parents.Length - 1];
            }

            var walker = new ImportWalker(targetStmt);

            _ast.Walk(walker);

            List <AP.ChangeInfo> changes = new List <AnalysisProtocol.ChangeInfo>();

            foreach (var removeInfo in walker.GetToRemove())
            {
                // see if we're removing some or all of the
                //var node = removeInfo.Node;
                var removing = removeInfo.ToRemove;
                var removed  = removeInfo.Statement;
                UpdatedStatement updatedStatement = removed.InitialStatement;

                int removeCount = 0;
                for (int i = 0, curRemoveIndex = 0; i < removed.NameCount; i++)
                {
                    if (removed.IsRemoved(i, removing))
                    {
                        removeCount++;
                        updatedStatement = updatedStatement.RemoveName(_ast, curRemoveIndex);
                    }
                    else
                    {
                        curRemoveIndex++;
                    }
                }

                var span = removed.Node.GetSpan(_ast);
                if (removeCount == removed.NameCount)
                {
                    removeInfo.SiblingCount.Value--;

                    DeleteStatement(changes, span, removeInfo.SiblingCount.Value == 0);
                }
                else
                {
                    var newCode = updatedStatement.ToCodeString(_ast);

                    int proceedingLength = (removed.Node.GetLeadingWhiteSpace(_ast) ?? "").Length;
                    int start            = _ast.LocationToIndex(span.Start) - proceedingLength;
                    int length           = _ast.GetSpanLength(span) + proceedingLength;

                    changes.Add(
                        new AP.ChangeInfo()
                    {
                        start   = start,
                        length  = length,
                        newText = ""
                    }
                        );
                    changes.Add(
                        new AP.ChangeInfo()
                    {
                        start   = _ast.LocationToIndex(span.Start),
                        length  = 0,
                        newText = newCode
                    }
                        );
                }
            }
            return(changes.ToArray());
        }