This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 139776

Summary: getAutoQueryTypes returns 0 and still calls query()
Product: editor Reporter: Samaresh Panda <samaresh>
Component: Completion & TemplatesAssignee: Dusan Balek <dbalek>
Status: RESOLVED FIXED    
Severity: blocker Keywords: API, SIMPLEFIX
Priority: P3    
Version: 6.x   
Hardware: All   
OS: All   
Issue Type: DEFECT Exception Reporter:
Bug Depends on:    
Bug Blocks: 139181    
Attachments: createTask is called only on providers whose getAQT != 0

Description Samaresh Panda 2008-07-12 00:59:50 UTC
While working on issue 139181, I did some optimization, not to get completion items. As per API it shouldn't come into
query() if my getAutoQueryType() returns 0. Apparently not. I thought this always used to work. Kindly take a look.


Module: xml.schema.completion.
- Write System.out.println() inside SchemaBasedCompletionProvider::getAutoQueryTypes() where it returns 0.
- Write another System.out.println() inside CompletionQuery::query().

You'll see that both getting printed. Find the attached diff.
Comment 1 Samaresh Panda 2008-07-12 01:02:11 UTC
diff -r 758167c63e88 xml.schema.completion/src/org/netbeans/modules/xml/schema/completion/CompletionQuery.java
--- a/xml.schema.completion/src/org/netbeans/modules/xml/schema/completion/CompletionQuery.java Fri Jul 11 17:59:18 2008
-0500
+++ b/xml.schema.completion/src/org/netbeans/modules/xml/schema/completion/CompletionQuery.java Fri Jul 11 19:00:35 2008
-0500
@@ -82,6 +82,7 @@
 //            return;
 //        }

+        System.out.println("Trying to get completion items!!!!!");
         List<CompletionResultItem> items = getCompletionItems(doc, caretOffset);
         if(items != null) resultSet.addAllItems(items);
         resultSet.finish();
diff -r 758167c63e88 xml.schema.completion/src/org/netbeans/modules/xml/schema/completion/SchemaBasedCompletionProvider.java
--- a/xml.schema.completion/src/org/netbeans/modules/xml/schema/completion/SchemaBasedCompletionProvider.java   Fri Jul
11 17:59:18 2008 -0500
+++ b/xml.schema.completion/src/org/netbeans/modules/xml/schema/completion/SchemaBasedCompletionProvider.java   Fri Jul
11 19:00:35 2008 -0500
@@ -72,6 +72,8 @@
             return 0;
         XMLSyntaxSupport support = ((XMLSyntaxSupport)doc.getSyntaxSupport());
         if(support.noCompletion(component) || !CompletionUtil.canProvideComplet
ion(doc)) {
+            System.out.println("No completion!!!!!");
+            System.out.println(this);
             return 0;
         }
Comment 2 Vitezslav Stejskal 2008-07-14 12:55:45 UTC
IMO SchemaBasedCompletionProvider::getAutoQueryTypes only controls automatic code completion. That is if CC popup window
should be shown or not after writing some characters. In java this is bound to the dot character ".". But if a user
requests CC by ctrl+space the provider is called no matter of getAutoQueryTypes' value.

Are you saying that SchemaBasedCompletionProvider.createTask is called even if you do __not__ press ctrl+space and
getAutoQueryTypes returns 0?
Comment 3 Samaresh Panda 2008-07-14 22:25:05 UTC
I do understand that getAutoQueryType() controls automatic completion that is it decides whether or not to show
completion items. However in this case, even if it returns 0, it goes into createTask and calls query() which I do not want.

To reproduce:
- add System.out.println() in SchemaBasedCompletionProvider
- Open a plain XML that neither is based on DTD nor schema. Something like:
        <?xml version="1.0" encoding="windows-1252"?>
        <root>
            |
        </root>
- Type some char at |
Comment 4 Vitezslav Stejskal 2008-07-20 13:08:25 UTC
Doing that shows no completion window. The SBCP.getAutoQueryTypes returns zero from the second 'if' section. Am I doing
something wrong?

