Autocomplete and Suggestions in Impex and FlexibleSearch HAC Consoles


In this article, I’m filling this gap and document how to enable autosuggestions in the Impex console. Additionally, I introduce my custom solution for Flexible Search suggestions and autocomplete.

I learned from the OOTB code that there was an attempt to add suggestions to the scripting (Groovy/Beanshell/JS) console. I decided not to fix the suggestions because half-measures basically wouldn’t add much, but the normal solution would take from weeks to months to implement. So I have confined my fixes only to Impex and Flexiblesearch.

Impex Suggestions in HAC

What may come as a surprise to you is that autocomplete is already implemented in the Impex Editor, but — not working out of the box. Why? Because the OOTB AJAX call to the type system endpoint doesn’t include the CSRF token (impex-hint.js). To fix it, add the following fragment to the “headers” section in the getContentTypes method:

headers: {
  'Accept': 'application/json',
  'X-CSRF-Token': $("meta[name='_csrf']").attr("content")
}

After fixing it, the suggestions mechanism will be enabled in the code editor. To call it, use a hot key, “Ctrl-Space”.

Press “Ctrl-Space” to see the context menu:

 

Each Impex statement starts with the command qualifier.  In the pop up window, we can see a list of available commands. To autocomplete the type name, you need to press Ctrl-Space again. HAC will make a request to the server to fetch all available types starting with what you typed.

Start typing an attribute name and press Ctrl-Space again. In the popup, you will find the names of the attributes relevant to the Product. Click at any to pick the suggestion.

If you hover the mouse pointer over the variable, you will see the replacement for the variable. Of course, you can’t see the actual value, but for some scenarios it is helpful.

The variables are also involved in the suggestion mechanism:

It is hard to say why we see two $var not one, but we are still dealing with the undocumented feature. That’s good it just works.

The suggestion mechanism supports attribute modifiers:

That’s pretty much it.

So everything is great. In fact, it is hard to add anything else on top of what is already provided.  

Dear SAP, I hate to be a nag, but one point needs to be improved if possible. The current implementation can’t understand the context under the cursor as well as it could be. If you put a cursor in the middle of `batchmode` and press Ctrl-Space, the system will create a mess:

However, it is not a big deal. Once learned, you can stop calling autosuggestions from the middle of the word. Do you remember Steve Jobs’s on the iPhone 4 antenna issue: “avoid holding it in this way”? Just don’t press Ctrl-Space in the middle of the word! 🙂 

Flexible Search Suggestions in HAC

Unlike IMPEX suggestions, the FlexibleSearch console is not equipped with this feature. More specifically, it is present in some form, but not working. It seems to be unfinished or abandoned.

My implementation

In my implementation, the autocomplete is contextual. You don’t need to press “Ctrl-Space” each time to see the suggestions. In the following example, the contextual menu was shown right after I had entered a curly brace and started typing “Product”.  

The system behaves similarly when you add a “dot” symbol to the type. The attributes on the list are the Product’s attributes.  

It also supports JOINs (where a type name is not enclosed within the curly braces):

It also supports aliases for types:

And joins:

Implementation and Technical Details

SAP uses CodeMirror 3.11. This version was released in March 2013.

Since 2013, CodeMirror was significantly updated. The latest stable release is 5.58.3. So the version used by SAP Commerce is 135 releases behind the latest release with the original API. The latest version is CodeMirror 6 which is completely rewritten and not compatible with the original one.

The autosuggestion mechanism is based on CodeMirror hints. The list of suggestions for types and attributes comes from the service already present in SAP Commerce (but not used).

Source code

https://github.com/raliev/hacautosuggestions

Disclaimers

This mechanism is designed only for local and development environments. Don’t use it on the live system.

Leave a Reply