~vil/pydev/upstream

« back to all changes in this revision

Viewing changes to org.python.pydev/src/org/python/pydev/editor/correctionassist/PythonCorrectionProcessor.java

  • Committer: Vladimír Lapáček
  • Date: 2006-08-30 18:38:44 UTC
  • Revision ID: vladimir.lapacek@gmail.com-20060830183844-f4d82c1239a7770a
Initial import of upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Created on Sep 23, 2004
 
3
 *
 
4
 * @author Fabio Zadrozny
 
5
 */
 
6
package org.python.pydev.editor.correctionassist;
 
7
 
 
8
import java.util.ArrayList;
 
9
import java.util.Collections;
 
10
import java.util.HashMap;
 
11
import java.util.List;
 
12
import java.util.Map;
 
13
 
 
14
import org.eclipse.jface.text.BadLocationException;
 
15
import org.eclipse.jface.text.ITextViewer;
 
16
import org.eclipse.jface.text.contentassist.ICompletionProposal;
 
17
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
 
18
import org.eclipse.jface.text.contentassist.IContextInformation;
 
19
import org.eclipse.jface.text.contentassist.IContextInformationValidator;
 
20
import org.python.pydev.core.ExtensionHelper;
 
21
import org.python.pydev.core.bundle.ImageCache;
 
22
import org.python.pydev.core.docutils.PySelection;
 
23
import org.python.pydev.editor.PyEdit;
 
24
import org.python.pydev.editor.actions.PyAction;
 
25
import org.python.pydev.editor.codecompletion.PyCodeCompletion;
 
26
import org.python.pydev.editor.correctionassist.heuristics.AssistAssign;
 
27
import org.python.pydev.editor.correctionassist.heuristics.AssistDocString;
 
28
import org.python.pydev.editor.correctionassist.heuristics.AssistImport;
 
29
import org.python.pydev.editor.correctionassist.heuristics.AssistTry;
 
30
import org.python.pydev.editor.correctionassist.heuristics.IAssistProps;
 
31
import org.python.pydev.plugin.PydevPlugin;
 
32
 
 
33
/**
 
34
 * This class should be used to give context help
 
35
 * 
 
36
 * Help depending on context (Ctrl+1):
 
37
 * 
 
38
 * class A: pass
 
39
 * 
 
40
 * class C:
 
41
 * 
 
42
 * def __init__(self, param): 
 
43
 *          self.newMethod()<- create new method on class C  (with params if needed)
 
44
 *                                              <- assign result to new local variable 
 
45
 *                                              <- assign result to new field 
 
46
 * 
 
47
 *              a = A()
 
48
 *              a.newMethod()   <- create new method on class A 
 
49
 *                                              <- assign result to new local variable 
 
50
 *                                              <- assign result to new field
 
51
 * 
 
52
 *              param.b() <- don't show anything.
 
53
 * 
 
54
 *              self.a1 = A() 
 
55
 *              self.a1.newMethod() <- create new method on class A (difficult part is discovering class)
 
56
 *                                                      <- assign result to new local variable 
 
57
 *                                                      <- assign result to new field
 
58
 * 
 
59
 *              def m(self): 
 
60
 *                      self.a1.newMethod() <- create new method on class A 
 
61
 *                                                              <- assign result to new local variable 
 
62
 *                                                              <- assign result to new field
 
63
 * 
 
64
 *                      import compiler <- move import to global context
 
65
 *                      NewClass() <- Create class NewClass (Depends on new class wizard)
 
66
 *
 
67
 *         a() <-- make this a new method in this class 
 
68
 *                                                                                                                                                                                       
 
69
 * @author Fabio Zadrozny
 
70
 */
 
