Autogeneration of completion

Questions on using RSyntaxTextArea should go here.

Moderator: robert

Autogeneration of completion

Postby torgrim » Fri Dec 16, 2011 1:36 pm


I am having problems using autocompletion. For words added by provider.addCompletion(...) it works, but I cannot find how to automatically add words entered by the user. E.g. if the user type "var someVariable", i want "var" and "someVariable" to be added to the completionlist automatically. Do I have to parse the text manually or are there already any functionality I havent found in the code/forum/blog?

Thank you for a greate component!

Re: Autogeneration of completion

Postby robert » Fri Dec 16, 2011 11:35 pm

Hi torgrim,

Yes, you'll have to create your own CompletionProvider that parses the code to determine what variables are in scope at the caret position. For an example, check out PerlCompletionProvider in RSTALanguageSupport.

That's the simplest way to do it - every time the user types Ctrl+Space (or a trigger key), the code in the editor is parsed then and there. The vast majority of the time, this is fast enough. However, for a super robust solution you should consider creating a Parser implementation that will get notified whenever the user edits the document, so that it can be re-parsed. Then code completion will only force a re-parsing if the AST (or whatever you keep in memory) is "dirty". See JavaParser, and the Java code completion in general, in RSTALanguageSupport, for an example of doing it this way.

But again, the method used in PerlCompletionProvider is probably sufficient. It's super-easy to do too, if you're parsing a language that makes variable declarations easy to identify, such as prefixing them all with '$' or declaring them with "var". I'd take advantage of CodeBlock and other utility classes in the library to make things easier.
User avatar
Posts: 819
Joined: Sat May 10, 2008 5:16 pm

Return to Help

Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests