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);
}
}
}
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);
}
}
}
Aggregations