AutoComplite append text instead of replace it

General Discussion on RSyntaxTextArea.

Moderator: robert

AutoComplite append text instead of replace it

Postby szhorvath » Wed Jul 11, 2012 1:22 pm

I would like to append text in autoComplete instead of replace it. Is there any possibilities?

But I will be appreciated any help in the following problem:

I would like to use autoComplite in a path.

e.g.

car
car.small
car.big
car.big.red
car.big.blue

etc.

When I push ctrl+space on "car." I would like to show a list with "big" and "small" list items. After I push ctrl+space on "car.big" I would like to see list with "red" and "blue" list elements.

A put a keylistener to textArea and it is dynamic build the provider with properly list.
I have override getCompletionsImpl what is simple return the modifies list.
But unfortunatelly, the InsertCompletion method in AutoCompletion class replace text with item what selected in list.
I can't ovveride properly the InsertCompletion method in AutoCompletion class because some fields and methods are private.
I need a parameter or new InsertCompletion method what will append to alreadyEntered the selected text.

I have found a post with similar problem (AutoComplete instance of an object), but my problem is less difficult and I don't want to write a new LanguageSupport.

Any help is welcome!
szhorvath
 

Re: AutoComplite append text instead of replace it

Postby robert » Thu Jul 12, 2012 3:21 am

<long-explanation>
One way to do this is to create a custom Completion class (subclassing BasicCompletion or one of the other built-in implementations most likely) and override the getAlreadyEntered(JTextComponent) method. This method determines what region of text will actually get replaced.

The default implementation in AbstractCompletion calls the "parent" CompletionProvider's getAlreadyEnteredText() method, the likely implementation of which is in DefaultCompletionProvider, which scans backwards until it finds an invalid replacement char. I'm guessing you specify '.' as a valid character? Thus you can do what is done in AbstractJavaSourceCompletion:
</long-explanation>

<short-explanation>

In your Completion class, override getAlreadyEntered() like so:

java code:

public String getAlreadyEntered(JTextComponent comp) {
String temp = getProvider().getAlreadyEnteredText(comp);
int lastDot = temp.lastIndexOf('.');
if (lastDot>-1) {
temp = temp.substring(lastDot+1);
}
return temp;
}

</short-explanation>
User avatar
robert
 
Posts: 794
Joined: Sat May 10, 2008 5:16 pm

Re: AutoComplite append text instead of replace it

Postby szhorvath » Thu Jul 12, 2012 10:48 am

Dear Robert!
I gratefully thank you for your quick answer!
When it return empty string in getAlreadyEntered method, everything works fine!
szhorvath
 


Return to Open Discussion

Who is online

Users browsing this forum: No registered users and 5 guests