SourceCodeEditor ignores suggestion.getEndPosition() in applySuggestion

Hi all!
As stated in subject, we found a potential issue in SourceCodeEditor.

Use Case:
When using SourceCodeEditor and applying a suggestion with both startPosition and endPosition greater than -1, the endPosition parameter seems to be ignored, regardless of what JavaDoc says about it, while startPosition is correctly applied.

Cause:
Digging into CUBA’s code, we found out that in

com.haulmont.cuba.web.gui.components.WebSourceCodeEditor

the field

suggestionExtension

is initialized using a new instance based on the protected inner class of WebSourceCodeEditor itself, which doesn’t support endPosition parameter for selected Suggestion:

image

image

Workaround:
To bypass this issue, we didn’t find any other solution than overwrite suggestionExtension field, using reflection, with our own implementation of

com.haulmont.cuba.web.widgets.addons.aceeditor.Suggester

which of course is not the optimal approach, at least for the “reflection” part.
This is our code:

image
As you can see, it differs from the original one just for the applySuggestion method:

@Override public String applySuggestion(final com.haulmont.cuba.web.widgets.addons.aceeditor.Suggestion suggestion, final String text, final int cursor) {
    String suggestionText = suggestion.getSuggestionText();
    return StringUtils.substring(text, 0, suggestion.getStartPosition() > 0 ? suggestion.getStartPosition() : cursor)
            + suggestionText
            + StringUtils.substring(text, suggestion.getEndPosition() > 0 ? Math.min(suggestion.getEndPosition(), text.length()) : cursor);
}

Potential Solutions:
As far as we can see, the potential solutions are:

  • supporting endPosition parameter in applySuggestion method of WebSourceCodeEditor’s protected inner class
  • exposing suggestionExtension field via API to allow overwriting it without using reflection

Are we missing something else?

Thanks in advance and keep up the great work!
Luca R.

Hello @m.ruggiero,

Thank you for reporting the problem! I’ve created an issue: cuba-platform/cuba#3106.

You can override WebSourceCodeEditor component and extend SourceCodeEditorSuggester:

Extended WebSourceCodeEditor
public class ExtWebSourceCodeEditor extends WebSourceCodeEditor {

    @Override
    public void setSuggester(Suggester suggester) {
        this.suggester = suggester;

        if (suggester != null && suggestionExtension == null) {
            suggestionExtension = new SuggestionExtension(new ExtSourceCodeEditorSuggester());
            suggestionExtension.extend(component);
            suggestionExtension.setShowDescriptions(false);
            suggestionExtension.setSuggestOnDot(suggestOnDot);
        }
    }

    protected class ExtSourceCodeEditorSuggester extends SourceCodeEditorSuggester {

        @Override
        public String applySuggestion(Suggestion suggestion, String text, int cursor) {
            String suggestionText = suggestion.getSuggestionText();
            return StringUtils.substring(text, 0, suggestion.getStartPosition() > 0 ? suggestion.getStartPosition() : cursor)
                    + suggestionText
                    + StringUtils.substring(text, suggestion.getEndPosition() > 0 ? Math.min(suggestion.getEndPosition(), text.length()) : cursor);
        }
    }
}

How to register component you can see here: Checkbox with values 0 and 1 - #6 от пользователя Pinyazhin - CUBA.Platform

In your case, you can only extend WebSourceCodeEditor class and register it. See demo project with extended SourceCodeEditor: sourcedemo.zip (84.6 KB)

1 Like

Thanks, Roman!
We’ll try as you suggest!
Best regards
Luca R.