use of org.python.pydev.editor.PyEdit in project Pydev by fabioz.
the class UnitTestLaunchShortcut method launch.
@Override
public void launch(IEditorPart editor, String mode) {
this.arguments = "";
if (editor instanceof PyEdit) {
PyEdit pyEdit = (PyEdit) editor;
PySelection ps = pyEdit.createPySelection();
String selectedText;
try {
selectedText = ps.getSelectedText();
} catch (BadLocationException e) {
selectedText = "";
}
if (selectedText.length() > 0) {
String last = null;
FastStringBuffer buf = new FastStringBuffer();
List<stmtType> path = FastParser.parseToKnowGloballyAccessiblePath(ps.getDoc(), ps.getStartLineIndex());
for (stmtType stmtType : path) {
if (buf.length() > 0) {
buf.append('.');
}
last = NodeUtils.getRepresentationString(stmtType);
buf.append(last);
}
if (last != null) {
if (last.equals(selectedText)) {
this.arguments = buf.toString();
}
}
}
}
super.launch(editor, mode);
}
use of org.python.pydev.editor.PyEdit in project Pydev by fabioz.
the class DebugEditorBasedOnNatureTypeAction method run.
@Override
public void run(IAction action) {
PyEdit pyEdit = getPyEdit();
final Tuple<String, IInterpreterManager> launchConfigurationTypeAndInterpreterManager = this.getLaunchConfigurationTypeAndInterpreterManager(pyEdit, false);
AbstractLaunchShortcut shortcut = new AbstractLaunchShortcut() {
@Override
protected String getLaunchConfigurationType() {
return launchConfigurationTypeAndInterpreterManager.o1;
}
@Override
protected IInterpreterManager getInterpreterManager(IProject project) {
return launchConfigurationTypeAndInterpreterManager.o2;
}
};
shortcut.launch(pyEdit, "debug");
}
use of org.python.pydev.editor.PyEdit in project Pydev by fabioz.
the class ClearSyntaxMarkersPyeditListener method onDispose.
@Override
public void onDispose(BaseEditor baseEditor, IProgressMonitor monitor) {
// remove the markers if we want problems only in the active editor.
PyEdit edit = (PyEdit) baseEditor;
IEditorInput input = edit.getEditorInput();
removeMarkersFromInput(input);
}
use of org.python.pydev.editor.PyEdit in project Pydev by fabioz.
the class PythonCorrectionProcessor method computeQuickAssistProposals.
@Override
public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext invocationContext) {
int offset = invocationContext.getOffset();
PySelection base = edit.createPySelection();
if (!(this.edit instanceof PyEdit) || base == null) {
return new ICompletionProposal[0];
}
PyEdit editor = (PyEdit) this.edit;
List<ICompletionProposalHandle> results = new ArrayList<>();
String sel = PyAction.getLineWithoutComments(base);
List<IAssistProps> assists = new ArrayList<IAssistProps>();
synchronized (PythonCorrectionProcessor.additionalAssists) {
for (IAssistProps prop : additionalAssists.values()) {
assists.add(prop);
}
}
assists.add(new AssistSurroundWith());
assists.add(new AssistImport());
assists.add(new AssistDocString());
assists.add(new AssistAssign());
assists.add(new AssistPercentToFormat());
assists.add(new AssistImportToLocal());
assists.add(new AssistFString());
assists.addAll(ExtensionHelper.getParticipants(ExtensionHelper.PYDEV_CTRL_1));
IImageCache imageCache = SharedUiPlugin.getImageCache();
File editorFile = edit.getEditorFile();
IPythonNature pythonNature = null;
try {
pythonNature = edit.getPythonNature();
} catch (MisconfigurationException e1) {
Log.log(e1);
}
for (IAssistProps assist : assists) {
// Always create a new for each assist, as any given assist may change it.
PySelection ps = new PySelection(base);
try {
if (assist.isValid(ps, sel, editor, offset)) {
try {
results.addAll(assist.getProps(ps, imageCache, editorFile, pythonNature, editor, offset));
} catch (Exception e) {
Log.log(e);
}
}
} catch (Exception e) {
Log.log(e);
}
}
Collections.sort(results, new ProposalsComparator("", new ProposalsComparator.CompareContext(pythonNature)));
try {
// handling spelling... (we only want to show spelling fixes if a spell problem annotation is found at the current location).
// we'll only show some spelling proposal if there's some spelling problem (so, we don't have to check the preferences at this place,
// as no annotations on spelling will be here if the spelling is not enabled).
ICompletionProposal[] spellProps = null;
IAnnotationModel annotationModel = editor.getPySourceViewer().getAnnotationModel();
Iterator<Annotation> it = annotationModel.getAnnotationIterator();
while (it.hasNext()) {
Annotation annotation = it.next();
if (annotation instanceof SpellingAnnotation) {
SpellingAnnotation spellingAnnotation = (SpellingAnnotation) annotation;
SpellingProblem spellingProblem = spellingAnnotation.getSpellingProblem();
int problemOffset = spellingProblem.getOffset();
int problemLen = spellingProblem.getLength();
if (problemOffset <= offset && problemOffset + problemLen >= offset) {
SpellingCorrectionProcessor spellingCorrectionProcessor = new SpellingCorrectionProcessor();
spellProps = spellingCorrectionProcessor.computeQuickAssistProposals(invocationContext);
break;
}
}
}
if (spellProps == null || (spellProps.length == 1 && spellProps[0] instanceof NoCompletionsProposal)) {
// no proposals from the spelling
return results.toArray(new ICompletionProposal[results.size()]);
}
// ok, add the spell problems and return...
ICompletionProposal[] ret = results.toArray(new ICompletionProposal[results.size() + spellProps.length]);
System.arraycopy(spellProps, 0, ret, results.size(), spellProps.length);
return ret;
} catch (Throwable e) {
if (e instanceof ClassNotFoundException || e instanceof LinkageError || e instanceof NoSuchMethodException || e instanceof NoSuchMethodError || e instanceof NoClassDefFoundError) {
// Eclipse 3.2 support
return results.toArray(new ICompletionProposal[results.size()]);
}
throw new RuntimeException(e);
}
}
use of org.python.pydev.editor.PyEdit in project Pydev by fabioz.
the class AssistImportToLocal method getProps.
@Override
public List<ICompletionProposalHandle> getProps(PySelection ps, IImageCache imageCache, File f, IPythonNature nature, IPyEdit edit, int offset) throws BadLocationException, MisconfigurationException {
boolean addOnlyGlobalImports = true;
boolean allowBadInput = false;
Tuple<String, Integer> currToken = ps.getCurrToken();
List<ICompletionProposalHandle> ret = new ArrayList<ICompletionProposalHandle>();
if (currToken.o1 != null && currToken.o1.length() > 0) {
int startOfImportLine = ps.getStartOfImportLine();
if (startOfImportLine == -1) {
return ret;
}
int startOffset = ps.getLineOffset(startOfImportLine);
PyImportsIterator pyImportsIterator = new PyImportsIterator(ps.getDoc(), addOnlyGlobalImports, allowBadInput, startOffset);
OUT: while (pyImportsIterator.hasNext()) {
ImportHandle handle = pyImportsIterator.next();
List<ImportHandleInfo> importInfo = handle.getImportInfo();
for (ImportHandleInfo importHandleInfo : importInfo) {
List<String> importedStr = importHandleInfo.getImportedStr();
int startLine = importHandleInfo.getStartLine();
int endLine = importHandleInfo.getEndLine();
if (ps.getLineOfOffset() < startLine) {
continue;
}
if (ps.getLineOfOffset() > endLine) {
// Stop iterating.
break OUT;
}
for (String s : importedStr) {
if (s.equals(currToken.o1)) {
// Found!
IProgressMonitor monitor = new NullProgressMonitor();
final RefactoringRequest req = PyRefactorAction.createRefactoringRequest(monitor, (PyEdit) edit, ps);
req.setAdditionalInfo(RefactoringRequest.FIND_DEFINITION_IN_ADDITIONAL_INFO, false);
req.setAdditionalInfo(RefactoringRequest.FIND_REFERENCES_ONLY_IN_LOCAL_SCOPE, true);
req.fillActivationTokenAndQualifier();
ret.add(CompletionProposalFactory.get().createMoveImportsToLocalCompletionProposal(req, s, importHandleInfo, imageCache.get(UIConstants.ASSIST_MOVE_IMPORT), "Move import to local scope(s)"));
}
}
}
}
}
return ret;
}
Aggregations