You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by vi...@apache.org on 2022/02/02 19:06:54 UTC

[netbeans-website] branch reviews/tutorials/nbm-code-completion created (now 31606a5)

This is an automated email from the ASF dual-hosted git repository.

vieiro pushed a change to branch reviews/tutorials/nbm-code-completion
in repository https://gitbox.apache.org/repos/asf/netbeans-website.git.


      at 31606a5  Review of nbm-code-completion

This branch includes the following new commits:

     new 31606a5  Review of nbm-code-completion

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@netbeans.apache.org
For additional commands, e-mail: commits-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[netbeans-website] 01/01: Review of nbm-code-completion

Posted by vi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

vieiro pushed a commit to branch reviews/tutorials/nbm-code-completion
in repository https://gitbox.apache.org/repos/asf/netbeans-website.git

commit 31606a5113327e81b3788ff7561f8910ec8c5fd0
Author: Antonio Vieiro <vi...@apache.org>
AuthorDate: Wed Feb 2 20:06:05 2022 +0100

    Review of nbm-code-completion
---
 .../content/tutorials/nbm-code-completion.asciidoc | 94 ++++++++++------------
 1 file changed, 44 insertions(+), 50 deletions(-)

diff --git a/netbeans.apache.org/src/content/tutorials/nbm-code-completion.asciidoc b/netbeans.apache.org/src/content/tutorials/nbm-code-completion.asciidoc
index 68793f9..38cc3c9 100644
--- a/netbeans.apache.org/src/content/tutorials/nbm-code-completion.asciidoc
+++ b/netbeans.apache.org/src/content/tutorials/nbm-code-completion.asciidoc
@@ -29,18 +29,18 @@
 :experimental:
 :description: NetBeans Code Completion Tutorial - Apache NetBeans
 :keywords: Apache NetBeans Platform, Platform Tutorials, NetBeans Code Completion Tutorial
+:reviewed: 2022-02-02
 
-This tutorial shows you how to implement the  link:https://bits.netbeans.org/dev/javadoc/org-netbeans-modules-editor-completion/overview-summary.html[Editor Code Completion API]. You will be shown how to implement the API in the context of HTML files. When the user invokes the code completion feature, a code completion box will appear, displaying words that can complete the text typed in the editor. At the end of this tutorial, HTML files will have a code completion box as follows:
-
-The content of the code completion box will come from country names retrieved from the JDK's  ``java.util.Locale``  package.
-
-NOTE: This document uses NetBeans IDE 8.0 and NetBeans Platform 8.0. If you are using an earlier version, see  link:74/nbm-code-completion.html[the previous version of this document].
-
-
-
-
-
+This tutorial shows you how to implement the
+link:https://bits.netbeans.org/dev/javadoc/org-netbeans-modules-editor-completion/overview-summary.html[Editor
+Code Completion API]. You will be shown how to implement the API in the context
+of HTML files. When the user invokes the code completion feature, a code
+completion box will appear, displaying words that can complete the text typed
+in the editor. At the end of this tutorial, HTML files will have a code
+completion box as follows:
 
+The content of the code completion box will come from country names retrieved
+from the JDK's  ``java.util.Locale``  package.
 
 For troubleshooting purposes, you are welcome to download the  link:http://web.archive.org/web/20150927111721/https://java.net/projects/nb-api-samples/sources/api-samples/show/versions/8.0/tutorials/CountryCodeCompleter[completed tutorial source code].
 
@@ -97,7 +97,7 @@ Set dependencies on the following:
 In the  ``CountriesCompletionProvider``  class, change the signature so that the class  ``implements CompletionProvider`` . Place the cursor on the line that defines the signature. A lightbulb appears. Click it and the IDE adds an import statement for  ``org.netbeans.spi.editor.completion.CompletionProvider`` . The lightbulb appears again. Click it again and the IDE creates skeleton methods for the two methods required by the CompletionProvider class. You should now see this:
 
 
-[source,java]
+[source,java,subs="macros"]
 ----
 
 package org.netbeans.modules.countries;
@@ -129,17 +129,17 @@ public class CountriesCompletionProvider implements  link:https://bits.netbeans.
 [start=4]
 1. Before coding the  ``CompletionProvider``  class, let's register it in the XML layer file, via a NetBeans Platform annotation:
 
-[source,java]
+[source,java,subs="macros"]
 ----
 
 package org.netbeans.modules.countries;
 
 import javax.swing.text.JTextComponent;
-*import org.netbeans.api.editor.mimelookup.MimeRegistration;*
+import org.netbeans.api.editor.mimelookup.MimeRegistration;
 import org.netbeans.spi.editor.completion.CompletionProvider;
 import org.netbeans.spi.editor.completion.CompletionTask;
 