I looked in the code and there is a possibility to call CompletionProvider.createTask even when CP.getAutoQueryTypes
returns 0. It happens in a situation when there is multiple CP implementations registered and at least one of them
returns CP.getAQT != 0. Then CP.createTask is called for __all__ registered CPs. I guess this is a deficiency in the CC
infrastructure, which we should fix.

However, since I can't reproduce the problem I can't confirm that this is what happens in SBCP. Samaresh, could you
please confirm that? Maybe add some logs in CompletionImpl.insertUpdate and CompletionImpl.completionQuery and see how
many providers there are and what they return from getAQT. Thanks
Comment 5 Vitezslav Stejskal 2008-07-20 15:56:22 UTC
Created attachment 65064 [details]
createTask is called only on providers whose getAQT != 0
Comment 6 Vitezslav Stejskal 2008-07-20 16:16:38 UTC
I nearly pushed the above patch to main, but then I realized that the current behavior is probably correct. The
CP.getAQT __only__ determines if the CC should auto-popup. And we really have to call createTask on all providers,
because obviously even those providers that say getAQT == 0 may still want to contribute completion items if the CC is
actually shown. The point is that CP.getAQT says nothing about completion items available for the given context.

So, in general CompletionProviders should be ready to have their createTask called even after they returned 0 from
getAutoQueryType, because there can be some other CP which getAQT returned non-zero (showing the CC). We should describe
this in detail in javadoc.

Anyway, my original question still remains: Samaresh, is this what is causing you problems in XML?
Comment 7 Dusan Balek 2008-07-21 09:19:20 UTC
The last Vita's comment is correct. There seems to be 'other' CompletionProvider registered for your document's
mime-type that triggers automatic completion invocation. Try to put a breakpoint at CompletionImpl.insertUpdate line 301.
Comment 8 Samaresh Panda 2008-07-21 22:45:30 UTC
Yes. There are two providers at this point. getAQT() for one is non-zero while for SBCP, it is 0.

It may be the desired behavior but doesn't make any sense. If my getAQT() returns 0, I do not expect it to go inside
createTask and query(). As a CC provider author (by reading API docs) I was in the impression that getAQT() is sort of a
validation that is supposed to determine whether or not to show completion.

Anyway, if this is not true, createTask/query will be carried out and in some way add to issues like issue 139181. And
I'll have to do the same check again inside query(). If there is no fix, pl. mark it as invalid.

At the very least, declare a constant (similar to CompletionProvider.COMPLETION_QUERY_TYPE) for no completion (0).
Comment 9 Jan Lahoda 2008-07-21 23:14:08 UTC
Well, although I agree that the second paragraph in the javadoc of CP.getAQT might be clearer (w.r.t. multiple CPs),
from the first paragraph it is, IMO, quite clear that the result from the method is used only to determine whether the
CC should fire up automatically or not. If we asked only the providers that returned != 0 status from getAQT in the
auto-CC, the auto-CC content could differ from an explicit CC invocation (CPs are free to return 0 from getAQT, but
provide suggestion in the same context). Moreover, it might be considered to be an incompatible API change. I think that
the current state makes quite a good sense (the CompletionImpl.insertUpdate is not very nice from the impl. point of
view, but I think this is out-of-scope of this bug).

I propose to mark this issue invalid. I think that repeating the check inside the query is a correct solution.
Comment 10 Vitezslav Stejskal 2008-07-22 14:54:38 UTC
I'll update javadoc to clearly describe the semantics of getAQT and createTask.
Comment 11 Vitezslav Stejskal 2008-08-11 12:13:23 UTC
http://hg.netbeans.org/main/rev/3c64889f6a52
Comment 12 Quality Engineering 2008-08-11 15:38:01 UTC
Integrated into 'main-golden', available in build *200808111401* on http://bits.netbeans.org/dev/nightly/
Changeset: http://hg.netbeans.org/main/rev/3c64889f6a52
User: Vita Stejskal <vstejskal@netbeans.org>
Log: #139776 (fixed): javadoc updated