diff --git a/plugins/org.python.pydev.ast/META-INF/MANIFEST.MF b/plugins/org.python.pydev.ast/META-INF/MANIFEST.MF index cddabdf174..ec7a4e6168 100644 --- a/plugins/org.python.pydev.ast/META-INF/MANIFEST.MF +++ b/plugins/org.python.pydev.ast/META-INF/MANIFEST.MF @@ -33,6 +33,7 @@ Export-Package: org.python.pydev.ast, org.python.pydev.ast.runners, org.python.pydev.ast.simpleassist, org.python.pydev.ast.sort_imports, + org.python.pydev.ast.surround_with, org.python.pydev.core.pep8, org.python.pydev.core.templates, org.python.pydev.plugin, diff --git a/plugins/org.python.pydev/src/org/python/pydev/editor/correctionassist/heuristics/AssistSurroundWith.java b/plugins/org.python.pydev.ast/src/org/python/pydev/ast/surround_with/AssistSurroundWith.java similarity index 78% rename from plugins/org.python.pydev/src/org/python/pydev/editor/correctionassist/heuristics/AssistSurroundWith.java rename to plugins/org.python.pydev.ast/src/org/python/pydev/ast/surround_with/AssistSurroundWith.java index 53294201bb..e8eb22ef68 100644 --- a/plugins/org.python.pydev/src/org/python/pydev/editor/correctionassist/heuristics/AssistSurroundWith.java +++ b/plugins/org.python.pydev.ast/src/org/python/pydev/ast/surround_with/AssistSurroundWith.java @@ -1,46 +1,45 @@ /** - * Copyright (c) 2005-2013 by Appcelerator, Inc. All Rights Reserved. + * Copyright (c) 2024 by Brainwy Software Ltda. All Rights Reserved. * Licensed under the terms of the Eclipse Public License (EPL). * Please see the license.txt included with this distribution for details. * Any modifications to this file must keep this entire header intact. */ -/* - * Created on Apr 12, 2005 - * - * @author Fabio Zadrozny - */ -package org.python.pydev.editor.correctionassist.heuristics; +package org.python.pydev.ast.surround_with; import java.io.File; import java.util.ArrayList; import java.util.List; -import org.eclipse.core.runtime.CoreException; import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.templates.DocumentTemplateContext; import org.eclipse.jface.text.templates.Template; import org.eclipse.jface.text.templates.TemplateContext; -import org.python.pydev.ast.codecompletion.AbstractTemplateCodeCompletion; -import org.python.pydev.ast.codecompletion.CompletionRequest; +import org.eclipse.jface.text.templates.TemplateContextType; import org.python.pydev.core.IAssistProps; import org.python.pydev.core.IPyEdit; import org.python.pydev.core.IPythonNature; -import org.python.pydev.core.TokensOrProposalsList; import org.python.pydev.core.autoedit.DefaultIndentPrefs; import org.python.pydev.core.docutils.PySelection; import org.python.pydev.core.proposals.CompletionProposalFactory; -import org.python.pydev.editor.codecompletion.PyTemplateProposal; +import org.python.pydev.core.templates.PyAddTemplateResolvers; import org.python.pydev.shared_core.code_completion.ICompletionProposalHandle; import org.python.pydev.shared_core.image.IImageCache; import org.python.pydev.shared_core.image.UIConstants; import org.python.pydev.shared_core.string.FastStringBuffer; import org.python.pydev.shared_core.string.StringUtils; -import org.python.pydev.shared_ui.ImageCache; /** * @author Fabio Zadrozny */ -public class AssistSurroundWith extends AbstractTemplateCodeCompletion implements IAssistProps { +public class AssistSurroundWith implements IAssistProps { + + protected TemplateContext createContext(IRegion region, IDocument document) { + TemplateContextType contextType = new TemplateContextType(); + PyAddTemplateResolvers.addDefaultResolvers(contextType); + return new DocumentTemplateContext(contextType, document, region.getOffset(), region.getLength()); + } /** * @throws BadLocationException @@ -107,10 +106,7 @@ public List getProps(PySelection ps, IImageCache imag //region IRegion region = ps.getRegion(); - TemplateContext context = null; - if (edit != null) { - context = createContext(region, ps.getDoc()); - } + TemplateContext context = createContext(region, ps.getDoc()); //not static because we need the actual code. String[] replace0to3 = new String[] { startIndent, delimiter, surroundedCode, delimiter, startIndent, @@ -136,20 +132,8 @@ public List getProps(PySelection ps, IImageCache imag private ICompletionProposalHandle createProposal(PySelection ps, IImageCache imageCache, IPyEdit edit, final String startIndent, IRegion region, int iComp, String comp, TemplateContext context) { Template t = new Template("Surround with", SURROUND_WITH_COMPLETIONS[iComp + 1], "", comp, false); - if (context != null) { - PyTemplateProposal proposal = new PyTemplateProposal(t, context, region, - ImageCache.asImage(imageCache.get(UIConstants.COMPLETION_TEMPLATE)), 5) { - @Override - public String getAdditionalProposalInfo() { - return startIndent + super.getAdditionalProposalInfo(); - } - }; - return proposal; - } else { - //In tests - return CompletionProposalFactory.get().createPyCompletionProposal(comp, region.getOffset(), - region.getLength(), 0, 0); - } + return CompletionProposalFactory.get().createPyTemplateProposal(t, context, region, + imageCache == null ? null : imageCache.get(UIConstants.COMPLETION_TEMPLATE), 0); } /** @@ -163,7 +147,7 @@ public String getAdditionalProposalInfo() { */ public static final String[] SURROUND_WITH_COMPLETIONS = new String[] { "%stry:%s%s%s%sexcept${cursor}:%s%s%sraise", "try..except", - "%stry:%s%s%s%sexcept (${RuntimeError}, ), e:%s%s%s${raise}${cursor}", "try..except (RuntimeError, ), e", + "%stry:%s%s%s%sexcept ${Exception} as e:%s%s%s${raise}${cursor}", "try..except Exception as e", "%stry:%s%s%s%sfinally:%s%s%s${pass}", "try..finally", "%sif ${True}:%s%s%s%selse:%s%s%s${pass}", "if..else", @@ -178,11 +162,4 @@ public boolean isValid(PySelection ps, String sel, IPyEdit edit, int offset) { return ps.getTextSelection().getLength() > 0; } - @Override - public TokensOrProposalsList getCodeCompletionProposals(CompletionRequest request) - throws CoreException, - BadLocationException { - throw new RuntimeException("Not implemented: completions should be gotten from the IAssistProps interface."); - } - } diff --git a/plugins/org.python.pydev/src/org/python/pydev/editor/PyInformationPresenter.java b/plugins/org.python.pydev/src/org/python/pydev/editor/PyInformationPresenter.java index 84dfa8bbf1..5c028c436a 100644 --- a/plugins/org.python.pydev/src/org/python/pydev/editor/PyInformationPresenter.java +++ b/plugins/org.python.pydev/src/org/python/pydev/editor/PyInformationPresenter.java @@ -298,7 +298,7 @@ private String trim(FastStringBuffer buffer, TextPresentation presentation) { } int start = 0; - while (start < end && Character.isWhitespace(buffer.charAt(start))) { + while (start < end && (buffer.charAt(start) == '\n' || buffer.charAt(start) == '\r')) { ++start; } diff --git a/plugins/org.python.pydev/src/org/python/pydev/editor/correctionassist/PythonCorrectionProcessor.java b/plugins/org.python.pydev/src/org/python/pydev/editor/correctionassist/PythonCorrectionProcessor.java index 1db01f7ddb..84351e1284 100644 --- a/plugins/org.python.pydev/src/org/python/pydev/editor/correctionassist/PythonCorrectionProcessor.java +++ b/plugins/org.python.pydev/src/org/python/pydev/editor/correctionassist/PythonCorrectionProcessor.java @@ -29,6 +29,7 @@ import org.eclipse.ui.texteditor.spelling.SpellingCorrectionProcessor; import org.eclipse.ui.texteditor.spelling.SpellingProblem; import org.python.pydev.ast.codecompletion.ProposalsComparator; +import org.python.pydev.ast.surround_with.AssistSurroundWith; import org.python.pydev.core.ExtensionHelper; import org.python.pydev.core.IAssistProps; import org.python.pydev.core.IPySyntaxHighlightingAndCodeCompletionEditor; @@ -43,7 +44,6 @@ import org.python.pydev.editor.correctionassist.heuristics.AssistImport; import org.python.pydev.editor.correctionassist.heuristics.AssistImportToLocal; import org.python.pydev.editor.correctionassist.heuristics.AssistPercentToFormat; -import org.python.pydev.editor.correctionassist.heuristics.AssistSurroundWith; import org.python.pydev.shared_core.code_completion.ICompletionProposalHandle; import org.python.pydev.shared_core.image.IImageCache; import org.python.pydev.shared_core.string.TextSelectionUtils; diff --git a/plugins/org.python.pydev/tests/org/python/pydev/editor/correctionassist/heuristics/AssistSurroundWithTest.java b/plugins/org.python.pydev/tests/org/python/pydev/editor/correctionassist/heuristics/AssistSurroundWithTest.java index 227c182552..ed6e6fc659 100644 --- a/plugins/org.python.pydev/tests/org/python/pydev/editor/correctionassist/heuristics/AssistSurroundWithTest.java +++ b/plugins/org.python.pydev/tests/org/python/pydev/editor/correctionassist/heuristics/AssistSurroundWithTest.java @@ -9,7 +9,26 @@ import java.util.List; import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.IAutoIndentStrategy; import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IEventConsumer; +import org.eclipse.jface.text.IFindReplaceTarget; +import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.ITextDoubleClickStrategy; +import org.eclipse.jface.text.ITextHover; +import org.eclipse.jface.text.ITextInputListener; +import org.eclipse.jface.text.ITextListener; +import org.eclipse.jface.text.ITextOperationTarget; +import org.eclipse.jface.text.ITextViewer; +import org.eclipse.jface.text.IUndoManager; +import org.eclipse.jface.text.IViewportListener; +import org.eclipse.jface.text.TextPresentation; +import org.eclipse.jface.text.templates.TemplateProposal; +import org.eclipse.jface.viewers.ISelectionProvider; +import org.eclipse.swt.custom.StyledText; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.Point; +import org.python.pydev.ast.surround_with.AssistSurroundWith; import org.python.pydev.core.TestCaseUtils; import org.python.pydev.core.docutils.PySelection; import org.python.pydev.core.proposals.CompletionProposalFactory; @@ -62,7 +81,7 @@ public void testSurround2() throws Exception { PySelection ps = new PySelection(doc, 1, 0, 13); int offset = ps.getAbsoluteCursorOffset(); List props = assistSurroundWith.getProps(ps, null, null, null, null, offset); - props.get(0).apply(doc); + apply(props.get(0), doc); TestCaseUtils.assertContentsEqual("" + "def m1():\n" + " try:\n" + @@ -70,13 +89,18 @@ public void testSurround2() throws Exception { " #c\n" + " a = 10\n" + - " except${cursor}:\n" + + " except:\n" + " raise\n" + "\n" + "\n" + "", doc.get()); } + private void apply(ICompletionProposalHandle iCompletionProposalHandle, IDocument doc) { + TemplateProposal p = (TemplateProposal) iCompletionProposalHandle; + p.apply(createViewerWithDoc(doc), ' ', 0, 0); + } + public void testSurround3() throws Exception { AssistSurroundWith assistSurroundWith = new AssistSurroundWith(); IDocument doc = new Document("" + @@ -89,7 +113,7 @@ public void testSurround3() throws Exception { PySelection ps = new PySelection(doc, 1, 0, 14); int offset = ps.getAbsoluteCursorOffset(); List props = assistSurroundWith.getProps(ps, null, null, null, null, offset); - props.get(0).apply(doc); + apply(props.get(0), doc); TestCaseUtils.assertContentsEqual("" + "def m1():\n" + "try:\n" + @@ -97,7 +121,7 @@ public void testSurround3() throws Exception { " #c\n" + " # a = 10\n" + - "except${cursor}:\n" + + "except:\n" + " raise\n" + "\n" + "\n" + @@ -110,11 +134,11 @@ public void testSurround() throws Exception { IDocument doc = new Document("a = 10"); PySelection ps = new PySelection(doc, 0, 0, 3); List props = assistSurroundWith.getProps(ps, null, null, null, null, offset); - props.get(0).apply(doc); + apply(props.get(0), doc); TestCaseUtils.assertContentsEqual("" + "try:\n" + " a = 10\n" + - "except${cursor}:\n" + + "except:\n" + " raise" + "", doc.get()); @@ -128,7 +152,18 @@ public void testSurround() throws Exception { "\n"); ps = new PySelection(doc, 1, 0, 11); props = assistSurroundWith.getProps(ps, null, null, null, null, offset); - props.get(0).apply(doc); + String additionalProposalInfo = props.get(0).getAdditionalProposalInfo(); + TestCaseUtils.assertContentsEqual( + " try:\n" + + " \n" + + " \n" + + " a = 10\n" + + " except:\n" + + " raise", + additionalProposalInfo); + + apply(props.get(0), doc); + TestCaseUtils.assertContentsEqual("" + "def m1():\n" + " try:\n" + @@ -136,7 +171,7 @@ public void testSurround() throws Exception { " \n" + " a = 10\n" + - " except${cursor}:\n" + + " except:\n" + " raise\n" + "\n" + "\n" + @@ -150,4 +185,256 @@ public void testSurround() throws Exception { props = assistSurroundWith.getProps(ps, null, null, null, null, offset); assertEquals(0, props.size()); } + + private ITextViewer createViewerWithDoc(IDocument doc) { + // TODO Auto-generated method stub + return new ITextViewer() { + + @Override + public void setVisibleRegion(int offset, int length) { + // TODO Auto-generated method stub + + } + + @Override + public void setUndoManager(IUndoManager undoManager) { + // TODO Auto-generated method stub + + } + + @Override + public void setTopIndex(int index) { + // TODO Auto-generated method stub + + } + + @Override + public void setTextHover(ITextHover textViewerHover, String contentType) { + // TODO Auto-generated method stub + + } + + @Override + public void setTextDoubleClickStrategy(ITextDoubleClickStrategy strategy, String contentType) { + // TODO Auto-generated method stub + + } + + @Override + public void setTextColor(Color color, int offset, int length, boolean controlRedraw) { + // TODO Auto-generated method stub + + } + + @Override + public void setTextColor(Color color) { + // TODO Auto-generated method stub + + } + + @Override + public void setSelectedRange(int offset, int length) { + // TODO Auto-generated method stub + + } + + @Override + public void setIndentPrefixes(String[] indentPrefixes, String contentType) { + // TODO Auto-generated method stub + + } + + @Override + public void setEventConsumer(IEventConsumer consumer) { + // TODO Auto-generated method stub + + } + + @Override + public void setEditable(boolean editable) { + // TODO Auto-generated method stub + + } + + @Override + public void setDocument(IDocument document, int modelRangeOffset, int modelRangeLength) { + // TODO Auto-generated method stub + + } + + @Override + public void setDocument(IDocument document) { + // TODO Auto-generated method stub + + } + + @Override + public void setDefaultPrefixes(String[] defaultPrefixes, String contentType) { + // TODO Auto-generated method stub + + } + + @Override + public void setAutoIndentStrategy(IAutoIndentStrategy strategy, String contentType) { + // TODO Auto-generated method stub + + } + + @Override + public void revealRange(int offset, int length) { + // TODO Auto-generated method stub + + } + + @Override + public void resetVisibleRegion() { + // TODO Auto-generated method stub + + } + + @Override + public void resetPlugins() { + // TODO Auto-generated method stub + + } + + @Override + public void removeViewportListener(IViewportListener listener) { + // TODO Auto-generated method stub + + } + + @Override + public void removeTextListener(ITextListener listener) { + // TODO Auto-generated method stub + + } + + @Override + public void removeTextInputListener(ITextInputListener listener) { + // TODO Auto-generated method stub + + } + + @Override + public boolean overlapsWithVisibleRegion(int offset, int length) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean isEditable() { + // TODO Auto-generated method stub + return false; + } + + @Override + public void invalidateTextPresentation() { + // TODO Auto-generated method stub + + } + + @Override + public IRegion getVisibleRegion() { + // TODO Auto-generated method stub + return null; + } + + @Override + public int getTopInset() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public int getTopIndexStartOffset() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public int getTopIndex() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public StyledText getTextWidget() { + // TODO Auto-generated method stub + return null; + } + + @Override + public ITextOperationTarget getTextOperationTarget() { + // TODO Auto-generated method stub + return null; + } + + @Override + public ISelectionProvider getSelectionProvider() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Point getSelectedRange() { + // TODO Auto-generated method stub + return null; + } + + @Override + public IFindReplaceTarget getFindReplaceTarget() { + // TODO Auto-generated method stub + return null; + } + + @Override + public IDocument getDocument() { + // TODO Auto-generated method stub + return doc; + } + + @Override + public int getBottomIndexEndOffset() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public int getBottomIndex() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public void changeTextPresentation(TextPresentation presentation, boolean controlRedraw) { + // TODO Auto-generated method stub + + } + + @Override + public void addViewportListener(IViewportListener listener) { + // TODO Auto-generated method stub + + } + + @Override + public void addTextListener(ITextListener listener) { + // TODO Auto-generated method stub + + } + + @Override + public void addTextInputListener(ITextInputListener listener) { + // TODO Auto-generated method stub + + } + + @Override + public void activatePlugins() { + // TODO Auto-generated method stub + + } + }; + } }