71
public class PythonCorrectionProcessor implements IContentAssistProcessor {
 
72
 
 
73
    private PyEdit edit;
 
74
    private ImageCache imageCache;
 
75
    private static Map<String, IAssistProps> additionalAssists = new HashMap<String, IAssistProps>();
 
76
    
 
77
    public static boolean hasAdditionalAssist(String id){
 
78
        synchronized (additionalAssists) {
 
79
                return additionalAssists.containsKey(id);
 
80
        }
 
81
    }
 
82
    public static void addAdditionalAssist(String id, IAssistProps assist){
 
83
        synchronized (additionalAssists) {
 
84
                additionalAssists.put(id, assist);
 
85
        }
 
86
    }
 
87
    public static void removeAdditionalAssist(String id, IAssistProps assist){
 
88
        synchronized (additionalAssists) {
 
89
                additionalAssists.remove(id);
 
90
                }
 
91
    }
 
92
 
 
93
    /**
 
94
     * @param edit
 
95
     */
 
96
    public PythonCorrectionProcessor(PyEdit edit) {
 
97
        this.edit = edit;
 
98
        this.imageCache = new ImageCache(PydevPlugin.getDefault().getBundle().getEntry("/"));
 
99
    }
 
100
 
 
101
    /*
 
102
     * (non-Javadoc)
 
103
     * 
 
104
     * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer,
 
105
     *      int)
 
106
     */
 
107
    public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
 
108
 
 
109
        PySelection ps = new PySelection(edit);
 
110
 
 
111
        List<ICompletionProposal> results = new ArrayList<ICompletionProposal>();
 
112
        String sel = PyAction.getLineWithoutComments(ps);
 
113
        
 
114
        
 
115
        List<IAssistProps> assists = new ArrayList<IAssistProps>();
 
116
        synchronized (this.additionalAssists) {
 
117
                for (IAssistProps prop : additionalAssists.values()) {
 
118
                                assists.add(prop);
 
119
                        }
 
120
        }
 
121
        
 
122
        assists.add(new AssistTry());
 
123
        assists.add(new AssistImport());
 
124
        assists.add(new AssistDocString());
 
125
        assists.add(new AssistAssign());
 
126
        
 
127
        assists.addAll(ExtensionHelper.getParticipants(ExtensionHelper.PYDEV_CTRL_1));
 
128
        
 
129
        for (IAssistProps assist : assists) {
 
130
            try {
 
131
                if (assist.isValid(ps, sel, edit, offset)) {
 
132
                    try {
 
133
                        results.addAll(
 
134
                                assist.getProps(
 
135
                                        ps, 
 
136
                                        imageCache, 
 
137
                                        edit.getEditorFile(), 
 
138
                                        edit.getPythonNature(), 
 
139
                                        edit,
 
140
                                        offset)
 
141
                        );
 
142
                    } catch (BadLocationException e) {
 
143
                        PydevPlugin.log(e);
 
144
                    }
 
145
                }
 
146
            } catch (Exception e) {
 
147
                PydevPlugin.log(e);
 
148
            }
 
149
        }
 
150
 
 
151
        Collections.sort(results, PyCodeCompletion.PROPOSAL_COMPARATOR);
 
152
 
 
153
        return (ICompletionProposal[]) results.toArray(new ICompletionProposal[0]);
 
154
    }
 
155
 
 
156
    
 
157
    /*
 
158
     * (non-Javadoc)
 
159
     * 
 
160
     * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeContextInformation(org.eclipse.jface.text.ITextViewer,
 
161
     *      int)
 
162
     */
 
163
    public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
 
164
        return null;
 
165
    }
 
166
 
 
167
    /*
 
168
     * (non-Javadoc)
 
169
     * 
 
170
     * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
 
171
     */
 
172
    public char[] getCompletionProposalAutoActivationCharacters() {
 
173
        return null;
 
174
    }
 
175
 
 
176
    /*
 
177
     * (non-Javadoc)
 
178
     * 
 
179
     * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationAutoActivationCharacters()
 
180
     */
 
181
    public char[] getContextInformationAutoActivationCharacters() {
 
182
        return null;
 
183
    }
 
184
 
 
185
    /*
 
186
     * (non-Javadoc)
 
187
     * 
 
188
     * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getErrorMessage()
 
189
     */
 
190
    public String getErrorMessage() {
 
191
        return null;
 
192
    }
 
193
 
 
194
    /*
 
195
     * (non-Javadoc)
 
196
     * 
 
197
     * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationValidator()
 
198
     */
 
199
    public IContextInformationValidator getContextInformationValidator() {
 
200
        return null;
 
201
    }
 
202
 
 
203
}
 
 
b'\\ No newline at end of file'