Esempio n. 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());
        }
Esempio n. 2
0
        private void AddMissingImports(List <string> importList)
        {
            if (importList.Count > 0)
            {
                var projEntry = _textView.TextBuffer.GetPythonProjectEntry();
                if (projEntry != null)
                {
                    PythonAst       ast;
                    IAnalysisCookie cookie;
                    projEntry.GetTreeAndCookie(out ast, out cookie);
                    if (ast != null)
                    {
                        var walker = new ImportWalker();
                        ast.Walk(walker);

                        foreach (var import in importList)
                        {
                            if (!walker.Imports.Contains(import))
                            {
                                MissingImportAnalysis.AddImport(
                                    _serviceProvider,
                                    _textView.TextBuffer,
                                    _textView,
                                    null,
                                    import
                                    );
                            }
                        }
                    }
                }
            }
        }
Esempio n. 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 = span.Start.Index - proceedingLength;
                    int length = span.Length + proceedingLength;

                    changes.Add(
                        new AP.ChangeInfo() {
                            start = start,
                            length = length,
                            newText = ""
                        }
                    );
                    changes.Add(
                        new AP.ChangeInfo() {
                            start = span.Start.Index,
                            length = 0,
                            newText = newCode
                        }
                    );
                }
            }
            return changes.ToArray();
        }
Esempio n. 4
0
        internal void RemoveImports()
        {
            ScopeStatement targetStmt = null;
            if (!_allScopes) {
                var enclosingNodeWalker = new EnclosingNodeWalker(_ast, _view.Caret.Position.BufferPosition, _view.Caret.Position.BufferPosition + 1);
                _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 + removed.Node.GetTrailingWhitespace(_ast).Length + proceedingLength;

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

                edit.Apply();
            }
        }
Esempio n. 5
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();
            }
        }
Esempio n. 6
0
        private void AddMissingImports(List<string> importList) {
            if (importList.Count > 0) {
                var projEntry = _textView.TextBuffer.GetPythonProjectEntry();
                if (projEntry != null) {
                    PythonAst ast;
                    IAnalysisCookie cookie;
                    projEntry.GetTreeAndCookie(out ast, out cookie);
                    if (ast != null) {
                        var walker = new ImportWalker();
                        ast.Walk(walker);

                        foreach (var import in importList) {
                            if (!walker.Imports.Contains(import)) {
                                MissingImportAnalysis.AddImport(
                                    _serviceProvider,
                                    _textView.TextBuffer,
                                    _textView,
                                    null,
                                    import
                                );
                            }
                        }
                    }
                }
            }
        }
Esempio n. 7
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());
        }