-*@MimeRegistration(mimeType = "text/html", service = CompletionProvider.class)*
+@MimeRegistration(mimeType = "text/html", service = CompletionProvider.class)
 public class CountriesCompletionProvider implements CompletionProvider {
 
     @Override
@@ -166,7 +166,7 @@ In this section we create a skeleton implementation of  `` link:https://bits.net
 [start=1]
 1. In the createTask method, below the code from the previous section, add the following lines:
 
-[source,java]
+[source,java,subs="macros"]
 ----
 
 return new AsyncCompletionTask(new AsyncCompletionQuery() {
@@ -182,7 +182,7 @@ Here, we're returning  `` link:https://bits.netbeans.org/dev/javadoc/org-netbean
 [start=3]
 1. Next, we need to specify which code completion type we are working with. When the user clicks Ctrl-Space, or an alternative key combination defined by the user, our code completion entries should appear. This is the COMPLETION_QUERY_TYPE. Alternative query types exist, such as DOCUMENTATION_QUERY_TYPE and TOOLTIP_QUERY_TYPE. We need to test whether the user pressed the keys applicable to the COMPLETION_QUERY_TYPE. Therefore add the following test to the start of the  ``createTask``  method:
 
-[source,java]
+[source,java,subs="macros"]
 ----
 
 if (queryType != CompletionProvider.COMPLETION_QUERY_TYPE)
@@ -192,7 +192,7 @@ if (queryType != CompletionProvider.COMPLETION_QUERY_TYPE)
 At this stage, the  ``createTask``  method should look as follows:
 
 
-[source,java]
+[source,java,subs="macros"]
 ----
 
 @Override
@@ -217,7 +217,7 @@ In this section we return 0 as our  ``AutoQueryType`` , so that the code complet
 Before filling out the  ``query``  method, let's look at the  `` link:https://bits.netbeans.org/dev/javadoc/org-netbeans-modules-editor-completion/org/netbeans/spi/editor/completion/CompletionProvider.html#getAutoQueryTypes(javax.swing.text.JTextComponent,%20java.lang.String)[getAutoQueryTypes(JTextComponent jTextComponent, String string)]``  method. This method determines whether the code completion box appears _automatically_ or not. For now, let's return 0. This means that the code co [...]
 
 
-[source,java]
+[source,java,subs="macros"]
 ----
 
 @Override
@@ -240,7 +240,7 @@ In this section we will create a class that implements  `` link:https://bits.net
 [start=2]
 1. We will return to this class in later steps. For now, we will fill out the query method that we defined in the CompletionProvider class. Fill out the AsyncCompletionTask as follows, and note the explanatory comments in the code:
 
-[source,java]
+[source,java,subs="macros"]
 ----
 
 return new  link:http://bits.netbeans.org/dev/javadoc/org-netbeans-modules-editor-completion/org/netbeans/spi/editor/completion/support/AsyncCompletionTask.html[AsyncCompletionTask](new AsyncCompletionQuery() {
@@ -277,7 +277,7 @@ Read  link:http://blogs.oracle.com/geertjan/entry/java_classes_in_code_completio
 [start=4]
 1. In the CountriesCompletionItem class, define the constructor as follows:
 
-[source,java]
+[source,java,subs="macros"]
 ----
 
 private String text;
@@ -292,18 +292,14 @@ public CountriesCompletionItem(String text, int caretOffset) {
 }
 ----
 
-Note that here we're referencing an icon. This is the icon that will appear next to each entry represented by the CompletionItem in the code completion box. The icon can be any icon with a dimension of 16x16 pixels. For example, you could make use of this icon:
-
-
-image::images/cc_icon.png[]
+Note that here we're referencing an icon. This is the icon that will appear next to each entry represented by the CompletionItem in the code completion box. The icon can be any icon with a dimension of 16x16 pixels. For example, you could make use of this icon: image:images/cc_icon.png[]
 
 If you like, you can right-click the image above and save it to the location specified in the ImageIcon definition above.
 
-
 [start=5]
 1. Next define the  `` link:https://bits.netbeans.org/dev/javadoc/org-netbeans-modules-editor-completion/org/netbeans/spi/editor/completion/CompletionItem.html#getPreferredWidth(java.awt.Graphics,%20java.awt.Font)[getPreferredWidth()]``  and  `` link:https://bits.netbeans.org/dev/javadoc/org-netbeans-modules-editor-completion/org/netbeans/spi/editor/completion/CompletionItem.html#render(java.awt.Graphics,%20java.awt.Font,%20java.awt.Color,%20java.awt.Color,%20int,%20int,%20boolean)[rende [...]
 
-[source,java]
+[source,java,subs="macros"]
 ----
 
 @Override
@@ -322,7 +318,7 @@ public void render(Graphics g, Font defaultFont, Color defaultColor,
 Define the  `` link:https://bits.netbeans.org/dev/javadoc/org-netbeans-modules-editor-completion/org/netbeans/spi/editor/completion/CompletionItem.html#getSortText()[getSortText()]``  method as follows:
 
 
-[source,java]
+[source,java,subs="macros"]
 ----
 
 @Override
@@ -334,7 +330,7 @@ public CharSequence getSortText() {
 Next, define the  `` link:https://bits.netbeans.org/dev/javadoc/org-netbeans-modules-editor-completion/org/netbeans/spi/editor/completion/CompletionItem.html#getInsertPrefix()[getInsertPrefix()]``  method:
 
 
-[source,java]
+[source,java,subs="macros"]
 ----
 
 @Override
@@ -363,7 +359,7 @@ In this section we specify what happens when the user presses the Enter key or c
 [start=1]
 1. Fill out the  ``defaultAction()``  method as follows:
 
-[source,java]
+[source,java,subs="macros"]
 ----
 
 @Override
@@ -397,7 +393,7 @@ In this section we enable the code completion box to narrow while the user is ty
 [start=1]
 1. In the CountriesCompletionProvider class, rewrite the  ``AsyncCompletionTask()``  method by adding the statements highlighted in bold below:
 
-[source,java]
+[source,java,subs="macros"]
 ----
 
 return new AsyncCompletionTask(new AsyncCompletionQuery() {
@@ -405,7 +401,7 @@ return new AsyncCompletionTask(new AsyncCompletionQuery() {
     @Override
     protected void query(CompletionResultSet completionResultSet, Document document, int caretOffset) {
 
-        *String filter = null;
+        String filter = null;
         int startOffset = caretOffset - 1;
 
         try {
@@ -421,7 +417,7 @@ return new AsyncCompletionTask(new AsyncCompletionQuery() {
             }
         } catch (BadLocationException ex) {
             Exceptions.printStackTrace(ex);
-        }*
+        }
 
         //Iterate through the available locales
         //and assign each country display name
@@ -430,11 +426,11 @@ return new AsyncCompletionTask(new AsyncCompletionQuery() {
         for (int i = 0; i < locales.length; i++) {
             final Locale locale = locales[i];
             final String country = locale.getDisplayCountry();
-            *//Here we test whether the country starts with the filter defined above:*
-            if (!country.equals("") *&amp;&amp; country.startsWith(filter)*) {
-                *//Here we include the start offset, so that we'll be able to figure out
-                //the number of characters that we'll need to remove:*
-                completionResultSet.addItem(new CountriesCompletionItem(country, *startOffset,* caretOffset));
+            //Here we test whether the country starts with the filter defined above:
+            if (!country.equals("") && country.startsWith(filter)) {
+                //Here we include the start offset, so that we'll be able to figure out
+                //the number of characters that we'll need to remove:
+                completionResultSet.addItem(new CountriesCompletionItem(country, startOffset, caretOffset));
             }
         }
         completionResultSet.finish();
@@ -448,7 +444,7 @@ return new AsyncCompletionTask(new AsyncCompletionQuery() {
 [start=2]
 1. Right at the end of the CountriesCompletionProvider, add the following two methods:
 
-[source,java]
+[source,java,subs="macros"]
 ----
 
 static int getRowFirstNonWhite(StyledDocument doc, int offset)
@@ -473,7 +469,7 @@ throws BadLocationException {
 ----
 
 
-[source,java]
+[source,java,subs="macros"]
 ----
 
 static int indexOfWhite(char[] line){
@@ -491,16 +487,16 @@ static int indexOfWhite(char[] line){
 
 
 [start=3]
-1. Change the constructor of the  ``CountriesCompletionItem``  to receive the start offset. Then change the  ``defaultAction``  so that the start offset will be used in determining the characters that will be removed when the selected country is inserted. Below, the statements highlighted in bold are those that should be added:*private int dotOffset;*
-
-[source,java]
-----
+1. Change the constructor of the  ``CountriesCompletionItem``  to receive the start offset. Then change the  ``defaultAction``  so that the start offset will be used in determining the characters that will be removed when the selected country is inserted. 
 
+Below, the statements that should be added: `private int dotOffset;`
 
+[source,java,subs="macros"]
+----
 
-public CountriesCompletionItem(String text, *int dotOffset,* int caretOffset) {
+public CountriesCompletionItem(String text, int dotOffset, int caretOffset) {
     this.text = text;
-    *this.dotOffset = dotOffset;*
+    this.dotOffset = dotOffset;
     this.caretOffset = caretOffset;
 }
 
@@ -547,7 +543,7 @@ image::images/cc_65-result-of-cc3.png[]
 Here is the code that will achieve the result shown in the screenshot above:
 
 
-[source,java]
+[source,java,subs="macros"]
 ----
 
 @Override
@@ -574,7 +570,7 @@ image::images/cc_65-result-of-cc4.png[]
 Make use of the documentation box like this, by implementing the  ``createDocumentationTask``  method in the  ``CountriesCompletionItem``  class:
 
 
-[source,java]
+[source,java,subs="macros"]
 ----
 
 @Override
@@ -592,7 +588,7 @@ public CompletionTask createDocumentationTask() {
 In the code above, the reference to the CountriesCompletionDocumentation class could be implemented as follows:
 
 
-[source,java]
+[source,java,subs="macros"]
 ----
 
 public class CountriesCompletionDocumentation implements CompletionDocumentation {
@@ -638,8 +634,6 @@ Congratulations, you have now completed a simple implementation of a code comple
 
 link:http://netbeans.apache.org/community/mailing-lists.html[ Send Us Your Feedback]
 
-
-
 == See Also
 
 For more information about creating and developing plugins, see the following resources:

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@netbeans.apache.org
For additional commands, e-mail: commits-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists