Skip to content

Commit

Permalink
More changes to use templates without UI.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioz committed Apr 21, 2024
1 parent e78310a commit afb6a68
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import org.python.pydev.core.IPyEdit;
import org.python.pydev.core.IPySourceViewer;
import org.python.pydev.core.IPythonNature;
import org.python.pydev.core.ISourceViewerForTemplates;
import org.python.pydev.core.MisconfigurationException;
import org.python.pydev.core.autoedit.DefaultIndentPrefs;
import org.python.pydev.core.docutils.PySelection;
import org.python.pydev.core.interactive_console.IScriptConsoleViewer;
import org.python.pydev.parser.fastparser.FastParser;
Expand All @@ -36,7 +36,7 @@
*/
public final class PyDocumentTemplateContext extends DocumentTemplateContextWithIndent {

public IPyEdit viewer; //May be null
public ISourceViewerForTemplates edit; //May be null

/**
* This constructor is meant for tests!
Expand All @@ -47,9 +47,9 @@ public PyDocumentTemplateContext(TemplateContextType type, IDocument document, i
}

public PyDocumentTemplateContext(TemplateContextType type, IDocument document, int offset, int length,
String indentTo, IPyEdit viewer) {
this(type, document, offset, length, indentTo, getIndentPrefs(viewer));
this.viewer = viewer;
String indentTo, ISourceViewerForTemplates viewer) {
this(type, document, offset, length, indentTo, viewer.getIndentPrefs());
this.edit = viewer;
}

// Methods below are Used in scripting
Expand Down Expand Up @@ -79,15 +79,15 @@ public Class<BadLocationException> getBadLocationExceptionClass() {
}

public boolean isCythonFile() {
if (this.viewer instanceof IPySourceViewer) {
return ((IPySourceViewer) this.viewer).getEdit().isCythonFile();
if (this.edit != null) {
return edit.isCythonFile();
}
return false;
}

public File getEditorFile() {
if (this.viewer instanceof IPySourceViewer) {
return ((IPySourceViewer) this.viewer).getEdit().getEditorFile();
if (this.edit != null) {
return this.edit.getEditorFile();
}
return new File("");
}
Expand All @@ -96,19 +96,19 @@ public int getGrammarVersion() {
//Other possibilities
//org.eclipse.jface.text.source.SourceViewer (in compare)

if (this.viewer instanceof IPySourceViewer) {
if (this.edit instanceof IPySourceViewer) {
try {
IPythonNature nature = ((IPySourceViewer) this.viewer).getEdit().getPythonNature();
IPythonNature nature = ((IPySourceViewer) this.edit).getEdit().getPythonNature();
if (nature != null) {
return nature.getGrammarVersion();
}
} catch (MisconfigurationException e) {
}
}

if (this.viewer instanceof IScriptConsoleViewer) {
if (this.edit instanceof IScriptConsoleViewer) {
//interactive console
IScriptConsoleViewer v = (IScriptConsoleViewer) this.viewer;
IScriptConsoleViewer v = (IScriptConsoleViewer) this.edit;
IInterpreterInfo interpreterInfo = (IInterpreterInfo) v.getInterpreterInfo();
if (interpreterInfo != null) {
return interpreterInfo.getGrammarVersion();
Expand All @@ -119,9 +119,9 @@ public int getGrammarVersion() {
}

public String getModuleName() {
if (this.viewer instanceof IPySourceViewer) {
if (this.edit instanceof IPySourceViewer) {
try {
IPySourceViewer pyViewer = (IPySourceViewer) this.viewer;
IPySourceViewer pyViewer = (IPySourceViewer) this.edit;
IPyEdit edit = pyViewer.getEdit();
IPythonNature nature = edit.getPythonNature();
if (nature != null) {
Expand All @@ -139,43 +139,31 @@ public String getModuleName() {
* returns a <code>DocumentTemplateContext</code> for the context type at the given location.
*
* @param contextType the context type for the template.
* @param viewer the viewer for which the context is created
* @param edit the viewer for which the context is created
* @param region the region into <code>document</code> for which the context is created
* @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 IPyEdit viewer, final IRegion region, String indent) {
final ISourceViewerForTemplates edit, final IRegion region, String indent) {
if (contextType != null) {
IDocument document = viewer.getDocument();
IDocument document = edit.getDocument();
final String indentTo = indent;
return new PyDocumentTemplateContext(contextType, document, region.getOffset(), region.getLength(),
indentTo, viewer);
indentTo, edit);
}
return null;
}

public static PyDocumentTemplateContext createContext(final TemplateContextType contextType,
final IPyEdit viewer, final IRegion region) {
final ISourceViewerForTemplates edit, final IRegion region) {
if (contextType != null) {
IDocument document = viewer.getDocument();
ICoreTextSelection textSelection = viewer.getTextSelection();
IDocument document = edit.getDocument();
ICoreTextSelection textSelection = edit.getTextSelection();
PySelection selection = new PySelection(document, textSelection);
String indent = selection.getIndentationFromLine();
return PyDocumentTemplateContext.createContext(contextType, viewer, region, indent);
return PyDocumentTemplateContext.createContext(contextType, edit, region, indent);
}
return null;
}

/**
* @return the indent preferences to be used.
*/
private static IIndentPrefs getIndentPrefs(IPyEdit viewer) {
if (viewer instanceof IPySourceViewer) {
IPySourceViewer pyViewer = (IPySourceViewer) viewer;
return pyViewer.getEdit().getIndentPrefs();
} else {
return DefaultIndentPrefs.get(null);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@
*/
package org.python.pydev.core;

import java.io.File;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.python.pydev.shared_core.editor.IBaseEditor;
import org.python.pydev.shared_core.parsing.IParserObserver;
import org.python.pydev.shared_core.string.ICoreTextSelection;

/**
* @author Fabio
*/
public interface IPyEdit extends IParserObserver, IBaseEditor, IPyFormatStdProvider, IGrammarVersionProvider,
IPyEditOfflineActionListener {
IPyEditOfflineActionListener, IPyEditCore, ISourceViewerForTemplates {

static public final String EDITOR_ID = "org.python.pydev.editor.PythonEditor";

Expand All @@ -42,16 +39,10 @@ public interface IPyEdit extends IParserObserver, IBaseEditor, IPyFormatStdProvi

/* SimpleNode*/ Object getAST();

File getEditorFile();

long getAstModificationTimeStamp();

IFile getIFile();

boolean isCythonFile();

/* PyParser */ Object getParser();

ICoreTextSelection getTextSelection();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.python.pydev.core;

import org.eclipse.jface.text.IDocument;

public interface IPyEditCore {

IDocument getDocument();

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.python.pydev.core;

public interface IPySourceViewer {
public interface IPySourceViewer extends ISourceViewerForTemplates {

IPyEdit getEdit();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.python.pydev.core;

import java.io.File;

import org.eclipse.jface.text.IDocument;
import org.python.pydev.shared_core.string.ICoreTextSelection;

public interface ISourceViewerForTemplates {

boolean isCythonFile();

File getEditorFile();

IIndentPrefs getIndentPrefs();

IDocument getDocument();

ICoreTextSelection getTextSelection();

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
*******************************************************************************/
package org.python.pydev.core.interactive_console;

import org.eclipse.jface.text.IDocument;
import org.python.pydev.core.ISourceViewerForTemplates;

/**
* Interface that must be implemented by the console viewer. Provides info related to what
* may be edited or not.
*/
public interface IScriptConsoleViewer /* extends ITextViewer */ {
public interface IScriptConsoleViewer extends ISourceViewerForTemplates /* extends ITextViewer */ {

/**
* @return the contents of the current buffer (text edited still not passed to the shell)
Expand All @@ -39,11 +39,6 @@ public interface IScriptConsoleViewer /* extends ITextViewer */ {
*/
public void setCaretOffset(int offset, boolean async);

/**
* @return the document being viewed by this console viewer
*/
public IDocument getDocument();

/**
* @return the interpreter info (used to get the grammar version for resolving templates)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package org.python.pydev.debug.console;

import java.io.File;

import org.eclipse.jface.text.IAutoEditStrategy;
import org.eclipse.jface.text.IAutoIndentStrategy;
import org.eclipse.jface.text.IDocument;
Expand Down Expand Up @@ -55,10 +57,14 @@
import org.eclipse.swt.widgets.Item;
import org.eclipse.ui.console.IHyperlink;
import org.eclipse.ui.console.TextConsoleViewer;
import org.python.pydev.core.IIndentPrefs;
import org.python.pydev.core.IInterpreterInfo;
import org.python.pydev.core.autoedit.DefaultIndentPrefs;
import org.python.pydev.core.docutils.PySelection;
import org.python.pydev.core.interactive_console.IScriptConsoleViewer;
import org.python.pydev.editor.PySelectionFromEditor;
import org.python.pydev.shared_core.string.CoreTextSelection;
import org.python.pydev.shared_core.string.ICoreTextSelection;

public class ScriptConsoleViewerWrapper implements ITextViewer, IScriptConsoleViewer {

Expand Down Expand Up @@ -698,4 +704,25 @@ public void setTabsToSpacesConverter(IAutoEditStrategy converter) {
viewer.setTabsToSpacesConverter(converter);
}

@Override
public boolean isCythonFile() {
return false;
}

@Override
public File getEditorFile() {
return new File("");
}

@Override
public IIndentPrefs getIndentPrefs() {
return DefaultIndentPrefs.get(null);
}

@Override
public ICoreTextSelection getTextSelection() {
ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
return new CoreTextSelection(selection.getOffset(), selection.getLength());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*******************************************************************************/
package org.python.pydev.shared_interactive_console.console.ui.internal;

import java.io.File;
import java.lang.reflect.Method;
import java.util.List;

Expand Down Expand Up @@ -53,9 +54,13 @@
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.console.TextConsoleViewer;
import org.python.pydev.core.IIndentPrefs;
import org.python.pydev.core.autoedit.DefaultIndentPrefs;
import org.python.pydev.core.autoedit.IHandleScriptAutoEditStrategy;
import org.python.pydev.core.interactive_console.IScriptConsoleViewer;
import org.python.pydev.shared_core.log.Log;
import org.python.pydev.shared_core.string.CoreTextSelection;
import org.python.pydev.shared_core.string.ICoreTextSelection;
import org.python.pydev.shared_core.string.StringUtils;
import org.python.pydev.shared_interactive_console.console.ScriptConsoleHistory;
import org.python.pydev.shared_interactive_console.console.codegen.IScriptConsoleCodeGenerator;
Expand Down Expand Up @@ -984,4 +989,25 @@ public void setScrollLock(boolean scrollLock) {
public boolean getScrollLock() {
return listener.getScrollLock();
}

@Override
public boolean isCythonFile() {
return false;
}

@Override
public File getEditorFile() {
return new File("");
}

@Override
public IIndentPrefs getIndentPrefs() {
return DefaultIndentPrefs.get(null);
}

@Override
public ICoreTextSelection getTextSelection() {
ITextSelection selection = (ITextSelection) getSelectionProvider().getSelection();
return new CoreTextSelection(selection.getOffset(), selection.getLength());
}
}
4 changes: 2 additions & 2 deletions plugins/org.python.pydev/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1925,11 +1925,11 @@
autoinsert="false"
name="super"
icon="platform:/plugin/org.python.pydev.shared_ui/icons/template.gif"
description="Call super(current_class, self).current_method()"
description="Call super().current_method()"
contextTypeId="org.python.pydev.editor.templates.python"
id="org.python.pydev.editor.templates.python.super">
<pattern>
super(${current_class}, ${self_or_cls}).${current_method}(${cursor})
super().${current_method}(${cursor})
</pattern>
</template>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
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.ISourceViewerForTemplates;
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.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 @@ -108,12 +107,12 @@ protected ICompletionProposal createProposal(Template template, TemplateContext
@Override
protected TemplateContext createContext(final ITextViewer viewer, final IRegion region) {
TemplateContextType contextType = getContextType(viewer, region);
if (viewer instanceof IPySourceViewer) {
PySourceViewer pySourceViewer = (PySourceViewer) viewer;
if (viewer instanceof ISourceViewerForTemplates) {
ISourceViewerForTemplates pySourceViewer = (ISourceViewerForTemplates) viewer;

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

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int
* Returns the python proposals as a list.
* First parameter of tuple is a list and second is a Boolean object indicating whether the templates
* should be also shown or not.
* @param viewer
* @throws CoreException
* @throws BadLocationException
* @throws MisconfigurationException
Expand Down
Loading

0 comments on commit afb6a68

Please sign in to comment.