Skip to content

Commit 8b897f6

Browse files
committed
Add a code completion for FlashComponent #22
1 parent 230e5e6 commit 8b897f6

File tree

4 files changed

+299
-3
lines changed

4 files changed

+299
-3
lines changed

nbproject/genfiles.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
build.xml.data.CRC32=e8740970
1+
build.xml.data.CRC32=1d892e9b
22
build.xml.script.CRC32=99224ca9
33
build.xml.stylesheet.CRC32=a56c6a5b@2.67.1
44
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
55
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
6-
nbproject/build-impl.xml.data.CRC32=e8740970
6+
nbproject/build-impl.xml.data.CRC32=1d892e9b
77
nbproject/build-impl.xml.script.CRC32=3e0b9a07
88
nbproject/build-impl.xml.stylesheet.CRC32=238281d1@2.67.1

nbproject/project.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@
5151
<specification-version>1.79.1.5.22.43</specification-version>
5252
</run-dependency>
5353
</dependency>
54+
<dependency>
55+
<code-name-base>org.netbeans.modules.editor.codetemplates</code-name-base>
56+
<build-prerequisite/>
57+
<compile-dependency/>
58+
<run-dependency>
59+
<release-version>1</release-version>
60+
<specification-version>1.38.1.1</specification-version>
61+
</run-dependency>
62+
</dependency>
5463
<dependency>
5564
<code-name-base>org.netbeans.modules.editor.completion</code-name-base>
5665
<build-prerequisite/>

src/org/netbeans/modules/php/cake3/editor/completion/CakePHP3CompletionItem.java

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,20 @@
4545
import java.awt.Font;
4646
import java.awt.Graphics;
4747
import java.awt.event.KeyEvent;
48+
import java.util.Arrays;
49+
import java.util.Collections;
50+
import java.util.List;
4851
import java.util.logging.Level;
4952
import java.util.logging.Logger;
5053
import javax.swing.ImageIcon;
5154
import javax.swing.text.BadLocationException;
55+
import javax.swing.text.Document;
5256
import javax.swing.text.JTextComponent;
5357
import javax.swing.text.StyledDocument;
5458
import org.netbeans.api.editor.completion.Completion;
59+
import org.netbeans.lib.editor.codetemplates.api.CodeTemplate;
60+
import org.netbeans.lib.editor.codetemplates.api.CodeTemplateManager;
61+
import org.netbeans.modules.php.api.util.StringUtils;
5562
import org.netbeans.modules.php.cake3.CakePHP3Constants;
5663
import org.netbeans.spi.editor.completion.CompletionItem;
5764
import org.netbeans.spi.editor.completion.CompletionTask;
@@ -78,6 +85,18 @@ public CakePHP3CompletionItem(String text, String filter, int startOffset) {
7885
this.startOffset = startOffset;
7986
}
8087

