Skip to content

Commit

Permalink
Moving template-related classes which are not ui-related to org.pytho…
Browse files Browse the repository at this point in the history
…n.pydev.ast.
  • Loading branch information
fabioz committed Apr 20, 2024
1 parent ed4f498 commit 1904e1e
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.python.pydev.core.docutils.PyStringUtils;
import org.python.pydev.core.log.Log;
import org.python.pydev.core.proposals.CompletionProposalFactory;
import org.python.pydev.core.templates.PyDocumentTemplateContext;
import org.python.pydev.editor.PyEdit;
import org.python.pydev.editor.codecompletion.templates.PyDocumentTemplateContext;
import org.python.pydev.editor.correctionassist.heuristics.AssistAssign;
import org.python.pydev.parser.jython.ast.ClassDef;
import org.python.pydev.parser.jython.ast.Pass;
Expand Down Expand Up @@ -179,7 +179,7 @@ protected ICompletionProposalHandle createProposal(PySelection pySelection, Stri
TemplateContextType contextType = new TemplateContextType();
contextType.addResolver(new GlobalTemplateVariables.Cursor()); //We do want the cursor thought.
PyDocumentTemplateContext context = PyDocumentTemplateContext.createContext(contextType,
((PyEdit) targetEditor).getPySourceViewer(), region, indent);
targetEditor, region, indent);

Template template = new Template("Create " + creationStr, "Create " + creationStr, "", source, true);
ICompletionProposalHandle templateProposal = CompletionProposalFactory.get()
Expand Down
1 change: 1 addition & 0 deletions plugins/org.python.pydev.ast/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Export-Package: org.python.pydev.ast,
org.python.pydev.ast.runners,
org.python.pydev.ast.simpleassist,
org.python.pydev.core.pep8,
org.python.pydev.core.templates,
org.python.pydev.plugin,
org.python.pydev.plugin.nature
Bundle-Vendor: Brainwy Software Ltda
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Contributors:
* Fabio Zadrozny <fabiofz@gmail.com> - initial API and implementation
******************************************************************************/
package org.python.pydev.shared_ui.templates;
package org.python.pydev.core.templates;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
* Please see the license.txt included with this distribution for details.
* Any modifications to this file must keep this entire header intact.
*/
package org.python.pydev.editor.codecompletion.templates;
package org.python.pydev.core.templates;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.templates.TemplateContextType;
import org.python.pydev.core.IIndentPrefs;
import org.python.pydev.shared_ui.templates.AbstractDocumentTemplateContextWithIndent;

