first PHP parser version (doesn't work actually)
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / php / PHPCompletionProcessor.java
1 /**********************************************************************
2 Copyright (c) 2000, 2002 IBM Corp. and others.
3 All rights reserved. This program and the accompanying materials
4 are made available under the terms of the Common Public License v1.0
5 which accompanies this distribution, and is available at
6 http://www.eclipse.org/legal/cpl-v10.html
7
8 Contributors:
9     IBM Corporation - Initial implementation
10     Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor.php;
13
14 import java.text.MessageFormat;
15 import java.util.ArrayList;
16
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.ITextViewer;
20 import org.eclipse.jface.text.TextPresentation;
21 import org.eclipse.jface.text.contentassist.CompletionProposal;
22 import org.eclipse.jface.text.contentassist.ContextInformation;
23 import org.eclipse.jface.text.contentassist.ICompletionProposal;
24 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
25 import org.eclipse.jface.text.contentassist.IContextInformation;
26 import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
27 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
28 import org.eclipse.swt.graphics.Point;
29
30 /**
31  * Example PHP completion processor.
32  */
33 public class PHPCompletionProcessor implements IContentAssistProcessor {
34
35   /**
36    * Simple content assist tip closer. The tip is valid in a range
37    * of 5 characters around its popup location.
38    */
39   protected static class Validator implements IContextInformationValidator, IContextInformationPresenter {
40
41     protected int fInstallOffset;
42
43     /*
44      * @see IContextInformationValidator#isContextInformationValid(int)
45      */
46     public boolean isContextInformationValid(int offset) {
47       return Math.abs(fInstallOffset - offset) < 5;
48     }
49
50     /*
51      * @see IContextInformationValidator#install(IContextInformation, ITextViewer, int)
52      */
53     public void install(IContextInformation info, ITextViewer viewer, int offset) {
54       fInstallOffset = offset;
55     }
56
57     /*
58      * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int, TextPresentation)
59      */
60     public boolean updatePresentation(int documentPosition, TextPresentation presentation) {
61       return false;
62     }
63   };
64
65   protected final static String[] fgProposals = PHPFunctionNames.FUNCTION_NAMES;
66   //    {
67   //      "array",
68   //      "break",
69   //      "class",
70   //      "continue",
71   //      "do",
72   //      "echo",
73   //      "else",
74   //      "elseif",
75   //      "endfor",
76   //      "endif",
77   //      "for",
78   //      "if",
79   //      "while",
80   //      "endwhile",
81   //      "switch",
82   //      "case",
83   //      "endswitch",
84   //      "return",
85   //      "define",
86   //      "include",
87   //      "include_once",
88   //      "require",
89   //      "require_once",
90   //      "function",
91   //      "new",
92   //      "old_function",
93   //      "default",
94   //      "global",
95   //      "static",
96   //      "foreach",
97   //      "endforeach",
98   //      "extends",
99   //      "empty",
100   //      "isset",
101   //      "var" };
102
103   protected IContextInformationValidator fValidator = new Validator();
104
105   /* (non-Javadoc)
106    * Method declared on IContentAssistProcessor
107    */
108   public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
109
110     //    ArrayList arrList = new ArrayList(5);
111     //    IDocument document = viewer.getDocument();
112     //    if (documentOffset > 0) {
113     //      try {
114     //        char character = document.getChar(documentOffset - 1);
115     //        Point point = PHPWordExtractor.findWord(document, documentOffset);
116     //        if (point != null) {
117     //          String word = document.get(point.x, point.y);
118     //          for (int i = 0; i < fgProposals.length; i++) {
119     //            if ((fgProposals[i].length() >= point.y) && fgProposals[i].substring(0, point.y).equals(word)) {
120     //              IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
121     //              arrList.add(new CompletionProposal(fgProposals[i], documentOffset, 0, fgProposals[i].length(), null, fgProposals[i], info, MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.hoverinfo.pattern"), new Object[] { fgProposals[i] }))); //$NON-NLS-1$
122     //            }
123     //          }
124     //          if (arrList.size() > 0) {
125     //            ICompletionProposal[] result = new ICompletionProposal[arrList.size()];
126     //            for (int i=0;i<arrList.size();i++) {
127     //              result[i] = (CompletionProposal) arrList.get(i);
128     //            }
129     //            return result;
130     //          }
131     //        }
132     //      } catch (BadLocationException e) {
133     //      }
134     //
135     //    }
136
137     ArrayList arrList = new ArrayList(5);
138     IDocument document = viewer.getDocument();
139     if (documentOffset > 0) {
140       try {
141         ICompletionProposal[] result;
142         char character = document.getChar(documentOffset - 1);
143         if (character == '$') {
144 //viewer.  .getActivePage().getActiveEditor();
145           result = new ICompletionProposal[fgProposals.length];
146           for (int i = 0; i < fgProposals.length; i++) {
147             IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
148             result[i] = new CompletionProposal(fgProposals[i], documentOffset, 0, fgProposals[i].length(), null, fgProposals[i], info, MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.hoverinfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
149           }
150           return result;
151         }
152       } catch (BadLocationException e) {
153         return new ICompletionProposal[0];
154       }
155     }
156
157     ICompletionProposal[] result = new ICompletionProposal[fgProposals.length];
158     for (int i = 0; i < fgProposals.length; i++) {
159       IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
160       result[i] = new CompletionProposal(fgProposals[i], documentOffset, 0, fgProposals[i].length(), null, fgProposals[i], info, MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.hoverinfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
161     }
162
163     return result;
164   }
165
166   /* (non-Javadoc)
167    * Method declared on IContentAssistProcessor
168    */
169   public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
170     IContextInformation[] result = new IContextInformation[5];
171     for (int i = 0; i < result.length; i++)
172       result[i] = new ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"), new Object[] { new Integer(i), new Integer(documentOffset)}), //$NON-NLS-1$
173       MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"), new Object[] { new Integer(i), new Integer(documentOffset - 5), new Integer(documentOffset + 5)})); //$NON-NLS-1$
174     return result;
175   }
176
177   /* (non-Javadoc)
178    * Method declared on IContentAssistProcessor
179    */
180   public char[] getCompletionProposalAutoActivationCharacters() {
181     return new char[] { '$' };
182   }
183
184   /* (non-Javadoc)
185    * Method declared on IContentAssistProcessor
186    */
187   public char[] getContextInformationAutoActivationCharacters() {
188     return new char[] {
189     };
190   }
191
192   /* (non-Javadoc)
193    * Method declared on IContentAssistProcessor
194    */
195   public IContextInformationValidator getContextInformationValidator() {
196     return fValidator;
197   }
198
199   /* (non-Javadoc)
200    * Method declared on IContentAssistProcessor
201    */
202   public String getErrorMessage() {
203     return null;
204   }
205 }