Search in sources :

Example 1 with AddTokenAndImportStatement

use of com.python.pydev.analysis.refactoring.quick_fixes.AddTokenAndImportStatement in project Pydev by fabioz.

the class PyMoveImportsToLocalCompletionProposal method apply.

@Override
public void apply(IDocument doc) {
    RefactoringRequest req = refactoringRequest;
    final IPyRefactoring2 r = (IPyRefactoring2) AbstractPyRefactoring.getPyRefactoring();
    if (req.qualifier != null && req.qualifier.trim().length() > 0) {
        try {
            final Map<Tuple<String, File>, HashSet<ASTEntry>> occurrences = r.findAllOccurrences(req);
            final Set<Entry<Tuple<String, File>, HashSet<ASTEntry>>> entrySet = occurrences.entrySet();
            final MultiTextEdit multiTextEdit = new MultiTextEdit();
            final IDocument document = req.getDoc();
            final Set<Integer> appliedContextLines = new HashSet<Integer>();
            for (Map.Entry<Tuple<String, File>, HashSet<ASTEntry>> o : entrySet) {
                HashSet<ASTEntry> entries = o.getValue();
                ASTEntry[] ordered = entries.toArray(new ASTEntry[0]);
                Arrays.sort(ordered, (entry0, entry1) -> {
                    // Note: order is reversed.
                    return Integer.compare(entry1.node.beginLine, entry0.node.beginLine);
                });
                for (ASTEntry entry : entries) {
                    if (entry.node != null) {
                        int beginLine = entry.node.beginLine;
                        int useLine = beginLine - 1;
                        if (useLine >= importHandleInfo.getStartLine() && useLine <= importHandleInfo.getEndLine()) {
                            // Skip the import itself.
                            continue;
                        }
                        String currLine = TextSelectionUtils.getLine(document, useLine);
                        if (!currLine.isEmpty() && !Character.isWhitespace(currLine.charAt(0))) {
                            // Skip global occurrences of the token
                            continue;
                        }
                        for (int i = useLine; i < document.getNumberOfLines(); i++) {
                            String line = TextSelectionUtils.getLine(document, i);
                            if (!line.trim().isEmpty()) {
                                if (Character.isWhitespace(line.charAt(0))) {
                                    useLine = i;
                                    break;
                                }
                            }
                        }
                        boolean addLocalImport = true;
                        boolean addLocalImportsOnTopOfMethod = true;
                        boolean groupImports = false;
                        int offset = new PySelection(req.ps.getDoc(), useLine, 0).getAbsoluteCursorOffset();
                        int maxCols = 200;
                        char trigger = ' ';
                        String fromImportStr = importHandleInfo.getFromImportStr();
                        String realImportRep;
                        if (fromImportStr == null || fromImportStr.isEmpty()) {
                            realImportRep = "import " + this.importedToken;
                        } else {
                            realImportRep = "from " + fromImportStr + " import " + this.importedToken;
                        }
                        int fReplacementOffset = offset;
                        int fLen = 0;
                        String indentString = "               ";
                        this.fReplacementString = "";
                        AddTokenAndImportStatement.ComputedInfo computedInfo = new AddTokenAndImportStatement.ComputedInfo(realImportRep, fReplacementOffset, fLen, indentString, fReplacementString, appliedWithTrigger, importLen, document);
                        this.appliedWithTrigger = computedInfo.appliedWithTrigger;
                        this.importLen = computedInfo.importLen;
                        AddTokenAndImportStatement t = new AddTokenAndImportStatement(document, trigger, offset, addLocalImport, addLocalImportsOnTopOfMethod, groupImports, maxCols);
                        LineStartingScope previousLineThatStartsScope = t.getPreviousLineThatStartsScope();
                        if (previousLineThatStartsScope != null) {
                            if (appliedContextLines.contains(previousLineThatStartsScope.iLineStartingScope)) {
                                continue;
                            }
                            appliedContextLines.add(previousLineThatStartsScope.iLineStartingScope);
                            t.createTextEdit(computedInfo);
                            for (ReplaceEdit edit : computedInfo.replaceEdit) {
                                multiTextEdit.addChild(edit);
                            }
                        }
                    }
                }
            }
            try {
                multiTextEdit.apply(document);
            } catch (Exception e) {
                Log.log(e);
            }
        } catch (OperationCanceledException | CoreException e) {
            Log.log(e);
        }
    }
}
Also used : RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) AddTokenAndImportStatement(com.python.pydev.analysis.refactoring.quick_fixes.AddTokenAndImportStatement) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IPyRefactoring2(org.python.pydev.ast.refactoring.IPyRefactoring2) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) Entry(java.util.Map.Entry) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) HashSet(java.util.HashSet) Point(org.eclipse.swt.graphics.Point) LineStartingScope(org.python.pydev.core.docutils.PySelection.LineStartingScope) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) CoreException(org.eclipse.core.runtime.CoreException) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) PySelection(org.python.pydev.core.docutils.PySelection) File(java.io.File) Map(java.util.Map) Tuple(org.python.pydev.shared_core.structure.Tuple) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) IDocument(org.eclipse.jface.text.IDocument)

