⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sourcemoduleproposal.java

📁 Python Development Environment (Python IDE plugin for Eclipse). Features editor, code completion, re
💻 JAVA
字号:
/*
 * Created on Apr 12, 2005
 *
 * @author Fabio Zadrozny
 */
package org.python.pydev.editor.codecompletion.revisited;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.IEditorPart;
import org.python.pydev.core.REF;
import org.python.pydev.editor.PyEdit;
import org.python.pydev.editor.codecompletion.PyCompletionProposal;
import org.python.pydev.editor.codecompletion.revisited.modules.SourceModule;
import org.python.pydev.editor.codecompletion.revisited.visitors.Definition;
import org.python.pydev.plugin.PydevPlugin;

/**
 * This class is a proposal that should be applied in a module and not in the original doc.
 * 
 * @author Fabio Zadrozny
 */
public class SourceModuleProposal extends PyCompletionProposal {

    public final SourceModule module;
    public PyEdit edit;
    public IDocument doc;
    public Definition definition;
    
    public static final int ADD_TO_DEFAULT = -1;
    public static final int ADD_TO_LAST_LINE_BEFORE_MAIN = 0;
    public static final int ADD_TO_LAST_CLASS_LINE = 1;
    public int addTo = ADD_TO_LAST_LINE_BEFORE_MAIN;
    
    public SourceModuleProposal(
            String replacementString, 
            int replacementOffset, 
            int replacementLength, 
            int cursorPosition, 
            Image image, 
            String displayString,
            IContextInformation contextInformation, 
            String additionalProposalInfo, 
            SourceModule s,
            int priority) {
        super(replacementString, replacementOffset, replacementLength, cursorPosition, image, displayString, contextInformation, additionalProposalInfo, priority);
        this.module = s;
    }

    /**
     * @see org.python.pydev.editor.codecompletion.PyCompletionProposal#apply(org.eclipse.jface.text.IDocument)
     */
    public void apply(IDocument dummy) {
        //OK, module can really be another or could be same...
        
        if(doc == null){ //doc can be preset
            
            //if not preset, let's find it.
	        if(module.getFile() == null){ //its same
	            doc = dummy;
	            
	        }else{//another
		        IPath path = new Path(REF.getFileAbsolutePath(module.getFile()));
		        IEditorPart part = PydevPlugin.doOpenEditor(path, true);
		
		        if(part instanceof PyEdit){
		            edit = (PyEdit) part;
		            doc = edit.getDocumentProvider().getDocument(edit.getEditorInput());
		        }else{
		            String contents = REF.getFileContents(module.getFile());
		            doc = new Document(contents);
		        }
	        }        
        }
        
        fReplacementOffset = getReplacementOffset();
        super.apply(doc);
    }

    /**
     * @return
     * 
     */
    public int getReplacementOffset() {
        //Replacement
        int i = -1;
        if(addTo == ADD_TO_LAST_LINE_BEFORE_MAIN){
            i = module.findIfMain()-2;
        }else if(addTo == ADD_TO_LAST_CLASS_LINE){
            i = module.findAstEnd(definition.ast)-2;
        }

        if(i < 0){
            i = doc.getNumberOfLines();
        }
        try {
            IRegion lineInformation = doc.getLineInformation(i);
            return lineInformation.getOffset()+lineInformation.getLength();
        } catch (BadLocationException e) {
            e.printStackTrace();
        }

        //return original...
	    return fReplacementOffset; 
    }

    /**
     * @see org.python.pydev.editor.codecompletion.PyCompletionProposal#getSelection(org.eclipse.jface.text.IDocument)
     */
    public Point getSelection(IDocument dummy) {
        Point sel = super.getSelection(doc);
        if(module.getFile() == null){ //its same
            return sel;
        }

        edit.setSelection(sel.x, sel.y);
        return null;
    }

    /**
     * @return
     * 
     */
    public String getReplacementStr() {
        return fReplacementString;
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -