a9b88fc7cf42b3bcd8d4706c844c3d4a7b1fe34a
[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
16 import org.eclipse.jface.text.ITextViewer;
17 import org.eclipse.jface.text.TextPresentation;
18 import org.eclipse.jface.text.contentassist.CompletionProposal;
19 import org.eclipse.jface.text.contentassist.ContextInformation;
20 import org.eclipse.jface.text.contentassist.ICompletionProposal;
21 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
22 import org.eclipse.jface.text.contentassist.IContextInformation;
23 import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
24 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
25
26 /**
27  * Example PHP completion processor.
28  */
29 public class PHPCompletionProcessor implements IContentAssistProcessor {
30
31         /**
32          * Simple content assist tip closer. The tip is valid in a range
33          * of 5 characters around its popup location.
34          */
35         protected static class Validator implements IContextInformationValidator, IContextInformationPresenter {
36
37                 protected int fInstallOffset;
38
39                 /*
40                  * @see IContextInformationValidator#isContextInformationValid(int)
41                  */
42                 public boolean isContextInformationValid(int offset) {
43                         return Math.abs(fInstallOffset - offset) < 5;
44                 }
45
46                 /*
47                  * @see IContextInformationValidator#install(IContextInformation, ITextViewer, int)
48                  */
49                 public void install(IContextInformation info, ITextViewer viewer, int offset) {
50                         fInstallOffset = offset;
51                 }
52
53                 /*
54                  * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int, TextPresentation)
55                  */
56                 public boolean updatePresentation(int documentPosition, TextPresentation presentation) {
57                         return false;
58                 }
59         };
60
61         protected final static String[] fgProposals =
62                 {
63                         "array",
64                         "break",
65                         "class",
66                         "continue",
67                         "do",
68                         "echo",
69                         "else",
70                         "elseif",
71                         "endfor",
72                         "endif",
73                         "for",
74                         "if",
75                         "while",
76                         "endwhile",
77                         "switch",
78                         "case",
79                         "endswitch",
80                         "return",
81                         "define",
82                         "include",
83                         "include_once",
84                         "require",
85                         "require_once",
86                         "function",
87                         "new",
88                         "old_function",
89                         "default",
90                         "global",
91                         "static",
92                         "foreach",
93                         "endforeach",
94                         "extends",
95                         "empty",
96                         "isset",
97                         "var" };
98       
99         protected IContextInformationValidator fValidator = new Validator();
100
101         /* (non-Javadoc)
102          * Method declared on IContentAssistProcessor
103          */
104         public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
105                 ICompletionProposal[] result = new ICompletionProposal[fgProposals.length];
106                 for (int i = 0; i < fgProposals.length; i++) {
107                         IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
108                         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$
109                 }
110                 return result;
111         }
112
113         /* (non-Javadoc)
114          * Method declared on IContentAssistProcessor
115          */
116         public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
117                 IContextInformation[] result = new IContextInformation[5];
118                 for (int i = 0; i < result.length; i++)
119                         result[i] = new ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"), new Object[] { new Integer(i), new Integer(documentOffset)}), //$NON-NLS-1$
120                         MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"), new Object[] { new Integer(i), new Integer(documentOffset - 5), new Integer(documentOffset + 5)})); //$NON-NLS-1$
121                 return result;
122         }
123
124         /* (non-Javadoc)
125          * Method declared on IContentAssistProcessor
126          */
127         public char[] getCompletionProposalAutoActivationCharacters() {
128                 return new char[] { '.', '(' };
129         }
130
131         /* (non-Javadoc)
132          * Method declared on IContentAssistProcessor
133          */
134         public char[] getContextInformationAutoActivationCharacters() {
135                 return new char[] { '#' };
136         }
137
138         /* (non-Javadoc)
139          * Method declared on IContentAssistProcessor
140          */
141         public IContextInformationValidator getContextInformationValidator() {
142                 return fValidator;
143         }
144
145         /* (non-Javadoc)
146          * Method declared on IContentAssistProcessor
147          */
148         public String getErrorMessage() {
149                 return null;
150         }
151 }