Example 2 with AddTokenAndImportStatement

use of com.python.pydev.analysis.refactoring.quick_fixes.AddTokenAndImportStatement in project Pydev by fabioz.

the class CtxInsensitiveImportComplProposal method apply.

protected void apply(IDocument document, char trigger, int stateMask, int offset, IAdaptable projectAdaptable) {
    PriorityLRU.appliedCompletion(realImportRep);
    if (this.indentString == null) {
        throw new RuntimeException("Indent string not set (not called with a PyEdit as viewer?)");
    }
    if (!triggerCharAppliesCurrentCompletion(trigger, document, offset)) {
        // +1 because that's the len of the trigger
        newForcedOffset = offset + 1;
        return;
    }
    final int maxCols;
    if (SharedCorePlugin.inTestMode()) {
        maxCols = 80;
    } else {
        IPreferenceStore chainedPrefStore = PyDevUiPrefs.getChainedPrefStore();
        maxCols = chainedPrefStore.getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN_COLUMN);
    }
    AddTokenAndImportStatement.ComputedInfo computedInfo = new AddTokenAndImportStatement.ComputedInfo(realImportRep, fReplacementOffset, fLen, indentString, fReplacementString, appliedWithTrigger, importLen, document);
    new AddTokenAndImportStatement(document, trigger, offset, addLocalImport, getAddLocalImportsOnTopOfMethod(), ImportsPreferencesPage.getGroupImports(projectAdaptable), maxCols).createTextEdit(computedInfo);
    this.fReplacementString = computedInfo.fReplacementString;
    this.appliedWithTrigger = computedInfo.appliedWithTrigger;
    this.importLen = computedInfo.importLen;
    for (ReplaceEdit edit : computedInfo.replaceEdit) {
        try {
            edit.apply(document);
        } catch (Exception e) {
            Log.log(e);
        }
    }
}
Also used : AddTokenAndImportStatement(com.python.pydev.analysis.refactoring.quick_fixes.AddTokenAndImportStatement) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) Point(org.eclipse.swt.graphics.Point)

Aggregations

AddTokenAndImportStatement (com.python.pydev.analysis.refactoring.quick_fixes.AddTokenAndImportStatement)2 Point (org.eclipse.swt.graphics.Point)2 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)2 File (java.io.File)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 CoreException (org.eclipse.core.runtime.CoreException)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)1 IDocument (org.eclipse.jface.text.IDocument)1 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)1 IPyRefactoring2 (org.python.pydev.ast.refactoring.IPyRefactoring2)1 RefactoringRequest (org.python.pydev.ast.refactoring.RefactoringRequest)1 PySelection (org.python.pydev.core.docutils.PySelection)1 LineStartingScope (org.python.pydev.core.docutils.PySelection.LineStartingScope)1 ASTEntry (org.python.pydev.parser.visitors.scope.ASTEntry)1 Tuple (org.python.pydev.shared_core.structure.Tuple)1