public class DocumentTemplateContextWithIndent extends AbstractDocumentTemplateContextWithIndent {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
* Please see the license.txt included with this distribution for details.
* Any modifications to this file must keep this entire header intact.
*/
package org.python.pydev.editor.codecompletion.templates;
package org.python.pydev.core.templates;

import java.io.File;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.templates.TemplateContextType;
import org.python.pydev.core.IGrammarVersionProvider;
import org.python.pydev.core.IIndentPrefs;
Expand All @@ -28,6 +26,7 @@
import org.python.pydev.parser.jython.ast.ClassDef;
import org.python.pydev.parser.jython.ast.FunctionDef;
import org.python.pydev.parser.visitors.NodeUtils;
import org.python.pydev.shared_core.string.ICoreTextSelection;

/**
* Makes a custom evaluation of the template buffer to be created (to put it in the correct indentation and
Expand All @@ -37,18 +36,18 @@
*/
public final class PyDocumentTemplateContext extends DocumentTemplateContextWithIndent {

public ITextViewer viewer; //May be null
public IPyEdit viewer; //May be null

/**
* Note that it's in the default context because it should be used on subclasses.
* This constructor is meant for tests!
*/
/*default*/ PyDocumentTemplateContext(TemplateContextType type, IDocument document, int offset, int length,
public PyDocumentTemplateContext(TemplateContextType type, IDocument document, int offset, int length,
String indentTo, IIndentPrefs indentPrefs) {
super(type, document, offset, length, indentTo, indentPrefs);
}

public PyDocumentTemplateContext(TemplateContextType type, IDocument document, int offset, int length,
String indentTo, ITextViewer viewer) {
String indentTo, IPyEdit viewer) {
this(type, document, offset, length, indentTo, getIndentPrefs(viewer));
this.viewer = viewer;
}
Expand Down Expand Up @@ -145,7 +144,7 @@ public String getModuleName() {
* @return a template context that can handle template insertion at the given location, or <code>null</code>
*/
public static PyDocumentTemplateContext createContext(final TemplateContextType contextType,
final ITextViewer viewer, final IRegion region, String indent) {
final IPyEdit viewer, final IRegion region, String indent) {
if (contextType != null) {
IDocument document = viewer.getDocument();
final String indentTo = indent;
Expand All @@ -156,11 +155,11 @@ public static PyDocumentTemplateContext createContext(final TemplateContextType
}

public static PyDocumentTemplateContext createContext(final TemplateContextType contextType,
final ITextViewer viewer, final IRegion region) {
final IPyEdit viewer, final IRegion region) {
if (contextType != null) {
IDocument document = viewer.getDocument();
PySelection selection = new PySelection(document,
((ITextSelection) viewer.getSelectionProvider().getSelection()).getOffset());
ICoreTextSelection textSelection = viewer.getTextSelection();
PySelection selection = new PySelection(document, textSelection);
String indent = selection.getIndentationFromLine();
return PyDocumentTemplateContext.createContext(contextType, viewer, region, indent);
}
Expand All @@ -170,7 +169,7 @@ public static PyDocumentTemplateContext createContext(final TemplateContextType
/**
* @return the indent preferences to be used.
*/
private static IIndentPrefs getIndentPrefs(ITextViewer viewer) {
private static IIndentPrefs getIndentPrefs(IPyEdit viewer) {
if (viewer instanceof IPySourceViewer) {
IPySourceViewer pyViewer = (IPySourceViewer) viewer;
return pyViewer.getEdit().getIndentPrefs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
import org.eclipse.jface.text.templates.TemplateContext;
import org.eclipse.jface.text.templates.TemplateContextType;
import org.eclipse.swt.graphics.Image;
import org.python.pydev.core.IPySourceViewer;
import org.python.pydev.core.log.Log;
import org.python.pydev.core.proposals.CompletionProposalFactory;
import org.python.pydev.editor.codecompletion.templates.PyDocumentTemplateContext;
import org.python.pydev.core.templates.PyDocumentTemplateContext;
import org.python.pydev.editor.codefolding.PySourceViewer;
import org.python.pydev.editor.templates.PyContextType;
import org.python.pydev.editor.templates.TemplateHelper;
import org.python.pydev.shared_core.code_completion.ICompletionProposalHandle;
Expand Down Expand Up @@ -105,7 +108,14 @@ protected ICompletionProposal createProposal(Template template, TemplateContext
@Override
protected TemplateContext createContext(final ITextViewer viewer, final IRegion region) {
TemplateContextType contextType = getContextType(viewer, region);
return PyDocumentTemplateContext.createContext(contextType, viewer, region);
if (viewer instanceof IPySourceViewer) {
PySourceViewer pySourceViewer = (PySourceViewer) viewer;

return PyDocumentTemplateContext.createContext(contextType, pySourceViewer.getEdit(), region);
}
Log.log("Expected an IPySourceViewer. Found: " + viewer);
return PyDocumentTemplateContext.createContext(contextType, null, region);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.eclipse.jface.text.templates.TemplateContext;
import org.python.pydev.core.IGrammarVersionProvider;
import org.python.pydev.editor.codecompletion.templates.PyDocumentTemplateContext;
import org.python.pydev.core.templates.PyDocumentTemplateContext;
import org.python.pydev.shared_core.callbacks.ICallback;

class CallableTemplateVariableResolver extends PyTemplateVariableResolver {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.python.pydev.core.CorePlugin;
import org.python.pydev.core.docutils.PySelection;
import org.python.pydev.core.log.Log;
import org.python.pydev.editor.codecompletion.templates.PyDocumentTemplateContext;
import org.python.pydev.core.templates.PyDocumentTemplateContext;
import org.python.pydev.parser.fastparser.FastParser;
import org.python.pydev.parser.jython.ast.ClassDef;
import org.python.pydev.parser.jython.ast.FunctionDef;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
import org.eclipse.ui.IEditorPart;
import org.python.pydev.core.log.Log;
import org.python.pydev.core.preferences.FileTypesPreferences;
import org.python.pydev.core.templates.PyDocumentTemplateContext;
import org.python.pydev.editor.PyEdit;
import org.python.pydev.editor.codecompletion.templates.PyDocumentTemplateContext;
import org.python.pydev.editor.templates.PyContextType;
import org.python.pydev.shared_ui.utils.RunInUiThread;

/**
* Python module creation wizard
*
*
* TODO: Create initial file content from a comment templates
*
*
* @author Mikko Ohtamaa
*
*
*/
public class PythonModuleWizard extends AbstractPythonWizard {

Expand Down Expand Up @@ -70,9 +70,9 @@ protected String checkNameText(String text) {

/**
* We will create a new module (file) here given the source folder and the package specified (which
* are currently validated in the page)
* @param monitor
* @throws CoreException
* are currently validated in the page)
* @param monitor
* @throws CoreException
*/
@Override
protected IFile doCreateNew(IProgressMonitor monitor) throws CoreException {
Expand Down Expand Up @@ -133,7 +133,7 @@ public void run() {

Region region = new Region(0, 0);
PyDocumentTemplateContext context = PyDocumentTemplateContext.createContext(new PyContextType(),
pyEdit.getPySourceViewer(), region);
pyEdit, region);

TemplateProposal templateProposal = new TemplateProposal(template, context, region, null);
templateProposal.apply(pyEdit.getPySourceViewer(), '\n', 0, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.eclipse.jface.text.templates.TemplateBuffer;
import org.eclipse.jface.text.templates.TemplateContextType;
import org.python.pydev.core.autoedit.TestIndentPrefs;
import org.python.pydev.editor.codecompletion.templates.PyDocumentTemplateContext;
import org.python.pydev.core.templates.PyDocumentTemplateContext;

public class PyDocumentTemplateContextTest extends TestCase {

Expand Down

0 comments on commit 1904e1e

Please sign in to comment.