ebresie commented on a change in pull request #2820:
URL: https://github.com/apache/netbeans/pull/2820#discussion_r598342753



##########
File path: 
ide/db.sql.editor/src/org/netbeans/modules/db/sql/editor/completion/SQLCompletionProvider.java
##########
@@ -41,42 +57,190 @@
  * @author Andrei Badea
  */
 public class SQLCompletionProvider implements CompletionProvider {
+    private static final String SQL_CONNECTION_HINT_ID = "sql_connection_hint";
+    private String selection;
+    private static JTextComponent component = null;
+    private static DatabaseConnection dbconn = null;
 
+    @Override
     public CompletionTask createTask(int queryType, JTextComponent component) {
-        if (queryType == CompletionProvider.COMPLETION_QUERY_TYPE || queryType 
== CompletionProvider.COMPLETION_ALL_QUERY_TYPE) {
+        if (queryType == CompletionProvider.COMPLETION_QUERY_TYPE || 
+                queryType == CompletionProvider.COMPLETION_ALL_QUERY_TYPE) {
+            
+            /* to support DB related completion tasks (i.e. auto populating 
table 
+            names or db columns for given schema) check for connection */
             DatabaseConnection dbconn = findDBConn(component);
-            if (dbconn == null) {
-                // XXX perhaps should have an item in the completion instead?
-                Completion.get().hideAll();
-                SQLExecutionBaseAction.notifyNoDatabaseConnection();
-                return null;
+            // No database connection set or active
+            if (SQLCompletionProvider.dbconn == null) {
+                createSuggestions(component,dbconn);
             }
+            
             return new AsyncCompletionTask(new SQLCompletionQuery(dbconn), 
component);
         }
+        
+        // not a completion query type so return nothing
         return null;
     }
 
+    /**
+     * getAutoQueryTypes is invoked to check whether a popup with suggestions
+     * should be shown without the user explicitly asking for it.
+     * 
+     * If either #getAutoQueryTypes return a non-zero value or the user
+     * explicitly asks for completion, #createTask is invoked with the
+     * requested type. In case of SQL see
+     * org.netbeans.modules.db.sql.editor.completion.SQLCompletionQuery.
+     * 
+     * @param component
+     * @param typedText
+     * @return 
+     */
     public int getAutoQueryTypes(JTextComponent component, String typedText) {
+        /* TODO: Need to check "enable/disable" autocomplete is setting. 
+        See NETBEANS-188 */
+
+        // If "." has not been typed then acceptable to start checking for 
options.
         if (!".".equals(typedText)) { // NOI18N
             return 0;
         }
+        // check typed text if dot is present at the selected offset
         if (!isDotAtOffset(component, component.getSelectionStart() - 1)) {
             return 0;
         }
+        
+//        List<Fix> fixes = new ArrayList<>(Collections.emptyList());
+//        Document doc = component.getDocument();
+//        List<ErrorDescription> warnings = new 
ArrayList<>(Collections.emptyList());
+
+        // check if there is a DB connection
         DatabaseConnection dbconn = findDBConn(component);
         if (dbconn == null) {
             String message = NbBundle.getMessage(SQLCompletionProvider.class, 
"MSG_NoDatabaseConnection");
             StatusDisplayer.getDefault().setStatusText(message);
-            return 0;
+            /* TODO: Find How to povide tip to create new connection and/or 
+            allow further typing and/or other options. 
+            
+            Maybe provide a list of available connections. 
+            
+            return 0; */
+//            createSuggestions(component, doc, fixes, warnings, dbconn);
+            createSuggestions(component, dbconn);
         }
-        if (dbconn.getJDBCConnection() == null) {
+
+        // check if DB connection is active
+        if (dbconn != null && dbconn.getJDBCConnection() == null) {
             String message = NbBundle.getMessage(SQLCompletionProvider.class, 
"MSG_NotConnected");
             StatusDisplayer.getDefault().setStatusText(message);
-            return 0;
+            /* TODO: Find How to povide tip to "allow to connect" and/or 
+            allow further typing and/or other options. */
+//            return 0;
+
+            // determine the selected line
+            selection = component.getSelectedText();
+            Document doc = component.getDocument();
+                    
+            if (selection.length() > 0) {
+                component.replaceSelection("<b>" + selection + "</b>");
+                // use NbEditorUtilities.getLine() to extract a line from the 
+                // current document and offset, 
+                // for which we need a DocumentListener, as described in the 
next step:
+                int lineOffset = component.getSelectionStart() - 1;
+                
+                Line myLine = NbEditorUtilities.getLine(doc, lineOffset, 
false);
+
+                // TODO: Figure out how to set message
+                //Here we attach our annotation to the line:
+                SqlConnectionAnnotation.DEFAULT.attach(myLine);
+            }
         }
         return COMPLETION_QUERY_TYPE;
     }
 
+    private void createSuggestions(JTextComponent component, 
DatabaseConnection dbconn) {

Review comment:
       It is used as seen in the earlier comment above.  
   
   The hints start to show up until following an attempted the Ctrl+space key 
component.  Not sure this is the way it should or the preferred way for this to 
be done, but it worked at the time.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

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

Reply via email to