Skip to content

Commit ad419db

Browse files
committed
Merge pull request #20 from junichi11/code-completion
Add code completions
2 parents 7b7d6b1 + 9c57241 commit ad419db

File tree

13 files changed

+1549
-4
lines changed

13 files changed

+1549
-4
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,19 @@ class BookmarksTable extends Table {
116116
$this->Bookmarks->Tags->[Ctrl+Space]
117117
```
118118

119+
**Method Parameters**
120+
```php
121+
// e.g. path/to/your/template/index.ctp
122+
// file path completion
123+
$this->Html->css('[Ctrl+Space]');
124+
125+
// constants
126+
$this->Html->docType('[Ctrl+Space]');
127+
```
128+
129+
**NOTE**
130+
At first, add a semicolon(;) if you want to use code completions for parameters. Tips: You can add it like the following: `Ctrl+;`
131+
119132
### Smart Go To
120133

121134
This feature is not enabled by default. If you want to use it, please set the KeyMap to it.

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=6e2440d2
1+
build.xml.data.CRC32=736c8bcc
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=6e2440d2
6+
nbproject/build-impl.xml.data.CRC32=736c8bcc
77
nbproject/build-impl.xml.script.CRC32=3e0b9a07
88
nbproject/build-impl.xml.stylesheet.CRC32=238281d1@2.67.1

nbproject/project.xml

Lines changed: 28 additions & 1 deletion
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.completion</code-name-base>
56+
<build-prerequisite/>
57+
<compile-dependency/>
58+
<run-dependency>
59+
<release-version>1</release-version>
60+
<specification-version>1.40.1.2</specification-version>
61+
</run-dependency>
62+
</dependency>
5463
<dependency>
5564
<code-name-base>org.netbeans.modules.editor.lib</code-name-base>
5665
<build-prerequisite/>
@@ -66,7 +75,16 @@
6675
<compile-dependency/>
6776
<run-dependency>
6877
<release-version>1</release-version>
69-
<specification-version>1.85.1.43</specification-version>
78+
<implementation-version/>
79+
</run-dependency>
80+
</dependency>
81+
<dependency>
82+
<code-name-base>org.netbeans.modules.editor.mimelookup</code-name-base>
83+
<build-prerequisite/>
84+
<compile-dependency/>
85+
<run-dependency>
86+
<release-version>1</release-version>
87+
<specification-version>1.36.1</specification-version>
7088
</run-dependency>
7189
</dependency>
7290
<dependency>
@@ -78,6 +96,15 @@
7896
<specification-version>1.42.1</specification-version>
7997
</run-dependency>
8098
</dependency>
99+
<dependency>
100+
<code-name-base>org.netbeans.modules.lexer</code-name-base>
101+
<build-prerequisite/>
102+
<compile-dependency/>
103+
<run-dependency>
104+
<release-version>2</release-version>
105+
<specification-version>1.59.1.1</specification-version>
106+
</run-dependency>
107+
</dependency>
81108
<dependency>
82109
<code-name-base>org.netbeans.modules.nbjunit</code-name-base>
83110
<build-prerequisite/>
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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.awt.Color;
45+
import java.awt.Font;
46+
import java.awt.Graphics;
47+
import java.awt.event.KeyEvent;
48+
import java.util.logging.Level;
49+
import java.util.logging.Logger;
50+
import javax.swing.ImageIcon;
51+
import javax.swing.text.BadLocationException;
52+
import javax.swing.text.JTextComponent;
53+
import javax.swing.text.StyledDocument;
54+
import org.netbeans.api.editor.completion.Completion;
55+
import org.netbeans.modules.php.cake3.CakePHP3Constants;
56+
import org.netbeans.spi.editor.completion.CompletionItem;
57+
import org.netbeans.spi.editor.completion.CompletionTask;
58+
import org.netbeans.spi.editor.completion.support.CompletionUtilities;
59+
import org.openide.text.NbDocument;
60+
import org.openide.util.Exceptions;
61+
import org.openide.util.ImageUtilities;
62+
63+
/**
64+
*
65+
* @author junichi11
66+
*/
67+
public class CakePHP3CompletionItem implements CompletionItem {
68+
69+
private final String text;
70+
private final String filter;
71+
private final int startOffset;
72+
private static final ImageIcon ICON = ImageUtilities.loadImageIcon(CakePHP3Constants.CAKE_ICON_16, true);
73+
private static final Logger LOGGER = Logger.getLogger(CakePHP3CompletionItem.class.getName());
74+
75+
public CakePHP3CompletionItem(String text, String filter, int startOffset) {
76+
this.text = text;
77+
this.filter = filter;
78+
this.startOffset = startOffset;
79+
}
80+
81+
@Override
82+
public void defaultAction(JTextComponent jtc) {
83+
try {
84+
final StyledDocument doc = (StyledDocument) jtc.getDocument();
85+
NbDocument.runAtomicAsUser(doc, new Runnable() {
86+
87+
@Override
88+
public void run() {
89+
try {
90+
String insertString;
91+
if (text.startsWith(filter)) {
92+
insertString = text.replace(filter, ""); // NOI18N
93+
doc.insertString(startOffset, insertString, null);
94+
} else {
95+
insertString = text;
96+
int removeLength = filter.length();
97+
int removeStart = startOffset - removeLength;
98+
if (removeStart >= 0) {
99+
doc.remove(removeStart, removeLength);
100+
doc.insertString(removeStart, insertString, null);
101+
} else {
102+
LOGGER.log(Level.WARNING, "Invalid start position[text: {0}, filter: {1}]", new Object[]{text, filter}); // NOI18N
103+
}
104+
}
105+
} catch (BadLocationException ex) {
106+
Exceptions.printStackTrace(ex);
107+
}
108+
Completion.get().hideAll();
109+
}
110+
});
111+
} catch (BadLocationException ex) {
112+
Exceptions.printStackTrace(ex);
113+
}
114+
}
115+
116+
@Override
117+
public void processKeyEvent(KeyEvent ke) {
118+
}
119+
120+
@Override
121+
public int getPreferredWidth(Graphics grphcs, Font font) {
122+
return CompletionUtilities.getPreferredWidth(text, null, grphcs, font);
123+
}
124+
125+
@Override
126+
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);
128+
}
129+
130+
@Override
131+
public CompletionTask createDocumentationTask() {
132+
return null;
133+
}
134+
135+
@Override
136+
public CompletionTask createToolTipTask() {
137+
return null;
138+
}
139+
140+
@Override
141+
public boolean instantSubstitution(JTextComponent jtc) {
142+
return false;
143+
}
144+
145+
@Override
146+
public int getSortPriority() {
147+
return 0;
148+
}
149+
150+
@Override
151+
public CharSequence getSortText() {
152+
return text;
153+
}
154+
155+
@Override
156+
public CharSequence getInsertPrefix() {
157+
return text;
158+
}
159+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 javax.swing.text.Document;
45+
import javax.swing.text.JTextComponent;
46+
import org.netbeans.modules.editor.NbEditorUtilities;
47+
import org.netbeans.modules.php.api.phpmodule.PhpModule;
48+
import org.netbeans.modules.php.cake3.modules.CakePHP3Module;
49+
import org.netbeans.spi.editor.completion.CompletionProvider;
50+
import org.netbeans.spi.editor.completion.CompletionTask;
51+
import org.openide.filesystems.FileObject;
52+
53+
/**
54+
*
55+
* @author junichi11
56+
*/
57+
public abstract class CakePHP3CompletionProvider implements CompletionProvider {
58+
59+
@Override
60+
public CompletionTask createTask(int queryType, JTextComponent textComponent) {
61+
if (queryType != CompletionProvider.COMPLETION_QUERY_TYPE) {
62+
return null;
63+
}
64+
Document document = textComponent.getDocument();
65+
if (document == null) {
66+
return null;
67+
}
68+
FileObject fo = NbEditorUtilities.getFileObject(document);
69+
if (fo == null) {
70+
return null;
71+
}
72+
PhpModule phpModule = PhpModule.Factory.forFileObject(fo);
73+
if (!CakePHP3Module.isCakePHP(phpModule)) {
74+
return null;
75+
}
76+
return createTask(queryType, textComponent, phpModule, fo);
77+
}
78+
79+
@Override
80+
public int getAutoQueryTypes(JTextComponent jtc, String string) {
81+
return 0;
82+
}
83+
84+
public abstract CompletionTask createTask(int queryType, JTextComponent textComponent, PhpModule phpModule, FileObject fo);
85+
86+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.ArrayList;
45+
import java.util.List;
46+
import org.netbeans.spi.editor.completion.CompletionItem;
47+
import org.openide.filesystems.FileObject;
48+
49+
public class ConstantParameter extends Parameter {
50+
51+
ConstantParameter(int position, String className, String methodName, FileObject fileObject) {
52+
super(position, className, methodName, fileObject);
53+
}
54+
55+
@Override
56+
public List<CompletionItem> getCompletionItems(String filter, int caretOffset) {
57+
List<CompletionItem> items = new ArrayList<>();
58+
for (String element : getElements(getPosition(), getClassName(), getMethodName())) {
59+
if (element.startsWith(filter)) {
60+
items.add(new CakePHP3CompletionItem(element, filter, caretOffset));
61+
}
62+
}
63+
return items;
64+
}
65+
66+
}

0 commit comments

Comments
 (0)