88+
public String getText() {
89+
return text;
90+
}
91+
92+
public String getFilter() {
93+
return filter;
94+
}
95+
96+
public int getStartOffset() {
97+
return startOffset;
98+
}
99+
81100
@Override
82101
public void defaultAction(JTextComponent jtc) {
83102
try {
@@ -124,7 +143,19 @@ public int getPreferredWidth(Graphics grphcs, Font font) {
124143

125144
@Override
126145
public void render(Graphics grphcs, Font font, Color defaultColor, Color backgroundColor, int width, int height, boolean selected) {
127-
CompletionUtilities.renderHtml(ICON, text, null, grphcs, font, defaultColor, width, height, selected);
146+
CompletionUtilities.renderHtml(getIcon(), getLeftHtmlText(), getRightHtmlText(), grphcs, font, defaultColor, width, height, selected);
147+
}
148+
149+
public ImageIcon getIcon() {
150+
return ICON;
151+
}
152+
153+
public String getLeftHtmlText() {
154+
return text;
155+
}
156+
157+
public String getRightHtmlText() {
158+
return null;
128159
}
129160

130161
@Override
@@ -156,4 +187,65 @@ public CharSequence getSortText() {
156187
public CharSequence getInsertPrefix() {
157188
return text;
158189
}
190+
191+
static class FlashMethodCompletionItem extends CakePHP3CompletionItem {
192+
193+
private static final String METHOD_FORMAT1 = "%s($name)"; // NOI18N
194+
private static final String METHOD_FORMAT2 = "%s($name, $options = [])"; // NOI18N
195+
private final int paramCount;
196+
197+
FlashMethodCompletionItem(String text, String filter, int startOffset, int paramCount) {
198+
super(text, filter, startOffset);
199+
this.paramCount = paramCount;
200+
}
201+
202+
@Override
203+
public void defaultAction(JTextComponent jtc) {
204+
Document document = jtc.getDocument();
205+
if (document == null) {
206+
return;
207+
}
208+
CodeTemplateManager manager = CodeTemplateManager.get(document);
209+
String text = getText();
210+
if (text.startsWith(getFilter())) {
211+
text = text.replace(getFilter(), ""); // NOI18N
212+
}
213+
StringBuilder sb = new StringBuilder();
214+
sb.append(text).append("(") // NOI18N
215+
.append("${$name}"); // NOI18N
216+
if (paramCount >= 2) {
217+
sb.append(", ${$options}"); // NOI18N
218+
}
219+
sb.append(")"); // NOI18N
220+
CodeTemplate template = manager.createTemporary(sb.toString());
221+
template.insert(jtc);
222+
}
223+
224+
@Override
225+
public String getLeftHtmlText() {
226+
String format;
227+
switch (paramCount) {
228+
case 1:
229+
format = METHOD_FORMAT1;
230+
break;
231+
case 2:
232+
format = METHOD_FORMAT2;
233+
break;
234+
default:
235+
throw new AssertionError();
236+
}
237+
return String.format(format, getText());
238+
}
239+
240+
public static List<CakePHP3CompletionItem> createItems(String text, String filter, int startOffset) {
241+
if (StringUtils.isEmpty(text)) {
242+
Collections.emptyList();
243+
}
244+
return Arrays.<CakePHP3CompletionItem>asList(
245+
new FlashMethodCompletionItem(text, filter, startOffset, 1),
246+
new FlashMethodCompletionItem(text, filter, startOffset, 2)
247+
);
248+
}
249+
250+
}
159251
}
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright 2015 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7+
* Other names may be trademarks of their respective owners.
8+
*
9+
* The contents of this file are subject to the terms of either the GNU
10+
* General Public License Version 2 only ("GPL") or the Common
11+
* Development and Distribution License("CDDL") (collectively, the
12+
* "License"). You may not use this file except in compliance with the
13+
* License. You can obtain a copy of the License at
14+
* http://www.netbeans.org/cddl-gplv2.html
15+
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16+
* specific language governing permissions and limitations under the
17+
* License. When distributing the software, include this License Header
18+
* Notice in each file and include the License file at
19+
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20+
* particular file as subject to the "Classpath" exception as provided
21+
* by Oracle in the GPL Version 2 section of the License file that
22+
* accompanied this code. If applicable, add the following below the
23+
* License Header, with the fields enclosed by brackets [] replaced by
24+
* your own identifying information:
25+
* "Portions Copyrighted [year] [name of copyright owner]"
26+
*
27+
* If you wish your version of this file to be governed by only the CDDL
28+
* or only the GPL Version 2, indicate your decision by adding
29+
* "[Contributor] elects to include this software in this distribution
30+
* under the [CDDL or GPL Version 2] license." If you do not indicate a
31+
* single choice of license, a recipient has the option to distribute
32+
* your version of this file under either the CDDL, the GPL Version 2 or
33+
* to extend the choice of license to its licensees as provided above.
34+
* However, if you add GPL Version 2 code and therefore, elected the GPL
35+
* Version 2 license, then the option applies only if the new code is
36+
* made subject to such option by the copyright holder.
37+
*
38+
* Contributor(s):
39+
*
40+
* Portions Copyrighted 2015 Sun Microsystems, Inc.
41+
*/
42+
package org.netbeans.modules.php.cake3.editor.completion;
43+
44+
import java.util.Arrays;
45+
import java.util.List;
46+
import javax.swing.text.Document;
47+
import javax.swing.text.JTextComponent;
48+
import org.netbeans.api.editor.mimelookup.MimeRegistration;
49+
import org.netbeans.api.lexer.Token;
50+
import org.netbeans.api.lexer.TokenSequence;
51+
import org.netbeans.modules.editor.NbEditorUtilities;
52+
import org.netbeans.modules.editor.lib2.DocUtils;
53+
import org.netbeans.modules.php.api.phpmodule.PhpModule;
54+
import static org.netbeans.modules.php.api.util.FileUtils.PHP_MIME_TYPE;
55+
import org.netbeans.modules.php.cake3.modules.CakePHP3Module;
56+
import org.netbeans.modules.php.cake3.modules.ModuleInfo;
57+
import org.netbeans.modules.php.editor.lexer.LexUtilities;
58+
import org.netbeans.modules.php.editor.lexer.PHPTokenId;
59+
import org.netbeans.spi.editor.completion.CompletionProvider;
60+
import static org.netbeans.spi.editor.completion.CompletionProvider.COMPLETION_QUERY_TYPE;
61+
import org.netbeans.spi.editor.completion.CompletionResultSet;
62+
import org.netbeans.spi.editor.completion.CompletionTask;
63+
import org.netbeans.spi.editor.completion.support.AsyncCompletionQuery;
64+
import org.netbeans.spi.editor.completion.support.AsyncCompletionTask;
65+
import org.openide.filesystems.FileObject;
66+
67+
/**
68+
*
69+
* @author junichi11
70+
*/
71+
@MimeRegistration(mimeType = PHP_MIME_TYPE, service = CompletionProvider.class)
72+
public class MethodCompletionProvider extends CakePHP3CompletionProvider {
73+
74+
@Override
75+
public CompletionTask createTask(int queryType, JTextComponent textComponent, PhpModule phpModule, FileObject fo) {
76+
if (queryType != COMPLETION_QUERY_TYPE) {
77+
return null;
78+
}
79+
Document document = textComponent.getDocument();
80+
if (document == null) {
81+
return null;
82+
}
83+
TokenSequence<PHPTokenId> tokenSequence = null;
84+
int caretPosition = textComponent.getCaretPosition();
85+
DocUtils.atomicLock(document);
86+
try {
87+
tokenSequence = LexUtilities.getPHPTokenSequence(document, caretPosition);
88+
} finally {
89+
DocUtils.atomicUnlock(document);
90+
}
91+
if (tokenSequence == null) {
92+
return null;
93+
}
94+
tokenSequence.move(caretPosition);
95+
tokenSequence.moveNext();
96+
Token<? extends PHPTokenId> previousToken = LexUtilities.findPreviousToken(tokenSequence, Arrays.asList(PHPTokenId.PHP_STRING, PHPTokenId.PHP_SEMICOLON));
97+
if (previousToken == null || previousToken.id() != PHPTokenId.PHP_STRING) {
98+
return null;
99+
}
100+
101+
return new AsyncCompletionTask(new AsyncCompletionQuery() {
102+
103+
@Override
104+
protected void query(CompletionResultSet resultSet, Document doc, int caretOffset) {
105+
try {
106+
autoComplete(resultSet, doc, caretOffset);
107+
} finally {
108+
resultSet.finish();
109+
}
110+
}
111+
}, textComponent);
112+
}
113+
114+
private void autoComplete(CompletionResultSet resultSet, Document document, int offset) {
115+
DocUtils.atomicLock(document);
116+
TokenSequence<PHPTokenId> tokenSequence = null;
117+
try {
118+
tokenSequence = LexUtilities.getPHPTokenSequence(document, offset);
119+
} finally {
120+
DocUtils.atomicUnlock(document);
121+
}
122+
123+
if (tokenSequence == null) {
124+
return;
125+
}
126+
127+
tokenSequence.move(offset);
128+
String filter = ""; // NOI18N
129+
if (tokenSequence.moveNext()) {
130+
filter = getFilter(tokenSequence, offset);
131+
}
132+
if (tokenSequence.movePrevious()) {
133+
int startOffset = tokenSequence.offset();
134+
if (tokenSequence.token().id() == PHPTokenId.PHP_STRING) {
135+
filter = getFilter(tokenSequence, offset);
136+
}
137+
if (tokenSequence.token().id() != PHPTokenId.PHP_PAAMAYIM_NEKUDOTAYIM
138+
&& tokenSequence.token().id() != PHPTokenId.PHP_OBJECT_OPERATOR) {
139+
tokenSequence.movePrevious();
140+
}
141+
tokenSequence.movePrevious();
142+
if (tokenSequence.token().id() == PHPTokenId.WHITESPACE) {
143+
tokenSequence.movePrevious();
144+
}
145+
String varName = tokenSequence.token().text().toString();
146+
tokenSequence.moveNext();
147+
148+
switch (varName) {
149+
case "Flash": // NOI18N
150+
autoCompleteFlash(document, filter, resultSet, startOffset);
151+
break;
152+
default:
153+
break;
154+
}
155+
156+
}
157+
158+
}
159+
160+
private String getFilter(TokenSequence<PHPTokenId> tokenSequence, int offset) {
161+
Token<PHPTokenId> token = tokenSequence.token();
162+
PHPTokenId id = token.id();
163+
if (id == PHPTokenId.PHP_STRING) {
164+
String tokenString = token.text().toString();
165+
int filterLength = offset - tokenSequence.offset();
166+
if (filterLength >= 0 && tokenString.length() >= filterLength) {
167+
return tokenString.substring(0, filterLength);
168+
}
169+
}
170+
return ""; // NOI18N
171+
}
172+
173+
private void autoCompleteFlash(Document document, String filter, CompletionResultSet resultSet, int startOffset) {
174+
FileObject fileObject = NbEditorUtilities.getFileObject(document);
175+
CakePHP3Module cakeModule = CakePHP3Module.forFileObject(fileObject);
176+
ModuleInfo info = cakeModule.createModuleInfo(fileObject);
177+
List<FileObject> directories = cakeModule.getDirectories(info.getBase(), CakePHP3Module.Category.ELEMENT, info.getPluginName());
178+
for (FileObject directory : directories) {
179+
FileObject flashDirectory = directory.getFileObject("Flash"); // NOI18N
180+
if (flashDirectory != null && flashDirectory.isFolder()) {
181+
for (FileObject child : flashDirectory.getChildren()) {
182+
if (child.isFolder()) {
183+
continue;
184+
}
185+
String name = child.getName();
186+
if (!name.startsWith(filter)) {
187+
continue;
188+
}
189+
resultSet.addAllItems(CakePHP3CompletionItem.FlashMethodCompletionItem.createItems(name, filter, startOffset));
190+
}
191+
}
192+
}
193+
}
194+
195+
}

0 commit comments

Comments
 (0)